172 lines
4.2 KiB
JavaScript
172 lines
4.2 KiB
JavaScript
'use strict';
|
|
|
|
const MainService = require('../../main');
|
|
|
|
class expressService extends MainService {
|
|
/**
|
|
* 初始化快递映射模块的权限配置。
|
|
*
|
|
* @param {Egg.Context} ctx 上下文对象
|
|
*/
|
|
constructor(ctx) {
|
|
super(ctx);
|
|
const _noNeedLogin = '';
|
|
const _noNeedRight = 'list,listBase,create,checkCodeUnique,update';
|
|
const _noNeedLog = '*';
|
|
const _noNeedAuth = 'list,listBase,create,checkCodeUnique,update';
|
|
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
|
|
}
|
|
|
|
/**
|
|
* 调用快递映射系统服务。
|
|
*
|
|
* @param {string} methodName 系统服务方法名
|
|
* @param {Object} data 请求参数
|
|
* @param {string} defaultMessage 默认错误信息
|
|
* @return {Promise<Object>} 统一返回结果
|
|
*/
|
|
async callSystemExpress(methodName, data, defaultMessage) {
|
|
const { service } = this;
|
|
|
|
try {
|
|
const result = await service.systems.setting.express[methodName](data);
|
|
return this.success('ok', result);
|
|
} catch (error) {
|
|
return this.fail(error.message || defaultMessage, {
|
|
code: Number(error.code) || 500,
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取快递映射列表。
|
|
*
|
|
* @param {Object} data 请求参数
|
|
* @param {Object} [data.pagination] 分页参数
|
|
* @return {Promise<Object>} 列表结果
|
|
*/
|
|
async list(data = {}) {
|
|
const { service } = this;
|
|
const {
|
|
where = {},
|
|
pagination: inputPagination,
|
|
page,
|
|
page_size: pageSize,
|
|
pageSize: camelPageSize,
|
|
...rest
|
|
} = data || {};
|
|
const pagination = {
|
|
...(inputPagination && typeof inputPagination === 'object' ? inputPagination : {}),
|
|
};
|
|
|
|
if (page !== undefined) {
|
|
pagination.page = page;
|
|
}
|
|
|
|
const resolvedPageSize = pageSize !== undefined ? pageSize : camelPageSize;
|
|
if (resolvedPageSize !== undefined) {
|
|
pagination.pageSize = resolvedPageSize;
|
|
}
|
|
|
|
const payload = {
|
|
...rest,
|
|
where,
|
|
pagination,
|
|
};
|
|
|
|
this.prepareWhereContext(payload, {
|
|
specialFieldMap: {
|
|
id: {
|
|
field: 'id',
|
|
},
|
|
code: {
|
|
field: 'code',
|
|
},
|
|
name: {
|
|
field: 'name',
|
|
},
|
|
express_code: {
|
|
field: 'express_code',
|
|
},
|
|
quickWhere: {
|
|
bucket: 'quick',
|
|
},
|
|
},
|
|
});
|
|
|
|
try {
|
|
const result = await service.systems.setting.express.list(payload);
|
|
return this.success('ok', result);
|
|
} catch (error) {
|
|
return this.fail(error.message || '获取快递映射列表失败', {
|
|
code: Number(error.code) || 500,
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取系统快递编码和名称列表。
|
|
*
|
|
* @return {Promise<Object>} 基础快递列表结果
|
|
*/
|
|
async listBase() {
|
|
return await this.callSystemExpress('listBase', {}, '获取系统快递基础列表失败');
|
|
}
|
|
|
|
/**
|
|
* 创建快递映射。
|
|
*
|
|
* @param {Object} data 请求参数
|
|
* @return {Promise<Object>} 创建结果
|
|
*/
|
|
async create(data = {}) {
|
|
const { code, name, express_code: expressCode, expressCode: camelExpressCode, ...rest } = data || {};
|
|
const payload = {
|
|
...rest,
|
|
code,
|
|
name,
|
|
express_code: expressCode !== undefined ? expressCode : camelExpressCode,
|
|
};
|
|
|
|
return await this.callSystemExpress('create', payload, '创建快递映射失败');
|
|
}
|
|
|
|
/**
|
|
* 校验快递编码是否已被占用。
|
|
*
|
|
* @param {Object} data 请求参数
|
|
* @return {Promise<Object>} 校验结果
|
|
*/
|
|
async checkCodeUnique(data = {}) {
|
|
const { id, code, ...rest } = data || {};
|
|
const payload = {
|
|
...rest,
|
|
id,
|
|
code,
|
|
};
|
|
|
|
return await this.callSystemExpress('checkCodeUnique', payload, '校验快递编码失败');
|
|
}
|
|
|
|
/**
|
|
* 修改快递映射。
|
|
*
|
|
* @param {Object} data 请求参数
|
|
* @return {Promise<Object>} 修改结果
|
|
*/
|
|
async update(data = {}) {
|
|
const { id, code, name, express_code: expressCode, expressCode: camelExpressCode, ...rest } = data || {};
|
|
const payload = {
|
|
...rest,
|
|
id,
|
|
code,
|
|
name,
|
|
express_code: expressCode !== undefined ? expressCode : camelExpressCode,
|
|
};
|
|
|
|
return await this.callSystemExpress('update', payload, '更新快递映射失败');
|
|
}
|
|
}
|
|
|
|
module.exports = expressService;
|