450 lines
13 KiB
JavaScript
450 lines
13 KiB
JavaScript
(function (root, factory) {
|
|
// 同一份接口客户端同时支持浏览器页面和 Node 测试环境。
|
|
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_BASE_URL = 'http://test-genealogy-api.ddxcjp.cn';
|
|
var DEFAULT_CLIENT_ID = 'ced7e5f0498645c6ec642dcf450b036f';
|
|
var DEFAULT_TENANT_ID = '000000';
|
|
var DEFAULT_TOKEN_KEY = 'genealogy_auth_token';
|
|
var DEFAULT_TOKEN_HEADER_NAME = 'Authorization';
|
|
|
|
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('../../utils/StorageUtil.js');
|
|
}
|
|
|
|
function getRequestUtil() {
|
|
return root.RequestUtil || loadNodeModule('../../utils/RequestUtil.js');
|
|
}
|
|
|
|
function getStorage(store) {
|
|
if (store) return store;
|
|
|
|
try {
|
|
return root.localStorage;
|
|
} catch (error) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function getTokenFromResponse(data) {
|
|
// 登录响应可能使用不同 token 字段,这里统一收敛为本地登录票据。
|
|
if (!data) return '';
|
|
return data.token || data.accessToken || data.tokenValue || '';
|
|
}
|
|
|
|
function ApiError(message, options) {
|
|
this.name = 'ApiError';
|
|
this.message = message || '接口请求失败';
|
|
this.status = options && options.status;
|
|
this.code = options && options.code;
|
|
this.data = options && options.data;
|
|
}
|
|
|
|
ApiError.prototype = Object.create(Error.prototype);
|
|
ApiError.prototype.constructor = ApiError;
|
|
|
|
function createUnavailableMethod(name) {
|
|
return function () {
|
|
var error = new Error('PC 接口文档未提供该业务接口:' + name);
|
|
error.code = 'PC_API_NOT_AVAILABLE';
|
|
throw error;
|
|
};
|
|
}
|
|
|
|
function createFormData(fileOrFormData) {
|
|
var FormDataCtor = root.FormData;
|
|
var formData;
|
|
|
|
if (FormDataCtor && fileOrFormData instanceof FormDataCtor) return fileOrFormData;
|
|
if (!FormDataCtor) throw new ApiError('当前环境不支持文件上传');
|
|
|
|
formData = new FormDataCtor();
|
|
formData.append('file', fileOrFormData);
|
|
return formData;
|
|
}
|
|
|
|
function createChunkFormData(body) {
|
|
var FormDataCtor = root.FormData;
|
|
var source = body || {};
|
|
var formData;
|
|
|
|
if (FormDataCtor && body instanceof FormDataCtor) return body;
|
|
if (!FormDataCtor) throw new ApiError('当前环境不支持文件上传');
|
|
|
|
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 createClient(options) {
|
|
var settings = options || {};
|
|
var configApi = getConfigApi();
|
|
var config = configApi && configApi.getConfig ? configApi.getConfig() : {};
|
|
var storageUtil = getStorageUtil();
|
|
var requestUtil = getRequestUtil();
|
|
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 tokenHeaderName = settings.tokenHeaderName || config.tokenHeaderName || DEFAULT_TOKEN_HEADER_NAME;
|
|
var baseUrl = settings.baseUrl || config.apiBaseUrl || DEFAULT_BASE_URL;
|
|
var requester;
|
|
|
|
if (!requestUtil || !requestUtil.createRequester) {
|
|
throw new ApiError('缺少 RequestUtil.createRequester 公共请求工具');
|
|
}
|
|
|
|
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 = requestUtil.createRequester({
|
|
baseUrl: baseUrl,
|
|
clientId: clientId,
|
|
tokenHeaderName: tokenHeaderName,
|
|
getToken: getToken,
|
|
fetchImpl: settings.fetchImpl || root.fetch
|
|
});
|
|
|
|
function request(method, path, requestOptions) {
|
|
return requester(method, path, requestOptions || {});
|
|
}
|
|
|
|
function withTenant(body) {
|
|
return Object.assign({
|
|
tenantId: tenantId,
|
|
clientId: clientId
|
|
}, body || {});
|
|
}
|
|
|
|
async function login(body) {
|
|
var data = await request('POST', '/genealogy/pc/auth/login', {
|
|
auth: false,
|
|
body: Object.assign({
|
|
grantType: 'password'
|
|
}, withTenant(body))
|
|
});
|
|
|
|
setToken(getTokenFromResponse(data));
|
|
return data;
|
|
}
|
|
|
|
async function register(body) {
|
|
var data = await request('POST', '/genealogy/pc/auth/register', {
|
|
auth: false,
|
|
body: Object.assign({
|
|
grantType: 'password',
|
|
registerSource: 'PC'
|
|
}, withTenant(body))
|
|
});
|
|
|
|
setToken(getTokenFromResponse(data));
|
|
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))
|
|
});
|
|
|
|
setToken(getTokenFromResponse(data));
|
|
return data;
|
|
}
|
|
|
|
function sendSmsCode(body) {
|
|
return request('POST', '/genealogy/pc/auth/sms/code', {
|
|
auth: false,
|
|
body: Object.assign({
|
|
grantType: 'sms'
|
|
}, withTenant(body))
|
|
});
|
|
}
|
|
|
|
function captchaRequire(body) {
|
|
var source = body || {};
|
|
|
|
return request('GET', '/captcha/require', {
|
|
auth: false,
|
|
query: {
|
|
tenantId: source.tenantId || tenantId,
|
|
clientId: source.clientId || clientId,
|
|
sceneCode: source.sceneCode,
|
|
subject: source.subject
|
|
}
|
|
});
|
|
}
|
|
|
|
function buildApiUrl(path, query) {
|
|
if (requestUtil.joinUrl && requestUtil.appendQuery) {
|
|
return requestUtil.appendQuery(requestUtil.joinUrl(baseUrl, path), query);
|
|
}
|
|
|
|
return baseUrl + path;
|
|
}
|
|
|
|
var client = {
|
|
clientId: clientId,
|
|
tenantId: tenantId,
|
|
tokenKey: tokenKey,
|
|
getToken: getToken,
|
|
setToken: setToken,
|
|
clearToken: clearToken,
|
|
request: request,
|
|
buildApiUrl: buildApiUrl,
|
|
get: function (path, query) {
|
|
return request('GET', path, { query: query });
|
|
},
|
|
post: function (path, body, reqOptions) {
|
|
return request('POST', path, Object.assign({}, reqOptions || {}, { body: body }));
|
|
},
|
|
put: function (path, body, reqOptions) {
|
|
return request('PUT', path, Object.assign({}, reqOptions || {}, { body: body }));
|
|
},
|
|
delete: function (path, reqOptions) {
|
|
return request('DELETE', path, reqOptions || {});
|
|
},
|
|
login: login,
|
|
register: register,
|
|
sendSmsCode: sendSmsCode,
|
|
loginBySms: loginBySms,
|
|
smsLogin: loginBySms,
|
|
getProfile: function () {
|
|
return request('GET', '/genealogy/pc/auth/profile');
|
|
},
|
|
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({
|
|
tenantId: tenantId
|
|
}, body || {})
|
|
});
|
|
},
|
|
changePhone: function (body) {
|
|
return request('PUT', '/genealogy/pc/auth/phone', { body: body });
|
|
},
|
|
deactivateAccount: async function (body) {
|
|
var data = await request('POST', '/genealogy/pc/auth/account/deactivate', { body: body });
|
|
|
|
clearToken();
|
|
return data;
|
|
},
|
|
logout: async function () {
|
|
var data = await request('DELETE', '/genealogy/pc/auth/logout');
|
|
|
|
clearToken();
|
|
return data;
|
|
},
|
|
uploadFile: function (fileOrFormData) {
|
|
return request('POST', '/genealogy/pc/files/upload', {
|
|
body: createFormData(fileOrFormData)
|
|
});
|
|
},
|
|
initResumableUpload: function (body) {
|
|
return request('POST', '/genealogy/pc/files/resumable/init', { body: body });
|
|
},
|
|
uploadChunk: function (body) {
|
|
return request('POST', '/genealogy/pc/files/resumable/chunk', {
|
|
body: createChunkFormData(body)
|
|
});
|
|
},
|
|
uploadResumableChunk: function (body) {
|
|
return this.uploadChunk(body);
|
|
},
|
|
completeResumableUpload: function (body) {
|
|
return request('POST', '/genealogy/pc/files/resumable/complete', { body: body });
|
|
},
|
|
bindFileReference: function (body) {
|
|
return request('POST', '/genealogy/pc/files/reference', { body: body });
|
|
},
|
|
releaseFileReference: function (query) {
|
|
return request('DELETE', '/genealogy/pc/files/reference', { query: query });
|
|
},
|
|
getRegionChildren: function (parentCode) {
|
|
return request('GET', '/genealogy/region/children', {
|
|
query: {
|
|
parentCode: parentCode
|
|
}
|
|
});
|
|
},
|
|
regionChildren: function (parentCode) {
|
|
return this.getRegionChildren(parentCode);
|
|
},
|
|
getRegionPath: function (regionCode) {
|
|
return request('GET', '/genealogy/region/path/' + encodeURIComponent(regionCode));
|
|
},
|
|
regionPath: function (regionCode) {
|
|
return this.getRegionPath(regionCode);
|
|
},
|
|
searchRegions: function (query) {
|
|
return request('GET', '/genealogy/region/search', { query: query });
|
|
},
|
|
regionSearch: function (query) {
|
|
return this.searchRegions(query);
|
|
},
|
|
getRegion: function (regionCode) {
|
|
return request('GET', '/genealogy/region/' + encodeURIComponent(regionCode));
|
|
},
|
|
regionDetail: function (regionCode) {
|
|
return this.getRegion(regionCode);
|
|
},
|
|
captchaRequire: captchaRequire,
|
|
captchaRequirement: captchaRequire,
|
|
captchaChallenge: function (body) {
|
|
return request('POST', '/captcha/challenge', { auth: false, body: body });
|
|
},
|
|
captchaVerify: function (body) {
|
|
return request('POST', '/captcha/verify', { auth: false, body: body });
|
|
},
|
|
legacyCaptcha: function () {
|
|
return request('GET', '/auth/code', { auth: false });
|
|
}
|
|
};
|
|
|
|
// PC YAML 未提供这些业务 paths,保留方法名只用于阻止旧移动端路径继续发请求。
|
|
[
|
|
'listGenealogies',
|
|
'createGenealogy',
|
|
'publicGenealogies',
|
|
'myGenealogies',
|
|
'genealogyDetail',
|
|
'genealogyOverview',
|
|
'applyJoinGenealogy',
|
|
'myJoinApplies',
|
|
'pendingJoinApplies',
|
|
'auditJoinApply',
|
|
'cancelJoinApply',
|
|
'genealogyMembers',
|
|
'genealogyMemberOptions',
|
|
'updateGenealogyMember',
|
|
'removeGenealogyMember',
|
|
'leaveGenealogy',
|
|
'transferGenealogyOwner',
|
|
'articleCategories',
|
|
'articles',
|
|
'articleDetail',
|
|
'createArticle',
|
|
'updateArticle',
|
|
'feeds',
|
|
'feedsPage',
|
|
'feedDetail',
|
|
'createFeed',
|
|
'updateFeed',
|
|
'likeFeed',
|
|
'unlikeFeed',
|
|
'feedComments',
|
|
'feedCommentsPage',
|
|
'createFeedComment',
|
|
'deleteFeedComment',
|
|
'albums',
|
|
'createAlbum',
|
|
'updateAlbum',
|
|
'albumPhotos',
|
|
'createAlbumPhoto',
|
|
'ceremonies',
|
|
'ceremonyDetail',
|
|
'createCeremony',
|
|
'updateCeremony',
|
|
'ceremonyGifts',
|
|
'createCeremonyGift',
|
|
'meritRecords',
|
|
'createMeritRecord',
|
|
'growthRecords',
|
|
'growthRecordDetail',
|
|
'createGrowthRecord',
|
|
'updateGrowthRecord',
|
|
'memos',
|
|
'memoDetail',
|
|
'createMemo',
|
|
'updateMemo',
|
|
'vipPackages',
|
|
'vipOrders',
|
|
'createVipOrder',
|
|
'generationPoems',
|
|
'createGenerationPoem',
|
|
'updateGenerationPoem',
|
|
'previewGenerationPoems',
|
|
'saveGenerationPoemsBatch',
|
|
'lineageTree',
|
|
'lineagePersons',
|
|
'lineagePersonsPage',
|
|
'lineagePersonOptions',
|
|
'lineagePersonDetail',
|
|
'createLineagePerson',
|
|
'updateLineagePerson',
|
|
'deleteLineagePerson',
|
|
'addLineageRelation',
|
|
'notifications',
|
|
'markNotificationRead',
|
|
'markAllNotificationsRead',
|
|
'helpArticles',
|
|
'helpArticleDetail',
|
|
'promotions',
|
|
'feedbackList',
|
|
'submitFeedback'
|
|
].forEach(function (name) {
|
|
client[name] = createUnavailableMethod(name);
|
|
});
|
|
|
|
return client;
|
|
}
|
|
|
|
return {
|
|
DEFAULT_BASE_URL: DEFAULT_BASE_URL,
|
|
DEFAULT_CLIENT_ID: DEFAULT_CLIENT_ID,
|
|
DEFAULT_TENANT_ID: DEFAULT_TENANT_ID,
|
|
TOKEN_KEY: DEFAULT_TOKEN_KEY,
|
|
ApiError: ApiError,
|
|
createClient: createClient,
|
|
defaultClient: createClient()
|
|
};
|
|
});
|