gx
queenwuli
2021-01-09 79f11f34546a394ab1c16ba427c31e9b59d05523
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
/**
 * Utils 工具类
 * */
 import StorageUtil from './storageUtils.js'
 const Utils = {
    /**
    * 只能输入正整数
    */
    checkInteger: (value) => {
         if (!value) {
           return '';
         }
         if (/^[0-9]*[1-9][0-9]*$/.test(value)) {
           return value;
         }
         value = value.replace(/^(0+)|[^\d]+/g, '');
         return Number(value);
    },
    /**
    * 正整数和小数,最大只能输入4位小数
    */
    checkNum: (value) => {
         if (!value) {
           return '';
         }
         if (typeof value === 'number') {
           value = value.toString();
         }
         let newText = value !== '' && value.substr(0, 1) === '.' ? '' : value;
         newText = newText.replace(/^0+[0-9]+/g, '0'); //不能以0开头输入
         newText = newText.replace(/[^\d.]/g, ''); //清除"数字"和"."以外的字符
         newText = newText.replace(/\.{2,}/g, '.'); //只保留第一个, 清除多余的
         newText = newText
           .replace('.', '$#$')
           .replace(/\./g, '')
           .replace('$#$', '.');
         newText = newText.replace(/^(\-)*(\d+)\.(\d{4}).*$/, '$1$2.$3'); //只能输入4位小数
         return newText;
    },
    /**
     * 校验手机号
    */
    checkPhone: (value) => {
        let reg = /^1[3456789]\d{9}$/;
        return reg.test(value)
    },
    /**
     * 校验密码
    */
    checkPwd: (value) => {
        let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,32}$/;
        return reg.test(value)
    },
    /**
     * 只能输入非中文
    */
    checkNotChinese: (value) => {
        return value.replace(/[\u4E00-\u9FA5]/g, '');
    },
    compare: (curV,reqV) => {
        if(curV && reqV){
             //将两个版本号拆成数字
             var arr1 = curV.split('.'),
                 arr2 = reqV.split('.');
             var minLength=Math.min(arr1.length,arr2.length),
                 position=0,
                 diff=0;
             //依次比较版本号每一位大小,当对比得出结果后跳出循环(后文有简单介绍)
             while(position<minLength && ((diff=parseInt(arr1[position])-parseInt(arr2[position]))==0)){
                 position++;
             }
             diff=(diff!=0)?diff:(arr1.length-arr2.length);
             //若curV大于reqV,则返回true
             return diff>=0;
        }else{
             //输入为空
             console.log("版本号不能为空");
             return false;
      }
    },
    formmatTime(fmt, date) {
        let ret;
        if(uni.getSystemInfoSync().platform == 'ios' && date){
            return date;
        }
        if(!date){
            date = new Date()
        } else {
            date = new Date(date)
        }
        let opt = {
            'Y+': date.getFullYear().toString(), // 年
            'm+': (date.getMonth() + 1).toString(), // 月
            'd+': date.getDate().toString(), // 日
            'H+': date.getHours().toString(), // 时
            'M+': date.getMinutes().toString(), // 分
            'S+': date.getSeconds().toString() // 秒
        };
        for (let k in opt) {
            ret = new RegExp('(' + k + ')').exec(fmt);
            if (ret) {
              fmt = fmt.replace(
                ret[1],
                ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, '0')
              );
            }
        }
        return fmt;
    },
    addData(date, days){
         var date = new Date(date);
          date.setDate(date.getDate() + days);
          var month = date.getMonth() + 1;
         var day = date.getDate();
         var mm = "'" + month + "'";
         var dd = "'" + day + "'";
         
         //单位数前面加0
         if(mm.length == 3) {
             month = "0" + month;
         }
         if(dd.length == 3) {
             day = "0" + day;
         }
     
         var time = date.getFullYear() + "-" + month + "-" + day
         return time;
    },
    // 加密账号
    encryptAccount(val) {
        if (/^1[3456789]\d{9}$/.test(val)) {
          return String(val).substr(0, 3) + '****' + String(val).substr(7);
        } else {
          return val;
        }
    },
    /**
     * code 1.无符号分隔表示单个功能,2.逗号分隔表示多个功能 3. 点分隔表示上下级关系,只支持2级
     * */
    hasPermission(code){
        let rolesInfo = StorageUtil.getStorage('userInfo','userFunction');
        let arr = [];
        if(!rolesInfo){
            return false
        }
        if(code.indexOf(',')>-1){
            arr = code.split(',').filter((item) => {
                item = item.trim();
                return rolesInfo[item]
            })
            return arr.length;
        }else if(code.indexOf('.')>-1){
            arr = code.split('.');
            let children = rolesInfo[arr[0]]['sysFnBtnRel'].filter((item) => {
                return item.btnValue == arr[1]
            });
            return rolesInfo[arr[0]] && children.length
        }else{
            return rolesInfo[code];
        }
    }
 }
 export default Utils;