TM
2020-12-14 1d1ceeeda9e5cb70bf10dafa82efddc8b7861360
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
const md5 = require('./md5.js')
const app = getApp()
const baseUrl = 'https://t.finlean.com'
/**
 * url请求地址
 * data 数据对象
 * method 方法
 * success成功回调
 * fail失败回调
 * **/ 
function $api(url,method,success,fail,needLogin,data){
  my.showLoading({
    title: '正在加载...'
  });
  // 记录发起请求的当前时间
  let timeStart = Date.now();
  var str = ''
  for(let key in data){
    str = str + data[key]
  }
  var params = {...data}
  // console.log(needLogin)
  if(needLogin){
    console.log('验签字符串',str+my.getStorageSync ({ key: 'sessionId' }).data.sessionId)
    console.log('加密',md5.hex_md5('30000003100阿萨德13阿三1588888888810759c883-e0b6-45ea-85db-07d67bb4f509'))
    let sessionId = my.getStorageSync ({ key: 'sessionId' }).data.sessionId
    // params.sessionId = sessionId
    params.jttechSign = md5.hex_md5(str+sessionId)
  }
  my.request({
    url: `${baseUrl}${url}`,
    headers: {'content-type':'application/json'},
    method: method,
    data: {
      ...params
    },
    timeout: 60000,
    dataType: 'json',
    success: success,
    fail: fail,
    complete:function(res){
      // console.log(`耗时${Date.now() - timeStart}`, baseUrl + url);
      
      if(res.status == 200){
        setTimeout(function () {
          my.hideLoading();
        }, 1000);
      }else{
        if(res.data.code == 99999){
          my.hideLoading();
          //登录超时
          my.showToast({
            type: 'none',
            content: res.data.errMsg,
            duration: 3000,
            success: () => {
              my.reLaunch({
                url: '/pages/index/index',
              })
            },
          });
        }
        if(res.data.errorCode == -999){
          //接口错误
          my.hideLoading();
          my.showToast({
            type: 'none',
            content: res.data.errMsg,
          })
        }
        if(res.status == 400 || res.status == 500){
          //接口错误
          my.hideLoading();
          if(res.data.status==90040006){
            //登录超时
            my.showToast({
              type: 'none',
              content: res.data.message,
              duration: 1000,
              success: () => {
                my.reLaunch({
                  url: '/pages/index/index',
                })
              },
            });
          }else{
            my.hideLoading();
            my.showToast({
              type: 'none',
              content: res.data.errMsg,
            })
          }
        }
      }
    }
  });
}
module.exports = {
    ajax: $api,
}