Commit 44727118 authored by 黄韬's avatar 黄韬

Merge branch 'feature/20250604-login' into 'dev'

feat: 免登&手机授权

See merge request sparkprojects/20250528_FHQ1!6
parents ddae6571 064671a6
// request.js
// 通常可以吧 baseUrl 单独放在一个 js 文件了
const baseUrl = "http://xxx.xxx.4.xxxx:8093/chemApp";
const request = (options = {}) => {
// 在这里可以对请求头进行一些设置
// 例如:
// options.header = {
// "Content-Type": "application/x-www-form-urlencoded"
// }
return new Promise((resolve, reject) => {
uni
.request({
url: baseUrl + options.url || "",
method: options.type || "GET",
data: options.data || {},
header: options.header || {},
})
.then((data) => {
let [err, res] = data;
resolve(res);
})
.catch((error) => {
reject(error);
});
});
};
const get = (url, data, options = {}) => {
options.type = "GET";
options.data = data;
options.url = url;
return request(options);
};
const post = (url, data, options = {}) => {
options.type = "POST";
options.data = data;
options.url = url;
return request(options);
};
export default {
request,
api: {
get,
post,
},
};
// request.js
// 通常可以吧 baseUrl 单独放在一个 js 文件了
const baseUrl = "https://docs.dui88.com/mock/1956";
const request = (options = {}) => {
// 在这里可以对请求头进行一些设置
// 例如:
// options.header = {
// "Content-Type": "application/x-www-form-urlencoded"
// }
return new Promise((resolve, reject) => {
uni
.request({
url: baseUrl + options.url || "",
method: options.type || "GET",
data: options.data || {},
header: options.header || {},
})
.then((data) => {
// console.log('request data ===>', data);
// const [err, res] = data;
// TODO uni.showToast errMsg
resolve(data.data);
})
.catch((error) => {
reject(error);
});
});
};
const get = (url, data, options = {}) => {
options.type = "GET";
options.data = data;
options.url = url;
return request(options);
};
const post = (url, data, options = {}) => {
options.type = "POST";
options.data = data;
options.url = url;
return request(options);
};
export default {
request,
api: {
get,
post
}
};
\ No newline at end of file
import requestModule from './request.js';
const { api } = requestModule;
/**
* 获取用户信息
* @param {*} cuk
* @returns
*/
export const fetchUserInfo = (cuk) =>
api.get('/c/user/memberInfo', {
cuk,
});
/**
* 获取宝宝信息
* @param {*} cuk
* @returns
*/
export const fetchBabyInfo = (cuk) =>
api.get('/c/user/babyInfo', {
cuk,
});
/**
* 根据wx.login接口返回的code完成登录
* @param {*} code
* @returns
*/
export const autoLoginByCode = (code) =>
api.get('/c/login/autologin', {
code,
});
/**
* 手机号授权,调用微信手机号快速验证组件,获取encryptedData、iv、code
* 通过此接口完成手机号授权,注册新用户
* @param {*} data : {phoneEncryptedData, phoneIv, code}
* @returns
*/
export const fetchAutoPhone = (data) => api.get('/c/login/authPhone', data);
{
"name" : "20250528_FHQ1",
"appid" : "",
"appid" : "__UNI__CE986B7",
"description" : "",
"versionName" : "1.0.0",
"versionCode" : "100",
......@@ -50,7 +50,7 @@
"quickapp" : {},
/* 小程序特有相关 */
"mp-weixin" : {
"appid" : "",
"appid" : "wxc83b55d61c7fc51d",
"setting" : {
"urlCheck" : false
},
......
import {
defineStore
} from 'pinia';
import {
autoLoginByCode,
fetchUserInfo,
fetchBabyInfo,
fetchAutoPhone
} from '../api/user.js';
export const useUserStore = defineStore('userInfo', {
state: () => {
return {
cuk: null, // 用户登录后获取的凭证,获取用户、宝宝信息接口时使用
userInfo: null,
babyInfo: null,
};
},
actions: {
/**
* 更新用户信息
* @param {Object} userInfo
*/
setUserInfo(userInfo) {
this.userInfo = userInfo;
},
/**
* 更新宝宝信息
* @param {Object} babyInfo
*/
setBabyInfo(babyInfo) {
this.babyInfo = babyInfo;
},
/**
* 用户手机号验证的回调方法,用于获取encryptedData、iv、code,然后调用fetchAutoPhone接口完成手机号授权
* @param {Object} data : {encryptedData, iv, code}
* @returns
*/
async phoneCallback(data) {
// 用户手机授权
await fetchAutoPhone({
phoneEncryptedData: data.encryptedData,
phoneIv: data.iv,
code: data.code,
});
// 授权注册成功后做一次登录
this.wxAutoLogin();
},
/**
* 获取用户信息
*/
async loadUserInfo() {
if (!this.cuk) {
return;
}
const {
data
} = await fetchUserInfo(this.cuk);
console.log('userInfo', data);
this.userInfo = data;
},
/**
* 获取宝宝信息
*/
async loadBabyInfo() {
if (!this.cuk) {
return;
}
const {
data
} = await fetchBabyInfo(this.cuk);
console.log('babyInfo', data);
this.babyInfo = data;
},
/**
* wx.login 获取code后,调用此方法完成登录
* @param {String} code
*/
async autoLoginByCode(code) {
const {
data
} = await autoLoginByCode(code);
console.log('autoLoginByCode', data);
// 如果登录成功,获取用户信息和宝宝信息,更新到state中,方便全局使用
if (data && data.cuk) {
this.cuk = data.cuk;
this.loadUserInfo();
this.loadBabyInfo();
}
},
/**
* 用户自动登录
*/
async wxAutoLogin() {
uni.login({
provider: 'weixin',
success: (res) => {
console.log('wxAutoLogin', res);
if (res.errMsg === 'login:ok') {
this.autoLoginByCode(res.code);
}
},
});
},
},
});
\ No newline at end of file
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