218 lines
4.5 KiB
JavaScript
218 lines
4.5 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 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
|
|
};
|
|
}
|
|
|
|
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,
|
|
data: { mobile, password, verificationCode },
|
|
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
|
|
}
|
|
});
|
|
}
|
|
|
|
async function fetchMyPoints({ pageNo = 1, pageSize = 10 } = {}) {
|
|
return request({
|
|
path: API.myPoints,
|
|
method: 'GET',
|
|
needAuth: true,
|
|
data: {
|
|
PageNo: pageNo,
|
|
PageSize: pageSize
|
|
}
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
loginWithMobilePassword,
|
|
loginWithMobileCode,
|
|
sendVerificationCode,
|
|
registerWithVerification,
|
|
resetPassword,
|
|
fetchMyProfile,
|
|
fetchMyAuth,
|
|
submitMyAuth,
|
|
fetchMyRegistrations,
|
|
fetchMyPoints
|
|
};
|