简化版本检查代码

Former-commit-id: 4f1bbdc34a208047aa26a37b3a15034f16463e9d [formerly 4f1bbdc34a208047aa26a37b3a15034f16463e9d [formerly 4f1bbdc34a208047aa26a37b3a15034f16463e9d [formerly 4f1bbdc34a208047aa26a37b3a15034f16463e9d [formerly f10219d0337fc94fcad4a395f634ff82da269a2e [formerly 3c58b53fea597ae0fe5418d44133a7e59ee1441e]]]]]
Former-commit-id: 94e99a356571ebf8274954ca601c593e8ea93514
Former-commit-id: 1cab1aa58616e215d1a39f4d6a3a7c2453ca2153
Former-commit-id: ed071e422a123fb3094e88a936724e9c65d70888 [formerly 12d167b90bfde092d7521b4041cee7c656d944b7]
Former-commit-id: 42a97fc37840b20ca517afabfa28a24bf557625d
Former-commit-id: 764d8f4a94a499abbd2aaa9c3307600c13b78028
Former-commit-id: db598cfc092862c94b8bd859f9b941135c124069
Former-commit-id: 77f5ca3d799fe9d4a8bb9b08e5be79686369e361
Former-commit-id: 18c090ca7aaa651db442c4b41d39ba0543a89944
This commit is contained in:
liyang 2018-08-21 23:25:57 +08:00
parent 29e6263504
commit 238b73225b
7 changed files with 58 additions and 76 deletions

4
.env
View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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()
},

View File

@ -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} 是否需要确认

View File

@ -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