uniapp微信小程式呼叫微信登入
login(context) {
uni.getUserProfile({
desc: '註冊',
success: (res) => {
console.log(res.userInfo);
let userInfo = res.userInfo;
uni.login({
provider: 'wexin',
success: async (res2) => {
uni.showLoading({
title: '登入中...'
})
// 獲取code
var code = res2.code
console.log(JSON.stringify(res2));
await uni.request({
url: 'http://192.168.0.181:8099/app/login/wechat', // 測試版本
// url: 'https://ktsh.cqylphj.com/app/login/wechat', // 線上版本
method: 'POST',
header: {
"content-type": "application/json",
"source": "fromApp"
},
data: {
"code": code,
"appUserName": userInfo.nickName,
"sex": userInfo.gender,
"headImgUrl": userInfo.avatarUrl
},
success: (res3) => {
console.log(res3);
if(res3.data.data){
context.commit('OPENID', res3.data.data);
context.commit('AVATAR', userInfo.avatarUrl);
context.commit('NICKNAME', userInfo.nickName);
uni.$emit('loginComplete');
}
uni.hideLoading();
},
fail: (err) => {
uni.hideLoading();
uni.showToast({
title: '登入失敗',
icon: 'none'
})
}
})
}
})
}
})
},