matchSign.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. const moment = require('moment');
  7. //
  8. class MatchSignService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'matchsign');
  11. this.model = this.ctx.model.Race.MatchSign;
  12. this.matchModel = this.ctx.model.Race.Match;
  13. this.matchGroupModel = this.ctx.model.Race.MatchGroup;
  14. this.matchProjectModel = this.ctx.model.Race.MatchProject;
  15. this.billModel = this.ctx.model.Race.Bill;
  16. }
  17. /**
  18. * 检查是否已经有该赛事,该组别,该项目的报名
  19. * @param {Object} body 参数体
  20. */
  21. async beforeCreate(body) {
  22. await this.checkHas(body);
  23. return body;
  24. }
  25. async beforeUpdate(filter, update) {
  26. // 比赛结束后不能退款
  27. return { filter, update };
  28. }
  29. // 退赛
  30. async afterUpdate(filter, body, data) {
  31. const { pay_status } = data;
  32. if (pay_status !== '-3') return data;
  33. // 线下管理人员允许退款,生成账单
  34. const obj = _.pick(data, [ 'match_id', 'group_id', 'project_id' ]);
  35. obj.payer_id = data.user_id;
  36. obj.time = moment().format('YYYY-MM-DD HH:mm:ss');
  37. obj.type = '-1';
  38. await this.billModel.create(obj);
  39. return data;
  40. }
  41. /**
  42. * 检查有没有报过名
  43. * @param {Object} body 参数体
  44. * @return {Boolean} true 可以继续进行
  45. */
  46. async checkHas(body) {
  47. const { match_id, group_id, project_id, user_id } = body;
  48. const query = { match_id, group_id, project_id, user_id, pay_status: [ '0', '1' ] };
  49. const num = await this.model.count(query);
  50. if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '您已报名');
  51. return true;
  52. }
  53. async checkFit(body) {
  54. const { match_id, group_id, project_id, user_id, card } = body;
  55. await this.checkHas(body);
  56. const { pass, msg, gender: cardGender, age: cardAge } = this.idCodeValid(card);
  57. if (!pass) throw new BusinessError(ErrorCode.SERVICE_FAULT, msg);
  58. // 1.看 match 状态 是否为报名中
  59. const mnum = await this.matchModel.count({ _id: match_id, status: '1' });
  60. if (mnum <= 0) throw new BusinessError(ErrorCode.DATA_INVALID, '当前赛事不处于报名期');
  61. // 2.看matchGroup 年龄限制是否符合要求
  62. const matchGroup = await this.matchGroupModel.findById(group_id);
  63. if (!matchGroup) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到当前组别');
  64. const { age } = matchGroup;
  65. let next = this.checkAgeInRange(age, cardAge);
  66. // 3.看 matchProject 年龄限制和性别限制,人数限制
  67. if (!next) throw new BusinessError(ErrorCode.DATA_INVALID, '年龄不符合组别条件');
  68. const matchProject = await this.matchProjectModel.findById(project_id);
  69. if (!matchProject) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到组别项目');
  70. const { age: projectAge, gender } = matchProject;
  71. next = this.checkAgeInRange(projectAge, cardAge);
  72. if (!next) throw new BusinessError(ErrorCode.DATA_INVALID, '年龄不符合组别项目条件');
  73. if (gender !== '2') {
  74. if (gender !== cardGender) throw new BusinessError(ErrorCode.DATA_INVALID, '性别不符合组别条件');
  75. }
  76. return true;
  77. }
  78. checkAgeInRange(age, cardAge) {
  79. let next = true;
  80. if (_.isString(age) && age.indexOf('-') > -1) {
  81. const arr = age.split('-');
  82. const start = parseInt(_.head(arr));
  83. const end = parseInt(_.last(arr));
  84. // inRange 是左闭右开区间,所以加1,形成闭区间
  85. const r = _.inRange(cardAge, start, end + 1);
  86. next = r;
  87. } else if (!age || age === '不限') next = true;
  88. return next;
  89. }
  90. // 身份证验证
  91. idCodeValid(code) {
  92. // 身份证号合法性验证
  93. // 支持15位和18位身份证号
  94. // 支持地址编码、出生日期、校验位验证
  95. const city = {
  96. 11: '北京',
  97. 12: '天津',
  98. 13: '河北',
  99. 14: '山西',
  100. 15: '内蒙古',
  101. 21: '辽宁',
  102. 22: '吉林',
  103. 23: '黑龙江 ',
  104. 31: '上海',
  105. 32: '江苏',
  106. 33: '浙江',
  107. 34: '安徽',
  108. 35: '福建',
  109. 36: '江西',
  110. 37: '山东',
  111. 41: '河南',
  112. 42: '湖北 ',
  113. 43: '湖南',
  114. 44: '广东',
  115. 45: '广西',
  116. 46: '海南',
  117. 50: '重庆',
  118. 51: '四川',
  119. 52: '贵州',
  120. 53: '云南',
  121. 54: '西藏 ',
  122. 61: '陕西',
  123. 62: '甘肃',
  124. 63: '青海',
  125. 64: '宁夏',
  126. 65: '新疆',
  127. 71: '台湾',
  128. 81: '香港',
  129. 82: '澳门',
  130. 91: '国外 ',
  131. };
  132. let row = {
  133. pass: true,
  134. msg: '验证成功',
  135. };
  136. if (!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|[xX])$/.test(code)) {
  137. row = {
  138. pass: false,
  139. msg: '身份证号格式错误',
  140. };
  141. } else if (!city[code.substr(0, 2)]) {
  142. row = {
  143. pass: false,
  144. msg: '身份证号地址编码错误',
  145. };
  146. }
  147. // else {
  148. // // 18位身份证需要验证最后一位校验位
  149. // if (code.length === 18) {
  150. // code = code.split('');
  151. // // ∑(ai×Wi)(mod 11)
  152. // // 加权因子
  153. // const factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
  154. // // 校验位
  155. // const parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
  156. // let sum = 0;
  157. // let ai = 0;
  158. // let wi = 0;
  159. // for (let i = 0; i < 17; i++) {
  160. // ai = code[i];
  161. // wi = factor[i];
  162. // sum += ai * wi;
  163. // }
  164. // if (parity[sum % 11] !== code[17].toUpperCase()) {
  165. // row = {
  166. // pass: false,
  167. // msg: '身份证号校验位错误',
  168. // };
  169. // }
  170. // }
  171. // }
  172. if (row.pass) {
  173. row.gender = this.maleOrFemalByIdCard(code);
  174. row.age = this.getAge(code);
  175. }
  176. return row;
  177. }
  178. // 性别
  179. maleOrFemalByIdCard(idCard) {
  180. if (_.isArray(idCard)) idCard = idCard.join('');
  181. idCard = _.trim(idCard.replace(/ /g, '')); // 对身份证号码做处理。包括字符间有空格。
  182. if (idCard.length === 15) {
  183. if (idCard.substring(14, 15) % 2 === 0) {
  184. return '1';
  185. }
  186. return '0';
  187. } else if (idCard.length === 18) {
  188. if (idCard.substring(14, 17) % 2 === 0) {
  189. return '1';
  190. }
  191. return '0';
  192. }
  193. return null;
  194. }
  195. // 年龄
  196. getAge(idCard) {
  197. if (_.isArray(idCard)) idCard = idCard.join('');
  198. const ageDate = new Date();
  199. const month = ageDate.getMonth() + 1;
  200. const day = ageDate.getDate();
  201. let age = ageDate.getFullYear() - idCard.substring(6, 10) - 1;
  202. if (idCard.substring(10, 12) < month || (idCard.substring(10, 12) === month && idCard.substring(12, 14) <= day)) {
  203. age++;
  204. }
  205. if (age <= 0) {
  206. age = 1;
  207. }
  208. return age;
  209. }
  210. }
  211. module.exports = MatchSignService;