diff --git a/litemall-wx/pages/goods/goods.js b/litemall-wx/pages/goods/goods.js index 57dcc5a0..9e0be4c5 100644 --- a/litemall-wx/pages/goods/goods.js +++ b/litemall-wx/pages/goods/goods.js @@ -2,6 +2,7 @@ var app = getApp(); var WxParse = require('../../lib/wxParse/wxParse.js'); var util = require('../../utils/util.js'); var api = require('../../config/api.js'); +var user = require('../../services/user.js'); Page({ data: { @@ -190,16 +191,7 @@ Page({ this.setData({ id: parseInt(options.id) }); - var that = this; this.getGoodsInfo(); - util.request(api.CartGoodsCount).then(function (res) { - if (res.errno === 0) { - that.setData({ - cartGoodsCount: res.data - }); - - } - }); }, onReady: function () { // 页面渲染完成 @@ -207,7 +199,14 @@ Page({ }, onShow: function () { // 页面显示 - + var that = this; + util.request(api.CartGoodsCount).then(function (res) { + if (res.errno === 0) { + that.setData({ + cartGoodsCount: res.data + }); + } + }); }, onHide: function () { // 页面隐藏 @@ -269,8 +268,11 @@ Page({ }, openCartPage: function () { - wx.switchTab({ - url: '/pages/cart/cart', + user.checkLogin().then(() => { + wx.switchTab({ url: '/pages/cart/cart' }); + }) + .catch(() => { + wx.navigateTo({ url: "/pages/auth/login/login" }); }); }, addFast: function () { diff --git a/litemall-wx/services/user.js b/litemall-wx/services/user.js index 8bbf98fb..48f4f2fa 100644 --- a/litemall-wx/services/user.js +++ b/litemall-wx/services/user.js @@ -52,7 +52,31 @@ function getUserInfo() { resolve(res); }, fail: function (err) { - reject(err); + + wx.showModal({ + title: '用户未授权', + content: '请给予您的用户信息授权。', + success: function (res) { + if (res.confirm) { + wx.openSetting({ + success: (res) => { + if (res.authSetting["scope.userInfo"] === true) { + wx.getUserInfo({ + withCredentials: true, + success: function (res) { + resolve(res); + }, + }) + } + } + }) + } else if (res.cancel) { + wx.navigateBack({ + delta: 1 + }) + } + } + }) } }) }); @@ -95,13 +119,11 @@ function loginByWeixin() { function checkLogin() { return new Promise(function (resolve, reject) { if (wx.getStorageSync('userInfo') && wx.getStorageSync('token')) { - checkSession().then(() => { resolve(true); }).catch(() => { reject(false); }); - } else { reject(false); }