-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
268 lines (250 loc) · 4.83 KB
/
index.js
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
"use strict";
let toString = Object.prototype.toString;
/**
* [判断一个值是否为数组]
*
* @param {Array} val 测试的值
* @returns {boolean} 如果是数组返回true,如果非数组返回false
*/
function isArray(val) {
return toString.call(val) === "[object Array]";
}
/**
* [判断一个值是否为对象]
*
* @param {Object} val 测试的值
* @returns {boolean} 如果是对象返回true,如果非对象返回false
*/
function isObject(val) {
return toString.call(val) === "[object Object]";
}
/**
* [去掉空格]
*
* @param {String} str 带有空格的字符串
* @returns {String} 去掉空格的字符串
*/
function trim(str) {
return str.replace(/^\s*/, "").replace(/\s*$/, "");
}
/**
* [是否为空对象]
*
* @param {Obj} obj 参数为对象
* @returns {boolean} 返回值为布尔值
*/
function isObjEmpty(obj){
for(let name in obj){
if(obj.hasOwnProperty(name)){
return false;
}
}
return true;
}
/**
* [检查是否为电话号码]
*
* @param {Number} phone //8
* @returns {Boolean} // true
*/
function isPhoneNum(phoneNum) {
const reg = /^[1][3,4,5,7,8][0-9]{9}$/;
if(!reg.test(phoneNum)) {
return false;
}
return true;
}
/**
* [获取两个数组的交集]
*
* @param {Array} arr1 //[1,2,3,4,5,8,9]
* @param {Array} arr2 //[1,11,9]
* @returns {Array} key // [1,9]
*/
function interArray(arr1, arr2, key) {
let commonarr = arr1.filter((x) => {
if(key) {
return arr2.includes(x[key]);
} else {
return arr2.includes(x);
}
});
return commonarr;
}
/**
* [获取当前的日期]
*
* @param {string} type '-'
* @returns {string} '2018-09-16'
*/
function nowDate(type = "-") {
const date = new Date();
const year = date.getFullYear();
const month = datePlus0(new Date().getMonth() + 1);
const currentDate = datePlus0(new Date().getDate());
return `${year}${type}${month}${type}${currentDate}`;
}
/**
* [补0]
*
* @param {Number} x //8
* @returns {String} // 08
*/
function datePlus0(x) {
if(x < 10) {
return `0${x}`;
} else {
return x;
}
}
/**
* [弹出确认窗口]
*
* @param {String} text //8
* @param {String} title // 08
*/
function modal(text, title = "提示") {
return new Promise((resolve, reject) => {
wx.showModal({
title: title,
content: text,
showCancel: false,
success: res => {
resolve(res);
},
fail: res => {
reject(res);
}
});
});
}
/**
* [提示弹层]
*
* @param {String} title 显示内容
* @param {Number} duration 显示时间
* @param {String} icon 图标
*/
function toast(title, duration=2000, icon = "success") {
return new Promise((resolve)=>{
wx.showToast({
title: title,
icon: icon,
mask: true,
duration: duration,
success:function(){
setTimeout(()=>{
resolve();
},duration);
}
});
});
}
/**
* [保存本地数据]
*
* @param {String} key 保存的key
* @param {String} value 保存的value
*/
function setStorage(key,value) {
return new Promise((resolve,reject)=>{
wx.setStorage({
key:key,
data:value,
success:()=>{
resolve();
},
fail:(err)=>{
reject(err);
}
});
});
}
/**
* [获取本地存储]
*
* @param {String} key 保存的key
*/
function getStorage(key) {
return new Promise((resolve,reject)=>{
wx.getStorage({
key:key,
success:(res)=>{
resolve(res.data);
},
fail:()=>{
reject();
}
});
});
}
/**
* [打电话]
*
* @param {String} phoneNumber 电话号码
*/
function phoneCall(phoneNumber) {
wx.makePhoneCall({
phoneNumber: phoneNumber
});
}
/**
* [复制]
*
* @param {String} data 复制的内容
* @param {String} tip 提示信息
*/
function copy(data,tip){
wx.setClipboardData({
data: data,
success: function(res) {
wx.getClipboardData({
success: function(res) {
toast(tip);
}
});
}
});
}
/**
* [深拷贝]
*
* @param {Object} data 需要拷贝的数据
* @return {Object} 返回拷贝后的数据
*/
function deepCopy(data) {
if (Object.prototype.toString.call(data) === "[object Array]"){
return data.map(((item) => {
if (Object.prototype.toString.call(item) === "[object Array]" || Object.prototype.toString.call(item) === "[object Object]") {
return deepCopy(item);
}
return item;
}));
} else if (Object.prototype.toString.call(data) === "[object Object]") {
let newData = {};
for (let i in data) {
if (Object.prototype.toString.call(data[i]) === "[object Array]" || Object.prototype.toString.call(data[i]) === "[object Object]") {
newData[i] = deepCopy(data[i]);
} else {
newData[i] = data[i];
}
}
return newData;
}
}
module.exports = {
isArray: isArray,
isObject: isObject,
trim: trim,
isObjEmpty: isObjEmpty,
isPhoneNum: isPhoneNum,
interArray: interArray,
nowDate: nowDate,
modal: modal,
toast: toast,
setStorage: setStorage,
getStorage: getStorage,
phoneCall: phoneCall,
copy: copy,
deepCopy: deepCopy
};