expertsuser.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. const jwt = require('jsonwebtoken');
  8. class ExpertuserService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'experts_user');
  11. this.model = this.ctx.model.Expertsuser;
  12. this.umodel = this.ctx.model.User;
  13. }
  14. async create(data) {
  15. const { name, password, role } = data;
  16. assert(name, '用户名不能为空');
  17. assert(password, '密码不能为空');
  18. const { phone } = data;
  19. const has_phone = await this.model.findOne({ phone, role });
  20. if (has_phone) {
  21. throw new BusinessError('此身份的手机号已被注册,请更换手机号');
  22. }
  23. const newdata = data;
  24. newdata.password = { secret: password };
  25. const res = await this.model.create(newdata);
  26. if (res) {
  27. const url = this.ctx.app.config.axios.auth.baseUrl;
  28. const newdata = { name, phone: data.phone, passwd: password, uid: res.id, role: data.role, pid: data.pid, deptname: data.deptname, code: data.code };
  29. const user = await this.ctx.curl(url, {
  30. method: 'post',
  31. headers: {
  32. 'content-type': 'application/json',
  33. },
  34. dataType: 'json',
  35. data: JSON.stringify(newdata),
  36. });
  37. if (user.data.errcode === 0) {
  38. const result = await this.model.findById(res.id);
  39. result.userid = user.data.data.id;
  40. result.save();
  41. }
  42. }
  43. return res;
  44. }
  45. async update({ id }, data) {
  46. const user = await this.model.findById(id);
  47. const { phone } = data;
  48. const phoneList = await this.model.find({ phone });
  49. const is_has = phoneList.find(f => f.id !== id);
  50. if (is_has) throw new BusinessError('此手机号已被注册,请更换手机号');
  51. if (data.name) {
  52. user.name = data.name;
  53. }
  54. if (data.password) {
  55. user.password = { secret: data.password };
  56. }
  57. user.cardnumber = data.cardnumber;
  58. user.gender = data.gender;
  59. user.phone = data.phone;
  60. user.email = data.email;
  61. user.addr = data.addr;
  62. user.img_path = data.img_path;
  63. user.birthday = data.birthday;
  64. user.level = data.level;
  65. user.levelname = data.levelname;
  66. user.position = data.position;
  67. user.school = data.school;
  68. user.xl = data.xl;
  69. user.xw = data.xw;
  70. user.major = data.major;
  71. user.professional = data.professional;
  72. user.resume = data.resume;
  73. user.project = data.project;
  74. user.academic = data.academic;
  75. user.paper = data.paper;
  76. user.remark = data.remark;
  77. user.role = data.role;
  78. user.status = data.status;
  79. const res = await user.save();
  80. if (res) {
  81. const url = this.ctx.app.config.axios.auth.baseUrl + '/updatebyuid/' + res.id;
  82. const newdata = { name: data.name, deptname: data.deptname };
  83. await this.ctx.curl(url, {
  84. method: 'post',
  85. headers: {
  86. 'content-type': 'application/json',
  87. },
  88. dataType: 'json',
  89. data: JSON.stringify(newdata),
  90. });
  91. }
  92. return res;
  93. }
  94. async upgrade({ id }, data) {
  95. await this.umodel.deleteOne({ _id: ObjectId(id) });
  96. const { phone, uid } = data;
  97. const phoneList = await this.model.find({ phone });
  98. const is_has = phoneList.find(f => f.id !== id);
  99. if (is_has) throw new BusinessError('此手机号已被注册,请更换手机号');
  100. const user = {};
  101. if (data.name) {
  102. user.name = data.name;
  103. }
  104. if (data.password) {
  105. user.password = { secret: data.password };
  106. }
  107. user.cardnumber = data.cardnumber;
  108. user.gender = data.gender;
  109. user.phone = data.phone;
  110. user.email = data.email;
  111. user.addr = data.addr;
  112. user.img_path = data.img_path;
  113. user.birthday = data.birthday;
  114. user.level = data.level;
  115. user.levelname = data.levelname;
  116. user.position = data.position;
  117. user.school = data.school;
  118. user.xl = data.xl;
  119. user.xw = data.xw;
  120. user.major = data.major;
  121. user.professional = data.professional;
  122. user.resume = data.resume;
  123. user.project = data.project;
  124. user.academic = data.academic;
  125. user.paper = data.paper;
  126. user.remark = data.remark;
  127. user.role = data.role;
  128. user.status = data.status;
  129. const res = await this.model.create(user);
  130. if (res) {
  131. const url = this.ctx.app.config.axios.auth.baseUrl + '/update/' + uid;
  132. const newdata = { uid: res.id, role: data.role, name: data.name };
  133. await this.ctx.curl(url, {
  134. method: 'post',
  135. headers: {
  136. 'content-type': 'application/json',
  137. },
  138. dataType: 'json',
  139. data: JSON.stringify(newdata),
  140. });
  141. }
  142. return res;
  143. }
  144. }
  145. module.exports = ExpertuserService;