2026-03-31 18:09:37 +08:00

142 lines
2.9 KiB
JavaScript

const { API } = require('./constants');
const { request } = require('./http');
function pickString(obj, key) {
return obj && typeof obj[key] === 'string' ? obj[key] : '';
}
async function loginWithMobilePassword({ mobile, password }) {
const loginData = await request({
path: API.wechatLogin,
method: 'POST',
needAuth: false,
data: {
mobile,
password: password != null ? password : ''
},
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
};
}
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 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({ name, mobile, idCardNumber, idCardImageUrl, idCardBackImageUrl }) {
const payload = {
name,
mobile,
idCardNumber
};
if (idCardImageUrl) payload.idCardImageUrl = idCardImageUrl;
if (idCardBackImageUrl) payload.idCardBackImageUrl = idCardBackImageUrl;
return request({
path: API.myAuth,
method: 'POST',
needAuth: true,
data: payload,
contentType: 'application/json'
});
}
async function fetchMyRegistrations({ pageNo = 1, pageSize = 10 } = {}) {
return request({
path: API.myRegistrations,
method: 'GET',
needAuth: true,
data: {
PageNo: pageNo,
PageSize: pageSize
}
});
}
module.exports = {
loginWithMobilePassword,
sendVerificationCode,
registerWithVerification,
fetchMyProfile,
fetchMyAuth,
submitMyAuth,
fetchMyRegistrations
};