'use strict'; const shopController = require('./shop.js'); // 用户地址控制器 module.exports = class AddressController extends shopController { // 使用模型 get useModel() { const that = this; return that.app.model.Address; } /** * [selectValidate 查询用户地址] * @return {[type]} [description] */ get selectValidate() { const that = this; return { is_use: that.ctx.rules.default(0) .required() .number(), user_id: that.ctx.rules.default(that.service.shop.getWebUserId()) .number(), }; } /** * [createValidate 添加收货地址] * @return {[type]} [description] */ get createValidate() { const that = this; return { user_id: that.ctx.rules.default(that.service.shop.getWebUserId()) .number(), uname: that.ctx.rules.name('收货人姓名') .required() .notEmpty() .trim(), mobile: that.ctx.rules.name('收货人电话') .required() .notEmpty() .trim() .phone(), province_name: that.ctx.rules.name('所在省') .required() .notEmpty() .trim(), province_id: that.ctx.rules.name('所在省ID') .required() .notEmpty() .number(), city_id: that.ctx.rules.name('所在市ID') .required() .notEmpty() .number(), city_name: that.ctx.rules.name('所在城市') .required() .notEmpty() .trim(), county_id: that.ctx.rules.name('所在区/县ID') .required() .notEmpty() .number(), county_name: that.ctx.rules.name('所在区/县') .required() .notEmpty() .trim(), address: that.ctx.rules.name('详细地址') .required() .notEmpty() .trim(), is_use: that.ctx.rules.default(0) .number(), create_time: that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s')) .required(), }; } /** * [updateValidate 更新收货地址] * @return {[type]} [description] */ get updateValidate() { const that = this; return { user_id: that.ctx.rules.default(that.service.shop.getWebUserId()) .number(), address_id: that.ctx.rules.name('收货地址ID') .required() .notEmpty() .number(), }; } /** * [deleteValidate 删除收货地址] * @return {[type]} [description] */ get deleteValidate() { const that = this; return { user_id: that.ctx.rules.default(that.service.shop.getWebUserId()) .number(), address_id: that.ctx.rules.name('收货地址ID') .required() .notEmpty() .number(), }; } /** * [select 用户收货地址列表] * @return {[type]} [description] */ async select() { const that = this; try { const data = await that.ctx.validate(that.selectValidate, await that.ctx.getParse()); const options = { where: { user_id: data.user_id } }; const all = !data.is_use; if (data.is_use) options.where.is_use = data.is_use; const selectBean = await that.app.comoBean.instance(data, options); const result = await that.service.base.select(selectBean, that.useModel, '收货地址查询失败,请稍候重试', all, all); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false)); } catch (err) { return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', false, false)); } } /** * [create 添加收货地址] * @return {[type]} [description] */ async create() { const that = this; try { const data = await that.ctx.validate(that.createValidate, await that.ctx.postParse()); const createBean = await that.app.comoBean.instance(data); const result = await that.service.base.create(createBean, that.useModel, '添加收货地址失败,请重试'); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false)); } catch (err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [update 更新收货地址] * @return {[type]} [description] */ async update() { const that = this; try { const data = await that.ctx.validate(that.updateValidate, await that.ctx.anyParse()); if (data.fore) { const updateBean = await that.app.comoBean.instance({ is_use: 0, update_time: that.app.szjcomo.date('Y-m-d H:i:s'), }, { where: { user_id: data.user_id } }); await that.service.base.update(updateBean, that.useModel, '更新所有收货地址失败,请重试'); } const options = { where: { user_id: data.user_id, address_id: data.address_id } }; const updateBean = await that.app.comoBean.instance(data, options); const result = await that.service.base.update(updateBean, that.useModel, '更新收货地址失败,请稍候重试'); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false)); } catch (err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [delete 删除收货地址] * @return {[type]} [description] */ async delete() { const that = this; try { const data = await that.ctx.validate(that.deleteValidate, await that.ctx.anyParse()); const options = { where: { user_id: data.user_id, address_id: data.address_id } }; const deleteBean = await that.app.comoBean.instance(data, options); const result = await that.service.base.delete(deleteBean, that.useModel, '删除收货地址失败,请稍候重试'); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false)); } catch (err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } };