'use strict'; const MainService = require('../main'); /** * 店铺 methods 出口。 */ class ShopsMethodService extends MainService { /** * 初始化店铺模块鉴权配置。 * * @param {Egg.Context} ctx 上下文对象 */ constructor(ctx) { super(ctx); const _noNeedLogin = ''; const _noNeedRight = ''; const _noNeedLog = '*'; const _noNeedAuth = ''; this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth); } /** * 统一调用店铺业务服务。 * * @param {string} methodName 业务方法名 * @param {Object} data 请求参数 * @param {string} fallbackMessage 默认错误文案 * @return {Promise} methods 标准返回 */ async callShopsSystem(methodName, data, fallbackMessage) { const { service } = this; try { const result = await service.systems.shops[methodName](data); return this.success('ok', { data: result, code: 200 }); } catch (error) { return this.fail(error.message || fallbackMessage, { code: Number(error.code) || 500, }); } } /** * shops.list:分页查询店铺。 * * @param {Object} [data={}] 请求参数 * @return {Promise} 查询结果 */ async list(data = {}) { const payload = { ...(data && typeof data === 'object' ? data : {}), }; this.prepareWhereContext(payload, { specialFieldMap: this.service.systems.shops.getListWhereSpecialFieldMap(), }); return await this.callShopsSystem('list', payload, '查询店铺列表失败'); } /** * shops.add:新增店铺。 * * @param {Object} [data={}] 请求参数 * @return {Promise} 新增结果 */ async add(data = {}) { const { custom_name: customName, customName: customNameCamel, platform_code: platformCode, platformCode: platformCodeCamel, } = data || {}; return await this.callShopsSystem('add', { custom_name: customName !== undefined ? customName : customNameCamel, platform_code: platformCode !== undefined ? platformCode : platformCodeCamel, }, '新增店铺失败'); } /** * shops.del:软删除店铺。 * * @param {Object} [data={}] 请求参数 * @return {Promise} 删除结果 */ async del(data = {}) { const { id, shop_id: shopId, shopId: shopIdCamel, } = data || {}; return await this.callShopsSystem('del', { id: id !== undefined ? id : (shopId !== undefined ? shopId : shopIdCamel), }, '删除店铺失败'); } /** * shops.edit:编辑店铺。 * * @param {Object} [data={}] 请求参数 * @return {Promise} 编辑结果 */ async edit(data = {}) { const { id, shop_id: shopId, shopId: shopIdCamel, shop_name: shopName, shopName: shopNameCamel, custom_name: customName, customName: customNameCamel, platform_code: platformCode, platformCode: platformCodeCamel, } = data || {}; const payload = { id: id !== undefined ? id : (shopId !== undefined ? shopId : shopIdCamel), }; if (shopName !== undefined || shopNameCamel !== undefined) { payload.shop_name = shopName !== undefined ? shopName : shopNameCamel; } if (customName !== undefined || customNameCamel !== undefined) { payload.custom_name = customName !== undefined ? customName : customNameCamel; } if (platformCode !== undefined || platformCodeCamel !== undefined) { payload.platform_code = platformCode !== undefined ? platformCode : platformCodeCamel; } return await this.callShopsSystem('edit', payload, '编辑店铺失败'); } /** * shop.GetAuthorizeSessionKey:按店铺平台生成授权会话。 * * @param {Object} [data={}] 请求参数 * @return {Promise} 会话结果 */ async GetAuthorizeSessionKey(data = {}) { const { id, shop_id: shopId, shopId: shopIdCamel, callbackurl, callback_url: callbackUrlSnake, callbackUrl, } = data || {}; return await this.callShopsSystem('getAuthorizeSessionKey', { id: id !== undefined ? id : (shopId !== undefined ? shopId : shopIdCamel), callbackurl: callbackurl !== undefined ? callbackurl : (callbackUrlSnake !== undefined ? callbackUrlSnake : callbackUrl), }, '获取授权会话失败'); } } module.exports = ShopsMethodService;