diff --git a/src/api/index.ts b/src/api/index.ts index 0e466a5..ccf3ca3 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -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); +}; + diff --git a/src/pages/login/indexMini.vue b/src/pages/login/indexMini.vue index 1de62d0..8ba0623 100644 --- a/src/pages/login/indexMini.vue +++ b/src/pages/login/indexMini.vue @@ -5,7 +5,18 @@ - 立即进入 + + + + @@ -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 {}; });