matchSign.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 } = matchProject;
  84. next = this.checkAgeInRange(projectAge, cardAge);
  85. if (!next) throw new BusinessError(ErrorCode.DATA_INVALID, '年龄不符合组别项目条件');
  86. if (gender !== '2') {
  87. if (gender !== cardGender) throw new BusinessError(ErrorCode.DATA_INVALID, '性别不符合组别条件');
  88. }
  89. return true;
  90. }
  91. checkAgeInRange(age, cardAge) {
  92. let next = true;
  93. if (_.isString(age) && age.indexOf('-') > -1) {
  94. const arr = age.split('-');
  95. const start = parseInt(_.head(arr));
  96. const end = parseInt(_.last(arr));
  97. // inRange 是左闭右开区间,所以加1,形成闭区间
  98. const r = _.inRange(cardAge, start, end + 1);
  99. next = r;
  100. } else if (!age || age === '不限') next = true;
  101. return next;
  102. }
  103. // 身份证验证
  104. idCodeValid(code) {
  105. // 身份证号合法性验证
  106. // 支持15位和18位身份证号
  107. // 支持地址编码、出生日期、校验位验证
  108. const city = {
  109. 11: '北京',
  110. 12: '天津',
  111. 13: '河北',
  112. 14: '山西',
  113. 15: '内蒙古',
  114. 21: '辽宁',
  115. 22: '吉林',
  116. 23: '黑龙江 ',
  117. 31: '上海',
  118. 32: '江苏',
  119. 33: '浙江',
  120. 34: '安徽',
  121. 35: '福建',
  122. 36: '江西',
  123. 37: '山东',
  124. 41: '河南',
  125. 42: '湖北 ',
  126. 43: '湖南',
  127. 44: '广东',
  128. 45: '广西',
  129. 46: '海南',
  130. 50: '重庆',
  131. 51: '四川',
  132. 52: '贵州',
  133. 53: '云南',
  134. 54: '西藏 ',
  135. 61: '陕西',
  136. 62: '甘肃',
  137. 63: '青海',
  138. 64: '宁夏',
  139. 65: '新疆',
  140. 71: '台湾',
  141. 81: '香港',
  142. 82: '澳门',
  143. 91: '国外 ',
  144. };
  145. let row = {
  146. pass: true,
  147. msg: '验证成功',
  148. };
  149. 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)) {
  150. row = {
  151. pass: false,
  152. msg: '身份证号格式错误',
  153. };
  154. } else if (!city[code.substr(0, 2)]) {
  155. row = {
  156. pass: false,
  157. msg: '身份证号地址编码错误',
  158. };
  159. }
  160. // else {
  161. // // 18位身份证需要验证最后一位校验位
  162. // if (code.length === 18) {
  163. // code = code.split('');
  164. // // ∑(ai×Wi)(mod 11)
  165. // // 加权因子
  166. // const factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
  167. // // 校验位
  168. // const parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
  169. // let sum = 0;
  170. // let ai = 0;
  171. // let wi = 0;
  172. // for (let i = 0; i < 17; i++) {
  173. // ai = code[i];
  174. // wi = factor[i];
  175. // sum += ai * wi;
  176. // }
  177. // if (parity[sum % 11] !== code[17].toUpperCase()) {
  178. // row = {
  179. // pass: false,
  180. // msg: '身份证号校验位错误',
  181. // };
  182. // }
  183. // }
  184. // }
  185. if (row.pass) {
  186. row.gender = this.maleOrFemalByIdCard(code);
  187. row.age = this.getAge(code);
  188. }
  189. return row;
  190. }
  191. // 性别
  192. maleOrFemalByIdCard(idCard) {
  193. if (_.isArray(idCard)) idCard = idCard.join('');
  194. idCard = _.trim(idCard.replace(/ /g, '')); // 对身份证号码做处理。包括字符间有空格。
  195. if (idCard.length === 15) {
  196. if (idCard.substring(14, 15) % 2 === 0) {
  197. return '1';
  198. }
  199. return '0';
  200. } else if (idCard.length === 18) {
  201. if (idCard.substring(14, 17) % 2 === 0) {
  202. return '1';
  203. }
  204. return '0';
  205. }
  206. return null;
  207. }
  208. // 年龄
  209. getAge(idCard) {
  210. if (_.isArray(idCard)) idCard = idCard.join('');
  211. const ageDate = new Date();
  212. const month = ageDate.getMonth() + 1;
  213. const day = ageDate.getDate();
  214. let age = ageDate.getFullYear() - idCard.substring(6, 10) - 1;
  215. if (idCard.substring(10, 12) < month || (idCard.substring(10, 12) === month && idCard.substring(12, 14) <= day)) {
  216. age++;
  217. }
  218. if (age <= 0) {
  219. age = 1;
  220. }
  221. return age;
  222. }
  223. }
  224. module.exports = MatchSignService;