Files
jiapu/utils/ApiClient.js
T

574 lines
23 KiB
JavaScript

(function (root, factory) {
if (typeof module === 'object' && module.exports) {
module.exports = factory(root);
return;
}
root.GenealogyApi = factory(root);
})(typeof globalThis !== 'undefined' ? globalThis : window, function (root) {
'use strict';
var DEFAULT_CLIENT_ID = 'ced7e5f0498645c6ec642dcf450b036f';
var DEFAULT_TENANT_ID = '000000';
var DEFAULT_TOKEN_KEY = 'genealogy_auth_token';
function loadNodeModule(path) {
if (typeof require !== 'function') return null;
try {
return require(path);
} catch (error) {
return null;
}
}
function getConfigApi() {
var configFactory;
if (root.GenealogyConfig) return root.GenealogyConfig;
configFactory = loadNodeModule('../config.js');
return configFactory ? configFactory(root) : null;
}
function getStorageUtil() {
return root.StorageUtil || loadNodeModule('./StorageUtil.js');
}
function getAxiosRequestUtil() {
return root.AxiosRequestUtil || loadNodeModule('./AxiosRequestUtil.js');
}
function getStorage(store) {
if (store) return store;
try {
return root.localStorage;
} catch (error) {
return null;
}
}
function getTokenFromResponse(data) {
return data && (data.token || data.accessToken || data.access_token || data.tokenValue) || '';
}
function createChunkFormData(bodyOrFormData) {
var FormDataCtor = root.FormData;
var source = bodyOrFormData || {};
var formData;
if (FormDataCtor && bodyOrFormData instanceof FormDataCtor) return bodyOrFormData;
if (!FormDataCtor) throw new Error('当前环境不支持文件上传');
formData = new FormDataCtor();
formData.append('uploadId', source.uploadId);
formData.append('chunkIndex', source.chunkIndex);
formData.append('chunkMd5', source.chunkMd5);
formData.append('file', source.file);
return formData;
}
function buildApiUrl(baseUrl, path, query) {
var url = String(baseUrl || '').replace(/\/+$/, '') + '/' + String(path || '').replace(/^\/+/, '');
var params = new URLSearchParams();
Object.keys(query || {}).forEach(function (key) {
var value = query[key];
if (value !== undefined && value !== null && value !== '') params.append(key, value);
});
return params.toString() ? url + '?' + params.toString() : url;
}
function createClient(options) {
var settings = options || {};
var configApi = getConfigApi();
var config = configApi && configApi.getConfig ? configApi.getConfig() : {};
var storageUtil = getStorageUtil();
var axiosRequestUtil = getAxiosRequestUtil();
var store = getStorage(settings.tokenStore || settings.storage);
var clientId = settings.clientId || config.clientId || DEFAULT_CLIENT_ID;
var tenantId = settings.tenantId || config.tenantId || DEFAULT_TENANT_ID;
var tokenKey = settings.tokenKey || config.tokenKey || DEFAULT_TOKEN_KEY;
var baseUrl = settings.baseUrl || config.apiBaseUrl;
var requester;
if (!axiosRequestUtil || !axiosRequestUtil.createRequester) throw new Error('缺少 AxiosRequestUtil.createRequester');
if (!baseUrl) throw new Error('缺少接口基础地址');
function getToken() {
return storageUtil && storageUtil.read ? storageUtil.read(store, tokenKey) || '' : '';
}
function setToken(token) {
if (token && storageUtil && storageUtil.write) storageUtil.write(store, tokenKey, token);
}
function clearToken() {
if (storageUtil && storageUtil.remove) storageUtil.remove(store, tokenKey);
}
requester = axiosRequestUtil.createRequester({
baseUrl: baseUrl,
clientId: clientId,
getToken: getToken,
axiosInstance: settings.axiosInstance || root.axios
});
function request(method, path, requestOptions) {
// 请求函数保持私有,页面只能调用下方已在接口文档中定义的业务方法。
return requester(method, path, requestOptions || {});
}
function withTenant(body) {
return Object.assign({ tenantId: tenantId }, body || {});
}
function buildVerificationPath(operationCode, suffix) {
return '/genealogy/pc/auth/verification/' + toRequiredPathId(operationCode, 'PC 认证动作') + (suffix || '');
}
function buildSmsCodePath(operationCode) {
return '/genealogy/pc/auth/sms/' + toRequiredPathId(operationCode, 'PC 短信认证动作') + '/code';
}
function toRequiredPathId(value, label) {
var text = String(value === undefined || value === null ? '' : value).trim();
if (!text) throw new Error('缺少' + label);
return encodeURIComponent(text);
}
function buildFeedPath(genealogyId, suffix) {
// 家族圈接口统一由家谱编号定位,避免页面拼接出未定义的请求路径。
return '/genealogy/pc/genealogies/' + toRequiredPathId(genealogyId, '家谱编号') + '/feeds' + (suffix || '');
}
function buildFeedDetailPath(genealogyId, feedId, suffix) {
return buildFeedPath(genealogyId, '/' + toRequiredPathId(feedId, '动态编号') + (suffix || ''));
}
function buildFeedCommentPath(genealogyId, feedId, commentId) {
var path = buildFeedDetailPath(genealogyId, feedId, '/comments');
return commentId === undefined || commentId === null || commentId === ''
? path
: path + '/' + toRequiredPathId(commentId, '评论编号');
}
function buildFeedReplyPath(genealogyId, feedId, commentId, suffix) {
return buildFeedCommentPath(genealogyId, feedId, commentId) + '/replies' + (suffix || '');
}
function buildGenerationPoemPath(genealogyId, suffix) {
return '/genealogy/pc/genealogies/' + toRequiredPathId(genealogyId, '家谱编号') + '/generation-poems' + (suffix || '');
}
function buildLineagePath(genealogyId, suffix) {
return '/genealogy/pc/genealogies/' + toRequiredPathId(genealogyId, '家谱编号') + '/lineage' + (suffix || '');
}
function buildLineagePersonPath(genealogyId, personId, suffix) {
return buildLineagePath(
genealogyId,
'/persons/' + toRequiredPathId(personId, '人物编号') + (suffix || '')
);
}
function buildGrowthRecordPath(genealogyId, recordId) {
var path = '/genealogy/pc/genealogies/' + toRequiredPathId(genealogyId, '家谱编号') + '/growth-records';
return recordId === undefined || recordId === null || recordId === ''
? path
: path + '/' + toRequiredPathId(recordId, '成长记录编号');
}
function buildRelativeRecordPath(genealogyId, relativeId) {
var path = '/genealogy/pc/genealogies/' + toRequiredPathId(genealogyId, '家谱编号') + '/relative-records';
return relativeId === undefined || relativeId === null || relativeId === ''
? path
: path + '/' + toRequiredPathId(relativeId, '亲友记录编号');
}
function buildMemoPath(genealogyId, memoId) {
var path = '/genealogy/pc/genealogies/' + toRequiredPathId(genealogyId, '家谱编号') + '/memos';
return memoId === undefined || memoId === null || memoId === ''
? path
: path + '/' + toRequiredPathId(memoId, '备忘录编号');
}
function buildMeritRecordPath(genealogyId, meritId) {
return '/genealogy/pc/genealogies/' + toRequiredPathId(genealogyId, '家谱编号') +
'/merit-records/' + toRequiredPathId(meritId, '功德记录编号');
}
function buildArticlePath(genealogyId, articleId) {
return '/genealogy/pc/genealogies/' + toRequiredPathId(genealogyId, '家谱编号') +
'/articles/' + toRequiredPathId(articleId, '谱文编号');
}
function buildAlbumPath(genealogyId, albumId) {
return '/genealogy/pc/genealogies/' + toRequiredPathId(genealogyId, '家谱编号') +
'/albums/' + toRequiredPathId(albumId, '相册编号');
}
function buildAlbumPhotoPath(genealogyId, albumId, photoId) {
return buildAlbumPath(genealogyId, albumId) + '/photos/' + toRequiredPathId(photoId, '相册照片编号');
}
function buildVideoPath(genealogyId, videoId) {
return '/genealogy/pc/genealogies/' + toRequiredPathId(genealogyId, '家谱编号') +
'/videos/' + toRequiredPathId(videoId, '视频编号');
}
function buildCeremonyPath(genealogyId, ceremonyId) {
return '/genealogy/pc/genealogies/' + toRequiredPathId(genealogyId, '家谱编号') +
'/ceremonies/' + toRequiredPathId(ceremonyId, '祭祀活动编号');
}
function buildCeremonyGiftPath(genealogyId, ceremonyId, giftId) {
return buildCeremonyPath(genealogyId, ceremonyId) + '/gifts/' + toRequiredPathId(giftId, '祭品编号');
}
async function login(body) {
var data = await request('POST', '/genealogy/pc/auth/login', {
auth: false,
body: Object.assign({ grantType: 'password' }, withTenant(body))
});
var token = getTokenFromResponse(data);
if (!token) throw new Error('登录响应缺少 token');
setToken(token);
return data;
}
async function loginBySms(body) {
var data = await request('POST', '/genealogy/pc/auth/login/sms', {
auth: false,
body: Object.assign({ grantType: 'sms' }, withTenant(body))
});
var token = getTokenFromResponse(data);
if (!token) throw new Error('登录响应缺少 token');
setToken(token);
return data;
}
async function deactivateAccount(body) {
var data = await request('POST', '/genealogy/pc/auth/account/deactivate', {
body: body
});
clearToken();
return data;
}
async function logout() {
try {
return await request('DELETE', '/genealogy/pc/auth/logout');
} finally {
clearToken();
}
}
return {
clientId: clientId,
tenantId: tenantId,
tokenKey: tokenKey,
getToken: getToken,
setToken: setToken,
clearToken: clearToken,
buildApiUrl: function (path, query) {
return buildApiUrl(baseUrl, path, query);
},
register: function (body) {
return request('POST', '/genealogy/pc/auth/register', {
auth: false,
body: Object.assign({ grantType: 'password', registerSource: 'PC' }, withTenant(body))
});
},
login: login,
loginBySms: loginBySms,
sendSmsCode: function (operationCode, body) {
return request('POST', buildSmsCodePath(operationCode), {
auth: false,
// PC 认证目录以路径 operationCode 绑定动作;clientid 只由请求头提供。
body: Object.assign({ grantType: 'sms', tenantId: tenantId }, body || {})
});
},
currentProfile: function () {
return request('GET', '/genealogy/pc/auth/profile');
},
updateProfile: function (body) {
return request('PUT', '/genealogy/pc/auth/profile', { body: body });
},
changePassword: function (body) {
return request('PUT', '/genealogy/pc/auth/password', { body: body });
},
resetPassword: function (body) {
return request('PUT', '/genealogy/pc/auth/password/reset', {
auth: false,
body: Object.assign({ grantType: 'password' }, withTenant(body))
});
},
changePhone: function (body) {
return request('PUT', '/genealogy/pc/auth/phone', { body: body });
},
deactivateAccount: deactivateAccount,
logout: logout,
initResumableUpload: function (body) {
return request('POST', '/genealogy/pc/files/resumable/init', { body: body });
},
uploadResumableChunk: function (bodyOrFormData) {
return request('POST', '/genealogy/pc/files/resumable/chunk', {
body: createChunkFormData(bodyOrFormData)
});
},
completeResumableUpload: function (body) {
return request('POST', '/genealogy/pc/files/resumable/complete', { body: body });
},
captchaRequirement: function (operationCode, query) {
var source = query || {};
return request('GET', buildVerificationPath(operationCode, '/require'), {
auth: false,
query: {
tenantId: source.tenantId || tenantId,
subject: source.subject
}
});
},
captchaChallenge: function (operationCode, body) {
return request('POST', buildVerificationPath(operationCode, '/challenge'), { auth: false, body: body });
},
captchaVerify: function (operationCode, body) {
return request('POST', buildVerificationPath(operationCode, '/verify'), { auth: false, body: body });
},
captchaChallengeUrl: function (operationCode) {
return buildApiUrl(baseUrl, buildVerificationPath(operationCode, '/challenge'));
},
captchaVerifyUrl: function (operationCode) {
return buildApiUrl(baseUrl, buildVerificationPath(operationCode, '/verify'));
},
regionChildren: function (parentCode) {
return request('GET', '/genealogy/region/children', { query: { parentCode: parentCode } });
},
regionPath: function (regionCode) {
return request('GET', '/genealogy/region/path/' + encodeURIComponent(regionCode));
},
regionSearch: function (query) {
return request('GET', '/genealogy/region/search', { query: query });
},
regionDetail: function (regionCode) {
return request('GET', '/genealogy/region/' + encodeURIComponent(regionCode));
},
genealogyQuota: function () {
return request('GET', '/genealogy/pc/genealogies/quota');
},
notifications: function (query) {
return request('GET', '/genealogy/pc/notifications', { query: query });
},
unreadNotificationCount: function () {
return request('GET', '/genealogy/pc/notifications/unread-count');
},
markNotificationRead: function (notificationId) {
return request('POST', '/genealogy/pc/notifications/' + toRequiredPathId(notificationId, '通知编号') + '/read');
},
markAllNotificationsRead: function () {
return request('POST', '/genealogy/pc/notifications/read-all');
},
feeds: function (genealogyId) {
return request('GET', buildFeedPath(genealogyId));
},
feedsPage: function (genealogyId, query) {
return request('GET', buildFeedPath(genealogyId, '/page'), { query: query });
},
feedDetail: function (genealogyId, feedId) {
return request('GET', buildFeedDetailPath(genealogyId, feedId));
},
createFeed: function (genealogyId, body) {
return request('POST', buildFeedPath(genealogyId), { body: body });
},
updateFeed: function (genealogyId, feedId, body) {
return request('PUT', buildFeedDetailPath(genealogyId, feedId), { body: body });
},
deleteFeed: function (genealogyId, feedId) {
return request('DELETE', buildFeedDetailPath(genealogyId, feedId));
},
likeFeed: function (genealogyId, feedId) {
return request('POST', buildFeedDetailPath(genealogyId, feedId, '/likes'));
},
unlikeFeed: function (genealogyId, feedId) {
return request('DELETE', buildFeedDetailPath(genealogyId, feedId, '/likes'));
},
feedComments: function (genealogyId, feedId) {
return request('GET', buildFeedCommentPath(genealogyId, feedId));
},
feedCommentsPage: function (genealogyId, feedId, query) {
return request('GET', buildFeedCommentPath(genealogyId, feedId) + '/page', { query: query });
},
feedCommentReplies: function (genealogyId, feedId, commentId) {
return request('GET', buildFeedReplyPath(genealogyId, feedId, commentId));
},
feedCommentRepliesPage: function (genealogyId, feedId, commentId, query) {
return request('GET', buildFeedReplyPath(genealogyId, feedId, commentId, '/page'), { query: query });
},
createFeedComment: function (genealogyId, feedId, body) {
return request('POST', buildFeedCommentPath(genealogyId, feedId), { body: body });
},
deleteFeedComment: function (genealogyId, feedId, commentId) {
return request('DELETE', buildFeedCommentPath(genealogyId, feedId, commentId));
},
generationPoems: function (genealogyId) {
return request('GET', buildGenerationPoemPath(genealogyId));
},
generationPoemsManagement: function (genealogyId) {
return request('GET', buildGenerationPoemPath(genealogyId, '/management'));
},
createGenerationPoem: function (genealogyId, body) {
return request('POST', buildGenerationPoemPath(genealogyId), { body: body });
},
updateGenerationPoem: function (genealogyId, poemId, body) {
return request('PUT', buildGenerationPoemPath(genealogyId, '/' + toRequiredPathId(poemId, '字辈编号')), { body: body });
},
previewGenerationPoems: function (genealogyId, body) {
return request('POST', buildGenerationPoemPath(genealogyId, '/batch/preview'), { body: body });
},
saveGenerationPoems: function (genealogyId, body) {
return request('POST', buildGenerationPoemPath(genealogyId, '/batch/save'), { body: body });
},
lineagePersons: function (genealogyId) {
return request('GET', buildLineagePath(genealogyId, '/persons'));
},
lineagePersonsPage: function (genealogyId, query) {
return request('GET', buildLineagePath(genealogyId, '/persons/page'), { query: query });
},
lineagePersonOptions: function (genealogyId) {
return request('GET', buildLineagePath(genealogyId, '/persons/options'));
},
lineageTree: function (genealogyId) {
return request('GET', buildLineagePath(genealogyId, '/tree'));
},
createLineagePerson: function (genealogyId, body) {
return request('POST', buildLineagePath(genealogyId, '/persons'), { body: body });
},
lineagePersonDetail: function (genealogyId, personId) {
return request('GET', buildLineagePersonPath(genealogyId, personId));
},
updateLineagePerson: function (genealogyId, personId, body) {
return request('PUT', buildLineagePersonPath(genealogyId, personId), { body: body });
},
disableLineagePerson: function (genealogyId, personId) {
return request('DELETE', buildLineagePersonPath(genealogyId, personId));
},
createLineageChild: function (genealogyId, personId, body) {
return request('POST', buildLineagePersonPath(genealogyId, personId, '/children'), { body: body });
},
createLineageParent: function (genealogyId, personId, body) {
return request('POST', buildLineagePersonPath(genealogyId, personId, '/parents'), { body: body });
},
createLineageSibling: function (genealogyId, personId, body) {
return request('POST', buildLineagePersonPath(genealogyId, personId, '/siblings'), { body: body });
},
createLineageSpouse: function (genealogyId, personId, body) {
return request('POST', buildLineagePersonPath(genealogyId, personId, '/spouses'), { body: body });
},
growthRecords: function (genealogyId) {
return request('GET', buildGrowthRecordPath(genealogyId));
},
createGrowthRecord: function (genealogyId, body) {
return request('POST', buildGrowthRecordPath(genealogyId), { body: body });
},
growthRecordDetail: function (genealogyId, recordId) {
return request('GET', buildGrowthRecordPath(genealogyId, recordId));
},
updateGrowthRecord: function (genealogyId, recordId, body) {
return request('PUT', buildGrowthRecordPath(genealogyId, recordId), { body: body });
},
deleteGrowthRecord: function (genealogyId, recordId) {
return request('DELETE', buildGrowthRecordPath(genealogyId, recordId));
},
relativeRecords: function (genealogyId) {
return request('GET', buildRelativeRecordPath(genealogyId));
},
createRelativeRecord: function (genealogyId, body) {
return request('POST', buildRelativeRecordPath(genealogyId), { body: body });
},
relativeRecordDetail: function (genealogyId, relativeId) {
return request('GET', buildRelativeRecordPath(genealogyId, relativeId));
},
updateRelativeRecord: function (genealogyId, relativeId, body) {
return request('PUT', buildRelativeRecordPath(genealogyId, relativeId), { body: body });
},
deleteRelativeRecord: function (genealogyId, relativeId) {
return request('DELETE', buildRelativeRecordPath(genealogyId, relativeId));
},
memos: function (genealogyId) {
return request('GET', buildMemoPath(genealogyId));
},
createMemo: function (genealogyId, body) {
return request('POST', buildMemoPath(genealogyId), { body: body });
},
memoDetail: function (genealogyId, memoId) {
return request('GET', buildMemoPath(genealogyId, memoId));
},
updateMemo: function (genealogyId, memoId, body) {
return request('PUT', buildMemoPath(genealogyId, memoId), { body: body });
},
deleteMemo: function (genealogyId, memoId) {
return request('DELETE', buildMemoPath(genealogyId, memoId));
},
deleteMeritRecord: function (genealogyId, meritId) {
return request('DELETE', buildMeritRecordPath(genealogyId, meritId));
},
deleteArticle: function (genealogyId, articleId) {
return request('DELETE', buildArticlePath(genealogyId, articleId));
},
deleteAlbum: function (genealogyId, albumId) {
return request('DELETE', buildAlbumPath(genealogyId, albumId));
},
deleteAlbumPhoto: function (genealogyId, albumId, photoId) {
return request('DELETE', buildAlbumPhotoPath(genealogyId, albumId, photoId));
},
deleteVideo: function (genealogyId, videoId) {
return request('DELETE', buildVideoPath(genealogyId, videoId));
},
replaceCeremonyInvitees: function (genealogyId, ceremonyId, body) {
return request('PUT', buildCeremonyPath(genealogyId, ceremonyId) + '/invitees', { body: body });
},
ceremonyInvitations: function (genealogyId, ceremonyId) {
return request('GET', buildCeremonyPath(genealogyId, ceremonyId) + '/invitations');
},
respondCeremonyInvitation: function (genealogyId, ceremonyId, body) {
return request('PUT', buildCeremonyPath(genealogyId, ceremonyId) + '/invitations/me', { body: body });
},
myCeremonyInvitations: function () {
return request('GET', '/genealogy/pc/genealogies/ceremony-invitations/mine');
},
deleteCeremony: function (genealogyId, ceremonyId) {
return request('DELETE', buildCeremonyPath(genealogyId, ceremonyId));
},
deleteCeremonyGift: function (genealogyId, ceremonyId, giftId) {
return request('DELETE', buildCeremonyGiftPath(genealogyId, ceremonyId, giftId));
}
};
}
function createDefaultClient() {
try {
return createClient();
} catch (error) {
return null;
}
}
return {
DEFAULT_CLIENT_ID: DEFAULT_CLIENT_ID,
DEFAULT_TENANT_ID: DEFAULT_TENANT_ID,
TOKEN_KEY: DEFAULT_TOKEN_KEY,
createClient: createClient,
defaultClient: createDefaultClient()
};
});