feat(login): 添加微信授权登录功能

- 新增微信授权API接口
- 替换立即进入按钮为微信获取手机号按钮
- 实现微信授权登录流程,包括获取code和用户信息
- 处理授权成功后的token和用户信息存储
This commit is contained in:
zzp 2025-08-17 17:26:24 +08:00
parent 13e67b8aa9
commit b303828164
2 changed files with 68 additions and 1 deletions

View File

@ -62,3 +62,8 @@ export const searchNews = (data: any) => {
return Request.post("/news/search", data);
};
// 微信授权
export const doWxAuth = (data: any) => {
return Request.post("/common/auth", data);
};

View File

@ -5,7 +5,18 @@
<!-- logo -->
<view class="logo"></view>
<view class="bottom_btn" @click="goIndex"> 立即进入 </view>
<!-- <view class="bottom_btn" @click="goIndex"> 立即进入 </view> -->
<button
open-type="getPhoneNumber"
@getphonenumber="getPhoneNumber"
class="bottom_btn"
>
<view>
<text>立即进入</text>
</view>
</button>
<!-- 用户协议 -->
<view class="tips">
<u-checkbox class="checkbox" shape="circle"></u-checkbox>
@ -28,6 +39,7 @@ import {
onShareTimeline,
onUnload,
} from "@dcloudio/uni-app";
import { doWxAuth } from "@/api/index";
import { onPullDownRefresh, onReachBottom } from "@dcloudio/uni-app";
import { useShareStore } from "@/stores/shareStore";
@ -52,6 +64,56 @@ function goIndex() {
});
}
//
function getPhoneNumber(phoneObj) {
uni.showLoading();
getWxCode(phoneObj.detail);
}
const codeWx = ref();
function getWxCode(phoneObj) {
console.log("🚀 ~ getWxCode ~ phoneObj:", phoneObj);
uni.login({
success: (res) => {
console.log("🚀 ~ getWxCode ~ res:", res);
if (res.code) {
// code
codeWx.value = res.code;
doWxAuthFn(phoneObj, res.code);
} else {
console.log("登录失败!" + res.errMsg);
}
},
});
}
/**
* 微信授权
* @param userInfo
*/
async function doWxAuthFn(phoneObj, jsCode) {
let { code, data } = await doWxAuth({
jsCode: jsCode,
encryptedData: phoneObj.encryptedData,
siv: phoneObj.iv,
});
uni.hideLoading();
if (code == 200) {
uni.setStorageSync("token", data.token);
uni.setStorageSync("userId", data.userId);
// #ifdef MP-WEIXIN
uni.setStorageSync("unionId", data.unionId);
// #endif
// #ifdef MP-ALIPAY
uni.setStorageSync("openId", data.openId);
// #endif
emit("authFinish", true);
} else {
emit("authFinish", false);
}
}
onShareAppMessage((res) => {
return {};
});