From 254cfb5598de84ccec4f7f279e91cd969f5ce8e1 Mon Sep 17 00:00:00 2001 From: Junling Bu Date: Tue, 27 Mar 2018 21:01:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E7=94=A8=E6=88=B7=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E6=B2=A1=E6=9C=89=E6=8E=88=E6=9D=83=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E7=9A=84=E6=83=85=E5=86=B5=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- litemall-wx/pages/goods/goods.js | 26 ++++++++++++++------------ litemall-wx/services/user.js | 28 +++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 15 deletions(-) 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); }