Browse Source

绑定微信登录

zhaoxiaohai 3 years ago
parent
commit
1f1ddd0906

+ 23 - 1
pages/login/index.js

@@ -1,4 +1,6 @@
 import Toast from '@vant/weapp/toast/toast';
+const _Http = getApp().globalData.http;
+const loginMsg = require("./modules/login");
 Page({
 
     /**
@@ -30,7 +32,27 @@ Page({
             isAgree: detail
         })
     },
-
+    wechatLogin() {
+        wx.login({
+            success(res) {
+                if (res.code) {
+                    _Http.loginbywechat({
+                        wechat_code: res.code,
+                        "systemclient": "wechatsaletool"
+                    }).then(res => {
+                        console.log(res)
+                        if (res.code == 0) return wx.showToast({
+                            title: res.msg,
+                            icon: "none"
+                        })
+                        loginMsg.loginMsg(res);
+                    })
+                } else {
+                    console.log('登录失败!' + res.errMsg)
+                }
+            }
+        })
+    },
 
     /**
      * 生命周期函数--监听页面初次渲染完成

+ 1 - 1
pages/login/index.wxml

@@ -1,5 +1,5 @@
 <view style="width: 100vw;text-align: center; margin-top: 480rpx;">
-    <van-button custom-class='login-fun' color="linear-gradient(90deg, #3874F6 0%, #095DE0 100%);">微信用户一键登录</van-button>
+    <van-button custom-class='login-fun' color="linear-gradient(90deg, #3874F6 0%, #095DE0 100%);" bindtap="wechatLogin">微信用户一键登录</van-button>
     <view style="height: 30rpx;" />
     <van-button custom-class='login-fun phone-login' color="#CCCCCC" plain bindtap="toPhoneLodin">账户登录</van-button>
 </view>

+ 16 - 0
pages/login/modules/login.js

@@ -0,0 +1,16 @@
+function loginMsg(res) {
+    wx.setStorageSync('account_list', res.account_list)
+    if (res.account_list.length == 1) {
+        wx.setStorageSync('userMsg', res.account_list[0])
+        wx.switchTab({
+            url: '/pages/tabbar/home/index'
+        })
+    } else {
+        wx.redirectTo({
+            url: './selectSite',
+        })
+    }
+}
+module.exports = {
+    loginMsg
+}

+ 3 - 12
pages/login/phone.js

@@ -1,6 +1,7 @@
 import Toast from '@vant/weapp/toast/toast';
 const _Http = getApp().globalData.http;
 const md5 = require("../../utils/md5");
+const loginMsg = require("./modules/login");
 Page({
 
     /**
@@ -45,24 +46,14 @@ Page({
                 loading: false
             })
             if (res.msg != '成功') return wx.showToast({
-                title: res.data,
+                title: res.msg,
                 icon: "none"
             })
-            wx.setStorageSync('account_list', res.account_list)
             wx.setStorageSync('loginMsg', {
                 accountno: this.data.accountno,
                 password: (this.data.memory) ? this.data.password : ''
             })
-            if (res.account_list.length == 1) {
-                wx.setStorageSync('userMsg', res.account_list[0])
-                wx.switchTab({
-                    url: '/pages/tabbar/home/index'
-                })
-            } else {
-                wx.redirectTo({
-                    url: './selectSite',
-                })
-            }
+            loginMsg.loginMsg(res);
         })
     },
     /* 改变密码输入框类型 */

+ 3 - 2
pages/login/selectSite.js

@@ -25,8 +25,9 @@ Page({
         } = e.currentTarget.dataset;
         wx.setStorageSync('userMsg', item)
         let pages = getCurrentPages();
-        let prevPage = pages[pages.length - 2]; // 获取当前的页面栈
-        if (prevPage.__route__ == 'pages/tabbar/home/index') prevPage.refreshData(item);
+        let prevPage = pages[pages.length - 2];
+        console.log(prevPage)
+        if (prevPage && prevPage.__route__ == 'pages/tabbar/home/index') prevPage.refreshData(item);
         wx.switchTab({
             url: '/pages/tabbar/home/index'
         })

+ 29 - 0
pages/tabbar/mine/index.js

@@ -113,4 +113,33 @@ Page({
             url: './userMsg/index'
         })
     },
+    listClick(e) {
+        const {
+            name
+        } = e.currentTarget.dataset;
+        console.log(name)
+        if (name == '绑定微信') {
+            wx.login({
+                success(res) {
+                    if (res.code) {
+                        _Http.basic({
+                            "classname": "common.usercenter.usercenter",
+                            "method": "WechatBinding",
+                            "content": {
+                                "wechat_code": res.code
+                            }
+                        }).then(res => {
+                            console.log(res)
+                           /*  if (res.code == 0) return wx.showToast({
+                                title: res.msg,
+                                icon: "none"
+                            }) */
+                        })
+                    } else {
+                        console.log('登录失败!' + res.errMsg)
+                    }
+                }
+            })
+        }
+    }
 })

+ 1 - 1
pages/tabbar/mine/index.wxml

@@ -23,7 +23,7 @@
     </My_card>
 </view>
 <My_card custom-class='operating' hover>
-    <navigator class="item" url="{{item.path}}" wx:for="{{pathList}}">
+    <navigator class="item" url="{{item.path}}" wx:for="{{pathList}}" data-name="{{item.name}}" bindtap="listClick">
         <view class="con">
             <view class="label">
                 <text class="iconfont {{item.icon}}" style="color: {{item.color}};" />