personal.js 11 KB

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