personal.js 11 KB

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