shijiplus-web-plugin 0.1.0

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.
@@ -0,0 +1,265 @@
1
+ /* eslint-disable no-extend-native */
2
+ const install = (vue, opts = {}) => {
3
+ console.log('install datePlugin')
4
+ vue.prototype.$formatDate = formatDate
5
+ vue.prototype.$getWeekLastDay = getWeekLastDay
6
+ vue.prototype.$getWeekFirstDay = getWeekFirstDay
7
+ vue.prototype.$getMonthFirstDay = getMonthFirstDay
8
+ vue.prototype.$getMonthLastDay = getMonthLastDay
9
+ vue.prototype.$getDayBeginTime = getDayBeginTime
10
+ vue.prototype.$getDayEndTime = getDayEndTime
11
+
12
+ vue.prototype.$addSeconds = addSeconds
13
+ vue.prototype.$addMinutes = addMinutes
14
+ vue.prototype.$addHours = addHours
15
+ vue.prototype.$addDays = addDays
16
+ vue.prototype.$addMonths = addMonths
17
+ vue.prototype.$addYears = addYears
18
+ vue.prototype.$getDateFromString = getDateFromString
19
+ vue.prototype.$formatterDateString = formatterDateString
20
+ vue.prototype.$dateDiff = dateDiff
21
+ vue.prototype.$beautyWeekDay = beautyWeekDay
22
+ Date.prototype.addDays = function (days) {
23
+ return addDays(this, days)
24
+ }
25
+ Date.prototype.formatDate = function (fmt) {
26
+ return formatDate(this, fmt)
27
+ }
28
+ Date.prototype.endTime = function (fmt) {
29
+ return getDayEndTime(this, fmt)
30
+ }
31
+
32
+ Date.prototype.beginTime = function (fmt) {
33
+ return getDayBeginTime(this, fmt)
34
+ }
35
+ }
36
+
37
+ function formatterDateString(dateStr, fmt = 'yyyy-MM-dd') {
38
+ if (!dateStr) {
39
+ return '--'
40
+ }
41
+ let date = getDateFromString(dateStr)
42
+ return formatDate(date, fmt)
43
+ }
44
+
45
+ function convertStrToDate(dateStr, fmt) {
46
+ var tempPa = dateStr
47
+ if (tempPa instanceof Date) {
48
+ return tempPa
49
+ }
50
+
51
+ if (fmt && typeof dateStr === 'string') {
52
+ let YFmt = /y+/.exec(fmt)
53
+ var year = ''
54
+ if (YFmt) {
55
+ year = dateStr.substr(YFmt.index, YFmt[0].length)
56
+ }
57
+ let MFmt = /M+/.exec(fmt)
58
+ var month = ''
59
+ if (MFmt) {
60
+ month = dateStr.substr(MFmt.index, MFmt[0].length)
61
+ }
62
+ let dFmt = /d+/.exec(fmt)
63
+ var date = ''
64
+ if (dFmt) {
65
+ date = dateStr.substr(dFmt.index, dFmt[0].length)
66
+ }
67
+ let HFmt = /H+/.exec(fmt)
68
+ var hours = ''
69
+ if (HFmt) {
70
+ hours = dateStr.substr(HFmt.index, HFmt[0].length)
71
+ }
72
+ let hFmt = /h+/.exec(fmt)
73
+ if (hFmt) {
74
+ hours = dateStr.substr(hFmt.index, hFmt[0].length)
75
+ }
76
+ let mFmt = /m+/.exec(fmt)
77
+ var minutes = ''
78
+ if (mFmt) {
79
+ minutes = dateStr.substr(mFmt.index, mFmt[0].length)
80
+ }
81
+ let sFmt = /s+/.exec(fmt)
82
+ var seconds = ''
83
+ if (sFmt) {
84
+ seconds = dateStr.substr(sFmt.index, sFmt[0].length)
85
+ }
86
+ tempPa = new Date(year, month - 1, date, hours, minutes, seconds)
87
+ }
88
+ let newDate = new Date(tempPa)
89
+ if (new Date(tempPa) == 'Invalid Date') {
90
+ return tempPa
91
+ }
92
+ return newDate
93
+ }
94
+
95
+ function getDateFromString(dateStr, fmt = null) {
96
+ var tempPa = dateStr
97
+ if (dateStr instanceof Date) {
98
+ dateStr = formatDate(dateStr, fmt)
99
+ }
100
+ if (fmt && typeof dateStr === 'string') {
101
+ let fDate = new Date()
102
+ let YFmt = /y+/.exec(fmt)
103
+ var year = ''
104
+ if (YFmt) {
105
+ year = dateStr.substr(YFmt.index, YFmt[0].length)
106
+ }
107
+ let MFmt = /M+/.exec(fmt)
108
+ var month = ''
109
+ if (MFmt) {
110
+ month = dateStr.substr(MFmt.index, MFmt[0].length)
111
+ }
112
+ let dFmt = /d+/.exec(fmt)
113
+ var date = ''
114
+ if (dFmt) {
115
+ date = dateStr.substr(dFmt.index, dFmt[0].length)
116
+ }
117
+ let HFmt = /H+/.exec(fmt)
118
+ var hours = ''
119
+ if (HFmt) {
120
+ hours = dateStr.substr(HFmt.index, HFmt[0].length)
121
+ }
122
+ let hFmt = /h+/.exec(fmt)
123
+ if (hFmt) {
124
+ hours = dateStr.substr(hFmt.index, hFmt[0].length)
125
+ }
126
+ let mFmt = /m+/.exec(fmt)
127
+ var minutes = ''
128
+ if (mFmt) {
129
+ minutes = dateStr.substr(mFmt.index, mFmt[0].length)
130
+ }
131
+ let sFmt = /s+/.exec(fmt)
132
+ var seconds = ''
133
+ if (sFmt) {
134
+ seconds = dateStr.substr(sFmt.index, sFmt[0].length)
135
+ }
136
+ tempPa = new Date(year, month - 1, date, hours, minutes, seconds)
137
+ }
138
+ let newDate = new Date(tempPa)
139
+ if (new Date(tempPa) == 'Invalid Date') {
140
+ return tempPa
141
+ }
142
+ return newDate
143
+ }
144
+ function addSeconds(date, seconds = 0) {
145
+ var newDate = new Date(date)
146
+ newDate.setSeconds(newDate.getSeconds() + (seconds || 0))
147
+ return newDate
148
+ }
149
+ function addMinutes(date, minutes = 0) {
150
+ var newDate = new Date(date)
151
+ newDate.setMinutes(newDate.getMinutes() + (minutes || 0))
152
+ return newDate
153
+ }
154
+ function addHours(date, hours = 0) {
155
+ var newDate = new Date(date)
156
+ newDate.setHours(newDate.getHours() + (hours || 0))
157
+ return newDate
158
+ }
159
+ function addDays(date, days = 0) {
160
+ var newDate = new Date(date)
161
+ newDate.setDate(newDate.getDate() + (days || 0))
162
+ return newDate
163
+ }
164
+ function addMonths(date, months = 0) {
165
+ var newDate = new Date(date)
166
+ newDate.setMonth(newDate.getMonth() + (months || 0))
167
+ return newDate
168
+ }
169
+ function addYears(date, years = 0) {
170
+ var newDate = new Date(date)
171
+ newDate.setFullYear(newDate.getFullYear() + (years || 0))
172
+ return newDate
173
+ }
174
+
175
+ // 获取本周第一天
176
+ function getWeekFirstDay(date) {
177
+ let dayOfWeek = date.getDay()
178
+ return addDays(date, dayOfWeek == 0 ? -6 : 5 - dayOfWeek)
179
+ }
180
+
181
+ // 获取本周最后一天
182
+ function getWeekLastDay(date) {
183
+ let diff = (7 - date.getDay()) % 7
184
+ return addDays(date, diff)
185
+ }
186
+
187
+ function beautyWeekDay(date) {
188
+ const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
189
+ const today = date.getDay()
190
+ const dayOfWeek = weekdays[today]
191
+ console.log(dayOfWeek) // 输出当天星期几
192
+ return dayOfWeek
193
+ }
194
+
195
+ // 获取本月第一天一天
196
+ function getMonthFirstDay(date) {
197
+ return new Date(date.getFullYear(), date.getMonth(), 1, date.getHours(), date.getMinutes(), date.getSeconds())
198
+ }
199
+ // 获取本月最后一天
200
+ function getMonthLastDay(date) {
201
+ var currentMonth = date.getMonth()
202
+ var nextMonth = ++currentMonth
203
+ var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1)
204
+ return addDays(nextMonthFirstDay, -1)
205
+ }
206
+
207
+ // 获取当天起始时间
208
+ function getDayBeginTime(date, fmt = null) {
209
+ let result = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
210
+ if (!fmt) {
211
+ return result
212
+ }
213
+ return formatDate(result, fmt)
214
+ }
215
+ // 获取当天结束时间
216
+ function getDayEndTime(date, fmt = null) {
217
+ let result = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
218
+ if (!fmt) {
219
+ return result
220
+ }
221
+ return formatDate(result, fmt)
222
+ }
223
+
224
+ function formatDate(date, fmt = 'yyyy-MM-dd HH:mm:ss') {
225
+ let newDate = date
226
+ if (!newDate) {
227
+ return ''
228
+ }
229
+ if (typeof newDate == 'string') {
230
+ newDate = convertStrToDate(newDate, fmt)
231
+ }
232
+ if (!fmt) {
233
+ fmt = 'yyyy-MM-dd HH:mm:ss'
234
+ }
235
+ if (/(y+)/.test(fmt)) {
236
+ fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
237
+ }
238
+ let o = {
239
+ 'M+': newDate.getMonth() + 1,
240
+ 'd+': newDate.getDate(),
241
+ 'h+': (24 - newDate.getHours()),
242
+ 'H+': newDate.getHours(),
243
+ 'm+': newDate.getMinutes(),
244
+ 's+': newDate.getSeconds()
245
+ }
246
+ for (let k in o) {
247
+ if (new RegExp(`(${k})`).test(fmt)) {
248
+ let str = o[k] + ''
249
+ fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str))
250
+ }
251
+ }
252
+ return fmt
253
+ }
254
+
255
+ function padLeftZero(str) {
256
+ return ('00' + str).substr(str.length)
257
+ }
258
+
259
+ function dateDiff(d1, d2) {
260
+ return new Date(d2) - new Date(d1)
261
+ }
262
+
263
+ export default {
264
+ install
265
+ }
@@ -0,0 +1,18 @@
1
+
2
+ import arrayPlugin from '@/extentionPlugin/array'
3
+ import datePlugin from '@/extentionPlugin/date'
4
+ import stringPlugin from '@/extentionPlugin/string'
5
+ import objectPlugin from '@/extentionPlugin/object'
6
+ import regularPlugin from '@/extentionPlugin/regular'
7
+
8
+ const install = (vue) => {
9
+ vue.use(arrayPlugin)
10
+ vue.use(datePlugin)
11
+ vue.use(stringPlugin)
12
+ vue.use(objectPlugin)
13
+ vue.use(regularPlugin)
14
+ }
15
+
16
+ export default {
17
+ install
18
+ }
@@ -0,0 +1,114 @@
1
+
2
+ import Vue from 'vue'
3
+ const install = (vue, opts = {}) => {
4
+ vue.prototype.$trimObj = trimObj
5
+ vue.prototype.$isEmpty = isEmpty
6
+ vue.prototype.$containKey = containKey
7
+ // 对象的深拷贝
8
+ vue.prototype.$deepCopy = function deepCopy(obj) {
9
+ if (typeof obj === 'string') {
10
+ return String(obj)
11
+ }
12
+ var result = Array.isArray(obj) ? [] : {}
13
+ for (var key in obj) {
14
+ if (obj.hasOwnProperty(key)) {
15
+ if (typeof obj[key] === 'object' && obj[key] !== null) {
16
+ if (obj[key] instanceof Date) {
17
+ result[key] = new Date(obj[key])
18
+ } else {
19
+ result[key] = deepCopy(obj[key])
20
+ }
21
+ } else {
22
+ result[key] = obj[key]
23
+ }
24
+ }
25
+ }
26
+ return result
27
+ }
28
+ vue.prototype.$forEachObj = forEachObj
29
+ vue.prototype.$objValueFromArr = objValueFromArr
30
+ vue.prototype.$convertNumberToString = convertNumberToString
31
+ vue.prototype.$merge = (target, source) => {
32
+ const srcKeys = Object.keys(source)
33
+ const tgKeys = Object.keys(target)
34
+ const resultKeys = new Set([...tgKeys, ...srcKeys])
35
+ const result = {}
36
+ resultKeys.forEach(key => {
37
+ result[key] = null
38
+ if (source[key]) {
39
+ result[key] = source[key]
40
+ } else if (target[key]) {
41
+ result[key] = target[key]
42
+ }
43
+ })
44
+ return result
45
+ }
46
+ }
47
+
48
+ function convertNumberToString(obj) {
49
+ forEachObj(obj, (item, key) => {
50
+ if (typeof item === 'object' && !Array.isArray(item)) {
51
+ convertNumberToString(item)
52
+ } else if (typeof item === 'number') {
53
+ if (key.toLowerCase().indexOf('id') < 0 && Vue.prototype.$regular.zeroNumber.test(item)) {
54
+ obj[key] = String(item)
55
+ }
56
+ }
57
+ })
58
+ return obj
59
+ }
60
+ function objValueFromArr(arr, key) {
61
+ let tempValues = []
62
+ arr.forEach(item => {
63
+ tempValues.push(item[key])
64
+ })
65
+ return tempValues
66
+ }
67
+
68
+ function forEachObj(obj, callback) {
69
+ for (let key in obj) {
70
+ if (obj.hasOwnProperty(key)) {
71
+ callback(obj[key], key)
72
+ }
73
+ }
74
+ }
75
+
76
+ function trimObj(obj) {
77
+ if (obj !== undefined) {
78
+ forEachObj(obj, (item, key) => {
79
+ if (typeof item === 'string') {
80
+ obj[key] = Vue.prototype.$trim(item)
81
+ }
82
+ if (typeof item === 'object') {
83
+ trimObj(item)
84
+ }
85
+ })
86
+ }
87
+ }
88
+
89
+ function isEmpty(obj) {
90
+ if (obj === undefined || obj === null) {
91
+ return true
92
+ }
93
+ if (typeof obj === 'object' && Object.keys(obj).length === 0) {
94
+ return true
95
+ }
96
+ if (typeof obj === 'string' && obj.length === 0) {
97
+ return true
98
+ }
99
+ return false
100
+ }
101
+
102
+ function containKey(obj, key) {
103
+ if (obj === undefined || obj === null) {
104
+ return false
105
+ }
106
+ if (typeof obj === 'string') {
107
+ throw new Error('containKey---传入的参数非对象')
108
+ }
109
+ return Object.keys(obj).indexOf(key) >= 0
110
+ }
111
+
112
+ export default {
113
+ install
114
+ }
@@ -0,0 +1,72 @@
1
+ const install = (vue, opts = {}) => {
2
+ vue.prototype.$regular = regular
3
+ vue.prototype.$regular.floatPointPlace = (len) => {
4
+ return new RegExp('^(([1-9]\\d*)|0)(\\.\\d{1,' + len + '})?$')
5
+ }
6
+ vue.prototype.$regular.numberLength = (len) => {
7
+ return new RegExp('^[1-9][0-9]{0,' + (len - 1) + '}$')
8
+ }
9
+ vue.prototype.$regular.zeroNumberLength = (len) => {
10
+ return new RegExp('^[0-9]{1,' + (len) + '}$')
11
+ }
12
+ vue.prototype.$regular.empt0NumberLength = (len) => {
13
+ return new RegExp('^[0-9]{0,' + (len) + '}$')
14
+ }
15
+ vue.prototype.$regular.prefix0Number = (value) => {
16
+ let numStr = String(value)
17
+ if (!numStr) {
18
+ return false
19
+ }
20
+ if (numStr.length == 1) {
21
+ return true
22
+ }
23
+ // 多个0的情况
24
+ if (numStr[0] == 0 && numStr[1] != '.') {
25
+ return false
26
+ }
27
+ // // 全是0的浮点数
28
+ // if (numStr[1] == '.') {
29
+ // return Number(numStr.replace('.', '')) != 0
30
+ // }
31
+ return true
32
+ }
33
+ vue.prototype.$regular.isNumber = (value) => {
34
+ if (value !== 0 && (value == null || value == undefined || value == '')) {
35
+ return false
36
+ }
37
+ return !isNaN(Number(value))
38
+ }
39
+
40
+ vue.prototype.$regular.nonnegativeInteger = (value) => {
41
+ if (!regular.zeroNumber.test(value)) {
42
+ return false
43
+ }
44
+ if (!vue.prototype.$regular.prefix0Number(value)) {
45
+ return false
46
+ }
47
+ return vue.prototype.$regular.isNumber(value)
48
+ }
49
+ vue.prototype.$regular.isPositiveInteger = (value) => {
50
+ if (!regular.number.test(value)) {
51
+ return false
52
+ }
53
+ if (!vue.prototype.$regular.prefix0Number(value)) {
54
+ return false
55
+ }
56
+ return vue.prototype.$regular.isNumber(value)
57
+ }
58
+ }
59
+ const regular = {
60
+ phoneNumber: /^[1][3-9][0-9]{9}$/,
61
+ password: /^[a-zA-Z0-9]{8,16}$/,
62
+ verifyCode: /^[a-zA-Z0-9]{4,10}$/,
63
+ number: /^[1-9][0-9]*$/,
64
+ zeroNumber: /^[0-9]*$/,
65
+ floatNumber: /^(([1-9]\d*)|0)(\.\d{1,2})?$/,
66
+ email: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
67
+ VARCHAR20: /^.{1,20}$/,
68
+ money: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
69
+ }
70
+ export default {
71
+ install
72
+ }
@@ -0,0 +1,113 @@
1
+ const install = (vue, opts = {}) => {
2
+ vue.prototype.$trim = trim
3
+ vue.prototype.$trimStart = trimStart
4
+ vue.prototype.$trimEnd = trimEnd
5
+ vue.prototype.$formtterCityName = formtterCityName
6
+ vue.prototype.$parseUrl = parseUrl
7
+ vue.prototype.$beautyNum = beautyNum
8
+ vue.prototype.$formatterPhoneNo = formatterPhoneNo
9
+ }
10
+
11
+ function formtterCityName(proName, cityName) {
12
+ let reg = RegExp(`(${proName}+)`)
13
+ if (reg.test(cityName)) {
14
+ return cityName
15
+ }
16
+ return `${proName}/${cityName}`
17
+ }
18
+
19
+ // 格式化手机号
20
+ function formatterPhoneNo(oriStr) {
21
+ var reg = /^(\d{3})\d+(\d{4})$/
22
+ if (oriStr) {
23
+ return oriStr.replace(reg, '$1****$2')
24
+ }
25
+ }
26
+
27
+ function convertChar(oriChar) {
28
+ if ('+.$()*[]{}?|'.indexOf(oriChar) >= 0) {
29
+ return `\\${oriChar}`
30
+ }
31
+ return oriChar
32
+ }
33
+
34
+ // 去除字符串头尾空格或指定字符
35
+ function trim(text, c = '\\s') {
36
+ c = convertChar(c)
37
+ var rg = new RegExp('(^' + c + '*)|(' + c + '*$)', 'g')
38
+ return text.replace(rg, '')
39
+ }
40
+ // 去除字符串头部空格或指定字符
41
+ function trimStart(text, c = '\\s') {
42
+ c = convertChar(c)
43
+ var rg = new RegExp('^' + c + '*', 'g')
44
+ return text.replace(rg, '')
45
+ }
46
+
47
+ // 去除字符串尾部空格或指定字符
48
+ function trimEnd(text, c = '\\s') {
49
+ c = convertChar(c)
50
+ var rg = new RegExp(c + '*$', 'g')
51
+ return text.replace(rg, '')
52
+ }
53
+
54
+ function formatQuery(str) {
55
+ return str.split('&').reduce((a, b) => {
56
+ let arr = b.split('=')
57
+ a[arr[0]] = arr[1]
58
+ return a
59
+ }, {})
60
+ }
61
+ function parseUrl(url) {
62
+ let urlObj = {
63
+ protocol: /^(.+):\/\//,
64
+ host: /:\/\/(.+?)[?#\s/]/,
65
+ path: /(\w||\s)(\/.*?)[?#\s]/,
66
+ query: /\?(.+?)[#/\s]/,
67
+ hash: /#(\w+)\s$/
68
+ }
69
+ url += ' '
70
+ for (let key in urlObj) {
71
+ let pattern = urlObj[key]
72
+ if (key === 'query') {
73
+ urlObj[key] = (pattern.exec(url) && formatQuery(pattern.exec(url)[1]))
74
+ } else if (key === 'path') {
75
+ urlObj[key] = (pattern.exec(url) && pattern.exec(url)[2])
76
+ } else {
77
+ urlObj[key] = (pattern.exec(url) && pattern.exec(url)[1])
78
+ }
79
+ }
80
+ return urlObj
81
+ }
82
+ function beautyNum(num, options = { signed: false, toFixed: 2 }) {
83
+ if (typeof num == 'number') {
84
+ if (isNaN(num)) {
85
+ return 0
86
+ }
87
+ const absNum = Math.abs(num)
88
+ let result = absNum
89
+ const toFixed = options.toFixed == null || options.toFixed == undefined ? 2 : options.toFixed
90
+ if (absNum > 10000 * 10000) {
91
+ result = `${(absNum / (10000 * 10000)).toFixed(toFixed)}亿`
92
+ } else if (absNum > 10000 * 1000) {
93
+ result = `${(absNum / (10000 * 1000)).toFixed(toFixed)}千万`
94
+ } else if (absNum > 10000 * 100) {
95
+ result = `${(absNum / (10000 * 100)).toFixed(toFixed)}百万`
96
+ } else if (absNum > 10000) {
97
+ result = `${(absNum / 10000).toFixed(toFixed)}万`
98
+ } else {
99
+ if (String(num).indexOf('.') != -1) {
100
+ result = absNum.toFixed(toFixed)
101
+ }
102
+ }
103
+ if (options.signed) {
104
+ return (num >= 0 ? '+' : '-') + result
105
+ }
106
+ return result
107
+ }
108
+ return num
109
+ }
110
+
111
+ export default {
112
+ install
113
+ }