shijiplus-web-plugin 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shijiplus-web-plugin",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "files": [
5
5
  "src"
6
6
  ],
@@ -45,7 +45,8 @@
45
45
  "parser": "@babel/eslint-parser"
46
46
  },
47
47
  "rules": {
48
- "no-unused-vars": "off"
48
+ "no-unused-vars": "off",
49
+ "no-prototype-builtins": "off"
49
50
  }
50
51
  },
51
52
  "browserslist": [
@@ -1,5 +1,4 @@
1
1
  import draggable from './module/draggable'
2
- import clipboard from './module/clipboard'
3
2
  import authAccess from './module/authAccess'
4
3
  import loading from './module/loading'
5
4
  import loadingText from './module/loading-text'
@@ -7,7 +6,6 @@ import loadingIcon from './module/loading-icon'
7
6
 
8
7
  const directives = {
9
8
  draggable,
10
- clipboard,
11
9
  authAccess,
12
10
  loading,
13
11
  loadingText,
@@ -1,6 +1,7 @@
1
1
  import directive from './directives'
2
2
 
3
3
  const importDirective = Vue => {
4
+ console.log('-------------importDirective-------------')
4
5
  /**
5
6
  * 拖拽指令 v-draggable="options"
6
7
  * options = {
@@ -10,15 +11,6 @@ const importDirective = Vue => {
10
11
  * }
11
12
  */
12
13
  Vue.directive('draggable', directive.draggable)
13
- /**
14
- * clipboard指令 v-draggable="options"
15
- * options = {
16
- * value: /在输入框中使用v-model绑定的值/,
17
- * success: /复制成功后的回调/,
18
- * error: /复制失败后的回调/
19
- * }
20
- */
21
- Vue.directive('clipboard', directive.clipboard)
22
14
  Vue.directive('access', directive.authAccess)
23
15
  Vue.directive('loading', directive.loading)
24
16
  }
@@ -1,17 +1,16 @@
1
- import store from '@/store'
2
1
  export default {
3
2
  inserted(el, binding, vnode, oldVnode) {
4
- console.log('plugin-access', store.state.user.access)
3
+ console.log('plugin-access', vnode.context.$store)
5
4
  if (document.location.hostname == 'localhost') {
6
5
  return
7
6
  }
8
- if (store.state.user.access === undefined || store.state.user.access.length === 0) {
7
+ if (vnode.context.$store.state.user.access === undefined || vnode.context.$store.state.user.access.length === 0) {
9
8
  return
10
9
  }
11
10
  if (binding.value === undefined) {
12
11
  throw new Error('v-access必须有值')
13
12
  }
14
- if (store.state.user.access.indexOf(binding.value) === -1) {
13
+ if (vnode.context.$store.state.user.access.indexOf(binding.value) === -1) {
15
14
  el.parentNode.removeChild(el)
16
15
  }
17
16
  }
@@ -0,0 +1,215 @@
1
+ export const forEach = (arr, fn) => {
2
+ if (!arr.length || !fn) return
3
+ let i = -1
4
+ let len = arr.length
5
+ while (++i < len) {
6
+ let item = arr[i]
7
+ fn(item, i, arr)
8
+ }
9
+ }
10
+
11
+ /**
12
+ * @param {Array} arr1
13
+ * @param {Array} arr2
14
+ * @description 得到两个数组的交集, 两个数组的元素为数值或字符串
15
+ */
16
+ export const getIntersection = (arr1, arr2) => {
17
+ let len = Math.min(arr1.length, arr2.length)
18
+ let i = -1
19
+ let res = []
20
+ while (++i < len) {
21
+ const item = arr2[i]
22
+ if (arr1.indexOf(item) > -1) res.push(item)
23
+ }
24
+ return res
25
+ }
26
+
27
+ /**
28
+ * @param {Array} arr1
29
+ * @param {Array} arr2
30
+ * @description 得到两个数组的并集, 两个数组的元素为数值或字符串
31
+ */
32
+ export const getUnion = (arr1, arr2) => {
33
+ return Array.from(new Set([...arr1, ...arr2]))
34
+ }
35
+
36
+ /**
37
+ * @param {Array} target 目标数组
38
+ * @param {Array} arr 需要查询的数组
39
+ * @description 判断要查询的数组是否至少有一个元素包含在目标数组中
40
+ */
41
+ export const hasOneOf = (targetarr, arr) => {
42
+ return targetarr.some(_ => arr.indexOf(_) > -1)
43
+ }
44
+
45
+ /**
46
+ * @param {String|Number} value 要验证的字符串或数值
47
+ * @param {*} validList 用来验证的列表
48
+ */
49
+ export function oneOf (value, validList) {
50
+ for (let i = 0; i < validList.length; i++) {
51
+ if (value === validList[i]) {
52
+ return true
53
+ }
54
+ }
55
+ return false
56
+ }
57
+
58
+ /**
59
+ * @param {Number} timeStamp 判断时间戳格式是否是毫秒
60
+ * @returns {Boolean}
61
+ */
62
+ const isMillisecond = timeStamp => {
63
+ const timeStr = String(timeStamp)
64
+ return timeStr.length > 10
65
+ }
66
+
67
+ /**
68
+ * @param {Number} timeStamp 传入的时间戳
69
+ * @param {Number} currentTime 当前时间时间戳
70
+ * @returns {Boolean} 传入的时间戳是否早于当前时间戳
71
+ */
72
+ const isEarly = (timeStamp, currentTime) => {
73
+ return timeStamp < currentTime
74
+ }
75
+
76
+ /**
77
+ * @param {Number} num 数值
78
+ * @returns {String} 处理后的字符串
79
+ * @description 如果传入的数值小于10,即位数只有1位,则在前面补充0
80
+ */
81
+ const getHandledValue = num => {
82
+ return num < 10 ? '0' + num : num
83
+ }
84
+
85
+ /**
86
+ * @param {Number} timeStamp 传入的时间戳
87
+ * @param {Number} startType 要返回的时间字符串的格式类型,传入'year'则返回年开头的完整时间
88
+ */
89
+ const getDate = (timeStamp, startType) => {
90
+ const d = new Date(timeStamp * 1000)
91
+ const year = d.getFullYear()
92
+ const month = getHandledValue(d.getMonth() + 1)
93
+ const date = getHandledValue(d.getDate())
94
+ const hours = getHandledValue(d.getHours())
95
+ const minutes = getHandledValue(d.getMinutes())
96
+ const second = getHandledValue(d.getSeconds())
97
+ let resStr = ''
98
+ if (startType === 'year') resStr = year + '-' + month + '-' + date + ' ' + hours + ':' + minutes + ':' + second
99
+ else resStr = month + '-' + date + ' ' + hours + ':' + minutes
100
+ return resStr
101
+ }
102
+
103
+ /**
104
+ * @param {String|Number} timeStamp 时间戳
105
+ * @returns {String} 相对时间字符串
106
+ */
107
+ export const getRelativeTime = timeStamp => {
108
+ // 判断当前传入的时间戳是秒格式还是毫秒
109
+ const IS_MILLISECOND = isMillisecond(timeStamp)
110
+ // 如果是毫秒格式则转为秒格式
111
+ if (IS_MILLISECOND) Math.floor(timeStamp /= 1000)
112
+ // 传入的时间戳可以是数值或字符串类型,这里统一转为数值类型
113
+ timeStamp = Number(timeStamp)
114
+ // 获取当前时间时间戳
115
+ const currentTime = Math.floor(Date.parse(new Date()) / 1000)
116
+ // 判断传入时间戳是否早于当前时间戳
117
+ const IS_EARLY = isEarly(timeStamp, currentTime)
118
+ // 获取两个时间戳差值
119
+ let diff = currentTime - timeStamp
120
+ // 如果IS_EARLY为false则差值取反
121
+ if (!IS_EARLY) diff = -diff
122
+ let resStr = ''
123
+ const dirStr = IS_EARLY ? '前' : '后'
124
+ // 少于等于59秒
125
+ if (diff <= 59) resStr = diff + '秒' + dirStr
126
+ // 多于59秒,少于等于59分钟59秒
127
+ else if (diff > 59 && diff <= 3599) resStr = Math.floor(diff / 60) + '分钟' + dirStr
128
+ // 多于59分钟59秒,少于等于23小时59分钟59秒
129
+ else if (diff > 3599 && diff <= 86399) resStr = Math.floor(diff / 3600) + '小时' + dirStr
130
+ // 多于23小时59分钟59秒,少于等于29天59分钟59秒
131
+ else if (diff > 86399 && diff <= 2623859) resStr = Math.floor(diff / 86400) + '天' + dirStr
132
+ // 多于29天59分钟59秒,少于364天23小时59分钟59秒,且传入的时间戳早于当前
133
+ else if (diff > 2623859 && diff <= 31567859 && IS_EARLY) resStr = getDate(timeStamp)
134
+ else resStr = getDate(timeStamp, 'year')
135
+ return resStr
136
+ }
137
+
138
+ /**
139
+ * @returns {String} 当前浏览器名称
140
+ */
141
+ export const getExplorer = () => {
142
+ const ua = window.navigator.userAgent
143
+ const isExplorer = (exp) => {
144
+ return ua.indexOf(exp) > -1
145
+ }
146
+ if (isExplorer('MSIE')) return 'IE'
147
+ else if (isExplorer('Firefox')) return 'Firefox'
148
+ else if (isExplorer('Chrome')) return 'Chrome'
149
+ else if (isExplorer('Opera')) return 'Opera'
150
+ else if (isExplorer('Safari')) return 'Safari'
151
+ }
152
+
153
+ /**
154
+ * @description 绑定事件 on(element, event, handler)
155
+ */
156
+ export const on = (function () {
157
+ if (document.addEventListener) {
158
+ return function (element, event, handler) {
159
+ if (element && event && handler) {
160
+ element.addEventListener(event, handler, false)
161
+ }
162
+ }
163
+ } else {
164
+ return function (element, event, handler) {
165
+ if (element && event && handler) {
166
+ element.attachEvent('on' + event, handler)
167
+ }
168
+ }
169
+ }
170
+ })()
171
+
172
+ /**
173
+ * @description 解绑事件 off(element, event, handler)
174
+ */
175
+ export const off = (function () {
176
+ if (document.removeEventListener) {
177
+ return function (element, event, handler) {
178
+ if (element && event) {
179
+ element.removeEventListener(event, handler, false)
180
+ }
181
+ }
182
+ } else {
183
+ return function (element, event, handler) {
184
+ if (element && event) {
185
+ element.detachEvent('on' + event, handler)
186
+ }
187
+ }
188
+ }
189
+ })()
190
+
191
+ /**
192
+ * 判断一个对象是否存在key,如果传入第二个参数key,则是判断这个obj对象是否存在key这个属性
193
+ * 如果没有传入key这个参数,则判断obj对象是否有键值对
194
+ */
195
+ export const hasKey = (obj, key) => {
196
+ if (key) return key in obj
197
+ else {
198
+ let keysArr = Object.keys(obj)
199
+ return keysArr.length
200
+ }
201
+ }
202
+
203
+ /**
204
+ * @param {*} obj1 对象
205
+ * @param {*} obj2 对象
206
+ * @description 判断两个对象是否相等,这两个对象的值只能是数字或字符串
207
+ */
208
+ export const objEqual = (obj1, obj2) => {
209
+ const keysArr1 = Object.keys(obj1)
210
+ const keysArr2 = Object.keys(obj2)
211
+ if (keysArr1.length !== keysArr2.length) return false
212
+ else if (keysArr1.length === 0 && keysArr2.length === 0) return true
213
+ /* eslint-disable-next-line */
214
+ else return !keysArr1.some(key => obj1[key] != obj2[key])
215
+ }
package/src/main.js CHANGED
@@ -8,15 +8,24 @@ import importDirective from '@/directive'
8
8
 
9
9
  Vue.config.productionTip = false
10
10
 
11
- /**
12
- * 注册指令
13
- */
14
- importDirective(Vue)
15
-
16
- Vue.use(ExtentionPlugin)
17
- Vue.use(iView, {
18
- i18n: (key, value) => i18n.t(key, value)
19
- })
20
- new Vue({
11
+
12
+
13
+
14
+ export const init = (outVue, outApp) => {
15
+ /**
16
+ * 注册指令
17
+ */
18
+ importDirective(outVue)
19
+ outVue.use(ExtentionPlugin)
20
+ outVue.use(iView, {
21
+ i18n: (key, value) => i18n.t(key, value)
22
+ })
23
+ console.log('init')
24
+ }
25
+
26
+
27
+ const vueApp = new Vue({
21
28
  render: h => h(App),
22
29
  }).$mount('#app')
30
+ init(Vue, vueApp)
31
+
@@ -1,30 +0,0 @@
1
- import Clipboard from 'clipboard'
2
- export default {
3
- bind: (el, binding) => {
4
- const clipboard = new Clipboard(el, {
5
- text: () => binding.value.value
6
- })
7
- el.__success_callback__ = binding.value.success
8
- el.__error_callback__ = binding.value.error
9
- clipboard.on('success', e => {
10
- const callback = el.__success_callback__
11
- callback && callback(e)
12
- })
13
- clipboard.on('error', e => {
14
- const callback = el.__error_callback__
15
- callback && callback(e)
16
- })
17
- el.__clipboard__ = clipboard
18
- },
19
- update: (el, binding) => {
20
- el.__clipboard__.text = () => binding.value.value
21
- el.__success_callback__ = binding.value.success
22
- el.__error_callback__ = binding.value.error
23
- },
24
- unbind: (el, binding) => {
25
- delete el.__success_callback__
26
- delete el.__error_callback__
27
- el.__clipboard__.destroy()
28
- delete el.__clipboard__
29
- }
30
- }