From d77c11e64afea02c5de52e13bb8df374bfb389fb Mon Sep 17 00:00:00 2001 From: 陶杰 <1378534974@qq.com> Date: 星期二, 31 十二月 2024 10:35:55 +0800 Subject: [PATCH] 1.微信登录 --- pages/login/supplier-login.vue | 232 +++++++++++++++++++++++++++++++++++++++++++++++----------- 1 files changed, 188 insertions(+), 44 deletions(-) diff --git a/pages/login/supplier-login.vue b/pages/login/supplier-login.vue index 15cd979..441ddef 100644 --- a/pages/login/supplier-login.vue +++ b/pages/login/supplier-login.vue @@ -127,16 +127,32 @@ <view class="flex" style="justify-content: center; margin-top: 150px;"> <u--image :showLoading="true" v-if="false" src="https://hmy-flower.oss-cn-shanghai.aliyuncs.com/6e/6e74997e6d4c4a29be3903b35bc1f38fwechat.png" - width="40rpx" height="40rpx" @click="handleWechatClick"> + width="40rpx" height="40rpx"> </u--image> <u-grid :border="false" :col="1"> - <u-grid-item @click="handleWechatClick"> + <u-grid-item v-if="wxUser.user" @click="handleWechatClick"> <u--image :showLoading="true" src="https://hmy-flower.oss-cn-shanghai.aliyuncs.com/6e/6e74997e6d4c4a29be3903b35bc1f38fwechat.png" - width="40rpx" height="40rpx" @click="handleWechatClick"> + width="60rpx" height="60rpx" > </u--image> - <text class="third-login-text" @click="handleWechatClick">微信登录</text> + + </u-grid-item> + + <u-grid-item v-else> + + <button class="component-button-contact" style="font-size: 18rpx; margin: auto;" open-type="getPhoneNumber" @getphonenumber="handleGetPhoneNumber" > + <view style="display: flex; align-items: center;"> + <u--image :showLoading="true" + src="https://hmy-flower.oss-cn-shanghai.aliyuncs.com/6e/6e74997e6d4c4a29be3903b35bc1f38fwechat.png" + width="60rpx" height="60rpx" > + </u--image> + + </view> + </button> + + </u-grid-item> + </u-grid> </view> <!-- #endif --> @@ -146,6 +162,10 @@ <script> export default { async onLoad(options) { + + // this.getOpenId(); + + this.source = options.source || '' this.sharePartnerUserId = options.sharePartnerUserId || '' // #ifdef PUB_CUSTOMER @@ -365,6 +385,10 @@ // #endif }, + + onShow() { + this.getOpenId(); + }, onShareAppMessage() { let that = this; var name = that.currentInfo.customerDTO && that.currentInfo.customerDTO.name || that.currentInfo.supplierDTO && @@ -419,10 +443,168 @@ apitype: 'loginCustomer', // #endif // return 'loginPartner' + + + wxUser:{ + openId:'', + sessionKey:'', + unionId:'', + user:{}, + phoneNumber:'', + purePhoneNumber:'', + }, + }; }, methods: { + getOpenId(){ + let that=this + uni.login({ + "provider": "weixin", + "onlyAuthorize": true, // 微信登录仅请求授权认证 + success: async function (event) { + console.log("aaaa") + console.log(event) + const { code } = event + await that.getExistUserByOpenId(code); + that.wxLoginCode=code + console.log("wxUser") + console.log(that.wxUser) + + }, + fail: function (err) { + // 登录授权失败 + // err.code是错误码 + } + }) + }, + + + + async handleGetPhoneNumber(e){ + let that = this + console.log(e) + console.log(e.detail.code) // 动态令牌 + console.log(e.detail.errMsg) // 回调信息(成功失败都会返回) + console.log(e.detail.errno) // 错误码(失败时返回) + if (e.detail.errMsg == 'getPhoneNumber:ok') { + console.log('获取成功') + const { + code,data + } = await that.$http.request('post', '/api/wx/getuserphonenumber', { + data: { + code: e.detail.code, + userType: that.apitype.replace("login", "").toLowerCase() + } + }) + if (code == 0) { + // 获取手机号码后,实现微信登录 + console.log(data) + const phoneNumber=data.phone_info.phoneNumber; + const purePhoneNumber=data.phone_info.purePhoneNumber; + that.wxUser.phoneNumber=phoneNumber + that.wxUser.purePhoneNumber=purePhoneNumber + that.handleWechatClick() + } else { + + } + }else{ + this.$message.showToast('获取授权失败,无法登录!') + } + }, + + async getExistUserByOpenId(accessCode){ + let that=this; + const { + code,data + } = await that.$http.request('post', '/api/wx/getExistUserByOpenId', { + data: { + code: accessCode, + userType: that.apitype.replace("login", "").toLowerCase() + } + }) + if (code == 0) { + console.log("查看当前用户是否已经绑定过") + console.log(data) + that.wxUser=data + if(that.wxUser&&that.wxUser.user){ + return true + } + return false + } else { + this.$message.showToast('系统异常,无法获取当前微信是否已经绑定过账号') + } + }, + + handleWechatClick22() { + uni.login({ + provider: 'univerify', + univerifyStyle: { + fullScreen: true + } + }) + }, + + handleWechatClick_new() { + let that = this + // 微信登录 + uni.login({ + "provider": "weixin", + "onlyAuthorize": true, // 微信登录仅请求授权认证 + success: async function (event) { + const { code } = event + that.$message.showLoading(); + // 1.解析当前code,查看当前的openid是否已经绑定过用户, + // 2.如果没有的话,则获取当前手机号,并注册信息用户绑定再登录 + // 3.如果已经绑定过用户,则直接登录 + console.log("查看当前用户是否已经绑定过") + const resp = await that.getExistUserByOpenId(code) + if(resp){ + // 已经绑定过用户,则直接登录 + const resp = await that.$store.dispatch('loginwx', { code }) + if (resp && resp.data) { + that.$forceUpdate() + uni.reLaunch({ + url: '/pages/home/home' + }) + } + }else{ + // 没有绑定过用户,则获取当前手机号,并注册信息用户绑定再登录 + // let el = uni.createSelectorQuery().in(this).select('#phoneButton'); + // console.log(el) + // 使用 uni.createSelectorQuery 获取按钮元素 + uni.createSelectorQuery().select('#phoneButton').boundingClientRect((rect) => { + if (rect) { + // 获取到按钮后,模拟点击 + uni.createSelectorQuery().select('#phoneButton').node((res) => { + if (res.node) { + res.node.click(); // 模拟点击 + } + }).exec(); + } + }).exec(); + } + + + // const resp = await that.$store.dispatch('loginwx', { code }) + // if (resp && resp.data) { + // that.$forceUpdate() + // uni.reLaunch({ + // url: '/pages/home/home' + // }) + // } + that.$message.hideLoading(); + }, + fail: function (err) { + // 登录授权失败 + // err.code是错误码 + } + }) + + }, + + handleWechatClick() { let that = this // 微信登录 @@ -430,55 +612,16 @@ "provider": "weixin", "onlyAuthorize": true, // 微信登录仅请求授权认证 success: async function (event) { - console.log("客户端成功获取授权临时票据(code),向业务服务器发起登录请求。") - console.log(event) const { code } = event - console.log('code', code) that.$message.showLoading(); - const resp = await that.$store.dispatch('loginwx', { code }) + const resp = await that.$store.dispatch('loginwx', { code,phoneNumber:that.wxUser.phoneNumber,purePhoneNumber:that.wxUser.purePhoneNumber }) if (resp && resp.data) { that.$forceUpdate() uni.reLaunch({ url: '/pages/home/home' }) } - // const resp = await that.$http.request('get', '/api/wx/jscode2session', { - // params: { - // jsCode: code, - // userType: that.apitype.replace("login", "").toLowerCase() - // } - // }); that.$message.hideLoading(); - // console.log('resp', resp) - // if (resp && resp.data) { - // var cts = resp.data; - // var openid = cts.openid; //openid 用户唯一标识 - // if (cts && cts.openid) { - // that.$storage.setItem('openid', cts.openid); - // } - - // var session_key = cts.session_key; //session_key 会话密钥 - - // if (true) { - // if (!!cts.errmsg) { - // that.$message.showToast('获取失败' + cts.errmsg); - // } else { - // //成功了,再调用 - // uni.getUserInfo({ - // provider: 'weixin', - // success: function (infoRes) { - // console.log("uni.getUserInfo") - // console.log(infoRes) - // console.log('用户信息:', infoRes.userInfo); - // }, - // fail: function (err) { - // console.error('获取用户信息失败:', err); - // }, - // }); - - // } - // } - // } }, fail: function (err) { // 登录授权失败 @@ -893,4 +1036,5 @@ font-size: 18rpx; margin-top:5rpx; } + </style> \ No newline at end of file -- Gitblit v1.9.3