同步小程序与管理端更新,完善抽烟记录和解绑冷静期
This commit is contained in:
+32
-5
@@ -1,4 +1,5 @@
|
||||
const { camelName, toWire, fromWire } = require('./api-naming');
|
||||
const dataCache = require('./data-cache');
|
||||
/**
|
||||
* 网络请求工具 - 对接后端 API
|
||||
*/
|
||||
@@ -13,6 +14,7 @@ const MEDIA_BASE = 'https://past.sundynix.cn/backend/';
|
||||
function getImageUrl(path) {
|
||||
if (!path) return '';
|
||||
if (path.startsWith('http://') || path.startsWith('https://')) return path;
|
||||
if (path.startsWith('/backend/')) return MEDIA_BASE + path.substring('/backend/'.length);
|
||||
if (path.startsWith('/')) return MEDIA_BASE + path.substring(1);
|
||||
return MEDIA_BASE + path;
|
||||
}
|
||||
@@ -75,6 +77,17 @@ async function waitForAuth() {
|
||||
if (app && app.ensureAuth) await app.ensureAuth();
|
||||
}
|
||||
async function request(action, data = null, method = 'POST') {
|
||||
const name = camelName(action);
|
||||
await waitForAuthIfNeeded(name);
|
||||
if (method === 'GET') return dataCache.read(name, toWire(data), () => authenticatedRequest(action, data, method));
|
||||
const result = await authenticatedRequest(action, data, method);
|
||||
dataCache.mutation(name, result);
|
||||
return result;
|
||||
}
|
||||
async function waitForAuthIfNeeded(action) {
|
||||
if (action !== 'wechatLogin') await waitForAuth();
|
||||
}
|
||||
async function authenticatedRequest(action, data = null, method = 'POST') {
|
||||
if (camelName(action) === 'wechatLogin') return sendRequest(action, data, method);
|
||||
await waitForAuth();
|
||||
var token = wx.getStorageSync('token');
|
||||
@@ -121,6 +134,13 @@ function sendRequest(action, data = null, method = 'POST') {
|
||||
data: requestData,
|
||||
header: header,
|
||||
success: (res) => {
|
||||
const relation = res.header && (res.header['X-Couple-ID'] !== undefined ? res.header['X-Couple-ID'] : res.header['x-couple-id']);
|
||||
const owner = res.header && (res.header['X-User-ID'] || res.header['x-user-id']);
|
||||
if (relation !== undefined && String(owner) === String(wx.getStorageSync('userId'))) {
|
||||
const next = String(relation) === '0' ? '' : String(relation);
|
||||
const previous = String(wx.getStorageSync('coupleId') || '');
|
||||
if (next !== previous) { wx.setStorageSync('coupleId', next); dataCache.clear(); const app = typeof getApp === 'function' && getApp(); if(app && app.onRelationChanged) app.onRelationChanged(); }
|
||||
}
|
||||
try { wx.hideLoading(); } catch(e) {}
|
||||
if (res.statusCode >= 400 || (res.data && res.data.error)) {
|
||||
reject({ ...fromWire(res.data), _statusCode: res.statusCode });
|
||||
@@ -198,7 +218,7 @@ const api = {
|
||||
{}
|
||||
);
|
||||
wx.hideLoading();
|
||||
if (data && (data.path || data.success)) return data;
|
||||
if (data && !data.error && (data.path || data.success)) { dataCache.mutation('uploadAvatar', data); return data; }
|
||||
wx.showToast({ title: formatUploadError(data), icon: 'none' });
|
||||
throw data || { error: '上传失败' };
|
||||
} catch(e) {
|
||||
@@ -218,7 +238,7 @@ const api = {
|
||||
{ partner_id: partnerId }
|
||||
);
|
||||
wx.hideLoading();
|
||||
if (data && (data.path || data.success)) return data;
|
||||
if (data && !data.error && (data.path || data.success)) { dataCache.mutation('uploadAvatar', data); return data; }
|
||||
wx.showToast({ title: formatUploadError(data), icon: 'none' });
|
||||
throw data || { error: '上传失败' };
|
||||
} catch(e) {
|
||||
@@ -238,6 +258,7 @@ const api = {
|
||||
unbindStatus: () => request('unbindStatus', null, 'GET'),
|
||||
myInvitations: () => request('myInvitations', null, 'GET'),
|
||||
respondInvitation: (data) => request('respondInvitation', data),
|
||||
generateInviteLink: () => request('generateInviteLink', null, 'POST'),
|
||||
acceptShareInvite: (data) => request('acceptShareInvite', data),
|
||||
updatePartnerProfile: (data) => request('updatePartnerProfile', data),
|
||||
|
||||
@@ -255,6 +276,7 @@ const api = {
|
||||
albumDelete: (data) => request('albumDelete', data),
|
||||
photoDelete: (data) => request('photoDelete', data),
|
||||
photoAdd: (data) => request('photoAdd', data),
|
||||
photoUpdate: (data) => request('photoUpdate', data),
|
||||
photos: (albumId, data, method) => request('photos', { album_id: albumId, ...data }, method || 'GET'),
|
||||
photoUpload: async (filePath, albumId) => {
|
||||
wx.showLoading({ title: '上传中...' });
|
||||
@@ -313,7 +335,7 @@ const api = {
|
||||
});
|
||||
});
|
||||
wx.hideLoading();
|
||||
if (data && (data.success || data.path || data.uploaded)) return data;
|
||||
if (data && !data.error && (data.success || data.path || data.uploaded)) { dataCache.mutation('photoUpload', data); return data; }
|
||||
wx.showToast({ title: formatUploadError(data), icon: 'none' });
|
||||
throw data || { error: '上传失败' };
|
||||
} catch(e) {
|
||||
@@ -341,6 +363,8 @@ const api = {
|
||||
songsDelete: (data) => request('songsDelete', data),
|
||||
|
||||
// 打卡
|
||||
smokingInfo: (userId) => request("smokingInfo", { user_id: userId || 0 }, "GET"),
|
||||
smokingAction: (data) => request("smokingAction", data),
|
||||
checkinList: () => request('checkinList', null, 'GET'),
|
||||
checkinAdd: (data) => request('checkinAdd', data),
|
||||
checkinDo: (data) => request('checkinDo', data),
|
||||
@@ -369,7 +393,7 @@ const api = {
|
||||
|
||||
// 姨姨
|
||||
// 权限和周期数据会随伴侣设置立即变化,始终请求最新结果。
|
||||
periodList: () => request('periodList', null, 'GET'),
|
||||
periodList: (userId) => request('periodList', userId ? {user_id:userId} : null, 'GET'),
|
||||
periodAdd: (data) => request('periodAdd', data),
|
||||
periodUpdate: (data) => request('periodUpdate', data),
|
||||
periodDelete: (data) => request('periodDelete', data),
|
||||
@@ -379,7 +403,7 @@ const api = {
|
||||
periodSexDelete: (data) => request('periodSexDelete', data),
|
||||
periodSexList: (data) => request('periodSexList', data, 'GET'),
|
||||
// 姨姨权限(对方可见)
|
||||
periodVisibleGet: () => request('periodVisibleGet', null, 'GET'),
|
||||
periodVisibleGet: (userId) => request('periodVisibleGet', userId ? {user_id:userId} : null, 'GET'),
|
||||
periodVisibleSet: (data) => request('periodVisibleSet', data),
|
||||
periodSourceSelect: (data) => request('periodSourceSelect', data),
|
||||
};
|
||||
@@ -448,6 +472,9 @@ async function uploadFileCompat(options) {
|
||||
success: (res) => {
|
||||
var data = parseUploadResponse(res.data);
|
||||
if (data) {
|
||||
if (res.statusCode < 400 && !data.error && (data.success || data.path || data.uploaded)) {
|
||||
dataCache.mutation(actionMatch ? decodeURIComponent(actionMatch[1] || actionMatch[2]) : '', data);
|
||||
}
|
||||
resolve(data);
|
||||
} else {
|
||||
reject({ error: '解析响应失败', raw: res.data });
|
||||
|
||||
Reference in New Issue
Block a user