Browse Source

处理用户初始没有授权用户信息的情况。

Junling Bu 7 years ago
parent
commit
254cfb5598
2 changed files with 39 additions and 15 deletions
  1. 14 12
      litemall-wx/pages/goods/goods.js
  2. 25 3
      litemall-wx/services/user.js

+ 14 - 12
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 () {

+ 25 - 3
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);
     }