personal.js 8.4 KB

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