admin
2020-12-19 84920ada00d69565bef33e7e31bc32b426ec5dc3
util/http.js
@@ -9,20 +9,19 @@
    //判断token是否存在,如果不存在就去获取
    http.getToken(function (token) {
      console.log("获取到token:"+token);
      params.sign = http.getSign(params);
      if (showLoading) {
        my.showLoading();
      }
      console.log("请求参数:"+JSON.stringify(params));
      console.log("请求参数:" + JSON.stringify(params));
      my.request({
        url: url,
        url: getApp().baseUrl + url,
        method: 'POST',
        data: params,
        headers: {
          'content-type': 'application/json',  //默认值
          'content-type': 'application/x-www-form-urlencoded',  //默认值
          'token': token
        },
        dataType: 'json',
@@ -53,15 +52,21 @@
  },
  getToken(success) {
    //本地获取token
    var token;
    let res = my.getStorageSync({ key: 'token' });
    console.log("token缓存结果")
    console.log(res)
    if (res != null) {
      token = res.data;
      token = res.data.token;
      const time = res.data.expireTime;
      if (time == null || time < new Date().getTime() - 1000 * 60) {
        token = null;
      }
    }
    if (token != null && token.length > 0) {
      success(token);
      return;
    }
@@ -71,14 +76,53 @@
        //授权码
        const authCode = res.authCode;
        //todo请求token
        success("token");
        token = "token";
        //将token保存到本地
        my.setStorageSync({
          key: 'token',
          data: token
        });
        http.requestToken(authCode, function (res) {
          console.log("请求结果")
          console.log(res)
          if (res.code == 0) {
            token = res.data.token;
            success(token);
            //res.data.expireTime 到期时间
            //将token保存到本地
            my.setStorageSync({
              key: 'token',
              data: {
                token: token,
                expireTime: res.data.expireTime
              }
            });
          }
        }, null);
      },
    });
  },
  requestToken(authCode, successCallBack, fail) {
    var params = {
      alipayCode: authCode
    };
    params.sign = http.getSign(params);
    my.request({
      url: getApp().baseUrl + "/api/client/user/getToken",
      method: 'POST',
      data: params,
      headers: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (successCallBack != null)
          successCallBack(res.data);
      },
      fail: function (res) {
        if (fail != null)
          fail(res);
      }
    });
  },
  getSign(params) {
@@ -104,7 +148,7 @@
    }
    str += SECRET;
    console.log("签名前字符串:"+str);
    console.log("签名前字符串:" + str);
    return md5.md5(str);
  }
};