Selaa lähdekoodia

fix 修复微信登录失败问题

tumobi 6 vuotta sitten
vanhempi
commit
95837e4231

+ 7 - 14
app.js

@@ -1,23 +1,16 @@
-var util = require('./utils/util.js');
-var api = require('./config/api.js');
-var user = require('./services/user.js');
-
 App({
   onLaunch: function () {
-    //获取用户的登录信息
-    user.checkLogin().then(res => {
-      console.log('app login')
-      this.globalData.userInfo = wx.getStorageSync('userInfo');
+    try {
+      this.globalData.userInfo = JSON.parse(wx.getStorageSync('userInfo'));
       this.globalData.token = wx.getStorageSync('token');
-    }).catch(() => {
-      
-    });
+    } catch (e) {
+      console.log(e);
+    }
   },
-  
+
   globalData: {
     userInfo: {
-      nickname: 'Hi,游客',
-      username: '点击去登录',
+      nickname: '点击登录',
       avatar: 'http://yanxuan.nosdn.127.net/8945ae63d940cc42406c3f67019c5cb6.png'
     },
     token: '',

+ 79 - 28
pages/ucenter/index/index.js

@@ -1,59 +1,110 @@
-var util = require('../../../utils/util.js');
-var api = require('../../../config/api.js');
-var user = require('../../../services/user.js');
-var app = getApp();
+const util = require('../../../utils/util.js');
+const api = require('../../../config/api.js');
+const user = require('../../../services/user.js');
+const app = getApp();
 
 Page({
   data: {
-    userInfo: {}
+    userInfo: {},
+    showLoginDialog: false
   },
-  onLoad: function (options) {
+  onLoad: function(options) {
     // 页面初始化 options为页面跳转所带来的参数
-    console.log(app.globalData)
   },
-  onReady: function () {
+  onReady: function() {
 
   },
-  onShow: function () {
-
-    let userInfo = wx.getStorageSync('userInfo');
-    let token = wx.getStorageSync('token');
-
-    // 页面显示
-    if (userInfo && token) {
-      app.globalData.userInfo = userInfo;
-      app.globalData.token = token;
-    }
-
+  onShow: function() {
     this.setData({
       userInfo: app.globalData.userInfo,
     });
-
   },
-  onHide: function () {
+  onHide: function() {
     // 页面隐藏
 
   },
-  onUnload: function () {
+  onUnload: function() {
     // 页面关闭
   },
-  goLogin(){
-    user.loginByWeixin().then(res => {
+
+  onUserInfoClick: function() {
+    if (wx.getStorageSync('token')) {
+
+    } else {
+      this.showLoginDialog();
+    }
+  },
+
+  showLoginDialog() {
+    this.setData({
+      showLoginDialog: true
+    })
+  },
+
+  onCloseLoginDialog () {
+    this.setData({
+      showLoginDialog: false
+    })
+  },
+
+  onDialogBody () {
+    // 阻止冒泡
+  },
+
+  onWechatLogin(e) {
+    if (e.detail.errMsg !== 'getUserInfo:ok') {
+      if (e.detail.errMsg === 'getUserInfo:fail auth deny') {
+        return false
+      }
+      wx.showToast({
+        title: '微信登录失败',
+      })
+      return false
+    }
+    util.login().then((res) => {
+      return util.request(api.AuthLoginByWeixin, {
+        code: res,
+        userInfo: e.detail
+      }, 'POST');
+    }).then((res) => {
+      console.log(res)
+      if (res.errno !== 0) {
+        wx.showToast({
+          title: '微信登录失败',
+        })
+        return false;
+      }
+      // 设置用户信息
       this.setData({
-        userInfo: res.data.userInfo
+        userInfo: res.data.userInfo,
+        showLoginDialog: false
       });
       app.globalData.userInfo = res.data.userInfo;
       app.globalData.token = res.data.token;
+      wx.setStorageSync('userInfo', JSON.stringify(res.data.userInfo));
+      wx.setStorageSync('token', res.data.token);
     }).catch((err) => {
       console.log(err)
-    });
+    })
+  },
+
+  onOrderInfoClick: function(event) {
+    wx.navigateTo({
+      url: '/pages/ucenter/order/order',
+    })
   },
-  exitLogin: function () {
+
+  onSectionItemClick: function(event) {
+
+  },
+
+  // TODO 移到个人信息页面
+  exitLogin: function() {
     wx.showModal({
       title: '',
       confirmColor: '#b4282d',
       content: '退出登录?',
-      success: function (res) {
+      success: function(res) {
         if (res.confirm) {
           wx.removeStorageSync('token');
           wx.removeStorageSync('userInfo');

+ 3 - 0
pages/ucenter/index/index.json

@@ -1,3 +1,6 @@
 {
+  "navigationBarBackgroundColor": "#333",
+  "navigationBarTitleText": "我的",
+  "navigationBarTextStyle": "white",
   "backgroundColor": "#f4f4f4"
 }

+ 16 - 5
pages/ucenter/index/index.wxml

@@ -1,10 +1,11 @@
 <view class="container">
-  <view class="profile-info" bindtap="goLogin">
-    <image class="avatar" src="{{userInfo.avatar}}"></image>
+  <view class="profile-info">
+    <image bindtap="onUserInfoClick" class="avatar" src="{{userInfo.avatar}}"></image>
     <view class="info">
-      <text class="name">{{userInfo.nickname}}</text>
-      <!-- <text class="level">{{userInfo.username}}</text> -->
+      <text class='name' bindtap='onUserInfoClick'>{{ userInfo.nickname || '点击登录' }}</text>
+      <!-- <text class='level' bindtap='onUserInfoClick'></text> -->
     </view>
+    <image bindtap="onUserInfoClick" class='btn' src='/static/images/address_right.png'></image>
   </view>
 
   <view class="user-menu">
@@ -76,5 +77,15 @@
     </view>
   </view>
 
-  <!--<view class="logout" bindtap="exitLogin">退出登录</view>-->
+  <!-- <view class="logout" bindtap="exitLogin">退出登录</view> -->
+</view>
+
+<view class='dialog-login' wx:if="{{showLoginDialog}}" bindtap='onCloseLoginDialog'>
+  <view class='dialog-body' catchtap='onDialogBody'>
+    <view class='title'>请选择登录方式</view>
+    <view class='content'>
+      <button type="primary" open-type="getUserInfo" bindgetuserinfo="onWechatLogin">微信登录</button>
+      <button open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="onWechatLogin">手机号登录</button>
+    </view>
+  </view>
 </view>

+ 76 - 31
pages/ucenter/index/index.wxss

@@ -9,45 +9,53 @@ page{
     overflow: hidden;
     width: 100%;
 }
-.profile-info{
-    width: 100%;
-    height: 280rpx;
-    display: flex;
-    flex-wrap: wrap;
-    align-items: center;
-    justify-content: flex-start;
-    padding: 0 30.25rpx;
-    background: #333;
+.profile-info {
+  width: 100%;
+  height: 280rpx;
+  display: flex;
+  flex-wrap: wrap;
+  align-items: center;
+  justify-content: flex-start;
+  padding: 0 30.25rpx;
+  background: #333;
 }
 
-.profile-info .avatar{
-    height: 148rpx;
-    width: 148rpx;
-    border-radius: 50%;
+.profile-info .avatar {
+  height: 148rpx;
+  width: 148rpx;
+  border-radius: 50%;
+  border: 4rpx solid #fff;
 }
 
-.profile-info .info{
-    flex: 1;
-    height: 85rpx;
-    padding-left: 31.25rpx;
+.profile-info .info {
+  flex: 1;
+  height: 85rpx;
+  margin-left: 30rpx;
+  display: flex;
+  flex-direction: column;
+  justify-content: center;
 }
 
-.profile-info .name{
-    display: block;
-    height: 45rpx;
-    line-height: 45rpx;
-    color: #fff;
-    font-size: 37.5rpx;
-    margin-bottom: 10rpx;
+.profile-info .name {
+  height: 45rpx;
+  line-height: 45rpx;
+  color: #fff;
+  font-size: 37.5rpx;
 }
 
-.profile-info .level{
-    display: block;
-    height: 30rpx;
-    line-height: 30rpx;
-    margin-bottom: 10rpx;
-    color: #7f7f7f;
-    font-size: 30rpx;
+.profile-info .level {
+  height: 30rpx;
+  line-height: 30rpx;
+  margin-top: 10rpx;
+  color: #7f7f7f;
+  font-size: 30rpx;
+}
+
+.profile-info .btn {
+  width: 50rpx;
+  height: 50rpx;
+  margin-left: 10rpx;
+  border-radius: 50%;
 }
 
 .user-menu{
@@ -150,3 +158,40 @@ page{
     color: #333;
     font-size: 30rpx;
 }
+
+
+.dialog-login{
+  width: 100%;
+  height: 100%;
+  position: fixed;
+  top: 0;
+  left: 0;
+  background: rgba(0, 0, 0, 0.5); 
+  display: flex;
+  justify-content: center;
+  align-items: center;
+}
+
+
+.dialog-login .dialog-body{
+  width: 540rpx;
+  height: auto;
+  overflow: hidden;
+  background: #fff;
+  border-radius: 5px;
+  padding: 40rpx;
+}
+
+.dialog-login .dialog-body .title{
+  width: 100%;
+  text-align: center;
+  line-height: 60rpx;
+}
+
+.dialog-login .dialog-body .content{
+  width: 100%;
+}
+
+.dialog-login .dialog-body button{
+  margin-top: 30rpx;
+}

+ 18 - 7
utils/util.js

@@ -1,4 +1,4 @@
-var api = require('../config/api.js');
+var api = require('../config/api.js')
 
 function formatTime(date) {
   var year = date.getFullYear()
@@ -50,7 +50,7 @@ function request(url, data = {}, method = "GET") {
                   //存储用户信息
                   wx.setStorageSync('userInfo', res.data.userInfo);
                   wx.setStorageSync('token', res.data.token);
-                  
+
                   resolve(res);
                 } else {
                   reject(res);
@@ -77,6 +77,14 @@ function request(url, data = {}, method = "GET") {
   });
 }
 
+function get(url, data = {}) {
+  return request(url, data, 'GET')
+}
+
+function post(url, data = {}) {
+  return request(url, data, 'POST')
+}
+
 /**
  * 检查微信会话是否过期
  */
@@ -101,9 +109,7 @@ function login() {
     wx.login({
       success: function (res) {
         if (res.code) {
-          //登录远程服务器
-          console.log(res)
-          resolve(res);
+          resolve(res.code);
         } else {
           reject(res);
         }
@@ -120,8 +126,11 @@ function getUserInfo() {
     wx.getUserInfo({
       withCredentials: true,
       success: function (res) {
-        console.log(res)
-        resolve(res);
+        if (res.detail.errMsg === 'getUserInfo:ok') {
+          resolve(res);
+        } else {
+          reject(res)
+        }
       },
       fail: function (err) {
         reject(err);
@@ -155,6 +164,8 @@ function showErrorToast(msg) {
 module.exports = {
   formatTime,
   request,
+  get,
+  post,
   redirect,
   showErrorToast,
   checkSession,