陶杰
2024-09-11 8840efb29d8818afcfd2176914bcf4811d9c60fe
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import moment from 'moment'
const utils = {
    checkCanMark(timestamp) {
        if (!!timestamp) {
            return new Date(timestamp).getTime() - (new Date()).getTime() <= 300000
        }
        return false
    },
    formatRichText(html) {
        // let newContent= html.replace(/<img[^>]*>/gi,function(match,capture){
        //   match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
        //   match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
        //   match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
        //   return match;
        // });
        // newContent = newContent.replace(/style="[^"]+"/gi,function(match,capture){
        //   match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
        //   return match;
        // });
        // newContent = newContent.replace(/<br[^>]*\/>/gi, '');
        // newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');
        let newContent = html.replace(/<video[^>]*>/gi, function(match, capture) {
            match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
            match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
            match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
            return match;
        }).replace(/<img[^>]*>/gi, function(match, capture) {
            match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
            match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
            match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
            return match;
        });
        newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
            match = match.replace(/width:[^;]+;/gi, 'max-width:100%;width:100%;').replace(/width:[^;]+;/gi,
                'max-width:100%;width:100%;');
            return match;
        });
        newContent = newContent.replace(/<br[^>]*\/>/gi, '');
        newContent = newContent.replace(/\<video/gi,
            '<video style="max-width:100%;width:100%;height:auto;display:inline-block;margin:10rpx auto;"').replace(
            /\<img/gi,
            '<img style="max-width:100%;width:100%;height:auto;display:inline-block;margin:10rpx auto;"');
 
        newContent = newContent.replaceAll('style="">', ' mode="widthFix"/>')
        newContent = newContent.replaceAll('style=""/>', ' />')
        // newContent = newContent.replace('style=""/>', ' />')
        // newContent = newContent.replace('<img ', '<img ')
        // newContent = newContent.replace(' mode="widthFix"/>')
        
        // return '<image style="max-width:200rpx;min-height:200rpx; height:auto;display:inline-block;margin:10rpx auto;" src="https://xy-go.oss-cn-beijing.aliyuncs.com/80/80a854925d49458ca945c85d6b3c8e99WechatIMG5.jpg" alt="" data-href=""  mode="widthFix"/>'
        // return '<img style="max-width:100px;width:50px;height:auto;display:inline-block;margin:10rpx auto;" src="https://xy-go.oss-cn-beijing.aliyuncs.com/80/80a854925d49458ca945c85d6b3c8e99WechatIMG5.jpg"  alt="" data-href=""  mode="widthFix"/>'
        // "<p style="text-align: center;"><img src="https://xy-go.oss-cn-beijing.aliyuncs.com/80/80a854925d49458ca945c85d6b3c8e99WechatIMG5.jpg" alt="" data-href="" style=""><img src="https://xy-go.oss-cn-beijing.aliyuncs.com/60/60bff4a8d1c644bbbbcb0d7bec864e03WechatIMG6.jpg" alt="" data-href="" style=""></p><p style="text-align: center;"><img src="https://xy-go.oss-cn-beijing.aliyuncs.com/79/7979091cabdf4c54ae5a7d08a18f0570WechatIMG34.jpg" alt="" data-href="" style=""><img src="https://xy-go.oss-cn-beijing.aliyuncs.com/bb/bb8b18e8d7444dc0b918b2129158c97bWechatIMG20.jpg" alt="" data-href="" style="width: 281.50px;height: 284.69px;"></p>"
        // return '<img style="max-width:100%;width:100%;height:auto;display:inline-block;margin:10rpx auto;" src="https://xy-go.oss-cn-beijing.aliyuncs.com/80/80a854925d49458ca945c85d6b3c8e99WechatIMG5.jpg" alt="" data-href=""  mode="widthFix"/>'
        // <img style="max-width:600rpx;width:600rpx;height:auto;display:inline-block;margin:10rpx auto;" src="https://xy-go.oss-cn-beijing.aliyuncs.com/60/60bff4a8d1c644bbbbcb0d7bec864e03WechatIMG6.jpg" alt="" data-href="" ><p style="text-align: center;"><img style="max-width:600rpx;width:600rpx;height:auto;display:inline-block;margin:10rpx auto;" src="https://xy-go.oss-cn-beijing.aliyuncs.com/79/7979091cabdf4c54ae5a7d08a18f0570WechatIMG34.jpg" alt="" data-href="" style=""><img style="max-width:600rpx;width:600rpx;height:auto;display:inline-block;margin:10rpx auto;" src="https://xy-go.oss-cn-beijing.aliyuncs.com/bb/bb8b18e8d7444dc0b918b2129158c97bWechatIMG20.jpg" alt="" data-href="" ></p>
        return newContent;
        // return html
    },
    getMin(time) {
        if (!time) {
            return '-'
        }
        if (time < 0) {
            time = -time
        }
        if (time < 60) {
            var t1 = parseFloat(time).toFixed(0)
            return '00:' + (t1 < 10 ? '0' + t1 : t1) + ''
        }
        var min = parseInt(time / 60)
        var t = (time - min * 60).toFixed(0)
        // if(min<60){
        return (min < 10 ? ('0' + min) : min) + ': ' + (t < 10 ? ('0' + t) : t)
        // }
        // else{
        //     var h = parseInt(min /60)
        //     t = (min - h*60).toFixed(0)
        //     return ( h<10?('0'+ h): h) + '小时' +  (t<10?('0'+t):t)+""
 
        // }
    },
    toDateByTime(timestamp) {
        if (!!timestamp) {
            return moment(timestamp).format('YYYY-MM-DD HH:mm')
        }
    },
    toDateTime(date) {
        if (date) {
            return moment(date).format('YYYY-MM-DD HH:mm:ss')
        }
        return ''
    },
    toDateTime33(date) {
        if (date) {
            return moment(date).format('DD日HH:mm:ss')
        }
        return ''
    },
    toDateMin(date) {
        if (date) {
            return moment(date).format('YYYY-MM-DD HH:mm')
        }
        return ''
    },
    toDateMinOnly(date) {
        if (date) {
            return moment(date).format('HH:mm')
        }
        return ''
    },
    toDateSecOnly(date) {
        if (date) {
            return moment(date).format('HH:mm:ss')
        }
        return ''
    },
    toDateDay(date) {
        if (date) {
            return moment(date).format('MM-DD HH:mm:ss')
        }
        return ''
    },
    toDateSec(date) {
        if (date) {
            return moment(date).format('YYYY-MM-DD HH:mm:ss')
        }
        return ''
    },
    toYear(date) {
        if (date) {
            return moment(date).format('YYYY')
        }
        return ''
    },
    toDate(date) {
        if (date) {
            return moment(date).format('YYYY-MM-DD')
        }
        return ''
    },
    formatDate(date, format) {
        if (date) {
            return moment(date).format(format)
        }
        return ''
    },
    isTrueEmpty(str) {
        if (str || str === 0 || str === false) {
            return false
        }
        return true
    },
    // 是否为数字
    isNumber(str) {
        if (!utils.isTrueEmpty(str)) {
            return !isNaN(str)
        }
        return false
    },
    // 是否为正数
    isPositive(str) {
        if (utils.isNumber(str)) {
            if (str > 0) {
                return true
            }
        }
        return false
    },
    // 是否为负数
    isNegative(str) {
        if (utils.isNumber(str)) {
            if (str < 0) {
                return true
            }
        }
        return false
    },
    // 是否是整数
    isInteger(str) {
        if (utils.isNumber(str)) {
            if (str % 1 === 0) {
                return true
            }
        }
        return false
    },
    // 打电话
    callPhone(phone) {
        uni.makePhoneCall({
            phoneNumber: phone
        });
    },
    // 保留小数位数
    toFixed(value, num) {
        value = value.toFixed(num)
        value = Number(value).toString()
        return value
    },
    // 是否是闰年
    isLeapYear(year) {
        year = parseInt(year, 10);
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            return true;
        } else {
            return false;
        }
    },
    // 手机号格式是否正确
    isMobilePhone(value) {
        const reg = /^1\d{10}$/
        if (reg.test(value)) {
            return true
        }
        return false
    },
    isTelephone(value) {
        const reg =
            /^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$/
        if (reg.test(value)) {
            return true
        }
        return false
    },
    // 邮箱格式是否正确
    isEmail(value) {
        const reg = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,}$/
        if (reg.test(value)) {
            return true
        }
        return false
    },
    isIdCard: (value) => {
        const reg =
            /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/
        if (reg.test(value)) {
            return true
        }
        return false
    },
    hex2rgb(color) {
        color = color.toLowerCase()
        let array = [];
        for (let i = 1; i < 7; i += 2) {
            array.push(parseInt("0x" + color.slice(i, i + 2)));
        }
        return array
    },
    delay(time) {
        return new Promise((resolve) => {
            setTimeout(() => {
                resolve()
            }, time)
        })
    },
    // 显示textarea的值
    toTextareaText(str) {
        if (str) {
            return str.replace(/\n/g, '<br/>')
        }
        return ''
    },
    // 获取天数
    getDay: (year, month) => {
        year = parseInt(year, 10);
        month = parseInt(month, 10);
 
        let dayNum = 0;
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                dayNum = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                dayNum = 30;
                break;
            case 2:
                dayNum = utils.isLeapYear(year) ? 29 : 28;
                break;
        }
        return dayNum;
    },
    /**
     * @description 铺平树形结构为一维数组
     * @function flatten
     * @param {Object|Object[]} arr 树节点或树节点数组
     * @param {string} childKey=children 树子节点数组属性
     * @returns {Object[]}
     */
    flatten: (arr, childKey = 'children') => {
        const flattenArray = (arr, childKey) => {
            if (Array.isArray(arr))
                return Array.prototype.concat.apply(
                    [],
                    arr.map((i) => flattenArray(i, childKey))
                )
            else if (arr.hasOwnProperty(childKey))
                return [arr, ...flattenArray(arr[childKey], childKey)]
            return [arr]
        }
        return flattenArray(arr, childKey)
    },
}
 
// const patternString = {
//     mobilePhone: '^1\\d{10}$',
//     telePhone: '^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$',
//     phone: '(^1\\d{10}$)|(^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)',
//     email: '^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z0-9]{2,}$',
//     idCard: '^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$|^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$',
//     nonnegativeInteger: '^[0-9]+?$',
//     isAmount: '^(([0-9]+)|([0-9]+\.[0-9]{0,2}))$',
// }
 
// Vue.prototype.$utils = utils
// Vue.prototype.$patternString = patternString
 
export default utils