2018-07-16 22:22:55 +08:00
|
|
|
import Cookies from 'js-cookie'
|
|
|
|
|
import axios from 'axios'
|
|
|
|
|
import semver from 'semver'
|
2018-07-17 23:59:20 +08:00
|
|
|
import UaParser from 'ua-parser-js'
|
2018-07-21 17:43:13 +08:00
|
|
|
import { version } from '../../package.json'
|
2018-07-16 22:22:55 +08:00
|
|
|
|
2018-07-24 11:02:51 +08:00
|
|
|
let util = {
|
2018-08-02 14:08:36 +08:00
|
|
|
cookies: {},
|
|
|
|
|
log: {}
|
2018-07-24 11:02:51 +08:00
|
|
|
}
|
2018-07-16 22:22:55 +08:00
|
|
|
|
2018-07-21 17:43:13 +08:00
|
|
|
/**
|
2018-07-24 10:21:46 +08:00
|
|
|
* @description 存储 cookie 值
|
|
|
|
|
* @param {String} name cookie name
|
|
|
|
|
* @param {String} value cookie value
|
2018-07-22 20:34:36 +08:00
|
|
|
* @param {Object} setting cookie setting
|
2018-07-21 17:43:13 +08:00
|
|
|
*/
|
2018-07-24 11:02:51 +08:00
|
|
|
util.cookies.set = function (name = 'default', value = '', setting = {}) {
|
2018-07-21 17:43:13 +08:00
|
|
|
let cookieSetting = {
|
|
|
|
|
expires: 1
|
|
|
|
|
}
|
|
|
|
|
Object.assign(cookieSetting, setting)
|
2018-07-24 10:21:46 +08:00
|
|
|
Cookies.set(`d2admin-${version}-${name}`, value, cookieSetting)
|
2018-07-21 17:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
2018-07-16 22:22:55 +08:00
|
|
|
/**
|
2018-07-24 10:21:46 +08:00
|
|
|
* @description 拿到 cookie 值
|
|
|
|
|
* @param {String} name cookie name
|
2018-07-16 22:22:55 +08:00
|
|
|
*/
|
2018-07-24 11:02:51 +08:00
|
|
|
util.cookies.get = function (name = 'default') {
|
2018-07-24 10:21:46 +08:00
|
|
|
return Cookies.get(`d2admin-${version}-${name}`)
|
2018-07-21 17:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
2018-07-24 11:02:51 +08:00
|
|
|
/**
|
|
|
|
|
* @description 拿到 cookie 全部的值
|
|
|
|
|
*/
|
|
|
|
|
util.cookies.getAll = function () {
|
|
|
|
|
return Cookies.get()
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-21 17:43:13 +08:00
|
|
|
/**
|
2018-07-24 10:21:46 +08:00
|
|
|
* @description 删除 cookie
|
|
|
|
|
* @param {String} name cookie name
|
2018-07-21 17:43:13 +08:00
|
|
|
*/
|
2018-07-24 11:02:51 +08:00
|
|
|
util.cookies.remove = function (name = 'default') {
|
2018-07-24 10:21:46 +08:00
|
|
|
return Cookies.remove(`d2admin-${version}-${name}`)
|
2018-07-16 22:22:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @description 更新标题
|
2018-07-22 20:34:36 +08:00
|
|
|
* @param {String} title 标题
|
2018-07-16 22:22:55 +08:00
|
|
|
*/
|
2018-07-17 23:59:20 +08:00
|
|
|
util.title = function (titleText) {
|
2018-07-16 22:22:55 +08:00
|
|
|
window.document.title = `${process.env.VUE_APP_TITLE}${titleText ? ` | ${titleText}` : ''}`
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-17 23:59:20 +08:00
|
|
|
/**
|
2018-07-21 08:47:51 +08:00
|
|
|
* @description 获取所有的 UA 信息
|
2018-07-17 23:59:20 +08:00
|
|
|
*/
|
2018-07-21 08:47:51 +08:00
|
|
|
util.ua = function () {
|
2018-07-17 23:59:20 +08:00
|
|
|
return new UaParser().getResult()
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-16 22:22:55 +08:00
|
|
|
/**
|
|
|
|
|
* @description 判断是否在其内
|
|
|
|
|
* @param {*} ele element
|
2018-07-22 20:34:36 +08:00
|
|
|
* @param {Array} targetArr array
|
2018-07-16 22:22:55 +08:00
|
|
|
*/
|
|
|
|
|
util.isOneOf = function (ele, targetArr) {
|
|
|
|
|
if (targetArr.indexOf(ele) >= 0) {
|
|
|
|
|
return true
|
|
|
|
|
} else {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-02 14:08:36 +08:00
|
|
|
/**
|
|
|
|
|
* @description 返回这个样式的颜色值
|
|
|
|
|
* @param {String} type 样式名称 [ primary | success | warning | danger | text ]
|
|
|
|
|
*/
|
|
|
|
|
util.typeColor = function (type = 'default') {
|
|
|
|
|
let color = ''
|
|
|
|
|
switch (type) {
|
|
|
|
|
case 'default': color = '35495E'; break
|
|
|
|
|
case 'primary': color = '#3488ff'; break
|
|
|
|
|
case 'success': color = '#43B883'; break
|
|
|
|
|
case 'warning': color = '#e6a23c'; break
|
|
|
|
|
case 'danger': color = '#f56c6c'; break
|
|
|
|
|
default:; break
|
|
|
|
|
}
|
|
|
|
|
return color
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-16 22:22:55 +08:00
|
|
|
/**
|
|
|
|
|
* @description 打印一个 “胶囊” 样式的信息
|
2018-07-22 20:34:36 +08:00
|
|
|
* @param {String} title title text
|
|
|
|
|
* @param {String} info info text
|
2018-08-02 14:08:36 +08:00
|
|
|
* @param {String} type style
|
2018-07-16 22:22:55 +08:00
|
|
|
*/
|
2018-08-02 14:08:36 +08:00
|
|
|
util.log.capsule = function (title, info, type = 'primary') {
|
2018-07-16 22:22:55 +08:00
|
|
|
console.log(
|
|
|
|
|
`%c ${title} %c ${info} %c`,
|
2018-08-02 14:08:36 +08:00
|
|
|
'background:#35495E; padding: 1px; border-radius: 3px 0 0 3px; color: #fff;',
|
|
|
|
|
`background:${util.typeColor(type)}; padding: 1px; border-radius: 0 3px 3px 0; color: #fff;`,
|
2018-07-16 22:22:55 +08:00
|
|
|
'background:transparent'
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-02 14:08:36 +08:00
|
|
|
/**
|
|
|
|
|
* @description 打印彩色文字
|
|
|
|
|
*/
|
|
|
|
|
util.log.colorful = function (textArr) {
|
|
|
|
|
console.log(
|
|
|
|
|
`%c ${textArr.map(t => t.text).join(' %c ')}`,
|
|
|
|
|
...textArr.map(t => `color: ${util.typeColor(t.type)};`)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @description 打印 danger 样式的文字
|
|
|
|
|
*/
|
|
|
|
|
util.log.error = function (text) {
|
|
|
|
|
util.log.colorful([{ text, type: 'danger' }])
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-16 22:22:55 +08:00
|
|
|
/**
|
|
|
|
|
* @description 检查版本更新
|
2018-07-22 20:34:36 +08:00
|
|
|
* @param {Object} vm vue
|
2018-07-16 22:22:55 +08:00
|
|
|
*/
|
|
|
|
|
util.checkUpdate = function (vm) {
|
2018-07-25 11:41:41 +08:00
|
|
|
if (!process.env.VUE_APP_RELEASES_API) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
axios.get(process.env.VUE_APP_RELEASES_API)
|
2018-07-16 22:22:55 +08:00
|
|
|
.then(res => {
|
2018-07-21 17:43:13 +08:00
|
|
|
let versionGet = res.tag_name
|
|
|
|
|
const update = semver.lt(version, versionGet)
|
2018-07-16 22:22:55 +08:00
|
|
|
if (update) {
|
2018-08-02 14:08:36 +08:00
|
|
|
util.log.capsule('D2Admin', `New version ${res.name}`)
|
2018-07-25 10:51:03 +08:00
|
|
|
console.log(`版本号: ${res.tag_name} | 详情${res.html_url}`)
|
2018-07-23 13:58:12 +08:00
|
|
|
vm.$store.commit('d2adminReleasesUpdateSet', true)
|
2018-07-16 22:22:55 +08:00
|
|
|
}
|
2018-07-23 13:58:12 +08:00
|
|
|
vm.$store.commit('d2adminReleasesLatestSet', res)
|
2018-07-16 22:22:55 +08:00
|
|
|
})
|
|
|
|
|
.catch(err => {
|
|
|
|
|
console.log('checkUpdate error', err)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @description 显示版本信息
|
|
|
|
|
*/
|
|
|
|
|
util.showInfo = function showInfo () {
|
2018-08-02 14:08:36 +08:00
|
|
|
util.log.capsule('D2Admin', `v${version}`)
|
2018-07-16 22:22:55 +08:00
|
|
|
console.log('Github https://github.com/d2-projects/d2-admin')
|
|
|
|
|
console.log('Doc http://d2admin.fairyever.com/zh/')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default util
|