feat(auth): 我的添加openid获取功能,并优化登录页

This commit is contained in:
fhysy 2025-03-27 09:56:38 +08:00
parent c5012e3e9a
commit c1dbf6b002
4 changed files with 105 additions and 19 deletions

View File

@ -183,7 +183,6 @@
}
})
}
},
onReady() {
this.$refs.uForm.setRules(this.rules);
@ -578,9 +577,9 @@
}
.login-version-info {
position: absolute;
left: 0;
bottom: 40rpx;
// position: fixed;
// left: 0;
// bottom: 40rpx;
width: 100%;
text-align: center;
font-size: $uni-font-size-sm;

View File

@ -572,9 +572,9 @@
}
.login-version-info {
position: absolute;
left: 0;
bottom: 40rpx;
// position: absolute;
// left: 0;
// bottom: 40rpx;
width: 100%;
text-align: center;
font-size: $uni-font-size-sm;

View File

@ -293,17 +293,6 @@ var jweixin = require('jweixin-module');
}).catch(() => {
})
// uni.request({
// url: 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='+this.officialAccount.appid+'&secret='+this.officialAccount.secret +'&grant_type=authorization_code&code='+code,
// success: (res) => {
// console.log('codeopenidaccessToken', res)
// // if(res.data.code === 200) {
// // // token
// // uni.setStorageSync('userInfo', res.data.result.userInfo)
// // uni.setStorageSync('token', res.data.result.token)
// // }
// }
// })
},
wxpay(data) {
// if (typeof WeixinJSBridge == "undefined") {

View File

@ -9,6 +9,7 @@
<view class="info-content">
<view class="nickname">{{ userInfo.nickName }}</view>
<view class="user-id">手机号{{ userInfo.phonenumber }}</view>
<view class="user-open-id">{{ openId }} <text class="iconfont icon-clipboard" @click="copyOpenID"></text> </view>
</view>
</view>
@ -110,10 +111,18 @@
background: "linear-gradient(0deg, #B22C9D, #FF6EC7);",
twoColor: "rgba(213,54,189,0.1);"
}
]
],
officialAccount: {
appid: '',
appsecret: '',
},
openId:''
}
},
onLoad() {
let appIdPublic = uni.getStorageSync('appIdPublic');
console.log("this.$store.state.app.appConfig.appIdPublic",this.$store.state.app.appConfig.appIdPublic)
this.officialAccount.appid = appIdPublic || '';
// #ifdef MP-WEIXIN
const accountInfo = wx.getAccountInfoSync();
if(accountInfo.miniProgram.envVersion=='release'){
@ -121,8 +130,87 @@
}
// #endif
this.getUserInfoFn();
},
onShow() {
// opneID
let res = uni.getStorageSync('openId');
if (res) {
this.openId = res;
} else {
// #ifdef H5
let code = this.getUrlCode('code')
if (code || this.code) {
this.code = code;
this.getOpenidAndUserinfo(code)
// // URLcode
// const url = window.location.href;
// const newUrl = url.split('?')[0]; //
// history.replaceState({}, '', newUrl+'');
} else {
this.getH5Code()
}
// #endif
}
},
methods: {
copyOpenID(){
uni.setClipboardData({
data: this.openId,
success: function () {
console.log('success');
}
});
},
isWechat() {
return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger";
},
getH5Code() {
if (this.isWechat()) {
// codecodecodecodeopenId
window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + this.officialAccount
.appid + '&redirect_uri=' + encodeURIComponent(window.location.href) +
'&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect'
console.log("跳转授权页面",'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + this.officialAccount
.appid + '&redirect_uri=' + encodeURIComponent(window.location.href) +
'&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect')
}
},
getUrlCode(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [,
''])[1].replace(/\+/g, '%20')) || null
},
getOpenidAndUserinfo(code) {
console.log("code换openid")
this.$api.orderApi.getWxOpenid({
code: code
}).then((res) => {
console.log("获取openid", res)
if (res.code == 200) {
if(res.data.openid){
this.openId = res.data.openid;
uni.setStorageSync('openId', res.data.openid)
this.clearUrlCode();
}else{
this.clearUrlCode();
this.getUrlCode();
}
// 1
// 2
}else{
}
}).catch(() => {
})
},
clearUrlCode(){
// URLcode
const url = window.location.href;
const newUrl = url.split('?')[0]; //
history.replaceState({}, '', newUrl+'/pages/order/detail?id='+this.orderId);
},
getUserInfoFn() {
// let userToken = uni.getStorageSync("userToken");
let userType = uni.getStorageSync("userType");
@ -201,6 +289,16 @@
font-size: 14px;
color: #999999;
}
.user-open-id{
font-size: 20rpx;
color: #999999;
display: flex;
align-items: center;
.iconfont{
padding: 10rpx;
font-size: 28rpx;
}
}
.function-list {
margin-top: 20rpx;