personal.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const { ObjectId } = require('mongoose').Types;
  5. const _ = require('lodash');
  6. const jwt = require('jsonwebtoken');
  7. const assert = require('assert');
  8. const Excel = require('exceljs');
  9. const { sep } = require('path');
  10. const fs = require('fs');
  11. // 个人用户
  12. class PersonalService extends CrudService {
  13. constructor(ctx) {
  14. super(ctx, 'personal');
  15. this.redis = this.app.redis;
  16. this.model = this.ctx.model.User.Personal;
  17. this.adminModel = this.ctx.model.User.Admin;
  18. this.util = this.ctx.service.util.util;
  19. }
  20. async query(condition, { skip = 0, limit = 0 }) {
  21. const query = await this.dealQueryCondition(_.cloneDeep(condition));
  22. const res = await this.model
  23. .find(query)
  24. .skip(parseInt(skip))
  25. .limit(parseInt(limit))
  26. .sort({ 'meta.createdAt': -1 });
  27. return res;
  28. }
  29. async count(condition) {
  30. const query = await this.dealQueryCondition(_.cloneDeep(condition));
  31. const res = await this.model.count(query);
  32. return res;
  33. }
  34. async dealQueryCondition({ role, code, ...condition } = {}) {
  35. condition = this.util.dealQuery(condition);
  36. if (role === '1') {
  37. // code查询机构,取出机构的code,用code到model(个人用户)里查询用户,合为一个数组,返回
  38. const orgRes = await this.adminModel.find({ code }, { _id: 1 });
  39. const ids = orgRes.map(i => i._id);
  40. const res2 = await this.adminModel.find({ pid: ids, role: '2' }, { code });
  41. const codes = res2.map(i => i.code);
  42. condition.code = codes;
  43. } else if (code) {
  44. // code直接查询用户返回即可
  45. condition.code = code;
  46. }
  47. return condition;
  48. }
  49. /**
  50. * 创建用户
  51. * @param {Object} params 用户信息
  52. */
  53. async create({ password, ...data }) {
  54. if (!password) password = '123456';
  55. data.password = { secret: password };
  56. const { name, phone, code } = data;
  57. // 检查手机号
  58. const num = await this.model.count({ phone, code, isdel: '0' });
  59. if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, `姓名:${name} ; 手机号: ${phone} ;已有机构所属的个人用户使用该手机号`);
  60. return await this.model.create(data);
  61. }
  62. /**
  63. * 修改密码
  64. * @param {Object} {id,password} 用户id和密码
  65. */
  66. async password({ id, password }) {
  67. const object = await this.model.findById(id);
  68. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  69. object.password = { secret: password };
  70. await object.save();
  71. }
  72. /**
  73. * 登陆
  74. * @param {Object} params 登陆信息
  75. * @property phone 手机号
  76. * @property password 密码
  77. */
  78. async login({ phone, password, code }) {
  79. const object = await this.model.findOne({ phone, code, isdel: '0' }, '+password');
  80. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  81. const { password: op, status } = object;
  82. const { secret } = op;
  83. if (status !== '1') throw new BusinessError(ErrorCode.ACCESS_DENIED, '拒绝访问!');
  84. if (secret !== password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
  85. const data = _.omit(JSON.parse(JSON.stringify(object)), [ 'meta', 'password', '__v' ]);
  86. const { secret: secrets } = this.config.jwt;
  87. const token = jwt.sign(data, secrets);
  88. // 记录登陆
  89. // let number = await this.redis.get('login_number') || 0;
  90. // number++;
  91. // await this.redis.set('login_number', number);
  92. return token;
  93. }
  94. // async delete({ id }) {
  95. // const object = await this.model.findById(id);
  96. // if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  97. // object.isdel = '1';
  98. // await object.save();
  99. // }
  100. /**
  101. * 个人升变专家
  102. * @param {Object} body 升变数据
  103. * 升级的时候要把所属的信息(产品,需求等)换到升级后的数据上,一种数据迁移
  104. */
  105. async upgrade({ id, ...data }) {
  106. assert(id, '缺少个人用户ID');
  107. const user = await this.model.findOne({ _id: ObjectId(id) }, '+password');
  108. if (!user) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  109. data = _.omit(data, [ 'meta', '__v' ]);
  110. data.user_id = id;
  111. // 创建专家
  112. const is_expert = await this.ctx.model.User.Expert.count({ user_id: ObjectId(id) });
  113. if (is_expert > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已升级为专家,无需再次升级');
  114. await this.ctx.model.User.Expert.create(data);
  115. user.is_expert = true;
  116. await user.save();
  117. }
  118. async import({ uri }) {
  119. assert(uri, '未获取到文件地址');
  120. const domain = 'http://127.0.0.1';
  121. const file = await this.ctx.curl(`${domain}${uri}`);
  122. if (!(file && file.data)) {
  123. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定文件');
  124. }
  125. const workbook = new Excel.Workbook();
  126. await workbook.xlsx.load(file.data);
  127. const sheet = workbook.getWorksheet(1);
  128. let meta = this.importMeta();
  129. // 根据excel,将列序号加入meta中
  130. const head = _.get(sheet.getRow(1), 'values', []);
  131. for (let i = 0; i < head.length; i++) {
  132. const e = head[i];
  133. if (!e) continue;
  134. const r = meta.find(f => f.column === e);
  135. if (r) r.index = i;
  136. }
  137. // 过滤出列表中不必要且不存在的字段
  138. meta = meta.filter(f => f.required || f.index);
  139. const errorList = [];
  140. const dataList = [];
  141. sheet.eachRow(async (row, index) => {
  142. if (index !== 1) {
  143. const values = row.values;
  144. const obj = {};
  145. for (const m of meta) {
  146. const { required, key, index, method, column } = m;
  147. const value = values[index];
  148. if (required && !value) {
  149. // 必填且没值的情况
  150. errorList.push({ message: `第${index}行数据,缺少必填项 ${column};` });
  151. continue;
  152. }
  153. if (_.isFunction(method)) obj[key] = method(value);
  154. else obj[key] = value;
  155. }
  156. dataList.push(obj);
  157. }
  158. });
  159. if (errorList.length > 0) return errorList;
  160. for (const i of dataList) {
  161. try {
  162. await this.create(i);
  163. } catch (error) {
  164. errorList.push(error);
  165. }
  166. }
  167. return errorList;
  168. }
  169. importMeta() {
  170. return [
  171. { required: true, column: '用户名', key: 'name' },
  172. { required: true, column: '手机号', key: 'phone' },
  173. { required: true, column: '登录密码', key: 'password' },
  174. { required: true, column: '邀请码', key: 'code' },
  175. { column: '身份证号', key: 'card' },
  176. { column: '电子邮箱', key: 'email' },
  177. { column: '联系地址', key: 'addr' },
  178. { column: '办公电话', key: 'office_phone' },
  179. { column: '所属辖区', key: 'juris' },
  180. { column: '院校', key: 'school' },
  181. { column: '专业', key: 'major' },
  182. { column: '职务职称', key: 'zwzc' },
  183. { column: '审核状态', key: 'status' },
  184. ];
  185. }
  186. async export() {
  187. // 查询位置
  188. let skip = 0;
  189. // 一次处理多少条数据
  190. const limit = 500;
  191. const total = await this.model.count({ isdel: 0 });
  192. // 循环次数
  193. const times = _.ceil(_.divide(total, limit));
  194. const nowDate = new Date().getTime();
  195. // 文件 名称 及 路径
  196. const filename = `个人用户导出-${nowDate}.xlsx`;
  197. const root_path = _.get(this.ctx.app.config.export, 'root_path');
  198. if (!fs.existsSync(`${root_path}`)) {
  199. // 如果不存在文件夹,就创建
  200. fs.mkdirSync(`${root_path}`);
  201. }
  202. const excel_path = `${sep}excel${sep}`;
  203. const path = `${root_path}${excel_path}`;
  204. if (!fs.existsSync(path)) {
  205. // 如果不存在文件夹,就创建
  206. fs.mkdirSync(path);
  207. }
  208. // 流式写入
  209. const options = {
  210. filename: `${path}${filename}`,
  211. };
  212. const workbook = new Excel.stream.xlsx.WorkbookWriter(options);
  213. const sheet = workbook.addWorksheet('sheet');
  214. const eMeta = this.exportMeta();
  215. const eColumn = eMeta.map(i => i.key);
  216. let needDeal = eMeta.map((i, index) => {
  217. if (_.isFunction(_.get(i, 'method'))) {
  218. i.index = index;
  219. return i;
  220. }
  221. });
  222. needDeal = _.compact(needDeal);
  223. for (let i = 1; i <= times; i++) {
  224. if (i === 1) {
  225. const head = eMeta.map(i => i.zh);
  226. sheet.addRow(head).commit();
  227. }
  228. const data = await this.model.find({ isdel: 0 }, '+password').skip(skip).limit(limit);
  229. for (const d of data) {
  230. const dup = Object.values(_.pick(d, eColumn));
  231. // 处理特殊情况
  232. for (const nd of needDeal) {
  233. const { index, method } = nd;
  234. dup[index] = method(dup[index]);
  235. }
  236. // 添加数据
  237. sheet.addRow(dup).commit();
  238. }
  239. skip += limit;
  240. }
  241. sheet.commit();
  242. await workbook.commit();
  243. return `/files/excel/${filename}`;
  244. }
  245. exportMeta() {
  246. return [
  247. { key: 'name', zh: '用户名' },
  248. { key: 'phone', zh: '手机号' },
  249. { key: 'password', zh: '登录密码', method: i => _.get(i, 'secret') },
  250. { key: 'code', zh: '邀请码' },
  251. { key: 'card', zh: '身份证号' },
  252. { key: 'email', zh: '邮箱' },
  253. { key: 'addr', zh: '地址' },
  254. { key: 'office_phone', zh: '办公电话' },
  255. { key: 'juris', zh: '所属辖区' },
  256. { key: 'school', zh: '院系' },
  257. { key: 'major', zh: '专业' },
  258. { key: 'zwzc', zh: '职务职称' },
  259. { key: 'status', zh: '审核状态' },
  260. ];
  261. }
  262. }
  263. module.exports = PersonalService;