bugfix: authrequest 方法上线:

需要登录的request使用该方法可以判断当前用户是否已经登录,没有登录会跳转 ‘我的’页面,否则自动登录
wechat_user_type
Ge Hao 8 years ago
parent 7a06977262
commit 33dbaa659b
  1. 39
      app.js
  2. 2
      pages/cart/coupon.js
  3. 12
      pages/mine/mine.js
  4. 2
      utils/order.js
  5. 6
      utils/profile.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:{

@ -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){

@ -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})

@ -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 || {},

@ -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) {

Loading…
Cancel
Save