微信小程序登录:手动登录完成

pull/32/MERGE
YunaiV 2023-12-23 17:53:07 +08:00
parent 1c218cd730
commit b23fca683b
9 changed files with 108 additions and 79 deletions

View File

@ -185,7 +185,7 @@
"versionCode": 100 "versionCode": 100
}, },
"mp-weixin": { "mp-weixin": {
"appid": "wx43051b2afa4ed3d0", "appid": "wx63c280fe3248a3e7",
"setting": { "setting": {
"urlCheck": false, "urlCheck": false,
"minified": true, "minified": true,

View File

@ -37,6 +37,38 @@ const AuthUtil = {
method: 'POST', 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 初始化所需的签名 // 创建微信 JS SDK 初始化所需的签名
createWeixinMpJsapiSignature: (url) => { createWeixinMpJsapiSignature: (url) => {
return request({ return request({

View File

@ -1,6 +1,18 @@
import request from '@/sheep/request2'; import request from '@/sheep/request2';
const UserApi = { const UserApi = {
// 修改基本信息
updateUser: (data) => {
return request({
url: '/app-api/member/user/update',
method: 'PUT',
data,
custom: {
showSuccess: true,
auth: true,
},
});
},
// 修改密码 // 修改密码
updateUserPassword: (data) => { updateUserPassword: (data) => {
return request({ return request({

View File

@ -30,25 +30,6 @@ export default {
auth: true, 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) => update: (data) =>
request2({ request2({
url: 'member/user/update', url: 'member/user/update',

View File

@ -18,6 +18,7 @@
labelWidth="140" labelWidth="140"
labelAlign="center" labelAlign="center"
> >
<!-- 获取头像昵称https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/userProfile.html -->
<uni-forms-item name="avatar" label="头像"> <uni-forms-item name="avatar" label="头像">
<button <button
class="ss-reset-button avatar-btn" class="ss-reset-button avatar-btn"
@ -29,8 +30,8 @@
:src="sheep.$url.cdn(state.model.avatar)" :src="sheep.$url.cdn(state.model.avatar)"
mode="aspectFill" mode="aspectFill"
@tap="sheep.$router.go('/pages/user/info')" @tap="sheep.$router.go('/pages/user/info')"
></image> />
<text class="cicon-forward"></text> <text class="cicon-forward" />
</button> </button>
</uni-forms-item> </uni-forms-item>
<uni-forms-item name="nickname" label="昵称"> <uni-forms-item name="nickname" label="昵称">
@ -39,10 +40,8 @@
placeholder="请输入昵称" placeholder="请输入昵称"
v-model="state.model.nickname" v-model="state.model.nickname"
:inputBorder="false" :inputBorder="false"
> />
</uni-easyinput>
</uni-forms-item> </uni-forms-item>
<view class="foot-box"> <view class="foot-box">
<button class="ss-reset-button authorization-btn" @tap="onConfirm"> </button> <button class="ss-reset-button authorization-btn" @tap="onConfirm"> </button>
</view> </view>
@ -51,9 +50,11 @@
</template> </template>
<script setup> <script setup>
import { computed, watch, ref, reactive, unref } from 'vue'; import { computed, ref, reactive } from 'vue';
import sheep from '@/sheep'; import sheep from '@/sheep';
import { showAuthModal, closeAuthModal } from '@/sheep/hooks/useModal'; import { closeAuthModal } from '@/sheep/hooks/useModal';
import FileApi from '@/sheep/api/infra/file';
import UserApi from '@/sheep/api/member/user';
const props = defineProps({ const props = defineProps({
agreeStatus: { agreeStatus: {
@ -72,23 +73,26 @@
nickname: userInfo.value.nickname, nickname: userInfo.value.nickname,
avatar: userInfo.value.avatar, avatar: userInfo.value.avatar,
}, },
rules: { rules: {},
},
disabledStyle: { disabledStyle: {
color: '#999', color: '#999',
disableColor: '#fff', disableColor: '#fff',
}, },
}); });
//
//
function onChooseAvatar(e) { function onChooseAvatar(e) {
const tempUrl = e.detail.avatarUrl || ''; const tempUrl = e.detail.avatarUrl || '';
uploadAvatar(tempUrl); uploadAvatar(tempUrl);
} }
//
async function uploadAvatar(tempUrl) { async function uploadAvatar(tempUrl) {
if (!tempUrl) return; if (!tempUrl) {
let { path } = await sheep.$api.app.upload(tempUrl, 'ugc'); return;
state.model.avatar = path; }
let { data } = await FileApi.uploadFile(tempUrl);
state.model.avatar = data;
} }
// //
@ -103,13 +107,16 @@
sheep.$helper.toast('请选择头像'); sheep.$helper.toast('请选择头像');
return; return;
} }
const { error, msg } = await sheep.$api.user.updateMpUserInfo(model); //
if (error === 0) { const { code } = await UserApi.updateUser({
avatar: state.model.avatar,
nickname: state.model.nickname,
});
//
if (code === 0) {
sheep.$helper.toast('授权成功'); sheep.$helper.toast('授权成功');
await sheep.$store('user').getInfo(); await sheep.$store('user').getInfo();
closeAuthModal(); closeAuthModal();
}else {
sheep.$helper.toast(msg);
} }
} }
</script> </script>

View File

@ -32,8 +32,9 @@
<!-- 立即注册&快捷登录 TextButton --> <!-- 立即注册&快捷登录 TextButton -->
<view v-if="sheep.$platform.name === 'WechatMiniProgram'" class="ss-flex register-box"> <view v-if="sheep.$platform.name === 'WechatMiniProgram'" class="ss-flex register-box">
<view class="register-title">还没有账号?</view> <view class="register-title">还没有账号?</view>
<button class="ss-reset-button register-btn" @tap="showAuthModal('smsRegister')"></button <button class="ss-reset-button register-btn" @tap="showAuthModal('smsRegister')">
> 立即注册
</button>
<view class="or-title"></view> <view class="or-title"></view>
<button class="ss-reset-button login-btn" @tap="thirdLogin('wechat')"></button> <button class="ss-reset-button login-btn" @tap="thirdLogin('wechat')"></button>
<view class="circle"></view> <view class="circle"></view>
@ -147,7 +148,7 @@
}, 1000); }, 1000);
} }
// // Apple
const thirdLogin = async (provider) => { const thirdLogin = async (provider) => {
if (!state.protocol) { if (!state.protocol) {
currentProtocol.value = true; currentProtocol.value = true;
@ -160,17 +161,9 @@
const loginRes = await sheep.$platform.useProvider(provider).login(); const loginRes = await sheep.$platform.useProvider(provider).login();
if (loginRes) { if (loginRes) {
closeAuthModal(); closeAuthModal();
const userInfo = await sheep.$store('user').getInfo();
// //
// #ifdef MP-WEIXIN // #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 // #endif
} }
}; };

View File

@ -1,6 +1,8 @@
import { isEmpty } from 'lodash';
import third from '@/sheep/api/third'; import third from '@/sheep/api/third';
import $store from '@/sheep/store'; import $store from '@/sheep/store';
import AuthUtil from '@/sheep/api/member/auth';
const socialType = 34; // 社交类型 - 微信小程序
let sessionId = uni.getStorageSync('sessionId'); let sessionId = uni.getStorageSync('sessionId');
let subscribeEventList = []; let subscribeEventList = [];
@ -17,30 +19,32 @@ function load() {
getSubscribeTemplate(); getSubscribeTemplate();
} }
// 微信小程序静默授权登陆 TODO-ldh: code > 0 问题 改为error // 微信小程序静默授权登陆
const login = async () => { const login = async () => {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
const { error } = await third.wechat.login({ // 1. 获得微信 code
platform: 'miniProgram', const codeResult = await uni.login();
shareInfo: uni.getStorageSync('shareLog') || {}, if (codeResult.errMsg !== 'login:ok') {
payload: encodeURIComponent(
JSON.stringify({
sessionId: uni.getStorageSync('sessionId'),
}),
),
});
if (error === 0) {
resolve(true);
}
if (error === -1) {
getSessionId(false);
}
resolve(false); resolve(false);
}
// 2. 社交登录
const loginResult = await loginByCode(codeResult.code);
if (loginResult.code === 0) {
setOpenid(loginResult.data.openid);
resolve(true);
} else {
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) => { const mobileLogin = async (e) => {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
@ -240,6 +244,11 @@ function subscribeMessage(event) {
}); });
} }
// 设置 openid 到本地存储,目前只有 pay 支付时会使用
function setOpenid(openid) {
uni.setStorageSync('openid', openid);
}
export default { export default {
load, load,
login, login,

View File

@ -1,6 +1,5 @@
import third from '@/sheep/api/third'; import third from '@/sheep/api/third';
import $wxsdk from '@/sheep/libs/sdk-h5-weixin'; import $wxsdk from '@/sheep/libs/sdk-h5-weixin';
import $store from '@/sheep/store';
import { getRootUrl } from '@/sheep/helper'; import { getRootUrl } from '@/sheep/helper';
import AuthUtil from '@/sheep/api/member/auth'; import AuthUtil from '@/sheep/api/member/auth';
@ -8,32 +7,24 @@ const socialType = 31; // 社交类型 - 微信公众号
// 加载微信公众号JSSDK // 加载微信公众号JSSDK
async function load() { async function load() {
// TODO 芋艿:自动登录的逻辑
if ($store('app').platform.auto_login
&& $store('user').isLogin
&& location.href.search('pages/index/login') === -1) {
// 发起自动登陆
login();
}
$wxsdk.init(); $wxsdk.init();
} }
// 微信公众号登陆 // 微信公众号登陆
async function login(code = '', state = '') { async function login(code = '', state = '') {
// 获取登陆地址 // 情况一:没有 code 时,去获取 code
if (!code) { if (!code) {
const loginUrl = await getLoginUrl(); const loginUrl = await getLoginUrl();
if (loginUrl) { if (loginUrl) {
uni.setStorageSync('returnUrl', location.href); uni.setStorageSync('returnUrl', location.href);
window.location = loginUrl; window.location = loginUrl;
} }
// 情况二:有 code 时,使用 code 去自动登录
} else { } else {
// 解密 code 发起登陆 // 解密 code 发起登陆
const loginResult = await loginByCode(code, state); const loginResult = await loginByCode(code, state);
if (loginResult.code === 0) { if (loginResult.code === 0) {
if (loginResult.data.openid) {
setOpenid(loginResult.data.openid); setOpenid(loginResult.data.openid);
}
return loginResult; return loginResult;
} }
} }

View File

@ -114,6 +114,10 @@ http.interceptors.response.use(
$store('user').setToken(response.header.authorization || response.header.Authorization); $store('user').setToken(response.header.authorization || response.header.Authorization);
} }
// TODO 芋艿:如果是登录的 API则自动设置 token // TODO 芋艿:如果是登录的 API则自动设置 token
if (response.data?.data?.accessToken) {
debugger
$store('user').setToken(response.data.data.accessToken);
}
response.config.custom.showLoading && closeLoading(); response.config.custom.showLoading && closeLoading();
if (response.data.error !== 0 && response.data.code !== 0) { if (response.data.error !== 0 && response.data.code !== 0) {