2db47680
hexiaodong
chore: 初始提交 php 遗...
|
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
325
326
327
|
const app_info = require("@/static/appInfo.js");
const api_host = app_info.api_host
const version = app_info.version
const api_prefix = api_host + '/addons/exam/'
//工具类
module.exports = {
isShowTip(apiUrl) {
let filterList = ['/agency/getAuditVersion']
if (filterList.indexOf(apiUrl) != -1) {
return false
} else {
return true
}
},
timestamp() {
return parseInt(new Date().getTime() / 1000);
},
cons_log(...params) {
for (let p of params) {
console.log(p);
}
},
cons_log_json(...params) {
for (let p of params) {
console.log(JSON.stringify(p));
}
},
goto(url) {
uni.navigateTo({
url: url
});
},
goreto(url) {
uni.redirectTo({
url: url
});
},
gotore(url) {
uni.reLaunch({
url: url
});
},
goback(delta = 1) {
uni.navigateBack({
delta: delta
});
},
toast(msg, icon = 'none', duration = 2000, mask = false, image = '') {
uni.showToast({
title: msg,
icon: icon,
duration: duration,
mask: mask,
image: image
})
},
alert(content, callBack) {
uni.hideLoading();
uni.showModal({
content: content,
showCancel: false,
success: function() {
if (callBack != undefined) {
callBack();
}
}
})
},
getData(key, is_clear = false) {
var value = uni.getStorageSync(key);
if (is_clear) {
uni.setStorageSync(key, '');
}
// console.log('get cache', key, value)
return value;
},
setData(key, value) {
// console.log('set cache', key, value)
uni.setStorageSync(key, value);
},
array_column(obj, key) {
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
objects = objects.concat(this.array_column(obj[i], key));
} else if (i == key) {
objects.push(obj[i]);
}
}
return objects;
},
shuffle(arr) {
var l = arr.length
var index, temp
while (l > 0) {
index = Math.floor(Math.random() * l)
temp = arr[l - 1]
arr[l - 1] = arr[index]
arr[index] = temp
l--
}
return arr
},
// 秒数转时分秒格式
sec_to_time(s) {
var t;
if (s > -1) {
var hour = Math.floor(s / 3600);
var min = Math.floor(s / 60) % 60;
var sec = s % 60;
if (hour < 10) {
t = '0' + hour + ":";
} else {
t = hour + ":";
}
if (min < 10) {
t += "0";
}
t += min + ":";
if (sec < 10) {
t += "0";
}
t += sec.toFixed(2);
}
return t;
},
login() {
var token = this.getData('token');
var user = this.getData('user');
if (!token || !user) {
const pages = getCurrentPages()
const currentPage = pages[pages.length - 1]
const url = currentPage.route
const options = currentPage.options
let urlWithArgs = `/${url}?`
for (let key in options) {
const value = options[key]
urlWithArgs += `${key}=${value}&`
}
urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)
uni.setStorageSync('route', urlWithArgs);
console.log('没有缓存数据,需先授权')
uni.redirectTo({
url: "/pages/login/login"
})
return;
}
return true;
},
/**
* rich-text富文本中图片宽度最大100%
* @param {String} html
*/
formatRichText(html) {
// 去掉img标签里的style、width、height属性
let content_data= 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
})
// 修改所有style里的width属性为max-width:100%
content_data = content_data.replace(/style="[^"]+"/gi,function(match,capture){
match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;')
return match
})
// 去掉<br/>标签
content_data = content_data.replace(/<br[^>]*\/>/gi, '')
// img标签添加style属性:max-width:100%;height:auto
content_data = content_data.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin:0px auto;"')
return content_data
},
/**
* http请求
* @param handler
* @param path
* @param data
* @param method
* @param showLoading
* @returns {Promise<unknown>}
*/
http(handler, path, data = {}, method = 'post', showLoading = true) {
let url = api_prefix + path
let timestamp = Date.parse(new Date())
if (typeof(data) == 'object') {
data['timestamp'] = timestamp
// data['version'] = version
}
if (showLoading) {
uni.showLoading({
title: '加载中..',
mask: true
})
}
return new Promise((resolve, reject) => {
uni.request({
url: url,
method: method,
data: data,
header: {
'content-type': 'application/json',
'token': this.getData('token'),
'app-version': version
},
success: (res) => {
if (res.statusCode == 200) {
// 过滤掉一些接口code为0的报错不提示,默认所有接口code为0时提示报错
// 不需要报错提示,自行到isShowTip方法里面的filterList数组添加不需要提示的接口名
let isShowTip = this.isShowTip(path)
if(res.data.code == 0){
if(isShowTip){
uni.showToast({
title: res.data.msg,
icon: 'none'
})
}
}
resolve(res.data)
} else {
console.log('http statusCode', res.statusCode)
uni.showToast({
title: res.data.msg,
icon: 'none'
})
// 未登录
if (res.statusCode == 401) {
// console.log('this.$refs', this.$refs)
// this.$refs.login.modal = true
// uni.$emit('uniLogin', handler)
handler.$refs.login.modal = true
// handler.$refs.login.afterMethod = () => {
// this.http(handler, path, data)
// }
}
reject(res)
}
},
fail: (res) => {
console.log(res)
reject(res)
// return false;
},
complete: (res) => {
showLoading && uni.hideLoading()
}
})
})
},
/**
* 上传文件
* @returns {Promise<*>}
*/
uploadFile(handler, filePath, data = {}, fileType = 'image', showLoading = true) {
if (showLoading) {
uni.showLoading({
title: '加载中..',
mask: true
})
}
return new Promise((resolve, reject) => {
uni.uploadFile({
url: api_prefix + 'common/upload',
header: {
'token': uni.getStorageSync('token'),
"Content-Type": "multipart/form-data"
},
fileType: fileType,
filePath: filePath,
formData: data,
name: 'file',
success: (res) => {
console.log('upload res', res)
let data = JSON.parse(res.data)
if (res.statusCode == 200) {
resolve(data)
} else {
console.log('upload statusCode', res.statusCode)
uni.showToast({
title: data.message,
icon: 'none'
})
// 未登录
if (res.statusCode == 401) {
handler.$refs.login.modal = true
}
}
},
fail: (res) => {
console.log('fail res', res)
return false;
},
complete: (res => {
showLoading && uni.hideLoading()
})
})
})
},
};
|