feat: 联调code换openid、手机code换手机号码、获取用户信息、一键登录注册并简单封装对应方法
This commit is contained in:
parent
b3ef52a10d
commit
5a6938a857
@ -1,26 +1,12 @@
|
|||||||
<template>
|
<template>
|
||||||
<view class="loginPopup-container">
|
<view class="loginPopup-container">
|
||||||
<uni-popup
|
<uni-popup ref="popup" type="bottom" :mask-click="false" :safe-area="true" :borderRadius="'10px 10x 10px 10px'">
|
||||||
ref="popup"
|
|
||||||
type="bottom"
|
|
||||||
:mask-click="false"
|
|
||||||
:safe-area="true"
|
|
||||||
:borderRadius="'10px 10x 10px 10px'"
|
|
||||||
>
|
|
||||||
<view class="popup-box">
|
<view class="popup-box">
|
||||||
<view class="head-img"></view>
|
<view class="head-img"></view>
|
||||||
<view
|
<view class="uni-icon uni-icon-closeempty" color="#888" @click="close"></view>
|
||||||
class="uni-icon uni-icon-closeempty"
|
|
||||||
color="#888"
|
|
||||||
@click="close"
|
|
||||||
></view>
|
|
||||||
<view class="popup-content">
|
<view class="popup-content">
|
||||||
<view class="title"> 微信账号登录小发同城小程序 </view>
|
<view class="title"> 微信账号登录小发同城小程序 </view>
|
||||||
<button
|
<button class="btn-login" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
|
||||||
class="btn-login"
|
|
||||||
open-type="getPhoneNumber"
|
|
||||||
@getphonenumber="getPhoneNumber"
|
|
||||||
>
|
|
||||||
<view class="iconfontAili icon-weixin"></view>
|
<view class="iconfontAili icon-weixin"></view>
|
||||||
<view>微信一键登录</view>
|
<view>微信一键登录</view>
|
||||||
</button>
|
</button>
|
||||||
@ -101,25 +87,87 @@ export default {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
getPhoneNumber(e) {
|
|
||||||
|
// code 换 openid sessionkey uid
|
||||||
|
async getWxOpenId() {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
var that = this;
|
var that = this;
|
||||||
console.log(e);
|
wx.login({
|
||||||
console.log(e.detail.code); // 动态令牌
|
success: (res) => {
|
||||||
console.log(e.detail.errMsg); // 回调信息(成功失败都会返回)
|
if (res.code) {
|
||||||
console.log(e.detail.errno); // 错误码(失败时返回)
|
|
||||||
// 允许授权
|
|
||||||
if (typeof e.detail.code !== "undefined") {
|
|
||||||
console.log(e.detail.code);
|
|
||||||
//调用接口利用 e.detail.encryptedData, e.detail.iv 信息来解密手机号
|
|
||||||
that.$.request({
|
that.$.request({
|
||||||
url: this.Config.URL.wx.get_user_phone_number,
|
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: {
|
data: {
|
||||||
code: e.detail.code,
|
code: e.detail.code,
|
||||||
},
|
},
|
||||||
|
success: function (res) {
|
||||||
|
that.doWxUserRegisterAndLogin(res.phoneNumber)
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
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",
|
dataType: "json",
|
||||||
success: function (userInfo, status, msg, code) {
|
success: function (res) {
|
||||||
if (status == 200) {
|
if (res.status != 200) {
|
||||||
debugger;
|
that.$.confirm(that.__(res.msg || '登录失败,请重试!'));
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
},
|
||||||
|
});
|
||||||
// that.$.request({
|
// that.$.request({
|
||||||
// url: that.Config.URL.user.overview,
|
// url: that.Config.URL.user.overview,
|
||||||
// success: function (data, status, msg, code) {
|
// success: function (data, status, msg, code) {
|
||||||
@ -130,14 +178,6 @@ export default {
|
|||||||
// that.close();
|
// that.close();
|
||||||
// },
|
// },
|
||||||
// });
|
// });
|
||||||
} else {
|
|
||||||
that.$.confirm(msg);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
that.$.confirm(that.__("请允许手机授权!"));
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@ -266,7 +266,10 @@ export default {
|
|||||||
"pay": bu + "/account/modules/pay/api/payment/wx/pay.php",
|
"pay": bu + "/account/modules/pay/api/payment/wx/pay.php",
|
||||||
"get_tpl_msg_config": au + "?ctl=Connect_Weixin&met=getTmpMsgConfig&typ=json&flag=app",
|
"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",
|
"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": {
|
"store": {
|
||||||
"get": iu + "?ctl=Store&met=get&typ=json",
|
"get": iu + "?ctl=Store&met=get&typ=json",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user