Browse Source

重新实现小商场的用户登陆流程。

Junling Bu 7 years ago
parent
commit
c0fb073258

+ 0 - 15
litemall-wx/app.js

@@ -4,23 +4,8 @@ var user = require('./services/user.js');
 
 App({
   onLaunch: function () {
-    //获取用户的登录信息
-    user.checkLogin().then(res => {
-      console.log('app login')
-      this.globalData.userInfo = wx.getStorageSync('userInfo');
-      this.globalData.token = wx.getStorageSync('token');
-    }).catch(() => {
-      
-    });
   },
   
   globalData: {
-    userInfo: {
-      nickName: 'Hi,游客',
-      userName: '点击去登录',
-      avatarUrl: 'http://yanxuan.nosdn.127.net/8945ae63d940cc42406c3f67019c5cb6.png'
-    },
-    token: '',
-    hasLogin: false
   }
 })

+ 21 - 18
litemall-wx/pages/ucenter/index/index.js

@@ -5,28 +5,29 @@ var app = getApp();
 
 Page({
   data: {
-    userInfo: {}
+    userInfo: {
+      nickName: '点击登录',
+      avatarUrl: 'http://yanxuan.nosdn.127.net/8945ae63d940cc42406c3f67019c5cb6.png'
+    }
   },
   onLoad: function (options) {
     // 页面初始化 options为页面跳转所带来的参数
-    console.log(app.globalData)
   },
   onReady: function () {
 
   },
   onShow: function () {
 
-    let userInfo = wx.getStorageSync('userInfo');
-    let token = wx.getStorageSync('token');
+    //获取用户的登录信息
+    user.checkLogin().then(res => {
+      let userInfo = wx.getStorageSync('userInfo');
 
-    // 页面显示
-    if (userInfo && token) {
-      app.globalData.userInfo = userInfo;
-      app.globalData.token = token;
-    }
+      this.setData({
+        userInfo: userInfo,
+      });
+
+    }).catch(() => {
 
-    this.setData({
-      userInfo: app.globalData.userInfo,
     });
 
   },
@@ -38,14 +39,16 @@ Page({
     // 页面关闭
   },
   goLogin(){
-    user.loginByWeixin().then(res => {
-      this.setData({
-        userInfo: res.data.userInfo
+    user.checkLogin().catch(() => {
+
+      user.loginByWeixin().then(res => {
+        this.setData({
+          userInfo: res.data.userInfo,
+        });
+      }).catch((err) => {
+        util.showErrorToast('登陆失败');
       });
-      app.globalData.userInfo = res.data.userInfo;
-      app.globalData.token = res.data.token;
-    }).catch((err) => {
-      console.log(err)
+
     });
   },
   exitLogin: function () {

+ 1 - 3
litemall-wx/pages/ucenter/index/index.wxml

@@ -2,9 +2,7 @@
   <view class="profile-info" bindtap="goLogin">
     <image class="avatar" src="{{userInfo.avatarUrl}}"></image>
     <view class="info">
-      <!-- <text class="name">{{userInfo.nickName}}</text> -->
-      <text class="name">litemall用户</text> 
-      <text class="level">{{userInfo.userName}}</text> 
+       <text class="name">{{userInfo.nickName}}</text> 
     </view>
   </view>