/* 登录授权方式1,前端传值给后台获得oppenId */
weChatPreLogin(data) {
uni.login({
provider: 'weixin',
success: loginRes => {
// console.log('-------用户授权,并获取用户code------');
console.log(loginRes.code);
console.log(loginRes);
// 获取用户信息
uni.getUserInfo({
provider: 'weixin',
success: userRes => {
// console.log('-------用户授权,并获取用户基本信息和微信授权需要的参数');
console.log(userRes);
iv = userRes.iv;
encrypted_data = userRes.encryptedData;
uni.showLoading({
title: '授权登录中...'
});
//获取openId
wx.request({
url: 'https://moly.wojiapang.cn/sns/jscode2session',
data: {
//小程序唯一标识
appid: this.$http.appid,
//小程序的 app secret
secret: this.$http.secret,
grant_type: 'authorization_code',
js_code: loginRes.code
},
method: 'GET',
header: { 'content-type': 'application/json' },
success: openIdRes=> {
console.info('登录成功返回的openId:' + openIdRes.data.openid);
// 判断openId是否获取成功
if ((openIdRes.data.openid != null) & (openIdRes.data.openid != undefined)) {
this.$http
.weChatAuthor2(openIdRes.data.openid, iv, encrypted_data, this.urlInviteCodeId, data.detail.rawData, data.detail.signature)
.then(ajaxRes => {
uni.hideLoading();
// console.log(ajaxRes);
// 登录成功 返回的用户信息存在vuex中 示例如下,只需要修改userinfo的参数即可
this.$store.commit('USER_INFO', ajaxRes.data.user);
// 登录成功 存储TOKEN 同时删掉预登陆id
uni.setStorageSync('user_token', ajaxRes.data.token);
this.$store.commit('USER_TOKEN', ajaxRes.data.token);
uni.removeStorageSync('preLoginUserId');
uni.setStorageSync('myUsername', ajaxRes.data.user.hxId);
// 登录成功后 判断当前用户是否是新用户,(判断是否有完善资料,如果已经完善了 直接进首页,如果没有走完善资料流程)
// 判断完善资料的条件是 是否设置过性别和学历 年龄
if (ajaxRes.data.user.firstFlag == 0) {
// 老用户
uni.showToast({
title: '登陆成功'
});
setTimeout(() => {
uni.reLaunch({
url: '/pages/home/home'
});
}, 1500);
} else if (ajaxRes.data.user.firstFlag == 1) {
// 新用户
uni.navigateTo({
url: '/pages/login/myGender/myGender'
});
}
})
.catch(err => {
console.log(err);
uni.hideLoading();
});
} else {
console.info('获取用户openId失败');
uni.showToast({
icon: "none",
title: "用户信息解析失败,请重试"
})
}
},
fail: error=> {
console.info(error);
uni.showToast({
icon: "none",
title: "用户信息解析失败,请重试"
})
}
});
},
fail: () => {
uni.showToast({
icon: 'none',
title: '网络错误,请重试'
});
}
});
}
});
},
/* 登录授权方式2,前端直接获得oppenId,因小程序添加域名机制,不能直接调用微信接口。所以还需要调用后台接口完成二次调用微信接口 */
weChatPreLogin() {
uni.login({
provider: 'weixin',
success: loginRes => {
console.log(loginRes);
//获取openId
wx.request({
url: 'https://moly.wojiapang.cn/sns/jscode2session',
data: {
//小程序唯一标识
appid: this.$http.appid,
//小程序的 app secret
secret: this.$http.secret,
grant_type: 'authorization_code',
js_code: loginRes.code
},
method: 'GET',
header: { 'content-type': 'application/json' },
success: openIdRes=> {
console.info('登录成功返回的openId:' + openIdRes.data.openid);
// 判断openId是否获取成功
if ((openIdRes.data.openid != null) & (openIdRes.data.openid != undefined)) {
this.$http.weChatPreLogin2(openIdRes.data.openid).then(res => {
this.preUserId = res.data;
uni.setStorageSync('preLoginUserId', res.data);
this.getHomeList(res.data);
});
} else {
console.info('获取用户openId失败');
uni.showToast({
icon: "none",
title: "用户信息解析失败,请重试"
})
}
},
fail: error=> {
console.info(error);
uni.showToast({
icon: "none",
title: "用户信息解析失败,请重试"
})
}
});
},
fail: err => {
console.log(err);
}
});
},
前端 uniapp微信登录授权
最新推荐文章于 2024-06-13 16:17:49 发布