matchSign.js 7.5 KB

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