const { API } = require('./constants'); const { request } = require('./http'); function pickString(obj, key) { return obj && typeof obj[key] === 'string' ? obj[key] : ''; } 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; } 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, newPassword: 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(payload) { // 兼容旧调用方式:submitMyAuth({ name, mobile, idCardNumber, idCardImageUrl, idCardBackImageUrl }) // 新页面会直接把表单对象透传进来(如 authType、passport 字段等),后端若不识别会忽略。 const p = payload && typeof payload === 'object' ? payload : {}; return request({ path: API.myAuth, method: 'POST', needAuth: true, data: p, 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, // 新接口参数(按 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; return request({ path: API.myPoints, method: 'GET', needAuth: true, data }); } async function fetchMyTeams({ pageNo = 1, pageSize = 20 } = {}) { return request({ path: API.myTeams, method: 'GET', needAuth: true, data: { PageNo: pageNo, PageSize: pageSize } }); } async function fetchMyOrders({ pageNo = 1, pageSize = 10 } = {}) { return request({ path: API.myOrders, method: 'GET', needAuth: true, data: { PageNo: pageNo, PageSize: pageSize } }); } 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 }); } module.exports = { loginWithMobilePassword, loginWithMobileCode, sendVerificationCode, registerWithVerification, resetPassword, fetchMyProfile, fetchMyAuth, submitMyAuth, fetchMyRegistrations, fetchMyPoints, fetchMyTeams, fetchMyOrders, fetchMyCertificates, refreshAccessToken };