diff --git a/.env b/.env index 7a796519..c6889c1a 100644 --- a/.env +++ b/.env @@ -1,6 +1,4 @@ # 所有环境 # 页面 title 前缀 -VUE_APP_TITLE=D2Admin -# 检查更新的接口地址 -VUE_APP_RELEASES_API=https://api.github.com/repos/FairyEver/d2-admin/releases/latest +VUE_APP_TITLE=D2Admin \ No newline at end of file diff --git a/src/libs/db.js b/src/libs/db.js index fed5597a..0df33761 100644 --- a/src/libs/db.js +++ b/src/libs/db.js @@ -5,12 +5,11 @@ import setting from '@/setting.js' const adapter = new LocalStorage(`d2admin-${setting.releases.version}`) const db = low(adapter) -// 初始化数据库 -db.defaults({ - // 系统 - sys: {}, - // 存储 - database: {} -}).write() +db + .defaults({ + sys: {}, + database: {} + }) + .write() export default db diff --git a/src/libs/util.cookies.js b/src/libs/util.cookies.js index 6cf6431c..24e922ed 100644 --- a/src/libs/util.cookies.js +++ b/src/libs/util.cookies.js @@ -1,5 +1,5 @@ import Cookies from 'js-cookie' -import { version } from '../../package.json' +import setting from '@/setting.js' const cookies = {} @@ -9,12 +9,12 @@ const cookies = {} * @param {String} value cookie value * @param {Object} setting cookie setting */ -cookies.set = function (name = 'default', value = '', setting = {}) { - let cookieSetting = { +cookies.set = function (name = 'default', value = '', cookieSetting = {}) { + let currentCookieSetting = { expires: 1 } - Object.assign(cookieSetting, setting) - Cookies.set(`d2admin-${version}-${name}`, value, cookieSetting) + Object.assign(currentCookieSetting, cookieSetting) + Cookies.set(`d2admin-${setting.releases.version}-${name}`, value, currentCookieSetting) } /** @@ -22,7 +22,7 @@ cookies.set = function (name = 'default', value = '', setting = {}) { * @param {String} name cookie name */ cookies.get = function (name = 'default') { - return Cookies.get(`d2admin-${version}-${name}`) + return Cookies.get(`d2admin-${setting.releases.version}-${name}`) } /** @@ -37,7 +37,7 @@ cookies.getAll = function () { * @param {String} name cookie name */ cookies.remove = function (name = 'default') { - return Cookies.remove(`d2admin-${version}-${name}`) + return Cookies.remove(`d2admin-${setting.releases.version}-${name}`) } export default cookies diff --git a/src/libs/util.js b/src/libs/util.js index 6d36dc1b..af73d10e 100644 --- a/src/libs/util.js +++ b/src/libs/util.js @@ -1,6 +1,3 @@ -import axios from 'axios' -import semver from 'semver' -import { version } from '../../package.json' import log from './util.log.js' import cookies from './util.cookies.js' @@ -14,7 +11,8 @@ let util = { * @param {String} title 标题 */ util.title = function (titleText) { - window.document.title = `${process.env.VUE_APP_TITLE}${titleText ? ` | ${titleText}` : ''}` + const processTitle = process.env.VUE_APP_TITLE || 'D2Admin' + window.document.title = `${processTitle}${titleText ? ` | ${titleText}` : ''}` } /** @@ -31,50 +29,4 @@ util.open = function (url) { document.body.removeChild(document.getElementById('d2admin-menu-link')) } -/** - * @description 判断是否在其内 - * @param {*} ele element - * @param {Array} targetArr array - */ -util.isOneOf = function (ele, targetArr) { - if (targetArr.indexOf(ele) >= 0) { - return true - } else { - return false - } -} - -/** - * @description 检查版本更新 - * @param {Object} vm vue - */ -util.checkUpdate = function (vm) { - if (!process.env.VUE_APP_RELEASES_API) { - return - } - axios.get(process.env.VUE_APP_RELEASES_API) - .then(res => { - let versionGet = res.tag_name - const update = semver.lt(version, versionGet) - if (update) { - log.capsule('D2Admin', `New version ${res.name}`) - console.log(`版本号: ${res.tag_name} | 详情${res.html_url}`) - vm.$store.commit('d2admin/releases/updateSet', true) - } - vm.$store.commit('d2admin/releases/latestSet', res) - }) - .catch(err => { - console.log('checkUpdate error', err) - }) -} - -/** - * @description 显示版本信息 - */ -util.showInfo = function showInfo () { - log.capsule('D2Admin', `v${version}`) - console.log('Github https://github.com/d2-projects/d2-admin') - console.log('Doc http://d2admin.fairyever.com/zh/') -} - export default util diff --git a/src/main.js b/src/main.js index f7f09724..7bb1f679 100644 --- a/src/main.js +++ b/src/main.js @@ -54,14 +54,14 @@ new Vue({ this.$store.commit('d2admin/menu/headerSet', menuHeader) }, mounted () { - // D2Admin 开发环境检查更新 - util.checkUpdate(this) - // 获取并记录用户 UA - this.$store.commit('d2admin/ua/get') // 展示系统信息 - util.showInfo() + this.$store.commit('d2admin/releases/versionShow') + // 检查最新版本 + this.$store.dispatch('d2admin/releases/checkUpdate') // 用户登陆后从数据库加载一系列的设置 this.$store.commit('d2admin/account/load') + // 获取并记录用户 UA + this.$store.commit('d2admin/ua/get') // 初始化全屏监听 this.fullscreenListenerInit() }, diff --git a/src/store/modules/d2admin/modules/account.js b/src/store/modules/d2admin/modules/account.js index 6aef0392..692788b4 100644 --- a/src/store/modules/d2admin/modules/account.js +++ b/src/store/modules/d2admin/modules/account.js @@ -4,7 +4,7 @@ export default { namespaced: true, actions: { /** - * 登陆 + * @description 登陆 * @param {Object} param context * @param {Object} param vm {Object} vue 实例 * @param {Object} param username {String} 用户账号 @@ -33,7 +33,7 @@ export default { name: res.data.name }, { root: true }) // 用户登陆后从持久化数据加载一系列的设置 - commit('d2admin/account/load', null, { root: true }) + commit('load') // 跳转路由 vm.$router.push({ name: 'index' @@ -46,7 +46,7 @@ export default { }) }, /** - * 注销用户并返回登陆页面 + * @description 注销用户并返回登陆页面 * @param {Object} param context * @param {Object} param vm {Object} vue 实例 * @param {Object} param confirm {Boolean} 是否需要确认 diff --git a/src/store/modules/d2admin/modules/releases.js b/src/store/modules/d2admin/modules/releases.js index 6cc4c3e4..b10fc6ae 100644 --- a/src/store/modules/d2admin/modules/releases.js +++ b/src/store/modules/d2admin/modules/releases.js @@ -1,4 +1,6 @@ -// 设置文件 +import axios from 'axios' +import semver from 'semver' +import util from '@/libs/util.js' import setting from '@/setting.js' export default { @@ -11,7 +13,38 @@ export default { // 有新版本 update: false }, + actions: { + /** + * @description 检查版本更新 + * @param {Object} param context + */ + checkUpdate ({ state, commit }) { + axios.get('https://api.github.com/repos/FairyEver/d2-admin/releases/latest') + .then(res => { + let versionGet = res.tag_name + const update = semver.lt(state.version, versionGet) + if (update) { + util.log.capsule('D2Admin', `New version ${res.name}`) + console.log(`版本号: ${res.tag_name} | 详情 ${res.html_url}`) + commit('updateSet', true) + } + commit('latestSet', res) + }) + .catch(err => { + console.log('checkUpdate error', err) + }) + } + }, mutations: { + /** + * @description 显示版本信息 + * @param {Object} state vuex state + */ + versionShow (state) { + util.log.capsule('D2Admin', `v${state.version}`) + console.log('Github https://github.com/d2-projects/d2-admin') + console.log('Doc http://d2admin.fairyever.com/zh/') + }, /** * @description 设置是否有新的 D2Admin 版本 * @param {Object} state vuex state