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

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
},
"mp-weixin": {
"appid": "wx43051b2afa4ed3d0",
"appid": "wx63c280fe3248a3e7",
"setting": {
"urlCheck": false,
"minified": true,

View File

@ -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({

View File

@ -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({

View File

@ -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',

View File

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

View File

@ -32,8 +32,9 @@
<!-- 立即注册&快捷登录 TextButton -->
<view v-if="sheep.$platform.name === 'WechatMiniProgram'" class="ss-flex register-box">
<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>
<button class="ss-reset-button login-btn" @tap="thirdLogin('wechat')"></button>
<view class="circle"></view>
@ -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
}
};

View File

@ -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,

View File

@ -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;
}
}

View File

@ -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) {