292 lines
6.6 KiB
JavaScript
Raw Normal View History

2026-03-30 18:08:27 +08:00
const { API } = require('./constants');
const { request } = require('./http');
2026-03-31 18:09:37 +08:00
function pickString(obj, key) {
return obj && typeof obj[key] === 'string' ? obj[key] : '';
2026-03-30 18:08:27 +08:00
}
2026-04-03 17:57:08 +08:00
let refreshPromise = null;
async function refreshAccessToken() {
if (refreshPromise) return refreshPromise;
const app = getApp();
const storedRefreshToken = wx.getStorageSync('refreshToken') || '';
const globalRefreshToken = app && app.globalData ? app.globalData.refreshToken : '';
const refreshToken = typeof globalRefreshToken === 'string' && globalRefreshToken.length > 0 ? globalRefreshToken : storedRefreshToken;
if (!refreshToken) throw new Error('refreshAccessToken: missing refreshToken');
refreshPromise = (async () => {
const tokenData = await request({
path: API.oauthRefreshToken,
method: 'POST',
needAuth: false,
data: { refreshToken },
contentType: 'application/json'
});
const accessToken = pickString(tokenData, 'accessToken');
const nextRefreshToken = pickString(tokenData, 'refreshToken') || refreshToken;
if (!accessToken) throw new Error('refreshAccessToken: missing accessToken');
app.setAuth({
accessToken,
refreshToken: nextRefreshToken
});
return {
accessToken,
refreshToken: nextRefreshToken,
tokenData
};
})().finally(() => {
refreshPromise = null;
});
return refreshPromise;
}
2026-03-31 18:09:37 +08:00
async function loginWithMobilePassword({ mobile, password }) {
2026-03-30 18:08:27 +08:00
const loginData = await request({
path: API.wechatLogin,
method: 'POST',
needAuth: false,
data: {
2026-03-31 18:09:37 +08:00
mobile,
password: password != null ? password : ''
2026-03-30 18:08:27 +08:00
},
contentType: 'application/json'
});
const app = getApp();
2026-03-31 18:09:37 +08:00
const accessToken = pickString(loginData, 'accessToken');
const refreshToken = pickString(loginData, 'refreshToken');
const userInfo =
loginData && loginData.userInfo && typeof loginData.userInfo === 'object'
? loginData.userInfo
: null;
const openid = userInfo ? pickString(userInfo, 'openid') : '';
2026-03-30 18:08:27 +08:00
app.setAuth({
accessToken,
2026-03-31 18:09:37 +08:00
refreshToken,
openid,
userProfile: userInfo
2026-03-30 18:08:27 +08:00
});
let myProfile = null;
try {
myProfile = await request({
path: API.myProfile,
method: 'GET',
needAuth: true
});
} catch (e) {
myProfile = null;
}
if (myProfile && typeof myProfile === 'object') {
app.setAuth({
userProfile: myProfile
});
}
return {
loginData,
myProfile
};
}
2026-03-31 23:27:35 +08:00
async function loginWithMobileCode({ mobile, verificationCode }) {
const loginData = await request({
path: API.loginByCode,
method: 'POST',
needAuth: false,
data: {
mobile,
verificationCode
},
contentType: 'application/json'
});
const app = getApp();
const accessToken = pickString(loginData, 'accessToken');
const refreshToken = pickString(loginData, 'refreshToken');
const userInfo =
loginData && loginData.userInfo && typeof loginData.userInfo === 'object'
? loginData.userInfo
: null;
const openid = userInfo ? pickString(userInfo, 'openid') : '';
app.setAuth({
accessToken,
refreshToken,
openid,
userProfile: userInfo
});
let myProfile = null;
try {
myProfile = await request({
path: API.myProfile,
method: 'GET',
needAuth: true
});
} catch (e) {
myProfile = null;
}
if (myProfile && typeof myProfile === 'object') {
app.setAuth({
userProfile: myProfile
});
}
return {
loginData,
myProfile
};
}
2026-03-31 18:09:37 +08:00
async function sendVerificationCode(phone) {
return request({
path: API.sendVerificationCode,
method: 'POST',
needAuth: false,
data: { phone },
contentType: 'application/json'
});
}
async function registerWithVerification({ mobile, password, verificationCode, nickName, avatar }) {
const data = {
mobile,
password,
verificationCode
};
if (nickName) data.nickName = nickName;
if (avatar) data.avatar = avatar;
return request({
path: API.oauthRegister,
method: 'POST',
needAuth: false,
data,
contentType: 'application/json'
});
}
async function resetPassword({ mobile, password, verificationCode }) {
return request({
path: API.oauthResetPassword,
method: 'POST',
needAuth: false,
2026-04-03 17:57:08 +08:00
data: { mobile, newPassword: password, verificationCode },
contentType: 'application/json'
});
}
2026-03-30 18:08:27 +08:00
async function fetchMyProfile() {
return request({
path: API.myProfile,
method: 'GET',
needAuth: true
});
}
async function fetchMyAuth() {
return request({
path: API.myAuth,
method: 'GET',
needAuth: true
});
}
async function submitMyAuth(payload) {
// 兼容旧调用方式:submitMyAuth({ name, mobile, idCardNumber, idCardImageUrl, idCardBackImageUrl })
// 新页面会直接把表单对象透传进来(如 authType、passport 字段等),后端若不识别会忽略。
const p = payload && typeof payload === 'object' ? payload : {};
2026-03-30 18:08:27 +08:00
return request({
path: API.myAuth,
method: 'POST',
needAuth: true,
data: p,
2026-03-30 18:08:27 +08:00
contentType: 'application/json'
});
}
async function fetchMyRegistrations({ pageNo = 1, pageSize = 10 } = {}) {
return request({
path: API.myRegistrations,
method: 'GET',
needAuth: true,
data: {
PageNo: pageNo,
PageSize: pageSize
}
});
}
2026-04-03 17:57:08 +08:00
async function fetchMyPoints({
pageNo = 1,
pageSize = 10,
// 新接口参数(按 swagger):1=获取,2=消耗,不传查全部
type,
// 单位认证ID筛选,不传查全部
merchantCertificationId,
// 兼容旧调用方:也可能传 Type / MerchantCertificationId
Type,
MerchantCertificationId
} = {}) {
const t = type != null ? type : Type;
const mcId = merchantCertificationId != null ? merchantCertificationId : MerchantCertificationId;
const data = {
PageNo: pageNo,
PageSize: pageSize
};
if (t != null && t !== '') data.Type = t;
if (mcId != null && mcId !== '') data.MerchantCertificationId = mcId;
2026-03-31 23:27:35 +08:00
return request({
path: API.myPoints,
method: 'GET',
needAuth: true,
2026-04-03 17:57:08 +08:00
data
2026-03-31 23:27:35 +08:00
});
}
async function fetchMyCertificates({ pageNo = 1, pageSize = 10, type } = {}) {
const data = {
PageNo: pageNo,
PageSize: pageSize
};
// type: 1=个人 2=团队 (若后端支持)
if (type != null) data.Type = type;
return request({
path: API.myCertificates,
method: 'GET',
needAuth: true,
data
});
}
2026-03-30 18:08:27 +08:00
module.exports = {
2026-03-31 18:09:37 +08:00
loginWithMobilePassword,
2026-03-31 23:27:35 +08:00
loginWithMobileCode,
2026-03-31 18:09:37 +08:00
sendVerificationCode,
registerWithVerification,
resetPassword,
2026-03-30 18:08:27 +08:00
fetchMyProfile,
fetchMyAuth,
submitMyAuth,
2026-03-31 23:27:35 +08:00
fetchMyRegistrations,
fetchMyPoints,
2026-04-03 17:57:08 +08:00
fetchMyCertificates,
refreshAccessToken
2026-03-30 18:08:27 +08:00
};