Mr.Shi
2023-08-21 3d1a4b4bf3d37e29a3d3345e00f9f84c0d7959a6
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
import * as dd from 'dingtalk-jsapi'
import sha1 from 'js-sha1'
import store from '@/store'
 
/**
 * 获取当前时间 时间戳
 * @returns {number}
 */
export function getTimestamp () {
    return new Date().getTime()
}
 
/**
 * 获取随机字符串
 * @param {number} len 可指定长度
 * @returns {string}
 */
export function randomString (len: number): string {
    len = len || 32
    let $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
    let maxPos = $chars.length
    let pwd = ''
    for (let i = 0; i < len; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos))
    }
    return pwd
}
 
/**
 * 拼接签名字符串
 * @param {string} ticket 通过access_token获取的jsapi_ticket
 * @param {string} noncestr 自己生成的随机字符串
 * @param {string} timestamp 生成签名时的时间戳
 * @param {string} urlLink url地址获取code
 * @param {string} return {*}
 */
export function getSignature (ticket: String, noncestr: String, timestamp: String, urlLink: String): String {
    let url = urlLink
    let jsapi_ticket = `jsapi_ticket=${ticket}&noncestr=${noncestr}&timestamp=${timestamp}&url=${url}`
    return sha1(jsapi_ticket)
}
 
/***
 * phoneRegular: 手机号验证
 * mailboxRegular: 邮箱验证
 * positiveInteger: 正整数
 * decimal: 小数(只支持四位小数)
 * positiveIntegerDecimal: 正整数小数(最多四位小数)
 * number:数字最多保留四位小数
 */
const REGULAR = {
    phoneRegular: /^1[3456789]\d{9}$/,
    mailboxRegular: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
    positiveInteger: /^[0-9]*[1-9][0-9]*$/,
    decimal: /^0$|^[1-9]\d{0,15}$|^[1-9]\d{0,15}\.{1}\d{1,4}$|^0\.{1}\d{1,4}$/g,
    positiveIntegerDecimal: /^([1-9][0-9]*|0)(\.[0-9]?[1-9][1-9][1-9])?$/,
    number: /^\d+(?:\.\d{1,4})?$/
}
 
/***
 * 判断设备机型适配底部安全距离
 * true 是
 * false 否
 */
const equipment = () => {
    // X XS, XS Max, XR,11, 11pro,11pro max,12mini,12, 12 pro,12 pro max
    const xSeriesConfig = [
        {
            devicePixelRatio: 3,
            width: 375,
            height: 812,
        },
        {
            devicePixelRatio: 3,
            width: 414,
            height: 896,
        },
        {
            devicePixelRatio: 2,
            width: 414,
            height: 896,
        },
        {
            devicePixelRatio: 3,
            width: 315,
            height: 812,
        },
        {
            devicePixelRatio: 3,
            width: 390,
            height: 844,
        },
        {
            devicePixelRatio: 3,
            width: 428,
            height: 926,
        }
    ];
    // h5
    if (typeof window !== 'undefined' && window) {
        const isIOS = /iphone/gi.test(window.navigator.userAgent);
        if (!isIOS) return false;
        const { devicePixelRatio, screen } = window;
        const { width, height } = screen;
        return xSeriesConfig.some(item => item.devicePixelRatio === devicePixelRatio && item.width === width && item.height === height);
    }
    return false;
}
 
/***
 *
 * @param size 文件大小
 */
const sizeTostr = (size: any): string => {
    let data = "";
    if (size < 0.1 * 1024) { //如果小于0.1KB转化成B
        data = size.toFixed(2) + "B";
    } else if (size < 0.1 * 1024 * 1024) {//如果小于0.1MB转化成KB
        data = (size / 1024).toFixed(2) + "KB";
    } else if (size < 0.1 * 1024 * 1024 * 1024) { //如果小于0.1GB转化成MB
        data = (size / (1024 * 1024)).toFixed(2) + "MB";
    } else { //其他转化成GB
        data = (size / (1024 * 1024 * 1024)).toFixed(2) + "GB";
    }
    let sizestr = data + "";
    let len = sizestr.indexOf("\.");
    let dec = sizestr.substr(len + 1, 2);
    if (dec == "00") {//当小数点后为00时 去掉小数部分
        return sizestr.substring(0, len) + sizestr.substr(len + 3, 2);
    }
    return sizestr;
}
 
