admin
2020-12-19 84920ada00d69565bef33e7e31bc32b426ec5dc3
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
var md5 = require("./md5.js");
const SECRET = "123123123";
var http = {
  //网络请求
  postApi(url, params, success, fail, complete, showLoading) {
    if (params == null) {
      params = {};
    }
    //判断token是否存在,如果不存在就去获取
 
    http.getToken(function (token) {
      console.log("获取到token:"+token);
      params.sign = http.getSign(params);
      if (showLoading) {
        my.showLoading();
      }
      console.log("请求参数:" + JSON.stringify(params));
 
      my.request({
        url: getApp().baseUrl + url,
        method: 'POST',
        data: params,
        headers: {
          'content-type': 'application/x-www-form-urlencoded',  //默认值
          'token': token
        },
        dataType: 'json',
        success: function (res) {
          success(res);
          if (res.code == 10001) {//token过期,刷新token
            //删除token
 
            //重新请求
            http.postApi(url, params, success, fail, complete, showLoading);
          }
        },
        fail: function (res) {
          if (fail != null)
            fail(res);
        },
        complete: function (res) {
          my.hideLoading();
          if (complete != null)
            complete(res);
        }
      });
    });
 
 
 
 
  },
  getToken(success) {
    //本地获取token
    var token;
    let res = my.getStorageSync({ key: 'token' });
    console.log("token缓存结果")
    console.log(res)
    if (res != null) {
      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;
    }
 
 
    my.getAuthCode({
      scopes: ['auth_base'],
      success: (res) => {
        //授权码
        const authCode = res.authCode;
        //todo请求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) {
    if (params == null)
      params = {};
    params.timestamp = new Date().getTime();
    params.appKey = getApp().appKey;
 
    var array = new Array();
    for (var key in params) {
      array.push(key + "=" + params[key]);
    }
 
    //排序
    array = array.sort();
    var str = "";
    for (var i = 0; i < array.length; i++) {
      str += array[i] + "&";
    }
 
    if (str.endsWith("&")) {
      str = str.substr(0, str.length - 1);
    }
 
    str += SECRET;
    console.log("签名前字符串:" + str);
    return md5.md5(str);
  }
};
 
module.exports = http;