Former-commit-id: 5ff408c16c699f9f091a62074faf5c106efd32f5 [formerly 0d9ff5deffb9da11589abd20110adf0e521eea2b] [formerly 5ff408c16c699f9f091a62074faf5c106efd32f5 [formerly 0d9ff5deffb9da11589abd20110adf0e521eea2b] [formerly 5ff408c16c699f9f091a62074faf5c106efd32f5 [formerly 0d9ff5deffb9da11589abd20110adf0e521eea2b] [formerly 0d9ff5deffb9da11589abd20110adf0e521eea2b [formerly ec676c00b7d7b5b3de61ef47d9aac8d011c283c1 [formerly c00f0271d0b118666a8756a52e249e4a586eacb3]]]]] Former-commit-id: 7130c868e62e0dba1950cc14574880c96aebb8a7 Former-commit-id: 6525fbdf43121e93600361ed4337e691c481ce6d Former-commit-id: 70ca5629d20eed82c03ddcd2a2c25b10ea336d8e [formerly 486ff5e6084bdc51abe51b4bdda7436477ccd26a] Former-commit-id: 2809e202549f5586a5a0be20f04be268c0e326ea Former-commit-id: 818655905c96238e07983c596e94326a4dc1a315 Former-commit-id: aa3e2b37cc07f231fb860edfc4803ebc3df5ec4f Former-commit-id: 22df71608a20275b7c0a7b290b1c588fa3c9d02b Former-commit-id: 121632e21d5161f53048cd6513d0f7d07a9cb580
136 lines
3.2 KiB
JavaScript
136 lines
3.2 KiB
JavaScript
import Cookies from 'js-cookie'
|
|
import axios from 'axios'
|
|
import semver from 'semver'
|
|
import dayjs from 'dayjs'
|
|
import UaParser from 'ua-parser-js'
|
|
import { version } from '../../package.json'
|
|
|
|
let util = {}
|
|
|
|
/**
|
|
* @description 存储 uuid 到 cookie
|
|
* @param {string} value uuid value
|
|
* @param {object} setting cookie setting
|
|
*/
|
|
util.uuidSet = function (value = '', setting = {}) {
|
|
let cookieSetting = {
|
|
expires: 1
|
|
}
|
|
Object.assign(cookieSetting, setting)
|
|
Cookies.set(`d2admin-${version}-uuid`, value, cookieSetting)
|
|
}
|
|
|
|
/**
|
|
* @description 得到现在的用户 uuid
|
|
*/
|
|
util.uuidGet = function () {
|
|
return Cookies.get(`d2admin-${version}-uuid`)
|
|
}
|
|
|
|
/**
|
|
* @description 删除用户 uuid
|
|
*/
|
|
util.uuidRemove = function () {
|
|
return Cookies.remove(`d2admin-${version}-uuid`)
|
|
}
|
|
|
|
/**
|
|
* @description 存储 token 到 cookie
|
|
* @param {string} value token value
|
|
* @param {object} setting cookie setting
|
|
*/
|
|
util.tokenSet = function (value = '', setting = {}) {
|
|
let cookieSetting = {
|
|
expires: 1
|
|
}
|
|
Object.assign(cookieSetting, setting)
|
|
Cookies.set(`d2admin-${version}-token`, value, cookieSetting)
|
|
}
|
|
|
|
/**
|
|
* @description 得到现在的用户 token
|
|
*/
|
|
util.tokenGet = function () {
|
|
return Cookies.get(`d2admin-${version}-token`)
|
|
}
|
|
|
|
/**
|
|
* @description 删除用户 token
|
|
*/
|
|
util.tokenRemove = function () {
|
|
return Cookies.remove(`d2admin-${version}-token`)
|
|
}
|
|
|
|
/**
|
|
* @description 更新标题
|
|
* @param {string} title 标题
|
|
*/
|
|
util.title = function (titleText) {
|
|
window.document.title = `${process.env.VUE_APP_TITLE}${titleText ? ` | ${titleText}` : ''}`
|
|
}
|
|
|
|
/**
|
|
* @description 获取所有的 UA 信息
|
|
*/
|
|
util.ua = function () {
|
|
return new UaParser().getResult()
|
|
}
|
|
|
|
/**
|
|
* @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 {string} title title text
|
|
* @param {string} info info text
|
|
*/
|
|
util.logCapsule = function (title, info) {
|
|
console.log(
|
|
`%c ${title} %c ${info} %c`,
|
|
'background:#29384b; padding: 1px; border-radius: 3px 0 0 3px; color: #fff',
|
|
'background:#3488ff; padding: 1px; border-radius: 0 3px 3px 0; color: #fff',
|
|
'background:transparent'
|
|
)
|
|
}
|
|
|
|
/**
|
|
* @description 检查版本更新
|
|
* @param {object} vm vue
|
|
*/
|
|
util.checkUpdate = function (vm) {
|
|
axios.get('https://api.github.com/repos/FairyEver/d2-admin/releases/latest')
|
|
.then(res => {
|
|
let versionGet = res.tag_name
|
|
const update = semver.lt(version, versionGet)
|
|
if (update) {
|
|
util.logCapsule('D2Admin', `New version ${res.name}`)
|
|
console.log(`${dayjs(res.created_at).format('YYYY年M月D日')}更新 版本号: ${res.tag_name} | 详情${res.html_url}`)
|
|
}
|
|
vm.$store.commit('d2adminUpdateSet', res)
|
|
})
|
|
.catch(err => {
|
|
console.log('checkUpdate error', err)
|
|
})
|
|
}
|
|
|
|
/**
|
|
* @description 显示版本信息
|
|
*/
|
|
util.showInfo = function showInfo () {
|
|
util.logCapsule('D2Admin', `v${version}`)
|
|
console.log('Github https://github.com/d2-projects/d2-admin')
|
|
console.log('Doc http://d2admin.fairyever.com/zh/')
|
|
}
|
|
|
|
export default util
|