/***
 *
 * @param type 文件类型
 */
function judgmentType (type: string): boolean {
    let arr = ['jpg', 'jpeg', 'png']
    // arr.includes(type)
    let open: boolean = false
    arr.forEach((item: string) => {
        if (type === item) {
            open = true
        }
    })
    return open;
}
 
/***
 *
 * @param time 日期 年月日
 */
function gsdate (time: any): string {
    let date = new Date(time)
    let y: any = date.getFullYear()
    let m: any = date.getMonth() + 1
    m = m < 10 ? ('0' + m) : m
    let d: any = date.getDate()
    d = d < 10 ? ('0' + d) : d
    return y + '-' + m + '-' + d;
}
 
/***
 *
 * @param time 日期 年月日时分秒
 * @param Sign 间隔
 */
function setTime (time: any, Sign: string): string {
    let year= time.getFullYear();
    let month = time.getMonth() + 1 >= 10 ? time.getMonth() + 1 : `0${time.getMonth() + 1}`;
    let day = time.getDate() >= 10 ? time.getDate() : `0${time.getDate()}`;
    let hour = time.getHours() >= 10 ? time.getHours() : `0${time.getHours()}`;
    let minute = time.getMinutes() >= 10 ? time.getMinutes() : `0${time.getMinutes()}`;
    let second = time.getSeconds() >= 10 ? time.getSeconds() : `0${time.getSeconds()}`;
    return [year, Sign, month , Sign, day, ' ', hour , ':', minute, ':', second ].join('');
}
 
/***
 *
 * @param time 日期 年月日时分秒
 * @param Sign 间隔
 */
function setTimeO (time: any, Sign: string): string {
    let year = time.getFullYear();
    let month = time.getMonth() + 1 >= 10 ? time.getMonth() + 1 : `0${time.getMonth() + 1}`;
    let day = time.getDate() >= 10 ? time.getDate() : `0${time.getDate()}`;
    let hour = time.getHours() >= 10 ? time.getHours() : `0${time.getHours()}`;
    let minute = time.getMinutes() >= 10 ? time.getMinutes() : `0${time.getMinutes()}`;
    let second = time.getSeconds() >= 10 ? time.getSeconds() : `0${time.getSeconds()}`;
    return [year, Sign, month , Sign, day, ' ', hour , ':', minute, ':', '00' ].join('');
}
 
 
/***
 * 返回 true:顶顶端
 * 返回 false:h5端
 */
function judgmentPlatform (): boolean {
    // alert(store.state.env)
    // alert(store.state.env !== 'DD')
    return store.state.env === 'DD'
    // return dd.env.platform !== 'notInDingTalk';
}
 
/***REGULAR
 *
 * @param auth 需要验证的权限
 */
function auth (auth: string): boolean {
    const configPermissions = auth
    if (store.state.userInfo.permissions === {}) {      // 没有权限
        return false
    }
    const authList = store.state.userInfo.permissions   // 获取所有权限
    const isShow = authList.indexOf(configPermissions)
    return isShow !== -1;
}
 
/***
 * DOCUMENT_TYPE: 出入库单据 - 单据类型
 */
const DOCUMENT_TYPE = {
    1: {
        name: '车间领料'
    },
    2: {
        name: '机台备料'
    },
    3: {
        name: '车间转库'
    },
    4: {
        name: '完工入库'
    },
    5: {
        name: '计划领料'
    },
    6: {
        name: '外协领料'
    },
    7: {
        name: '外协完工转库'
    },
    8: {
        name: '成品入库'
    },
    9: {
        name: '成品转库'
    },
    10: {
        name: '让步放行入库'
    },
    11: {
        name: '让步放行转库'
    },
    12: {
        name: '客返返修领料'
    },
    13: {
        name: '客返返修入库'
    },
    14: {
        name: '跨组织转库'
    },
    15: {
        name: '工单投料(出库),工单产出(入)'
    },
    16: {
        name: '销售出库'
    },
    17: {
        name: '让步销售'
    }
}
 
export {
    REGULAR,
    gsdate,
    setTime,
    judgmentPlatform,
    auth,
    judgmentType,
    sizeTostr,
    equipment,
    setTimeO,
    DOCUMENT_TYPE
}