90 lines
2.7 KiB
JavaScript

'use strict';
const MainService = require('../../main');
/**
* 规范签约请求入参。
*
* @param {Object} data 请求数据。
* @return {Object} 复制后的请求数据。
*/
function normalizeWarehouseStorePayload(data) {
if (!data || typeof data !== 'object' || Array.isArray(data)) {
return {};
}
return { ...data };
}
class StoreWarehouseWarehouseService extends MainService {
constructor(ctx) {
super(ctx);
const _noNeedLogin = '';
const _noNeedRight = 'list,sign,confirmSign';
const _noNeedLog = '*';
const _noNeedAuth = 'list,sign,confirmSign';
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
}
/**
* 仓库侧获取签约列表(含外部合作仓库)。
*
* @param {Object} data 请求数据。
* @return {Promise<Object>} 统一响应结果。
*/
async list(data = {}) {
const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse;
if (!targetService || typeof targetService.listForWarehouse !== 'function') {
return this.fail('获取签约列表失败', { code: 500 });
}
try {
const payload = normalizeWarehouseStorePayload(data);
const result = await targetService.listForWarehouse(payload);
return this.success('ok', result);
} catch (error) {
return this.fail(error.message || '获取签约列表失败', {
code: Number(error.code) || 500,
});
}
}
/**
* 仓库侧发起签约。
* @param data
*/
async sign(data = {}) {
const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse;
if (!targetService || typeof targetService.sign !== 'function') {
return this.fail('签约失败', { code: 500 });
}
try {
const payload = normalizeWarehouseStorePayload(data);
const result = await targetService.sign(payload);
return this.success('ok', result);
} catch (error) {
return this.fail(error.message || '签约失败', { code: Number(error.code) || 500 });
}
}
/**
* 仓库侧确认签约。
* @param data
*/
async confirmSign(data = {}) {
const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse;
if (!targetService || typeof targetService.confirmSign !== 'function') {
return this.fail('确认签约失败', { code: 500 });
}
try {
const payload = normalizeWarehouseStorePayload(data);
const result = await targetService.confirmSign(payload);
return this.success('ok', result);
} catch (error) {
return this.fail(error.message || '确认签约失败', { code: Number(error.code) || 500 });
}
}
}
module.exports = StoreWarehouseWarehouseService;