request.js
7.7 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
205
206
207
var t = require("util.js");
module.exports = {
uniqueId: "",
request: function(e, i, o) {
var n = this, a = o.header ? o.header : {
"content-type": "application/x-www-form-urlencoded"
//"content-type": "application/texts"
}, s = "GET" != (e = e.toUpperCase()) && o.data ? t.json2Form(o.data) : o.data;
i = this.modifyUrl(i, o), o.isShowLoading = void 0 === o.isShowLoading || o.isShowLoading,
o.isShowLoading && this.showLoading(), console.log("app.request", i, o), wx.request(Object.assign({}, o, {
url: i,
method: e,
data: s,
header: a,
success: function(t) {
o.isShowLoading && n.hideLoading(), n.doSuccess(o, t);
},
fail: function(t) {
o.isShowLoading && n.hideLoading(), n.doFail(o, t);
}
}));
},
get: function (t, e) {
this.request("GET", t, e);
},
post: function (t, e) {
this.request("POST", t, e);
},
delete: function (t, e) {
this.request("DELETE", t, e);
},
put: function (t, e) {
this.request("PUT", t, e);
},
uploadFile: function(t, e) {
var i = this;
t = this.modifyUrl(t, e), console.log("app.request", t, e), e.isShowLoading = void 0 === e.isShowLoading || e.isShowLoading,
e.isShowLoading && this.showLoading(), wx.uploadFile(Object.assign({}, e, {
url: t,
filePath: e.filePath,
name: e.name,
success: function(t) {
i.hideLoading();
if(t.statusCode==200)
t.data = JSON.parse(i.filterJsonData(t.data));
else { t={code:-1,msg:'调用接口失败'};}
i.doSuccess(e, t);
},
fail: function(t) {
i.hideLoading(), i.doFail(e, t);
}
}));
},
doSuccess: function(t, e) {
if (console.log("app.request", e), 1 != t.successReload) {
if (200 != e.statusCode) return this.showError("请求出错[" + e.statusCode + "]", t),
!1;
if(e.data.status!=undefined){
if (1 != e.data.status) {
if ("function" == typeof t.failStatus && 0 == t.failStatus(e)) return !1;
if (-100 == e.data.status || -101 == e.data.status || -102 == e.data.status) {
var i = getApp();
return i.auth.clearAuth(), i.showWarning("正在重新登录", function() {
var t = getCurrentPages();
"pages/user/index/index" != t[t.length - 1].route ? wx.switchTab({
url: "/pages/user/index/index"
}) : wx.switchTab({
url: "/pages/index/index/index"
});
}, null, !0), !1;
}
var o = "string" == typeof e.data.msg ? e.data.msg : "数据格式错误";
return this.showError(o, t), !1;
}
}
"function" == typeof t.success && t.success(e);
} else "function" == typeof t.success && t.success(e);
},
doFail: function(t, e) {
if (console.log("app.request", e), "function" == typeof t.fail && 0 == t.fail(e)) return !1;
this.showError("请求失败", t);
},
filterJsonData: function(t) {
for (var e = t, i = 0; i < t.length && (e = t.substr(i), "{" != t.charAt(i)); i++) ;
return e;
},
modifyUrl: function(t, e) {
if (void 0 === e && (e = {}), 0 != t.indexOf("http") && ("string" == typeof e.baseUrl ? t = e.baseUrl + t : void 0 === e.baseUrl && (t = getApp().globalData.setting.url + t)),
"boolean" == typeof e.notAuthParam && 1 == e.notAuthParam) return t;
//var i = "is_json=1&unique_id=" + this.getUniqueId() + "&token=" + this.getToken();
return t += (t.indexOf("?") > 0 ? "&" : "?");// + i;
},
modifyUrl2: function (t, e) {
if (void 0 === e && (e = {}), 0 != t.indexOf("http") && ("string" == typeof e.baseUrl ? t = e.baseUrl + t : void 0 === e.baseUrl && (t = getApp().globalData.setting.hurl + t)),
"boolean" == typeof e.notAuthParam && 1 == e.notAuthParam) return t;
//var i = "is_json=1&unique_id=" + this.getUniqueId() + "&token=" + this.getToken();
return t += (t.indexOf("?") > 0 ? "&" : "?");// + i;
},
getToken: function() {
var t = getApp();
return null == t.globalData.userInfo ? "" : t.globalData.userInfo.token;
},
getUniqueId: function() {
return this.uniqueId ? this.uniqueId : (this.uniqueId = "miniapp" + t.randomString(17),
this.uniqueId);
},
showLoading: function() {
wx.showLoading({
title: "加载中"
});
},
hideLoading: function() {
wx.hideLoading();
},
showError: function(t, e) {
wx.showModal({
title: t,
showCancel: !1,
complete: function() {
1 == e.failRollback && wx.navigateBack();
}
});
},
request2: function (e, i, o) {
var n = this, a = o.header ? o.header : {
"content-type": "application/x-www-form-urlencoded"
}, s = "GET" != (e = e.toUpperCase()) && o.data ? t.json2Form(o.data) : o.data;
i = this.modifyUrl2(i, o), o.isShowLoading = void 0 === o.isShowLoading || o.isShowLoading,
o.isShowLoading && this.showLoading(), console.log("app.request", i, o), wx.request(Object.assign({}, o, {
url: i,
method: e,
data: s,
header: a,
success: function (t) {
o.isShowLoading && n.hideLoading(), n.doSuccess2(o, t);
},
fail: function (t) {
o.isShowLoading && n.hideLoading(), n.doFail(o, t);
}
}));
},
get2: function (t, e) {
this.request2("GET", t, e);
},
doSuccess2: function (t, e){
if (console.log("app.request", e), 1 != t.successReload) {
if (200 != e.statusCode) return this.showError("请求出错[" + e.statusCode + "]", t),
!1;
if (0 != e.data.code) {
if ("function" == typeof t.failStatus && 0 == t.failStatus(e)) return !1;
var o = "string" == typeof e.data.msg ? e.data.msg : "数据格式错误";
return this.showError(o, t), !1;
}
"function" == typeof t.success && t.success(e);
} else "function" == typeof t.success && t.success(e);
},
//---promise的使用get----
promiseGet:function(url,data){
var th=this;
if(url.indexOf("http")==-1) url=getApp().globalData.setting.url +url;
return new Promise((resolve, reject) => {
data.isShowLoading && th.showLoading();
wx.request({
url,
method: 'GET',
header: {"content-type": "application/x-www-form-urlencoded" },
data:data.data,
success(res) {
data.isShowLoading && th.hideLoading();
resolve(res);
},
fail(err) { data.isShowLoading && th.hideLoading(); reject(err); }
})
})
},
//---promise的使用get----
promisePost:function(url,data){
var th=this;
if(url.indexOf("http")==-1) url=getApp().globalData.setting.url +url;
return new Promise((resolve, reject) => {
data.isShowLoading && th.showLoading();
wx.request({
url,
method: 'POST',
header: {"content-type": "application/x-www-form-urlencoded" },
data:data.data,
success(res) {
data.isShowLoading && th.hideLoading();
resolve(res);
},
fail(err) { data.isShowLoading && th.hideLoading(); reject(err); }
})
})
}
};