httpRequest.js
5.06 KB
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
/**
* 常用方法封装 请求,文件上传等
* @author echo.
**/
const tui = {
//接口地址
interfaceUrl: function() {
return 'https://www.thorui.cn'
//return 'https://test.thorui.cn'
//return 'https://uat.thorui.cn'
// return 'https://prod.thorui.cn'
},
toast: function(text, duration, success) {
uni.showToast({
title: text || "出错啦~",
icon: success ? 'success' : 'none',
duration: duration || 2000
})
},
modal: function(title, content, showCancel, callback, confirmColor, confirmText) {
uni.showModal({
title: title || '提示',
content: content,
showCancel: showCancel,
cancelColor: "#555",
confirmColor: confirmColor || "#5677fc",
confirmText: confirmText || "确定",
success(res) {
if (res.confirm) {
callback && callback(true)
} else {
callback && callback(false)
}
}
})
},
isAndroid: function() {
const res = uni.getSystemInfoSync();
return res.platform.toLocaleLowerCase() == "android"
},
isPhoneX: function() {
const res = uni.getSystemInfoSync();
let iphonex = false;
let models=['iphonex','iphonexr','iphonexsmax','iphone11','iphone11pro','iphone11promax']
const model=res.model.replace(/\s/g,"").toLowerCase()
if (models.includes(model)) {
iphonex = true;
}
return iphonex;
},
constNum: function() {
let time = 0;
// #ifdef APP-PLUS
time = this.isAndroid() ? 300 : 0;
// #endif
return time
},
delayed: null,
/**
* 请求数据处理
* @param string url 请求地址
* @param string method 请求方式
* GET or POST
* @param {*} postData 请求参数
* @param bool isDelay 是否延迟显示loading
* @param bool isForm 数据格式
* true: 'application/x-www-form-urlencoded'
* false:'application/json'
* @param bool hideLoading 是否隐藏loading
* true: 隐藏
* false:显示
*/
request: function(url, method, postData, isDelay, isForm, hideLoading) {
//接口请求
let loadding = false;
tui.delayed && uni.hideLoading();
clearTimeout(tui.delayed);
tui.delayed = null;
if (!hideLoading) {
tui.delayed = setTimeout(() => {
uni.showLoading({
mask: true,
title: '请稍候...',
success(res) {
loadding = true
}
})
}, isDelay ? 1000 : 0)
}
return new Promise((resolve, reject) => {
uni.request({
url: tui.interfaceUrl() + url,
data: postData,
header: {
'content-type': isForm ? 'application/x-www-form-urlencoded' : 'application/json',
'Authorization': tui.getToken()
},
method: method, //'GET','POST'
dataType: 'json',
success: (res) => {
clearTimeout(tui.delayed)
tui.delayed = null;
if (loadding && !hideLoading) {
uni.hideLoading()
}
// if (res.data && res.data.code == 1) {
// uni.clearStorageSync()
// tui.modal("登录信息已失效,请重新登录", false, () => {
// //store.commit("logout") 登录页面执行
// uni.redirectTo({
// url: '/pages/common/login/login'
// })
// })
// return
// }
resolve(res.data)
},
fail: (res) => {
clearTimeout(tui.delayed)
tui.delayed = null;
tui.toast("网络不给力,请稍后再试~")
reject(res)
}
})
})
},
/**
* 上传文件
* @param string url 请求地址
* @param string src 文件路径
*/
uploadFile: function(url, src) {
uni.showLoading({
title: '请稍候...'
})
return new Promise((resolve, reject) => {
const uploadTask = uni.uploadFile({
url: tui.interfaceUrl() + url,
filePath: src,
name: 'imageFile',
header: {
'Authorization': tui.getToken()
},
formData: {
// sizeArrayText:""
},
success: function(res) {
uni.hideLoading()
let d = JSON.parse(res.data.replace(/\ufeff/g, "") || "{}")
if (d.code % 100 == 0) {
//返回图片地址
let fileObj = d.data;
resolve(fileObj)
} else {
that.toast(res.msg);
}
},
fail: function(res) {
reject(res)
that.toast(res.msg);
}
})
})
},
tuiJsonp: function(url, callback, callbackname) {
// #ifdef H5
window[callbackname] = callback;
let tuiScript = document.createElement("script");
tuiScript.src = url;
tuiScript.type = "text/javascript";
document.head.appendChild(tuiScript);
document.head.removeChild(tuiScript);
// #endif
},
//设置用户信息
setUserInfo: function(mobile, token) {
//uni.setStorageSync("thorui_token", token)
uni.setStorageSync("thorui_mobile", mobile)
},
//获取token
getToken() {
return uni.getStorageSync("thorui_token")
},
//判断是否登录
isLogin: function() {
return uni.getStorageSync("thorui_mobile") ? true : false
},
//跳转页面,校验登录状态
href(url, isVerify) {
if (isVerify && !tui.isLogin()) {
uni.navigateTo({
url: '/pages/common/login/login'
})
} else {
uni.navigateTo({
url: url
});
}
}
}
export default tui