From 84920ada00d69565bef33e7e31bc32b426ec5dc3 Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期六, 19 十二月 2020 16:19:28 +0800
Subject: [PATCH] '接口完善'

---
 util/http.js |   74 +++++++++++++++++++++++++++++-------
 1 files changed, 59 insertions(+), 15 deletions(-)

diff --git a/util/http.js b/util/http.js
index 964a5b4..0ecb7b7 100644
--- a/util/http.js
+++ b/util/http.js
@@ -9,20 +9,19 @@
     //鍒ゆ柇token鏄惁瀛樺湪,濡傛灉涓嶅瓨鍦ㄥ氨鍘昏幏鍙�
 
     http.getToken(function (token) {
-
+      console.log("鑾峰彇鍒皌oken锛�"+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";
-        //灏唗oken淇濆瓨鍒版湰鍦�
-        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 鍒版湡鏃堕棿
+            //灏唗oken淇濆瓨鍒版湰鍦�
+            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);
   }
 };

--
Gitblit v1.8.0