address.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. 'use strict';
  2. const shopController = require('./shop.js');
  3. // 用户地址控制器
  4. module.exports = class AddressController extends shopController {
  5. // 使用模型
  6. get useModel() {
  7. const that = this;
  8. return that.app.model.Address;
  9. }
  10. /**
  11. * [selectValidate 查询用户地址]
  12. * @return {[type]} [description]
  13. */
  14. get selectValidate() {
  15. const that = this;
  16. return {
  17. is_use: that.ctx.rules.default(0).required().number(),
  18. user_id: that.ctx.rules.default(that.service.shop.getWebUserId()).number(),
  19. };
  20. }
  21. /**
  22. * [createValidate 添加收货地址]
  23. * @return {[type]} [description]
  24. */
  25. get createValidate() {
  26. const that = this;
  27. return {
  28. user_id: that.ctx.rules.default(that.service.shop.getWebUserId()).number(),
  29. uname: that.ctx.rules.name('收货人姓名').required().notEmpty()
  30. .trim(),
  31. mobile: that.ctx.rules.name('收货人电话').required().notEmpty()
  32. .trim()
  33. .phone(),
  34. province_name: that.ctx.rules.name('所在省').required().notEmpty()
  35. .trim(),
  36. province_id: that.ctx.rules.name('所在省ID').required().notEmpty()
  37. .number(),
  38. city_id: that.ctx.rules.name('所在市ID').required().notEmpty()
  39. .number(),
  40. city_name: that.ctx.rules.name('所在城市').required().notEmpty()
  41. .trim(),
  42. county_id: that.ctx.rules.name('所在区/县ID').required().notEmpty()
  43. .number(),
  44. county_name: that.ctx.rules.name('所在区/县').required().notEmpty()
  45. .trim(),
  46. address: that.ctx.rules.name('详细地址').required().notEmpty()
  47. .trim(),
  48. is_use: that.ctx.rules.default(0).number(),
  49. create_time: that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s')).required(),
  50. };
  51. }
  52. /**
  53. * [updateValidate 更新收货地址]
  54. * @return {[type]} [description]
  55. */
  56. get updateValidate() {
  57. const that = this;
  58. return {
  59. user_id: that.ctx.rules.default(that.service.shop.getWebUserId()).number(),
  60. address_id: that.ctx.rules.name('收货地址ID').required().notEmpty()
  61. .number(),
  62. };
  63. }
  64. /**
  65. * [deleteValidate 删除收货地址]
  66. * @return {[type]} [description]
  67. */
  68. get deleteValidate() {
  69. const that = this;
  70. return {
  71. user_id: that.ctx.rules.default(that.service.shop.getWebUserId()).number(),
  72. address_id: that.ctx.rules.name('收货地址ID').required().notEmpty()
  73. .number(),
  74. };
  75. }
  76. /**
  77. * [select 用户收货地址列表]
  78. * @return {[type]} [description]
  79. */
  80. async select() {
  81. const that = this;
  82. try {
  83. const data = await that.ctx.validate(that.selectValidate, await that.ctx.getParse());
  84. const options = { where: { user_id: data.user_id } };
  85. const all = !data.is_use;
  86. if (data.is_use) options.where.is_use = data.is_use;
  87. const selectBean = await that.app.comoBean.instance(data, options);
  88. const result = await that.service.base.select(selectBean, that.useModel, '收货地址查询失败,请稍候重试', all, all);
  89. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  90. } catch (err) {
  91. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', false, false));
  92. }
  93. }
  94. /**
  95. * [create 添加收货地址]
  96. * @return {[type]} [description]
  97. */
  98. async create() {
  99. const that = this;
  100. try {
  101. const data = await that.ctx.validate(that.createValidate, await that.ctx.postParse());
  102. const createBean = await that.app.comoBean.instance(data);
  103. const result = await that.service.base.create(createBean, that.useModel, '添加收货地址失败,请重试');
  104. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  105. } catch (err) {
  106. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  107. }
  108. }
  109. /**
  110. * [update 更新收货地址]
  111. * @return {[type]} [description]
  112. */
  113. async update() {
  114. const that = this;
  115. try {
  116. const data = await that.ctx.validate(that.updateValidate, await that.ctx.anyParse());
  117. if (data.fore) {
  118. const updateBean = await that.app.comoBean.instance({
  119. is_use: 0,
  120. update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  121. }, { where: { user_id: data.user_id } });
  122. await that.service.base.update(updateBean, that.useModel, '更新所有收货地址失败,请重试');
  123. }
  124. const options = { where: { user_id: data.user_id, address_id: data.address_id } };
  125. const updateBean = await that.app.comoBean.instance(data, options);
  126. const result = await that.service.base.update(updateBean, that.useModel, '更新收货地址失败,请稍候重试');
  127. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  128. } catch (err) {
  129. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  130. }
  131. }
  132. /**
  133. * [delete 删除收货地址]
  134. * @return {[type]} [description]
  135. */
  136. async delete() {
  137. const that = this;
  138. try {
  139. const data = await that.ctx.validate(that.deleteValidate, await that.ctx.anyParse());
  140. const options = { where: { user_id: data.user_id, address_id: data.address_id } };
  141. const deleteBean = await that.app.comoBean.instance(data, options);
  142. const result = await that.service.base.delete(deleteBean, that.useModel, '删除收货地址失败,请稍候重试');
  143. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  144. } catch (err) {
  145. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  146. }
  147. }
  148. };