-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (87 loc) · 1.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* @Author: hishion
* @Date: 2020-11-30 18:35:51
* @Description 工具集
*/
import throttle from './modules/throttle'
import debounce from './modules/debounce'
import route from './modules/route'
import loading from './modules/loading'
import ui from './modules/ui'
import checkUpdate from './modules/update'
const { name, version } = require('../package.json')
console.log(`%c ${name}: v${version} `, `background-color: #8e0;color:#fff;`)
/**
* 返回页面实例
* @param {Number} delta 页面相对层级
* 注意的是小程序里面一些数据在this.$getPage().$vm.$mp里面,例如链接参数.query
*/
export const getPage = (delta = 0) => {
const pages = getCurrentPages()
const page = pages[pages.length - delta - 1]
return page
}
/**
*
* @param {Object} res 接口响应
* @param {Boolean} showMsg 是否自动提示错误信息
*/
export const ck = (
res = {},
showMsg = true,
{
codeKey = 'return_code',
msgKey = 'return_msg',
successCode = '0',
excludeCode = ['80003']
} = {}
) => {
if (res[codeKey] === successCode) {
return true
} else {
if (!showMsg) return false
if (!excludeCode.includes(res[codeKey])) {
msg(res[msgKey])
}
return false
}
}
export const storage = {
get(key) {
return uni.getStorageSync(key)
},
set(key, value) {
uni.setStorageSync(key, value)
},
remove(key) {
// 单个key直接清除,key数组逐个清除
if (typeof key === 'string') {
uni.removeStorageSync(key)
} else if (Object.prototype.toString.call(key).slice(8, -1) === 'Array') {
key.length > 0 &&
key.forEach((item) => {
uni.removeStorageSync(item)
})
}
},
clear() {
uni.clearStorageSync()
}
}
const h = {
...route,
...loading,
...ui,
getPage,
ck,
storage,
throttle,
debounce,
checkUpdate
}
const install = (Vue) => {
Vue.prototype.$h = h
}
export default {
install
}