Commit 994fbba4 authored by zcc's avatar zcc

feat: 修改手机授权逻辑

parent 9aa6fb22
...@@ -51,9 +51,6 @@ const request = (options = {}) => { ...@@ -51,9 +51,6 @@ const request = (options = {}) => {
} else { } else {
resolve(data.data); resolve(data.data);
} }
// const [err, res] = data;
// TODO uni.showToast errMsg
}) })
.catch((error) => { .catch((error) => {
reject(error); reject(error);
......
import requestModule from './request.js'; import requestModule from './request.js';
const { api } = requestModule; const {
api
} = requestModule;
/** /**
* 获取用户信息 * 获取用户信息
* @param {*} cuk
* @returns * @returns
*/ */
export const fetchUserInfo = (cuk) => export const fetchUserInfo = () => api.get('/c/user/memberInfo');
api.get('/c/user/memberInfo', {
cuk,
});
/** /**
* 获取宝宝信息 * 获取宝宝信息
* @param {*} cuk
* @returns * @returns
*/ */
export const fetchBabyInfo = (cuk) => export const fetchBabyInfo = () => api.get('/c/user/babyInfo');
api.get('/c/user/babyInfo', {
cuk,
});
/** /**
* 根据wx.login接口返回的code完成登录 * 根据wx.login接口返回的code完成登录
* @param {*} code * @param {*} code
* @returns * @returns
*/ */
export const autoLoginByCode = (code) => export const autoLoginByCode = (code) => api.get('/c/login/autologin', {
api.get('/c/login/autologin', { code,
code, });
});
/** /**
* 手机号授权,调用微信手机号快速验证组件,获取encryptedData、iv、code * 手机号授权,调用微信手机号快速验证组件,获取encryptedData、iv、code
* 通过此接口完成手机号授权,注册新用户 * 通过此接口完成手机号授权,注册新用户
* @param {*} data : {phoneEncryptedData, phoneIv, code} * @param {*} data : {phoneEncryptedData, phoneIv, code, codeLogin}
* @returns * @returns
*/ */
export const fetchAutoPhone = (data) => api.post('/c/login/authPhone', data); export const fetchAutoPhone = (data) => api.post('/c/login/authPhone', data);
\ No newline at end of file
...@@ -7,7 +7,7 @@ const cuk = uni.getStorageSync('cuk') ...@@ -7,7 +7,7 @@ const cuk = uni.getStorageSync('cuk')
export const useGlobalStore = defineStore('global', { export const useGlobalStore = defineStore('global', {
state: () => { state: () => {
return { return {
cuk: cuk, // 用户登录后获取的凭证,获取用户、宝宝信息接口时使用 cuk: cuk, // 用户登录后获取的凭证,调用接口时headers中携带
}; };
}, },
actions: { actions: {
......
...@@ -9,7 +9,8 @@ import { ...@@ -9,7 +9,8 @@ import {
} from '../api/user.js'; } from '../api/user.js';
import { import {
useGlobalStore useGlobalStore
} from './global.js'; } from './global.js';
const globalStore = useGlobalStore(); const globalStore = useGlobalStore();
export const useUserStore = defineStore('userInfo', { export const useUserStore = defineStore('userInfo', {
...@@ -42,14 +43,29 @@ export const useUserStore = defineStore('userInfo', { ...@@ -42,14 +43,29 @@ export const useUserStore = defineStore('userInfo', {
* @returns * @returns
*/ */
async phoneCallback(data) { async phoneCallback(data) {
// 用户手机授权 uni.login({
await fetchAutoPhone({ provider: 'weixin',
phoneEncryptedData: data.encryptedData, success: async (res) => {
phoneIv: data.iv, // console.log('wxAutoLogin', res);
code: data.code, if (res.errMsg === 'login:ok') {
// 用户手机授权
await fetchAutoPhone({
phoneEncryptedData: data.encryptedData,
phoneIv: data.iv,
code: data.code,
codeLogin: res.code
});
// 授权注册成功后做一次登录
this.wxAutoLogin();
} else {
uni.showToast({
title: res.errMsg,
icon: 'error'
});
}
},
}); });
// 授权注册成功后做一次登录
this.wxAutoLogin();
}, },
/** /**
...@@ -106,6 +122,11 @@ export const useUserStore = defineStore('userInfo', { ...@@ -106,6 +122,11 @@ export const useUserStore = defineStore('userInfo', {
console.log('wxAutoLogin', res); console.log('wxAutoLogin', res);
if (res.errMsg === 'login:ok') { if (res.errMsg === 'login:ok') {
this.autoLoginByCode(res.code); this.autoLoginByCode(res.code);
} else {
uni.showToast({
title: res.errMsg,
icon: 'error'
});
} }
}, },
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment