address.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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)
  18. .required()
  19. .number(),
  20. user_id: that.ctx.rules.default(that.service.shop.getWebUserId())
  21. .number(),
  22. };
  23. }
  24. /**
  25. * [createValidate 添加收货地址]
  26. * @return {[type]} [description]
  27. */
  28. get createValidate() {
  29. const that = this;
  30. return {
  31. user_id: that.ctx.rules.default(that.service.shop.getWebUserId())
  32. .number(),
  33. uname: that.ctx.rules.name('收货人姓名')
  34. .required()
  35. .notEmpty()
  36. .trim(),
  37. mobile: that.ctx.rules.name('收货人电话')
  38. .required()
  39. .notEmpty()
  40. .trim()
  41. .phone(),
  42. province_name: that.ctx.rules.name('所在省')
  43. .required()
  44. .notEmpty()
  45. .trim(),
  46. province_id: that.ctx.rules.name('所在省ID')
  47. .required()
  48. .notEmpty()
  49. .number(),
  50. city_id: that.ctx.rules.name('所在市ID')
  51. .required()
  52. .notEmpty()
  53. .number(),
  54. city_name: that.ctx.rules.name('所在城市')
  55. .required()
  56. .notEmpty()
  57. .trim(),
  58. county_id: that.ctx.rules.name('所在区/县ID')
  59. .required()
  60. .notEmpty()
  61. .number(),
  62. county_name: that.ctx.rules.name('所在区/县')
  63. .required()
  64. .notEmpty()
  65. .trim(),
  66. address: that.ctx.rules.name('详细地址')
  67. .required()
  68. .notEmpty()
  69. .trim(),
  70. is_use: that.ctx.rules.default(0)
  71. .number(),
  72. create_time: that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s'))
  73. .required(),
  74. };
  75. }
  76. /**
  77. * [updateValidate 更新收货地址]
  78. * @return {[type]} [description]
  79. */
  80. get updateValidate() {
  81. const that = this;
  82. return {
  83. user_id: that.ctx.rules.default(that.service.shop.getWebUserId())
  84. .number(),
  85. address_id: that.ctx.rules.name('收货地址ID')
  86. .required()
  87. .notEmpty()
  88. .number(),
  89. };
  90. }
  91. /**
  92. * [deleteValidate 删除收货地址]
  93. * @return {[type]} [description]
  94. */
  95. get deleteValidate() {
  96. const that = this;
  97. return {
  98. user_id: that.ctx.rules.default(that.service.shop.getWebUserId())
  99. .number(),
  100. address_id: that.ctx.rules.name('收货地址ID')
  101. .required()
  102. .notEmpty()
  103. .number(),
  104. };
  105. }
  106. /**
  107. * [select 用户收货地址列表]
  108. * @return {[type]} [description]
  109. */
  110. async select() {
  111. const that = this;
  112. try {
  113. const data = await that.ctx.validate(that.selectValidate, await that.ctx.getParse());
  114. const options = { where: { user_id: data.user_id } };
  115. const all = !data.is_use;
  116. if (data.is_use) options.where.is_use = data.is_use;
  117. const selectBean = await that.app.comoBean.instance(data, options);
  118. const result = await that.service.base.select(selectBean, that.useModel, '收货地址查询失败,请稍候重试', all, all);
  119. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  120. } catch (err) {
  121. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', false, false));
  122. }
  123. }
  124. /**
  125. * [create 添加收货地址]
  126. * @return {[type]} [description]
  127. */
  128. async create() {
  129. const that = this;
  130. try {
  131. const data = await that.ctx.validate(that.createValidate, await that.ctx.postParse());
  132. const createBean = await that.app.comoBean.instance(data);
  133. const result = await that.service.base.create(createBean, that.useModel, '添加收货地址失败,请重试');
  134. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  135. } catch (err) {
  136. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  137. }
  138. }
  139. /**
  140. * [update 更新收货地址]
  141. * @return {[type]} [description]
  142. */
  143. async update() {
  144. const that = this;
  145. try {
  146. const data = await that.ctx.validate(that.updateValidate, await that.ctx.anyParse());
  147. if (data.fore) {
  148. const updateBean = await that.app.comoBean.instance({
  149. is_use: 0,
  150. update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  151. }, { where: { user_id: data.user_id } });
  152. await that.service.base.update(updateBean, that.useModel, '更新所有收货地址失败,请重试');
  153. }
  154. const options = { where: { user_id: data.user_id, address_id: data.address_id } };
  155. const updateBean = await that.app.comoBean.instance(data, options);
  156. const result = await that.service.base.update(updateBean, that.useModel, '更新收货地址失败,请稍候重试');
  157. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  158. } catch (err) {
  159. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  160. }
  161. }
  162. /**
  163. * [delete 删除收货地址]
  164. * @return {[type]} [description]
  165. */
  166. async delete() {
  167. const that = this;
  168. try {
  169. const data = await that.ctx.validate(that.deleteValidate, await that.ctx.anyParse());
  170. const options = { where: { user_id: data.user_id, address_id: data.address_id } };
  171. const deleteBean = await that.app.comoBean.instance(data, options);
  172. const result = await that.service.base.delete(deleteBean, that.useModel, '删除收货地址失败,请稍候重试');
  173. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  174. } catch (err) {
  175. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  176. }
  177. }
  178. };