From b23fca683b758b5b4a0190a233262ea7aecbc1ae Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 23 Dec 2023 17:53:07 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E5=BE=AE=E4=BF=A1=E5=B0=8F?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E7=99=BB=E5=BD=95=EF=BC=9A=E6=89=8B=E5=8A=A8?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manifest.json | 2 +- sheep/api/member/auth.js | 32 ++++++++++++++ sheep/api/member/user.js | 12 ++++++ sheep/api/user.js | 19 -------- .../components/mp-authorization.vue | 43 +++++++++++-------- .../components/s-auth-modal/s-auth-modal.vue | 17 +++----- sheep/platform/provider/wechat/miniProgram.js | 43 +++++++++++-------- .../provider/wechat/officialAccount.js | 15 ++----- sheep/request/index.js | 4 ++ 9 files changed, 108 insertions(+), 79 deletions(-) diff --git a/manifest.json b/manifest.json index 9f8a8007..5854a4c9 100644 --- a/manifest.json +++ b/manifest.json @@ -185,7 +185,7 @@ "versionCode": 100 }, "mp-weixin": { - "appid": "wx43051b2afa4ed3d0", + "appid": "wx63c280fe3248a3e7", "setting": { "urlCheck": false, "minified": true, diff --git a/sheep/api/member/auth.js b/sheep/api/member/auth.js index 1cb96b4b..776eed48 100644 --- a/sheep/api/member/auth.js +++ b/sheep/api/member/auth.js @@ -37,6 +37,38 @@ const AuthUtil = { method: 'POST', }); }, + // 社交授权的跳转 + socialAuthRedirect: (type, redirectUri) => { + return request({ + url: '/app-api/member/auth/social-auth-redirect', + method: 'GET', + params: { + type, + redirectUri, + }, + custom: { + showSuccess: true, + loadingMsg: '登陆中', + }, + }); + }, + // 社交快捷登录 + socialLogin: (type, code, state) => { + return request({ + url: '/app-api/member/auth/social-login', + method: 'POST', + data: { + type, + code, + state, + }, + custom: { + showSuccess: true, + loadingMsg: '登陆中', + // TODO 芋艿:登录成功??? + }, + }); + }, // 创建微信 JS SDK 初始化所需的签名 createWeixinMpJsapiSignature: (url) => { return request({ diff --git a/sheep/api/member/user.js b/sheep/api/member/user.js index 38ed1103..fef3d230 100644 --- a/sheep/api/member/user.js +++ b/sheep/api/member/user.js @@ -1,6 +1,18 @@ import request from '@/sheep/request2'; const UserApi = { + // 修改基本信息 + updateUser: (data) => { + return request({ + url: '/app-api/member/user/update', + method: 'PUT', + data, + custom: { + showSuccess: true, + auth: true, + }, + }); + }, // 修改密码 updateUserPassword: (data) => { return request({ diff --git a/sheep/api/user.js b/sheep/api/user.js index 9c2e862b..a02bfdd7 100644 --- a/sheep/api/user.js +++ b/sheep/api/user.js @@ -30,25 +30,6 @@ export default { auth: true, }, }), - // profile: () => - // request({ - // url: '/user/api/user/profile', - // method: 'GET', - // custom: { - // showLoading: false, - // auth: true, - // }, - // }), - // update: (data) => - // request({ - // url: '/user/api/user/update', - // method: 'POST', - // custom: { - // showSuccess: true, - // auth: true, - // }, - // data, - // }), update: (data) => request2({ url: 'member/user/update', diff --git a/sheep/components/s-auth-modal/components/mp-authorization.vue b/sheep/components/s-auth-modal/components/mp-authorization.vue index 7c35144d..df5867f6 100644 --- a/sheep/components/s-auth-modal/components/mp-authorization.vue +++ b/sheep/components/s-auth-modal/components/mp-authorization.vue @@ -18,6 +18,7 @@ labelWidth="140" labelAlign="center" > + @@ -39,10 +40,8 @@ placeholder="请输入昵称" v-model="state.model.nickname" :inputBorder="false" - > - + /> - @@ -51,9 +50,11 @@ diff --git a/sheep/components/s-auth-modal/s-auth-modal.vue b/sheep/components/s-auth-modal/s-auth-modal.vue index df51cbd9..b2bb2392 100644 --- a/sheep/components/s-auth-modal/s-auth-modal.vue +++ b/sheep/components/s-auth-modal/s-auth-modal.vue @@ -32,8 +32,9 @@ 还没有账号? - + @@ -147,7 +148,7 @@ }, 1000); } - // 第三方授权登陆 + // 第三方授权登陆(微信小程序、Apple) const thirdLogin = async (provider) => { if (!state.protocol) { currentProtocol.value = true; @@ -160,17 +161,9 @@ const loginRes = await sheep.$platform.useProvider(provider).login(); if (loginRes) { closeAuthModal(); - const userInfo = await sheep.$store('user').getInfo(); // 触发小程序授权信息弹框 // #ifdef MP-WEIXIN - if (userInfo.third_oauth.length > 0) { - const mpThirdOauthInfo = userInfo.third_oauth.find( - (item) => item.platform === 'miniProgram', - ); - if (mpThirdOauthInfo && !mpThirdOauthInfo.nickname) { - showAuthModal('mpAuthorization'); - } - } + showAuthModal('mpAuthorization'); // #endif } }; diff --git a/sheep/platform/provider/wechat/miniProgram.js b/sheep/platform/provider/wechat/miniProgram.js index 60341752..802f3a68 100644 --- a/sheep/platform/provider/wechat/miniProgram.js +++ b/sheep/platform/provider/wechat/miniProgram.js @@ -1,6 +1,8 @@ -import { isEmpty } from 'lodash'; import third from '@/sheep/api/third'; import $store from '@/sheep/store'; +import AuthUtil from '@/sheep/api/member/auth'; + +const socialType = 34; // 社交类型 - 微信小程序 let sessionId = uni.getStorageSync('sessionId'); let subscribeEventList = []; @@ -17,30 +19,32 @@ function load() { getSubscribeTemplate(); } -// 微信小程序静默授权登陆 TODO-ldh: code > 0 问题 改为error +// 微信小程序静默授权登陆 const login = async () => { return new Promise(async (resolve, reject) => { - const { error } = await third.wechat.login({ - platform: 'miniProgram', - shareInfo: uni.getStorageSync('shareLog') || {}, - payload: encodeURIComponent( - JSON.stringify({ - sessionId: uni.getStorageSync('sessionId'), - }), - ), - }); + // 1. 获得微信 code + const codeResult = await uni.login(); + if (codeResult.errMsg !== 'login:ok') { + resolve(false); + } - if (error === 0) { + // 2. 社交登录 + const loginResult = await loginByCode(codeResult.code); + if (loginResult.code === 0) { + setOpenid(loginResult.data.openid); resolve(true); + } else { + resolve(false); } - - if (error === -1) { - getSessionId(false); - } - resolve(false); + return loginResult.code === 0 ? resolve(true) : resolve(false); }); }; +function loginByCode(code) { + return AuthUtil.socialLogin(socialType, code, 'default'); + // TODO 芋艿:shareLog +} + // 微信小程序手机号授权登陆 const mobileLogin = async (e) => { return new Promise(async (resolve, reject) => { @@ -240,6 +244,11 @@ function subscribeMessage(event) { }); } +// 设置 openid 到本地存储,目前只有 pay 支付时会使用 +function setOpenid(openid) { + uni.setStorageSync('openid', openid); +} + export default { load, login, diff --git a/sheep/platform/provider/wechat/officialAccount.js b/sheep/platform/provider/wechat/officialAccount.js index e36280af..31963142 100644 --- a/sheep/platform/provider/wechat/officialAccount.js +++ b/sheep/platform/provider/wechat/officialAccount.js @@ -1,6 +1,5 @@ import third from '@/sheep/api/third'; import $wxsdk from '@/sheep/libs/sdk-h5-weixin'; -import $store from '@/sheep/store'; import { getRootUrl } from '@/sheep/helper'; import AuthUtil from '@/sheep/api/member/auth'; @@ -8,32 +7,24 @@ const socialType = 31; // 社交类型 - 微信公众号 // 加载微信公众号JSSDK async function load() { - // TODO 芋艿:自动登录的逻辑 - if ($store('app').platform.auto_login - && $store('user').isLogin - && location.href.search('pages/index/login') === -1) { - // 发起自动登陆 - login(); - } $wxsdk.init(); } // 微信公众号登陆 async function login(code = '', state = '') { - // 获取登陆地址 + // 情况一:没有 code 时,去获取 code if (!code) { const loginUrl = await getLoginUrl(); if (loginUrl) { uni.setStorageSync('returnUrl', location.href); window.location = loginUrl; } + // 情况二:有 code 时,使用 code 去自动登录 } else { // 解密 code 发起登陆 const loginResult = await loginByCode(code, state); if (loginResult.code === 0) { - if (loginResult.data.openid) { - setOpenid(loginResult.data.openid); - } + setOpenid(loginResult.data.openid); return loginResult; } } diff --git a/sheep/request/index.js b/sheep/request/index.js index 7ec6d3cd..dc4ee9b3 100644 --- a/sheep/request/index.js +++ b/sheep/request/index.js @@ -114,6 +114,10 @@ http.interceptors.response.use( $store('user').setToken(response.header.authorization || response.header.Authorization); } // TODO 芋艿:如果是登录的 API,则自动设置 token + if (response.data?.data?.accessToken) { + debugger + $store('user').setToken(response.data.data.accessToken); + } response.config.custom.showLoading && closeLoading(); if (response.data.error !== 0 && response.data.code !== 0) {