123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- App({
- globalData: {
- userInfo: null
- },
- /**
- * 网络请求
- * @param {请求的地址} url
- * @param {请求的方式,为GET和POST} method
- * @param {参数} data
- * @param {超时时间} timeout
- * @param {成功执行的方法} success
- * @param {失败执行的方法} fail
- * @param {最终执行的方法} complete
- */
- requestMethod(url, method, data, timeout, success, fail, complete) {
- //如果method参数为null或者空字符串,则默认设置为GET方法
- if (method === null || method === '') {
- method = "GET";
- }
- //如果超时时间为空或者null,则默认设置为60秒
- if (timeout === null || timeout === '') {
- timeout = 60;
- }
- timeout = timeout * 1000;
- //准备userKey
- var userKey = wx.getStorageSync('user_key');
- //访问网络请求
- wx.request({
- url: url,
- method: method,
- data: data,
- timeout: timeout,
- header: {
- "token": userKey
- },
- success(res) {
- //判断userKey是否失效
- if (res.data.code === -1) {
- if (res.data.msg === "unLogin" || res.data.msg === "Invalid Token") {
- wx.showModal({
- title: "提示信息",
- content: "当前未登录或登录超时,请重新登录",
- showCancel: false,
- success() {
- wx.removeStorageSync('user_key');
- wx.navigateTo({
- url: '/pages/login/login?isBack=1',
- });
- }
- })
- return;
- }
- }
- success && success(res.data);
- },
- fail(res) {
- fail && fail(res.data);
- },
- complate(res) {
- complete && complete(res.data);
- }
- })
- },
- })
|