From 33dbaa659bdbc53225ce78515db25768453e4f38 Mon Sep 17 00:00:00 2001 From: Ge Hao Date: Fri, 25 Nov 2016 19:50:30 +0800 Subject: [PATCH] =?UTF-8?q?bugfix:=20authrequest=20=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E4=B8=8A=E7=BA=BF=EF=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 需要登录的request使用该方法可以判断当前用户是否已经登录,没有登录会跳转 ‘我的’页面,否则自动登录 --- app.js | 39 +++++++++++++++++++++++++++++++++------ pages/cart/coupon.js | 2 +- pages/mine/mine.js | 12 ++++++++---- utils/order.js | 2 +- utils/profile.js | 6 ++---- 5 files changed, 45 insertions(+), 16 deletions(-) diff --git a/app.js b/app.js index 14ce3f7..63fc9a0 100644 --- a/app.js +++ b/app.js @@ -34,22 +34,20 @@ App({ request: function(obj) { var header = obj.header || {} - if (!header['content-type']) { - header['content-type'] = 'application/json' + if (!header['Content-Type']) { + header['Content-Type'] = 'application/json' } if (!header['Authorization']) { header['Authorization'] = getApp().globalData.token } + // This must be wx.request ! wx.request({ url: obj.url, data: obj.data || {}, method: obj.method || 'GET', header: header, success: function(res) { - if (res.statusCode === 401) { - - } typeof obj.success == "function" && obj.success(res) }, fail: obj.fail || function() {}, @@ -58,7 +56,36 @@ App({ }, authRequest: function(obj) { - function unauthorizeCallback() {} + var that = this + if (!that.globalData.token) { + var token = wx.getStorageSync('userToken') + that.globalData.token = token + that.request({ + url: `${that.globalData.API_URL}/sessions/new`, + data: {code: that.globalData.code}, + success: function(res) { + if (!res.data.token) { + wx.redirectTo({ + url: '../mine/mine', + success: function(res){}, + fail: function() {}, + complete: function() {} + }) + } else { + that.globalData.currentCustomer = res.data.customer + that.globalData.token = res.data.token + wx.setStorage({ + key: 'userToken', + data: res.data.token + }) + that.request(obj) + } + }, + fail: function(res) {} + }) + } else { + that.request(obj) + } }, globalData:{ diff --git a/pages/cart/coupon.js b/pages/cart/coupon.js index 12791c8..0c36aeb 100644 --- a/pages/cart/coupon.js +++ b/pages/cart/coupon.js @@ -17,7 +17,7 @@ Page({ // var data = wx.getStorageSync('coupons') // this.setData({coupons: data}) var that = this - app.request({ + app.authRequest({ url: `${app.globalData.API_URL}/coupons`, method: 'GET', success: function(res){ diff --git a/pages/mine/mine.js b/pages/mine/mine.js index dfcc48c..77a3171 100644 --- a/pages/mine/mine.js +++ b/pages/mine/mine.js @@ -30,10 +30,14 @@ Page({ onLoad: function() { var that = this - var token = wx.getStorageSync('userToken') - if (token) { - var data = {token: token} - profile.getCustomerInfo(data, that.infoCallback) + if (app.globalData.token) { + profile.getCustomerInfo({}, that.infoCallback) + } else { + var token = wx.getStorageSync('userToken') + if (token) { + app.globalData.token = token + profile.getCustomerInfo({}, that.infoCallback) + } } app.getUserInfo(function(userInfo){ that.setData({userInfo: userInfo}) diff --git a/utils/order.js b/utils/order.js index b532bfa..e486d61 100644 --- a/utils/order.js +++ b/utils/order.js @@ -1,7 +1,7 @@ const app = getApp() function postBilling (data, resolve) { - app.request({ + app.authRequest({ method: 'POST', url: `${app.globalData.API_URL}/orders/create_applet_order`, data: data || {}, diff --git a/utils/profile.js b/utils/profile.js index e704db1..2352ca9 100644 --- a/utils/profile.js +++ b/utils/profile.js @@ -1,9 +1,8 @@ const app = getApp() function getZichanSlides (resolve) { - app.request({ + app.authRequest({ url: `${app.globalData.API_URL}/my_assets`, - data: {token: app.globalData.token}, header: { 'Content-Type': 'application/json'}, success: resolve, fail: function(){} @@ -12,9 +11,8 @@ function getZichanSlides (resolve) { function getCustomerInfo (data, cb) { data['code'] = app.globalData.code - wx.request({ + app.request({ url: `${app.globalData.API_URL}/sessions/new`, - header: { 'Content-Type': 'application/json'}, data: data, success: function(res) { if (res.data.code === 4) {