feat: 联调code换openid、手机code换手机号码、获取用户信息、一键登录注册并简单封装对应方法

This commit is contained in:
mixtan 2025-05-07 10:22:53 +08:00
parent b3ef52a10d
commit 5a6938a857
2 changed files with 95 additions and 52 deletions

View File

@ -1,26 +1,12 @@
<template>
<view class="loginPopup-container">
<uni-popup
ref="popup"
type="bottom"
:mask-click="false"
:safe-area="true"
:borderRadius="'10px 10x 10px 10px'"
>
<uni-popup ref="popup" type="bottom" :mask-click="false" :safe-area="true" :borderRadius="'10px 10x 10px 10px'">
<view class="popup-box">
<view class="head-img"></view>
<view
class="uni-icon uni-icon-closeempty"
color="#888"
@click="close"
></view>
<view class="uni-icon uni-icon-closeempty" color="#888" @click="close"></view>
<view class="popup-content">
<view class="title"> 微信账号登录小发同城小程序 </view>
<button
class="btn-login"
open-type="getPhoneNumber"
@getphonenumber="getPhoneNumber"
>
<button class="btn-login" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
<view class="iconfontAili icon-weixin"></view>
<view>微信一键登录</view>
</button>
@ -57,7 +43,7 @@ export default {
// immediate: true,
},
},
onUnload() {},
onUnload() { },
onHide() {
this.close();
},
@ -89,8 +75,8 @@ export default {
that.$.request({
url: that.cf.URL.get_miniapp_open_id,
data: params,
success: (data, status, msg, code) => {},
fail: (err, status) => {},
success: (data, status, msg, code) => { },
fail: (err, status) => { },
});
} else {
that.$.showModal({
@ -101,44 +87,98 @@ export default {
},
});
},
getPhoneNumber(e) {
var that = this;
console.log(e);
console.log(e.detail.code); //
console.log(e.detail.errMsg); //
console.log(e.detail.errno); //
//
if (typeof e.detail.code !== "undefined") {
console.log(e.detail.code);
// e.detail.encryptedData, e.detail.iv
that.$.request({
url: this.Config.URL.wx.get_user_phone_number,
data: {
code: e.detail.code,
},
dataType: "json",
success: function (userInfo, status, msg, code) {
if (status == 200) {
debugger;
// that.$.request({
// url: that.Config.URL.user.overview,
// success: function (data, status, msg, code) {
// userInfo = Object.assign(userInfo, data.member_info);
// that.login(userInfo);
// that.close();
// },
// });
} else {
that.$.confirm(msg);
// code openid sessionkey uid
async getWxOpenId() {
return new Promise((resolve, reject) => {
var that = this;
wx.login({
success: (res) => {
if (res.code) {
that.$.request({
url: this.Config.URL.wx.get_wx_openid,
data: {
code: res.code,
},
dataType: "json",
success: function (res) {
resolve(res.openid)
},
});
}
},
});
})
},
//
async getUserprofile() {
return new Promise((resolve, reject) => {
wx.getUserInfo({
success: (res) => {
resolve(res)
}
})
})
},
//
async getPhoneNumber(e) {
var that = this;
if (e.detail.errMsg == "getPhoneNumber:ok") {
that.$.request({
url: this.Config.URL.wx.get_wx_phone_number,
data: {
code: e.detail.code,
},
success: function (res) {
that.doWxUserRegisterAndLogin(res.phoneNumber)
},
});
} else {
that.$.confirm(that.__("请允许手机授权!"));
that.$.confirm(that.__("授权失败"));
}
},
//
async doWxUserRegisterAndLogin(phoneNumber) {
this.close()
const openId = await this.getWxOpenId()
const user = await this.getUserprofile()
const that = this
this.$.request({
url: this.Config.URL.wx.doWxUserRegisterAndLogin,
data: {
nickName: user.userInfo.nickName,
avatarUrl: user.userInfo.avatarUrl,
phoneNumber,
openId
},
method: "POST",
header: {
'Content-Type': 'application/json;charset=utf-8'
},
dataType: "json",
success: function (res) {
if (res.status != 200) {
that.$.confirm(that.__(res.msg || '登录失败,请重试!'));
return
}
},
});
// that.$.request({
// url: that.Config.URL.user.overview,
// success: function (data, status, msg, code) {
// userInfo = Object.assign(userInfo, data.member_info);
// that.login(userInfo);
// that.close();
// },
// });
},
},
};
</script>

View File

@ -266,7 +266,10 @@ export default {
"pay": bu + "/account/modules/pay/api/payment/wx/pay.php",
"get_tpl_msg_config": au + "?ctl=Connect_Weixin&met=getTmpMsgConfig&typ=json&flag=app",
"send_tpl_msg": au + "?ctl=Connect_Weixin&met=sendTplMsg&typ=json&flag=app",
"get_user_phone_number": ip + "account/weiXin/getUserPhoneNumber"
"get_user_phone_number": ip + "account/weiXin/getUserPhoneNumber",
"get_wx_phone_number": ip + "account/weiXin/getWxUserPhoneNumber",
"get_wx_openid": ip + "account/weiXin/getWxOpenId",
"doWxUserRegisterAndLogin": ip + "account/login/doWxUserRegisterAndLogin",
},
"store": {
"get": iu + "?ctl=Store&met=get&typ=json",