ProxyApplyLogs.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. 'use strict';
  2. const shopController = require('./shop.js');
  3. // 用户申请代理记录
  4. module.exports = class ProxyApplyLogsController extends shopController {
  5. // 使用模型
  6. get useModel() {
  7. const that = this;
  8. return that.app.model.ProxyApplyLogs;
  9. }
  10. /**
  11. * [selectValidate 查询验证器]
  12. * @return {{user_id: *}} [description]
  13. */
  14. get selectValidate() {
  15. const that = this;
  16. return {
  17. user_id: that.ctx.rules.default(that.service.shop.getWebUserId())
  18. .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())
  29. .number(),
  30. real_name: that.ctx.rules.name('真实姓名')
  31. .required()
  32. .notEmpty()
  33. .trim(),
  34. phone_num: that.ctx.rules.name('电话号码')
  35. .required()
  36. .notEmpty()
  37. .trim()
  38. .phone(),
  39. phone_sms: that.ctx.rules.name('短信验证码')
  40. .required()
  41. .notEmpty()
  42. .trim(),
  43. id_num: that.ctx.rules.name('身份证号码')
  44. .required()
  45. .notEmpty()
  46. .trim(),
  47. province_name: that.ctx.rules.name('所在省')
  48. .required()
  49. .notEmpty()
  50. .trim(),
  51. province_id: that.ctx.rules.name('所在省ID')
  52. .required()
  53. .notEmpty()
  54. .number(),
  55. city_id: that.ctx.rules.name('所在市ID')
  56. .required()
  57. .notEmpty()
  58. .number(),
  59. city_name: that.ctx.rules.name('所在城市')
  60. .required()
  61. .notEmpty()
  62. .trim(),
  63. county_id: that.ctx.rules.name('所在区/县ID')
  64. .required()
  65. .notEmpty()
  66. .number(),
  67. county_name: that.ctx.rules.name('所在区/县')
  68. .required()
  69. .notEmpty()
  70. .trim(),
  71. address: that.ctx.rules.name('详细地址')
  72. .required()
  73. .notEmpty()
  74. .trim(),
  75. admin_id: that.ctx.rules.default(-1)
  76. .number(),
  77. verify_status: that.ctx.rules.default(-1)
  78. .number(),
  79. create_time: that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s'))
  80. .required(),
  81. };
  82. }
  83. /**
  84. * [updateValidate 更新验证器]
  85. * @return {[type]} [description]
  86. */
  87. get updateValidate() {
  88. const that = this;
  89. return {
  90. user_id: that.ctx.rules.default(that.service.shop.getWebUserId())
  91. .number(),
  92. proxy_apply_id: that.ctx.rules.name('代理申请记录ID')
  93. .required()
  94. .notEmpty()
  95. .number(),
  96. update_time: that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s'))
  97. .required(),
  98. };
  99. }
  100. /**
  101. * [deleteValidate 删除验证器]
  102. * @return {[type]} [description]
  103. */
  104. get deleteValidate() {
  105. const that = this;
  106. return {
  107. user_id: that.ctx.rules.default(that.service.shop.getWebUserId())
  108. .number(),
  109. proxy_apply_id: that.ctx.rules.name('代理申请记录ID')
  110. .required()
  111. .notEmpty()
  112. .number(),
  113. };
  114. }
  115. /**
  116. * [select 查询代理申请记录]
  117. * @return {[type]} [description]
  118. */
  119. async select() {
  120. const that = this;
  121. try {
  122. const data = await that.ctx.validate(that.selectValidate, await that.ctx.getParse());
  123. const options = { where: { user_id: data.user_id } };
  124. const selectBean = await that.app.comoBean.instance(data, options);
  125. const result = await that.service.base.select(selectBean, that.useModel, '代理认证记录查询失败,请稍候重试', false, false);
  126. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  127. } catch (err) {
  128. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', false, false));
  129. }
  130. }
  131. /**
  132. * [create 添加代理申请记录]
  133. * 记录:一个用户只有一条
  134. * @return {[type]} [description]
  135. */
  136. async create() {
  137. const that = this;
  138. try {
  139. const data = await that.ctx.validate(that.createValidate, await that.ctx.postParse());
  140. const smsCode = data.phone_sms;
  141. const smsRedis = await that.service.redis.get(data.phone_num);
  142. // eslint-disable-next-line eqeqeq
  143. if (smsCode != smsRedis) {
  144. throw new Error('短信验证码错误');
  145. }
  146. // 2022/12/13 短信验证码通过直接过审
  147. data.verify_status = 1;
  148. const createBean = await that.app.comoBean.instance(data);
  149. const selectOptions = { where: { user_id: data.user_id } };
  150. const selectResult = await that.useModel.findOne(selectOptions);
  151. let result;
  152. if (selectResult) {
  153. const updateBean = await that.app.comoBean.instance(data, {
  154. where: {
  155. user_id: data.user_id,
  156. proxy_apply_id: selectResult.proxy_apply_id,
  157. },
  158. });
  159. result = await that.service.base.update(updateBean, that.useModel, '更新所有代理申请记录失败,请稍后重试');
  160. } else {
  161. result = await that.service.base.create(createBean, that.useModel, '代理认证申请信息上传失败,请稍后重试');
  162. }
  163. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  164. } catch (err) {
  165. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  166. }
  167. }
  168. /**
  169. * [update 更新代理申请记录]
  170. * @return {[type]} [description]
  171. */
  172. async update() {
  173. const that = this;
  174. try {
  175. const data = await that.ctx.validate(that.updateValidate, await that.ctx.anyParse());
  176. const options = { where: { user_id: data.user_id, proxy_apply_id: data.proxy_apply_id } };
  177. const updateBean = await that.app.comoBean.instance(data, options);
  178. const result = await that.service.base.update(updateBean, that.useModel, '更新代理申请记录失败,请稍候重试');
  179. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  180. } catch (err) {
  181. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  182. }
  183. }
  184. /**
  185. * [delete 删除代理申请记录]
  186. * @return {[type]} [description]
  187. */
  188. async delete() {
  189. const that = this;
  190. try {
  191. const data = await that.ctx.validate(that.deleteValidate, await that.ctx.anyParse());
  192. const options = { where: { user_id: data.user_id, proxy_apply_id: data.proxy_apply_id } };
  193. const deleteBean = await that.app.comoBean.instance(data, options);
  194. const result = await that.service.base.delete(deleteBean, that.useModel, '删除代理申请记录失败,请稍候重试');
  195. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  196. } catch (err) {
  197. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  198. }
  199. }
  200. /**
  201. * 发送短信验证码
  202. * @return {Promise<boolean|*>}
  203. */
  204. async phoneSMS() {
  205. const that = this;
  206. try {
  207. const data = await that.ctx.getParse();
  208. const mobile = data.phone_num;
  209. // const oldCode = await that.service.redis.get(mobile);
  210. const keys = [ 'sms_domain', 'sms_identity', 'sms_template_id', 'sms_sign' ];
  211. const randCode = that.app.szjcomo.mt_rand(1000, 9999);
  212. await that.service.redis.set(mobile, randCode, 10 * 60);
  213. const config = await that.service.configs.getConfigMoreValue(keys);
  214. if (!Number(config.sms_template_id)) return false;
  215. const bodyData = {
  216. mobile,
  217. identity: config.sms_identity,
  218. params: [ randCode ],
  219. templateId: config.sms_template_id,
  220. sign: config.sms_sign,
  221. };
  222. const response = await that.app.curl(config.sms_domain, {
  223. method: 'POST', dataType: 'json', contentType: 'json', data: bodyData,
  224. });
  225. /**
  226. * response.data
  227. * message: 'SUCCESS',
  228. * result: '5tdf5k4kQj',
  229. * result_code: 0,
  230. * error: false
  231. */
  232. if (response.data.error !== false) throw new Error(response.data.message);
  233. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', response.data, false));
  234. } catch (err) {
  235. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  236. }
  237. }
  238. };