Former-commit-id: 236acdba4bc6cbd5921647fd5c1ee0258f43c143 [formerly 236acdba4bc6cbd5921647fd5c1ee0258f43c143 [formerly 236acdba4bc6cbd5921647fd5c1ee0258f43c143 [formerly 236acdba4bc6cbd5921647fd5c1ee0258f43c143 [formerly cd607a12b20a3d53a1967ee6012a35e03e3d31fe [formerly 645a010d0d8f84a370c509c39c560d93eaa8ae4f]]]]] Former-commit-id: fd4e72b6a4dc7cb2731c0b5fbf343dab6c251d3f Former-commit-id: 68305cd58cbd1927373a79c1b507de3c4f3d9924 Former-commit-id: 48312890868f48d5524b0de0dda489f2b2c57b0a [formerly fec91cd8f508ffe42c5e254dcee018c879f3d51e] Former-commit-id: 9e300a2baafd1eae86bb69a052f15b904724856e Former-commit-id: eaafbb0a410a49a286eb8e666b6784b96c9df371 Former-commit-id: ccef5438dddbe9762bd3574b33aab49bc1edee58 Former-commit-id: d2ca5c406860c43079d78a80d18ca2b1ed87f776 Former-commit-id: d4648c6865bff45b24aea999b60381b78417b802
181 lines
4.2 KiB
JavaScript
181 lines
4.2 KiB
JavaScript
import Cookies from 'js-cookie'
|
|
import axios from 'axios'
|
|
import semver from 'semver'
|
|
import UaParser from 'ua-parser-js'
|
|
import { version } from '../../package.json'
|
|
|
|
let util = {
|
|
cookies: {},
|
|
log: {}
|
|
}
|
|
|
|
/**
|
|
* @description 存储 cookie 值
|
|
* @param {String} name cookie name
|
|
* @param {String} value cookie value
|
|
* @param {Object} setting cookie setting
|
|
*/
|
|
util.cookies.set = function (name = 'default', value = '', setting = {}) {
|
|
let cookieSetting = {
|
|
expires: 1
|
|
}
|
|
Object.assign(cookieSetting, setting)
|
|
Cookies.set(`d2admin-${version}-${name}`, value, cookieSetting)
|
|
}
|
|
|
|
/**
|
|
* @description 拿到 cookie 值
|
|
* @param {String} name cookie name
|
|
*/
|
|
util.cookies.get = function (name = 'default') {
|
|
return Cookies.get(`d2admin-${version}-${name}`)
|
|
}
|
|
|
|
/**
|
|
* @description 拿到 cookie 全部的值
|
|
*/
|
|
util.cookies.getAll = function () {
|
|
return Cookies.get()
|
|
}
|
|
|
|
/**
|
|
* @description 删除 cookie
|
|
* @param {String} name cookie name
|
|
*/
|
|
util.cookies.remove = function (name = 'default') {
|
|
return Cookies.remove(`d2admin-${version}-${name}`)
|
|
}
|
|
|
|
/**
|
|
* @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} 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
|
|
}
|
|
|
|
/**
|
|
* @description 打印一个 “胶囊” 样式的信息
|
|
* @param {String} title title text
|
|
* @param {String} info info text
|
|
* @param {String} type style
|
|
*/
|
|
util.log.capsule = function (title, info, type = 'primary') {
|
|
console.log(
|
|
`%c ${title} %c ${info} %c`,
|
|
'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;`,
|
|
'background:transparent'
|
|
)
|
|
}
|
|
|
|
/**
|
|
* @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 打印 primary 样式的文字
|
|
*/
|
|
util.log.primary = function (text) {
|
|
util.log.colorful([{ text, type: 'primary' }])
|
|
}
|
|
|
|
/**
|
|
* @description 打印 success 样式的文字
|
|
*/
|
|
util.log.success = function (text) {
|
|
util.log.colorful([{ text, type: 'success' }])
|
|
}
|
|
|
|
/**
|
|
* @description 打印 warning 样式的文字
|
|
*/
|
|
util.log.warning = function (text) {
|
|
util.log.colorful([{ text, type: 'warning' }])
|
|
}
|
|
|
|
/**
|
|
* @description 打印 danger 样式的文字
|
|
*/
|
|
util.log.danger = function (text) {
|
|
util.log.colorful([{ text, type: 'danger' }])
|
|
}
|
|
|
|
/**
|
|
* @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) {
|
|
util.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 () {
|
|
util.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
|