Commit e183300d50df2e32ecc382e1ce96c019d42cdac5
1 parent
8b6211c6
app.js get请求超时解决
Showing
4 changed files
with
339 additions
and
262 deletions
app.js
... | ... | @@ -69,11 +69,12 @@ App({ |
69 | 69 | windowWidth: 0, //整个窗体的宽度 |
70 | 70 | room_id: null, //直播间分享的房间ID |
71 | 71 | room_goods_id: null, //直播间分享的商品ID |
72 | - | |
72 | + fail_url: new Map(), | |
73 | 73 | }, |
74 | 74 | auth: o, |
75 | 75 | request: a, |
76 | 76 | onLaunch: function (option) { |
77 | + | |
77 | 78 | //初始化美图测肤插件 |
78 | 79 | // var plugin = requirePlugin('mtSkinSdk'); |
79 | 80 | // params.login_id = wx.getStorageSync('login_id') || '' // 可将登录id缓存下来,方便下次进入小程序直接获取 |
... | ... | @@ -81,8 +82,7 @@ App({ |
81 | 82 | if(option) this.globalData.scene=option.scene; |
82 | 83 | wx.hideTabBar(); |
83 | 84 | this.initExt(); |
84 | - | |
85 | - | |
85 | + // this.overShare() | |
86 | 86 | |
87 | 87 | //检查更新 |
88 | 88 | this.checkUpdateVersion(); |
... | ... | @@ -206,7 +206,21 @@ App({ |
206 | 206 | this.getConfig(); |
207 | 207 | }, |
208 | 208 | |
209 | - | |
209 | + // overShare:function() { | |
210 | + // // 监听路由切换 | |
211 | + // wx.onAppRoute(function(res) { | |
212 | + // console.log('route',res) | |
213 | + // let pages = getCurrentPages() | |
214 | + // let view = pages[pages.length - 1] | |
215 | + // if(view) { | |
216 | + // wx.showShareMenu({ | |
217 | + // withShareTicket:true, | |
218 | + // menus:['shareAppMessage','shareTimeline'] | |
219 | + // }) | |
220 | + // } | |
221 | + // }) | |
222 | + // }, | |
223 | + | |
210 | 224 | //---初始化第三方---- |
211 | 225 | initExt: function () { |
212 | 226 | var tt = t; |
... | ... | @@ -962,8 +976,4 @@ App({ |
962 | 976 | }).then(res=>{}) |
963 | 977 | } |
964 | 978 | } |
965 | - | |
966 | - | |
967 | - | |
968 | - | |
969 | 979 | }); | ... | ... |
app.json
... | ... | @@ -258,6 +258,11 @@ |
258 | 258 | }, |
259 | 259 | "useExtendedLib": { |
260 | 260 | "weui": true |
261 | + }, | |
262 | + "networkTimeout": { | |
263 | + "request": 15000, | |
264 | + "downloadFile": 15000, | |
265 | + "uploadFile":15000, | |
266 | + "connectSocket":15000 | |
261 | 267 | } |
262 | - | |
263 | 268 | } |
264 | 269 | \ No newline at end of file | ... | ... |
utils/request.js
1 | 1 | var t = require("util.js"); |
2 | 2 | |
3 | 3 | module.exports = { |
4 | - uniqueId: "", | |
5 | - // i: url | |
6 | - // e: method | |
7 | - // s: data | |
8 | - // a: header | |
9 | - request: function(e, i, o) { | |
10 | - var n = this, a = o.header ? o.header : { | |
11 | - "content-type": "application/x-www-form-urlencoded" | |
12 | - //"content-type": "application/texts" | |
13 | - }, s = "GET" != (e = e.toUpperCase()) && o.data ? t.json2Form(o.data) : o.data; | |
14 | - i = this.modifyUrl(i, o), | |
4 | + uniqueId: "", | |
5 | + // i: url | |
6 | + // e: method | |
7 | + // s: data | |
8 | + // a: header | |
9 | + request: function (e, i, o, delay = 1000) { | |
10 | + this.check_timeout(i, delay, () => { | |
11 | + var n = this, a = o.header ? o.header : { | |
12 | + "content-type": "application/x-www-form-urlencoded" | |
13 | + //"content-type": "application/texts" | |
14 | + }, s = "GET" != (e = e.toUpperCase()) && o.data ? t.json2Form(o.data) : o.data; | |
15 | + i = this.modifyUrl(i, o), | |
15 | 16 | o.isShowLoading && this.showLoading(); |
16 | - var req=wx.request(Object.assign({}, o, { | |
17 | - url: i, | |
18 | - method: e, | |
19 | - data: s, | |
20 | - header: a, | |
21 | - success: function(t) { | |
22 | - o.isShowLoading && n.hideLoading(), n.doSuccess(o, t); | |
23 | - }, | |
24 | - fail: function(t) { | |
25 | - o.isShowLoading && n.hideLoading(), n.doFail(o, t); | |
26 | - } | |
27 | - })); | |
28 | - return req; | |
29 | - }, | |
17 | + var req = wx.request(Object.assign({}, o, { | |
18 | + url: i, | |
19 | + method: e, | |
20 | + data: s, | |
21 | + header: a, | |
22 | + success: function (t) { | |
23 | + o.isShowLoading && n.hideLoading(), n.doSuccess(o, t); | |
24 | + }, | |
25 | + fail(err) { | |
26 | + o.isShowLoading && n.hideLoading(); | |
27 | + if (err && err.errMsg && err.errMsg.indexOf('timeout') != -1) { | |
28 | + getApp().globalData.fail_url.set(t.gettimestamp(), i); | |
29 | + console.log('请求超时url---', getApp().globalData.fail_url); | |
30 | + n.doTimeOut(o, t); | |
31 | + } else { | |
32 | + n.doFail(o, t); | |
33 | + } | |
34 | + } | |
35 | + })); | |
36 | + return req; | |
37 | + }) | |
38 | + }, | |
30 | 39 | get: function (t, e) { |
31 | - return this.request("GET", t, e); | |
32 | - }, | |
40 | + console.log(t, e); | |
41 | + return this.request("GET", t, e); | |
42 | + }, | |
33 | 43 | post: function (t, e) { |
34 | - this.request("POST", t, e); | |
35 | - }, | |
44 | + this.request("POST", t, e); | |
45 | + }, | |
36 | 46 | delete: function (t, e) { |
37 | - this.request("DELETE", t, e); | |
38 | - }, | |
47 | + this.request("DELETE", t, e); | |
48 | + }, | |
39 | 49 | put: function (t, e) { |
40 | - this.request("PUT", t, e); | |
41 | - }, | |
50 | + this.request("PUT", t, e); | |
51 | + }, | |
42 | 52 | |
43 | 53 | |
44 | - uploadFile: function(t, e) { | |
45 | - var i = this; | |
46 | - t = this.modifyUrl(t, e), | |
47 | - // console.log("app.request", t, e), | |
48 | - e.isShowLoading = void 0 === e.isShowLoading || e.isShowLoading, | |
49 | - e.isShowLoading && this.showLoading(), wx.uploadFile(Object.assign({}, e, { | |
50 | - url: t, | |
51 | - filePath: e.filePath, | |
52 | - name: e.name, | |
53 | - success: function(t) { | |
54 | - i.hideLoading(); | |
55 | - if(t.statusCode==200) | |
56 | - t.data = JSON.parse(i.filterJsonData(t.data)); | |
57 | - else { t={code:-1,msg:'调用接口失败'};} | |
58 | - i.doSuccess(e, t); | |
59 | - }, | |
60 | - fail: function(t) { | |
61 | - i.hideLoading(), i.doFail(e, t); | |
62 | - } | |
63 | - })); | |
64 | - }, | |
54 | + uploadFile: function (t, e) { | |
55 | + var i = this; | |
56 | + t = this.modifyUrl(t, e), | |
57 | + // console.log("app.request", t, e), | |
58 | + e.isShowLoading = void 0 === e.isShowLoading || e.isShowLoading, | |
59 | + e.isShowLoading && this.showLoading(), wx.uploadFile(Object.assign({}, e, { | |
60 | + url: t, | |
61 | + filePath: e.filePath, | |
62 | + name: e.name, | |
63 | + success: function (t) { | |
64 | + i.hideLoading(); | |
65 | + if (t.statusCode == 200) | |
66 | + t.data = JSON.parse(i.filterJsonData(t.data)); | |
67 | + else { t = { code: -1, msg: '调用接口失败' }; } | |
68 | + i.doSuccess(e, t); | |
69 | + }, | |
70 | + fail: function (t) { | |
71 | + i.hideLoading(), i.doFail(e, t); | |
72 | + } | |
73 | + })); | |
74 | + }, | |
65 | 75 | |
66 | - doSuccess: function(t, e) { | |
67 | - if (console.log("app.request", e), 1 != t.successReload) { | |
68 | - if (200 != e.statusCode) return this.showError("请求出错[" + e.statusCode + "]", t), | |
69 | - !1; | |
76 | + doSuccess: function (t, e) { | |
77 | + if (console.log("app.request", e), 1 != t.successReload) { | |
78 | + if (200 != e.statusCode) return this.showError("请求出错[" + e.statusCode + "]", t), | |
79 | + !1; | |
70 | 80 | |
71 | - if(e.data.status!=undefined){ | |
72 | - if (1 != e.data.status) { | |
73 | - if ("function" == typeof t.failStatus && 0 == t.failStatus(e)) return !1; | |
74 | - if (-100 == e.data.status || -101 == e.data.status || -102 == e.data.status) { | |
75 | - var i = getApp(); | |
76 | - return i.auth.clearAuth(), i.showWarning("正在重新登录", function() { | |
77 | - var t = getCurrentPages(); | |
78 | - "pages/user/index/index" != t[t.length - 1].route ? wx.navigateTo({ | |
79 | - url: "/pages/user/index/index" | |
80 | - }) : wx.navigateTo({ | |
81 | - url: "/pages/index/index/index" | |
82 | - }); | |
83 | - }, null, !0), !1; | |
84 | - } | |
85 | - var o = "string" == typeof e.data.msg ? e.data.msg : "数据格式错误"; | |
86 | - return this.showError(o, t), !1; | |
87 | - } | |
88 | - } | |
89 | - "function" == typeof t.success && t.success(e); | |
90 | - } else "function" == typeof t.success && t.success(e); | |
91 | - }, | |
92 | - doFail: function(t, e) { | |
93 | - if (console.log("app.request", e), "function" == typeof t.fail && 0 == t.fail(e)) return !1; | |
94 | - this.showError("请求失败", t); | |
95 | - }, | |
96 | - filterJsonData: function(t) { | |
97 | - for (var e = t, i = 0; i < t.length && (e = t.substr(i), "{" != t.charAt(i)); i++) ; | |
98 | - return e; | |
99 | - }, | |
100 | - modifyUrl: function(t, e) { | |
101 | - 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)), | |
102 | - "boolean" == typeof e.notAuthParam && 1 == e.notAuthParam) return t; | |
103 | - //var i = "is_json=1&unique_id=" + this.getUniqueId() + "&token=" + this.getToken(); | |
104 | - return t += (t.indexOf("?") > 0 ? "&" : "?");// + i; | |
105 | - }, | |
81 | + if (e.data.status != undefined) { | |
82 | + if (1 != e.data.status) { | |
83 | + if ("function" == typeof t.failStatus && 0 == t.failStatus(e)) return !1; | |
84 | + if (-100 == e.data.status || -101 == e.data.status || -102 == e.data.status) { | |
85 | + var i = getApp(); | |
86 | + return i.auth.clearAuth(), i.showWarning("正在重新登录", function () { | |
87 | + var t = getCurrentPages(); | |
88 | + "pages/user/index/index" != t[t.length - 1].route ? wx.navigateTo({ | |
89 | + url: "/pages/user/index/index" | |
90 | + }) : wx.navigateTo({ | |
91 | + url: "/pages/index/index/index" | |
92 | + }); | |
93 | + }, null, !0), !1; | |
94 | + } | |
95 | + var o = "string" == typeof e.data.msg ? e.data.msg : "数据格式错误"; | |
96 | + return this.showError(o, t), !1; | |
97 | + } | |
98 | + } | |
99 | + "function" == typeof t.success && t.success(e); | |
100 | + } else "function" == typeof t.success && t.success(e); | |
101 | + }, | |
102 | + doTimeOut(t, e) { | |
103 | + if (console.log("app.request", e), "function" == typeof t.fail && 0 == t.fail(e)) return !1; | |
104 | + this.showError("请求超时", t); | |
105 | + }, | |
106 | + doFail: function (t, e) { | |
107 | + if (console.log("app.request", e), "function" == typeof t.fail && 0 == t.fail(e)) return !1; | |
108 | + this.showError("请求失败", t); | |
109 | + }, | |
110 | + filterJsonData: function (t) { | |
111 | + for (var e = t, i = 0; i < t.length && (e = t.substr(i), "{" != t.charAt(i)); i++); | |
112 | + return e; | |
113 | + }, | |
114 | + modifyUrl: function (t, e) { | |
115 | + 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)), | |
116 | + "boolean" == typeof e.notAuthParam && 1 == e.notAuthParam) return t; | |
117 | + //var i = "is_json=1&unique_id=" + this.getUniqueId() + "&token=" + this.getToken(); | |
118 | + return t += (t.indexOf("?") > 0 ? "&" : "?");// + i; | |
119 | + }, | |
106 | 120 | |
107 | - modifyUrl2: function (t, e) { | |
108 | - 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)), | |
121 | + modifyUrl2: function (t, e) { | |
122 | + 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)), | |
109 | 123 | "boolean" == typeof e.notAuthParam && 1 == e.notAuthParam) return t; |
110 | 124 | //var i = "is_json=1&unique_id=" + this.getUniqueId() + "&token=" + this.getToken(); |
111 | 125 | return t += (t.indexOf("?") > 0 ? "&" : "?");// + i; |
112 | 126 | }, |
113 | 127 | |
114 | 128 | |
115 | - getToken: function() { | |
116 | - var t = getApp(); | |
117 | - return null == t.globalData.userInfo ? "" : t.globalData.userInfo.token; | |
118 | - }, | |
119 | - getUniqueId: function() { | |
120 | - return this.uniqueId ? this.uniqueId : (this.uniqueId = "miniapp" + t.randomString(17), | |
121 | - this.uniqueId); | |
122 | - }, | |
123 | - showLoading: function() { | |
124 | - wx.showLoading({ | |
125 | - title: "加载中", | |
126 | - mask:true, | |
127 | - }); | |
128 | - }, | |
129 | - hideLoading: function() { | |
130 | - wx.hideLoading(); | |
131 | - }, | |
132 | - showError: function(t, e) { | |
133 | - wx.showModal({ | |
134 | - title: t, | |
135 | - showCancel: !1, | |
136 | - complete: function() { | |
137 | - 1 == e.failRollback && wx.navigateBack(); | |
138 | - } | |
139 | - }); | |
140 | - }, | |
129 | + getToken: function () { | |
130 | + var t = getApp(); | |
131 | + return null == t.globalData.userInfo ? "" : t.globalData.userInfo.token; | |
132 | + }, | |
133 | + getUniqueId: function () { | |
134 | + return this.uniqueId ? this.uniqueId : (this.uniqueId = "miniapp" + t.randomString(17), | |
135 | + this.uniqueId); | |
136 | + }, | |
137 | + showLoading: function () { | |
138 | + wx.showLoading({ | |
139 | + title: "加载中", | |
140 | + mask: true, | |
141 | + }); | |
142 | + }, | |
143 | + hideLoading: function () { | |
144 | + wx.hideLoading(); | |
145 | + }, | |
146 | + showError: function (t, e) { | |
147 | + wx.showModal({ | |
148 | + title: t, | |
149 | + showCancel: !1, | |
150 | + complete: function () { | |
151 | + 1 == e.failRollback && wx.navigateBack(); | |
152 | + } | |
153 | + }); | |
154 | + }, | |
141 | 155 | |
142 | - request2: function (e, i, o) { | |
156 | + request2: function (e, i, o) { | |
143 | 157 | var n = this, a = o.header ? o.header : { |
144 | 158 | "content-type": "application/x-www-form-urlencoded" |
145 | 159 | }, s = "GET" != (e = e.toUpperCase()) && o.data ? t.json2Form(o.data) : o.data; |
... | ... | @@ -150,7 +164,7 @@ module.exports = { |
150 | 164 | data: s, |
151 | 165 | header: a, |
152 | 166 | success: function (t) { |
153 | - o.isShowLoading && n.hideLoading(), n.doSuccess2(o, t); | |
167 | + o.isShowLoading && n.hideLoading(), n.doSuccess2(o, t); | |
154 | 168 | }, |
155 | 169 | fail: function (t) { |
156 | 170 | o.isShowLoading && n.hideLoading(), n.doFail(o, t); |
... | ... | @@ -160,7 +174,7 @@ module.exports = { |
160 | 174 | get2: function (t, e) { |
161 | 175 | this.request2("GET", t, e); |
162 | 176 | }, |
163 | - doSuccess2: function (t, e){ | |
177 | + doSuccess2: function (t, e) { | |
164 | 178 | if (console.log("app.request", e), 1 != t.successReload) { |
165 | 179 | if (200 != e.statusCode) return this.showError("请求出错[" + e.statusCode + "]", t), |
166 | 180 | !1; |
... | ... | @@ -173,146 +187,184 @@ module.exports = { |
173 | 187 | } else "function" == typeof t.success && t.success(e); |
174 | 188 | }, |
175 | 189 | |
190 | + check_timeout(url, delay, func) { | |
191 | + let now = t.gettimestamp(); | |
192 | + let fail_url = getApp().globalData.fail_url; | |
193 | + if (fail_url.size > 0) { | |
194 | + try { | |
195 | + fail_url.forEach((value, key) => { | |
196 | + if (value.indexOf(url) > 0 || value === url) { | |
197 | + if (now - key <= delay) { | |
198 | + throw new Error('请求超时'); | |
199 | + } else { | |
200 | + fail_url.delete(key); | |
201 | + } | |
202 | + } | |
203 | + }) | |
204 | + } catch (error) { | |
205 | + if (error) { | |
206 | + wx.showModal({ | |
207 | + title: '请求超时', | |
208 | + showCancel: !1, | |
209 | + }); | |
210 | + return false; | |
211 | + } | |
212 | + } | |
213 | + } | |
214 | + typeof func === "function" && func() | |
215 | + }, | |
216 | + | |
176 | 217 | //---promise的使用get---- |
177 | - promiseGet:function(url,data){ | |
178 | - var th=this; | |
179 | - if(url.indexOf("http")==-1) url=getApp().globalData.setting.url +url; | |
218 | + promiseGet: function (url, data, delay = 10000) { | |
219 | + return new Promise((resolve, reject) => { | |
220 | + this.check_timeout(url, delay, () => { | |
221 | + var th = this; | |
222 | + if (url.indexOf("http") == -1) url = getApp().globalData.setting.url + url; | |
223 | + data.isShowLoading && th.showLoading(); | |
224 | + wx.request({ | |
225 | + url, | |
226 | + method: 'GET', | |
227 | + header: { "content-type": "application/x-www-form-urlencoded" }, | |
228 | + data: data.data, | |
229 | + success(res) { | |
230 | + data.isShowLoading && th.hideLoading(); | |
231 | + resolve(res); | |
232 | + }, | |
233 | + fail(err) { | |
234 | + data.isShowLoading && th.hideLoading(); | |
235 | + if (err && err.errMsg && err.errMsg.indexOf('timeout') != -1) { | |
236 | + getApp().globalData.fail_url.set(t.gettimestamp(), url); | |
237 | + console.log('请求超时url---', getApp().globalData.fail_url); | |
238 | + th.doTimeOut() | |
239 | + }else{ | |
240 | + reject(err); | |
241 | + } | |
242 | + } | |
243 | + }) | |
244 | + }) | |
245 | + }) | |
246 | + }, | |
247 | + | |
248 | + //---promise的使用post---- | |
249 | + promisePost: function (url, data) { | |
250 | + var th = this; | |
251 | + if (url.indexOf("http") == -1) url = getApp().globalData.setting.url + url; | |
252 | + var post_data = data.data; | |
253 | + var header = { "content-type": "application/x-www-form-urlencoded" }; | |
254 | + if (data.is_json) { | |
255 | + header = { 'content-type': 'application/json' }; | |
256 | + post_data = JSON.stringify(post_data); | |
257 | + }; | |
180 | 258 | return new Promise((resolve, reject) => { |
181 | 259 | data.isShowLoading && th.showLoading(); |
182 | 260 | wx.request({ |
183 | 261 | url, |
184 | - method: 'GET', | |
185 | - header: {"content-type": "application/x-www-form-urlencoded" }, | |
186 | - data:data.data, | |
262 | + method: 'POST', | |
263 | + header: header, | |
264 | + data: post_data, | |
187 | 265 | success(res) { |
188 | - data.isShowLoading && th.hideLoading(); | |
189 | - resolve(res); | |
266 | + data.isShowLoading && th.hideLoading(); | |
267 | + resolve(res); | |
190 | 268 | }, |
191 | 269 | fail(err) { data.isShowLoading && th.hideLoading(); reject(err); } |
192 | 270 | }) |
193 | 271 | }) |
194 | 272 | }, |
195 | 273 | |
196 | - //---promise的使用post---- | |
197 | - promisePost:function(url,data){ | |
198 | - var th=this; | |
199 | - if(url.indexOf("http")==-1) url=getApp().globalData.setting.url +url; | |
200 | - var post_data=data.data; | |
201 | - var header={"content-type": "application/x-www-form-urlencoded" }; | |
202 | - if(data.is_json){ | |
203 | - header={ 'content-type': 'application/json'}; | |
204 | - post_data=JSON.stringify(post_data); | |
205 | - }; | |
206 | - return new Promise((resolve, reject) => { | |
207 | - data.isShowLoading && th.showLoading(); | |
208 | - wx.request({ | |
209 | - url, | |
210 | - method: 'POST', | |
211 | - header: header, | |
212 | - data:post_data, | |
213 | - success(res) { | |
214 | - data.isShowLoading && th.hideLoading(); | |
215 | - resolve(res); | |
216 | - }, | |
217 | - fail(err) { data.isShowLoading && th.hideLoading(); reject(err); } | |
218 | - }) | |
219 | - }) | |
220 | - }, | |
274 | + //---promise的使用delete---- | |
275 | + promiseDelete: function (url, data) { | |
276 | + var th = this; | |
277 | + if (url.indexOf("http") == -1) url = getApp().globalData.setting.url + url; | |
278 | + return new Promise((resolve, reject) => { | |
279 | + data.isShowLoading && th.showLoading(); | |
280 | + wx.request({ | |
281 | + url, | |
282 | + method: 'DELETE', | |
283 | + header: { "content-type": "application/x-www-form-urlencoded" }, | |
284 | + data: data.data, | |
285 | + success(res) { | |
286 | + data.isShowLoading && th.hideLoading(); | |
287 | + resolve(res); | |
288 | + }, | |
289 | + fail(err) { data.isShowLoading && th.hideLoading(); reject(err); } | |
290 | + }) | |
291 | + }) | |
292 | + }, | |
221 | 293 | |
222 | - //---promise的使用delete---- | |
223 | - promiseDelete:function(url,data){ | |
224 | - var th=this; | |
225 | - if(url.indexOf("http")==-1) url=getApp().globalData.setting.url +url; | |
226 | - return new Promise((resolve, reject) => { | |
227 | - data.isShowLoading && th.showLoading(); | |
228 | - wx.request({ | |
229 | - url, | |
230 | - method: 'DELETE', | |
231 | - header: {"content-type": "application/x-www-form-urlencoded" }, | |
232 | - data:data.data, | |
233 | - success(res) { | |
234 | - data.isShowLoading && th.hideLoading(); | |
235 | - resolve(res); | |
236 | - }, | |
237 | - fail(err) { data.isShowLoading && th.hideLoading(); reject(err); } | |
238 | - }) | |
239 | - }) | |
240 | - }, | |
241 | - | |
242 | - | |
243 | - //---promise的使用put---- | |
244 | - promisePut:function(url,data){ | |
245 | - var th=this; | |
246 | - if(url.indexOf("http")==-1) url=getApp().globalData.setting.url +url; | |
247 | - var post_data=data.data; | |
248 | - var header={"content-type": "application/x-www-form-urlencoded" }; | |
249 | - if(data.is_json){ | |
250 | - header={ 'content-type': 'application/json'}; | |
251 | - post_data=JSON.stringify(post_data); | |
252 | - }; | |
253 | - return new Promise((resolve, reject) => { | |
254 | - data.isShowLoading && th.showLoading(); | |
255 | - wx.request({ | |
256 | - url, | |
257 | - method: 'PUT', | |
258 | - header: header, | |
259 | - data:post_data, | |
260 | - success(res) { | |
261 | - data.isShowLoading && th.hideLoading(); | |
262 | - resolve(res); | |
263 | - }, | |
264 | - fail(err) { data.isShowLoading && th.hideLoading(); reject(err); } | |
265 | - }) | |
266 | - }) | |
267 | - }, | |
268 | 294 | |
269 | - //--------具有时间限制的读取接口---------- | |
270 | - time_limit_get(time,url,data){ | |
271 | - var req=this.get(url,data); | |
272 | - setTimeout(function () { | |
273 | - if(req) req.abort(); | |
274 | - },time*1000) | |
275 | - }, | |
276 | - | |
277 | - /** | |
278 | - * @param {Object} url //url地址 | |
279 | - * @param {Object} data //data入参 | |
280 | - * @param {Object} succ //成功的回调 | |
281 | - * @param {Object} fail //失败的回调 | |
282 | - */ | |
283 | - json_post:function(url,data,succ,fail,mathod){ | |
284 | - var th=this; | |
285 | - var Mathod="POST"; | |
295 | + //---promise的使用put---- | |
296 | + promisePut: function (url, data) { | |
297 | + var th = this; | |
298 | + if (url.indexOf("http") == -1) url = getApp().globalData.setting.url + url; | |
299 | + var post_data = data.data; | |
300 | + var header = { "content-type": "application/x-www-form-urlencoded" }; | |
301 | + if (data.is_json) { | |
302 | + header = { 'content-type': 'application/json' }; | |
303 | + post_data = JSON.stringify(post_data); | |
304 | + }; | |
305 | + return new Promise((resolve, reject) => { | |
306 | + data.isShowLoading && th.showLoading(); | |
307 | + wx.request({ | |
308 | + url, | |
309 | + method: 'PUT', | |
310 | + header: header, | |
311 | + data: post_data, | |
312 | + success(res) { | |
313 | + data.isShowLoading && th.hideLoading(); | |
314 | + resolve(res); | |
315 | + }, | |
316 | + fail(err) { data.isShowLoading && th.hideLoading(); reject(err); } | |
317 | + }) | |
318 | + }) | |
319 | + }, | |
320 | + | |
321 | + //--------具有时间限制的读取接口---------- | |
322 | + time_limit_get(time, url, data) { | |
323 | + var req = this.get(url, data); | |
324 | + setTimeout(function () { | |
325 | + if (req) req.abort(); | |
326 | + }, time * 1000) | |
327 | + }, | |
328 | + | |
329 | + /** | |
330 | + * @param {Object} url //url地址 | |
331 | + * @param {Object} data //data入参 | |
332 | + * @param {Object} succ //成功的回调 | |
333 | + * @param {Object} fail //失败的回调 | |
334 | + */ | |
335 | + json_post: function (url, data, succ, fail, mathod) { | |
336 | + var th = this; | |
337 | + var Mathod = "POST"; | |
286 | 338 | |
287 | 339 | if (mathod) Mathod = mathod; |
288 | - this.showLoading(); | |
289 | - if(url.indexOf("http")==-1) url=getApp().globalData.setting.url +url; | |
290 | - var str = JSON.stringify(data); | |
291 | - wx.request({ | |
292 | - url: url, | |
293 | - data: str, | |
294 | - method: Mathod, | |
295 | - header: { | |
296 | - 'content-type': 'application/json' | |
297 | - },// 设置请求的 header | |
298 | - success: function (res) { succ(res);th.hideLoading(); }, | |
299 | - fail:function(res){fail(res);} | |
300 | - }) | |
301 | - }, | |
302 | - | |
303 | - promise_downimg(url){ | |
304 | - return new Promise((resolve, reject) => { | |
305 | - wx.getImageInfo({ | |
306 | - src: url, | |
307 | - success: function(res) { | |
308 | - //res.path是网络图片的本地地址 | |
309 | - resolve(res.path); | |
310 | - }, | |
311 | - fail: function(err) { | |
312 | - reject(err); | |
313 | - } | |
314 | - }); | |
315 | - }) | |
316 | - } | |
340 | + this.showLoading(); | |
341 | + if (url.indexOf("http") == -1) url = getApp().globalData.setting.url + url; | |
342 | + var str = JSON.stringify(data); | |
343 | + wx.request({ | |
344 | + url: url, | |
345 | + data: str, | |
346 | + method: Mathod, | |
347 | + header: { | |
348 | + 'content-type': 'application/json' | |
349 | + },// 设置请求的 header | |
350 | + success: function (res) { succ(res); th.hideLoading(); }, | |
351 | + fail: function (res) { fail(res); } | |
352 | + }) | |
353 | + }, | |
354 | + | |
355 | + promise_downimg(url) { | |
356 | + return new Promise((resolve, reject) => { | |
357 | + wx.getImageInfo({ | |
358 | + src: url, | |
359 | + success: function (res) { | |
360 | + //res.path是网络图片的本地地址 | |
361 | + resolve(res.path); | |
362 | + }, | |
363 | + fail: function (err) { | |
364 | + reject(err); | |
365 | + } | |
366 | + }); | |
367 | + }) | |
368 | + } | |
317 | 369 | |
318 | 370 | }; |
319 | 371 | \ No newline at end of file | ... | ... |
utils/util.js
... | ... | @@ -16,11 +16,8 @@ function e(e) { |
16 | 16 | //节流 |
17 | 17 | let _throttle =function (fn, interval) { |
18 | 18 | var enterTime = 0;//触发的时间 |
19 | - console.log(1); | |
20 | - | |
21 | 19 | var gapTime = interval || 300 ;//间隔时间,如果interval不传,则默认300ms |
22 | 20 | return function() { |
23 | - console.log(2); | |
24 | 21 | var context = this; |
25 | 22 | var backTime = new Date();//第一次函数return即触发的时间 |
26 | 23 | if (backTime - enterTime > gapTime) { |
... | ... | @@ -29,6 +26,18 @@ let _throttle =function (fn, interval) { |
29 | 26 | } |
30 | 27 | }; |
31 | 28 | }.bind(this); |
29 | + | |
30 | +function _debounce(func, wait) { | |
31 | + let timer; | |
32 | + return function() { | |
33 | + let context = this; // 注意 this 指向 | |
34 | + let args = arguments; // arguments中存着e | |
35 | + if (timer) clearTimeout(timer); | |
36 | + timer = setTimeout(() => { | |
37 | + func.apply(this, args) | |
38 | + }, wait) | |
39 | + } | |
40 | +} | |
32 | 41 | function utf16to8(str) { |
33 | 42 | var out, i, len, c; out = ""; len = str.length; |
34 | 43 | for (i = 0; i < len; i++) { |
... | ... | @@ -616,5 +625,6 @@ module.exports = { |
616 | 625 | convert_arr_key:convert_arr_key, //将JS数组对象按其某个键值重组成Map对象 |
617 | 626 | ajax_ok:ajax_ok, //将JS数组对象按其某个键值重组成Map对象 |
618 | 627 | ajax_ok2:ajax_ok2, //将JS数组对象按其某个键值重组成Map对象 |
619 | - wx_back:wx_back | |
628 | + wx_back:wx_back, | |
629 | + _debounce, | |
620 | 630 | }; | ... | ... |