expertsuser.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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, phone } = data;
  16. assert(name, '用户名不能为空');
  17. assert(password, '密码不能为空');
  18. const has_phone = await this.model.findOne({ phone, role });
  19. if (has_phone) {
  20. throw new BusinessError('此身份手机号已被注册,请更换手机号');
  21. }
  22. data.password = { secret: password };
  23. const res = await this.model.create(data);
  24. if (res) {
  25. const newdata = {
  26. name,
  27. phone: data.phone,
  28. passwd: data.password,
  29. uid: res.id,
  30. role: data.role,
  31. pid: data.pid,
  32. deptname: data.deptname,
  33. code: data.code,
  34. status: data.status,
  35. };
  36. const auth_user = await this.umodel.findOne({ phone, role });
  37. if (auth_user) {
  38. throw new BusinessError('此身份手机号已被注册,请更换手机号');
  39. }
  40. const authres = await this.umodel.create(newdata);
  41. return authres;
  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. const model = [
  58. 'name',
  59. 'education',
  60. 'school',
  61. 'birthDate',
  62. 'phone',
  63. 'qqwx',
  64. 'email',
  65. 'company',
  66. 'zwzc',
  67. 'expertise',
  68. 'expertimage',
  69. 'workexperience',
  70. 'scientific',
  71. 'undertakingproject',
  72. 'scienceaward',
  73. 'social',
  74. 'userid',
  75. 'code',
  76. 'role',
  77. 'status',
  78. 'pid',
  79. 'deptname',
  80. 'password',
  81. ];
  82. for (const key of model) {
  83. if (data[key]) {
  84. user[key] = data[key];
  85. }
  86. }
  87. const res = await user.save();
  88. if (res) {
  89. const url =
  90. this.ctx.app.config.axios.auth.baseUrl + '/updatebyuid/' + res.id;
  91. const newdata = { name: data.name, deptname: data.deptname };
  92. await this.ctx.curl(url, {
  93. method: 'post',
  94. headers: {
  95. 'content-type': 'application/json',
  96. },
  97. dataType: 'json',
  98. data: JSON.stringify(newdata),
  99. });
  100. }
  101. return res;
  102. }
  103. async upgrade({ id }, data) {
  104. await this.umodel.deleteOne({ _id: ObjectId(id) });
  105. const { phone, uid } = data;
  106. const phoneList = await this.model.find({ phone });
  107. const is_has = phoneList.find(f => f.id !== id);
  108. if (is_has) throw new BusinessError('此手机号已被注册,请更换手机号');
  109. const user = {};
  110. if (data.name) {
  111. user.name = data.name;
  112. }
  113. if (data.password) {
  114. user.password = { secret: data.password };
  115. }
  116. user.phone = data.phone;
  117. user.code = data.code;
  118. user.role = data.role;
  119. user.status = data.status;
  120. user.company = data.company;
  121. user.school = data.school;
  122. user.birthDate = data.birthDate;
  123. user.qqwx = data.qqwx;
  124. user.email = data.email;
  125. user.expertimage = data.expertimage;
  126. user.workexperience = data.workexperience;
  127. user.scientific = data.scientific;
  128. user.undertakingproject = data.undertakingproject;
  129. user.scienceaward = data.scienceaward;
  130. user.expertise = data.expertise;
  131. user.education = data.education;
  132. user.zwzc = data.zwzc;
  133. user.social = data.social;
  134. const res = await this.model.create(user);
  135. if (res) {
  136. const url = this.ctx.app.config.axios.auth.baseUrl + '/update/' + uid;
  137. const newdata = { uid: res.id, role: data.role, name: data.name };
  138. await this.ctx.curl(url, {
  139. method: 'post',
  140. headers: {
  141. 'content-type': 'application/json',
  142. },
  143. dataType: 'json',
  144. data: JSON.stringify(newdata),
  145. });
  146. }
  147. return res;
  148. }
  149. // 首页查询
  150. async indexQuery(data, { limit }) {
  151. const res = await this.model.aggregate([
  152. { $match: data },
  153. { $sample: { size: parseInt(limit) } },
  154. ]);
  155. return res;
  156. }
  157. // 重写删除方法
  158. /**
  159. * 根据id删除=>修改isdel为1
  160. * @param {Object} {id} 只接收id,不过需要解构,因为是object形式过来的
  161. */
  162. async delete({ id }) {
  163. const res = await this.model.update({ _id: ObjectId(id) }, { isdel: '1' });
  164. return res;
  165. }
  166. }
  167. module.exports = ExpertuserService;