feat(auth): 我的添加openid获取功能,并优化登录页
This commit is contained in:
parent
c5012e3e9a
commit
c1dbf6b002
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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('通过code获取openid和accessToken', 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") {
|
||||
|
|
|
@ -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)
|
||||
// // 清除URL中的code参数
|
||||
// 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()) {
|
||||
// 截取地址中的code,如果没有code就去微信授权,如果已经获取到code了就直接把code传给后台获取openId
|
||||
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(){
|
||||
// 清除URL中的code参数
|
||||
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;
|
||||
|
|
Loading…
Reference in New Issue