profile_controller.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. 'use strict';
  2. /**
  3. * 账号相关控制器
  4. *
  5. * @author CaiAoLin
  6. * @date 2018/1/26
  7. * @version
  8. */
  9. const moment = require('moment');
  10. const profileMenu = require('../../config/menu').profileMenu;
  11. const smsTypeConst = require('../const/sms_type');
  12. const qr = require('qr-image');
  13. const path = require('path');
  14. const sendToWormhole = require('stream-wormhole');
  15. const loginWay = require('../const/setting').loginWay;
  16. const wxWork = require('../lib/wx_work');
  17. const profileConst = require('../const/profile');
  18. const DSK = require('../lib/dsk');
  19. module.exports = app => {
  20. class ProfileController extends app.BaseController {
  21. /**
  22. * 构造函数
  23. *
  24. * @param {Object} ctx - egg全局context
  25. * @return {void}
  26. */
  27. constructor(ctx) {
  28. super(ctx);
  29. ctx.subMenu = profileMenu;
  30. }
  31. /**
  32. * 账号资料页面
  33. *
  34. * @param {Object} ctx - egg全局变量
  35. * @return {void}
  36. */
  37. async info(ctx) {
  38. // 获取当前用户数据
  39. const sessionUser = ctx.session.sessionUser;
  40. // 获取账号数据
  41. const accountData = await ctx.service.projectAccount.getDataByCondition({ id: sessionUser.accountId });
  42. // 获取基础数据的字段规则
  43. const baseRule = ctx.service.projectAccount.rule('profileBase');
  44. const baseJsValidator = await this.jsValidator.convert(baseRule).setSelector('#base-form').build();
  45. const renderData = {
  46. accountData,
  47. baseJsValidator,
  48. };
  49. await this.layout('profile/info.ejs', renderData);
  50. }
  51. /**
  52. * 保存基本信息
  53. *
  54. * @param {Object} ctx - egg全局变量
  55. * @return {void}
  56. */
  57. async saveBase(ctx) {
  58. try {
  59. // 获取当前用户数据
  60. const sessionUser = ctx.session.sessionUser;
  61. // 获取基础数据的字段规则
  62. const baseRule = ctx.service.projectAccount.rule('profileBase');
  63. ctx.helper.validate(baseRule);
  64. const result = await ctx.service.projectAccount.saveInfo(ctx.request.body, sessionUser.accountId);
  65. if (!result) {
  66. throw '保存信息失败';
  67. }
  68. this.setMessage('修改成功', this.messageType.SUCCESS);
  69. } catch (error) {
  70. this.log(error);
  71. this.setMessage(error.toString(), this.messageType.ERROR);
  72. }
  73. ctx.redirect(ctx.request.header.referer);
  74. }
  75. /**
  76. * 账号资料页面
  77. *
  78. * @param {Object} ctx - egg全局变量
  79. * @return {void}
  80. */
  81. async cert(ctx) {
  82. // 获取当前用户数据
  83. const sessionUser = ctx.session.sessionUser;
  84. // 获取账号数据
  85. const accountData = await ctx.service.projectAccount.getDataByCondition({ id: sessionUser.accountId });
  86. const certList = await ctx.service.accountCert.getAllDataByCondition({ where: { uid: sessionUser.accountId }, orders: [['create_time', 'desc']] });
  87. // json转换
  88. certList.forEach(item => {
  89. item.edu_json = item.edu_json ? JSON.parse(item.edu_json) : [];
  90. });
  91. const renderData = {
  92. accountData,
  93. certList,
  94. fujianOssPath: ctx.app.config.fujianOssPath,
  95. certTypeConst: profileConst.cert.certType,
  96. certSourceConst: {
  97. cert_reg: ctx.helper._.orderBy(profileConst.cert.certReg, ['sort'], ['asc']),
  98. cert_qual: ctx.helper._.orderBy(profileConst.cert.certQual, ['sort'], ['asc']),
  99. },
  100. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.profile.cert),
  101. };
  102. await this.layout('profile/cert.ejs', renderData, 'profile/cert_modal.ejs');
  103. }
  104. async certSave(ctx) {
  105. const response = {
  106. err: 0,
  107. msg: '',
  108. data: {},
  109. };
  110. try {
  111. const sessionUser = ctx.session.sessionUser;
  112. const data = JSON.parse(ctx.request.body.data);
  113. switch (data.type) {
  114. case 'add_cert':
  115. response.data = await ctx.service.accountCert.addCert(sessionUser.accountId);
  116. break;
  117. case 'update_cert':
  118. response.data = await ctx.service.accountCert.updateCert(data.update_data);
  119. break;
  120. case 'add_jx':
  121. response.data = await ctx.service.accountCert.addEduJson(data.id);
  122. break;
  123. case 'update_jx':
  124. response.data = await ctx.service.accountCert.updateEduJson(data.update_data);
  125. break;
  126. case 'del_cert': // 包括删除附件
  127. response.data = await ctx.service.accountCert.delCert(data.delete_data);
  128. break;
  129. default:throw '参数有误';
  130. }
  131. } catch (error) {
  132. response.err = 1;
  133. response.msg = error.toString();
  134. }
  135. ctx.body = response;
  136. }
  137. /**
  138. * 上传证书(单选)
  139. *
  140. * @param {object} ctx - egg全局变量
  141. * @return {void}
  142. */
  143. async certUpload(ctx) {
  144. const responseData = {
  145. err: 0, msg: '', data: null,
  146. };
  147. try {
  148. const stream = await ctx.getFileStream();
  149. const create_time = Date.parse(new Date()) / 1000;
  150. const fileInfo = path.parse(stream.filename);
  151. const id = stream.fields && stream.fields.id ? stream.fields.id : 0;
  152. if (!id) throw '参数有误';
  153. let jxid = '';
  154. let type = '';
  155. if (stream.fields && stream.fields.type === 'upload_jx') {
  156. jxid = stream.fields.jxid ? stream.fields.jxid : '';
  157. if (!jxid) throw '参数有误';
  158. type = 'jx';
  159. } else if (stream.fields && stream.fields.type === 'upload_cert') {
  160. type = 'cert';
  161. }
  162. if (!type) throw '参数有误';
  163. // 判断用户是否选择上传文件
  164. if (!stream.filename) {
  165. throw '请选择上传的文件!';
  166. }
  167. const filename = stream.filename;
  168. const filepath = `app/public/upload/profile/${ctx.session.sessionUser.accountId}/cert/zhengshu_${create_time + fileInfo.ext}`;
  169. // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, filepath));
  170. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  171. await sendToWormhole(stream);
  172. if (type === 'jx') {
  173. const info = await ctx.service.accountCert.getDataById(id);
  174. if (!info) throw '数据有误';
  175. const edu_json = info.edu_json ? JSON.parse(info.edu_json) : [];
  176. const jxInfo = edu_json.find(item => item.id === jxid);
  177. if (!jxInfo) throw '数据有误';
  178. jxInfo.file_path = filepath;
  179. jxInfo.file_name = filename;
  180. await ctx.service.accountCert.update({ edu_json: JSON.stringify(edu_json) }, { id });
  181. } else {
  182. await ctx.service.accountCert.update({ file_path: filepath, file_name: filename }, { id });
  183. }
  184. responseData.data = { file_path: filepath, file_name: filename };
  185. } catch (err) {
  186. console.log(err);
  187. responseData.err = 1;
  188. responseData.msg = err;
  189. }
  190. ctx.body = responseData;
  191. }
  192. /**
  193. * 修改密码操作
  194. *
  195. * @param {Object} ctx - egg全局变量
  196. * @return {void}
  197. */
  198. async modifyPassword(ctx) {
  199. const password = ctx.request.body.password;
  200. const newPassword = ctx.request.body.new_password;
  201. try {
  202. const sessionUser = ctx.session.sessionUser;
  203. let accountId = sessionUser.accountId;
  204. accountId = parseInt(accountId);
  205. if (isNaN(accountId) || accountId <= 0) {
  206. throw '参数错误';
  207. }
  208. // 验证数据
  209. const passwordRule = ctx.service.projectAccount.rule('modifyPassword');
  210. ctx.helper.validate(passwordRule);
  211. // 判断新密码的强度
  212. const reg = /^(?![0-9]+$)(?![a-zA-Z]+$).{6,16}$/;
  213. if (!reg.test(newPassword)) {
  214. throw '请设置至少包含数字和字母的新密码';
  215. }
  216. const result = await ctx.service.projectAccount.modifyPassword(accountId, password, newPassword);
  217. if (!result) {
  218. throw '修改密码失败';
  219. }
  220. this.setMessage('修改密码成功', this.messageType.SUCCESS);
  221. ctx.redirect('/logout');
  222. } catch (error) {
  223. ctx.session.postError = error.toString();
  224. this.setMessage(error.toString(), this.messageType.ERROR);
  225. ctx.redirect(ctx.request.header.referer);
  226. }
  227. }
  228. /**
  229. * 设置短信验证码
  230. *
  231. * @param {object} ctx - egg全局变量
  232. * @return {void}
  233. */
  234. async getCode(ctx) {
  235. const response = {
  236. err: 0,
  237. msg: '',
  238. };
  239. try {
  240. const sessionUser = ctx.session.sessionUser;
  241. const mobile = ctx.request.body.mobile;
  242. let type = null;
  243. if (ctx.request.body.type) {
  244. type = ctx.request.body.type;
  245. delete ctx.request.body.type;
  246. }
  247. const rule = { mobile: { type: 'mobile', allowEmpty: false } };
  248. ctx.helper.validate(rule);
  249. if (type === null || type !== 'shenpi') {
  250. // 查找是否有重复的认证手机
  251. const accountData = await ctx.service.projectAccount.getDataByCondition({ project_id: ctx.session.sessionProject.id, auth_mobile: mobile });
  252. if (accountData !== null) {
  253. throw '此手机号码已被使用,请重新输入!';
  254. }
  255. }
  256. const result = await ctx.service.projectAccount.setSMSCode(sessionUser.accountId, mobile);
  257. if (!result) {
  258. throw '获取验证码失败';
  259. }
  260. } catch (error) {
  261. response.err = 1;
  262. response.msg = error.toString();
  263. }
  264. ctx.body = response;
  265. }
  266. /**
  267. * 绑定认证手机
  268. *
  269. * @param {object} ctx - egg全局变量
  270. * @return {void}
  271. */
  272. async bindMobile(ctx) {
  273. const response = {
  274. err: 0,
  275. msg: '',
  276. };
  277. try {
  278. const rule = ctx.service.projectAccount.rule('bindMobile');
  279. ctx.helper.validate(rule);
  280. const sessionUser = ctx.session.sessionUser;
  281. const result = await ctx.service.projectAccount.bindMobile(sessionUser.accountId, ctx.request.body, ctx.session.sessionProject.id);
  282. if (!result) {
  283. throw '绑定手机失败!';
  284. }
  285. // this.setMessage('绑定成功', this.messageType.SUCCESS);
  286. response.msg = '绑定成功';
  287. response.url = ctx.request.header.referer;
  288. } catch (error) {
  289. this.ctx.helper.log(error);
  290. response.err = 1;
  291. response.msg = error.toString();
  292. }
  293. ctx.body = response;
  294. }
  295. /**
  296. * 短信通知
  297. *
  298. * @param {object} ctx - egg全局变量
  299. * @return {void}
  300. */
  301. async sms(ctx) {
  302. // 获取当前用户数据
  303. const sessionUser = ctx.session.sessionUser;
  304. // 获取账号数据
  305. const accountData = await ctx.service.projectAccount.getDataByCondition({ id: sessionUser.accountId });
  306. accountData.dsk_account = accountData.dsk_account ? JSON.parse(accountData.dsk_account) : null;
  307. const renderData = {
  308. accountData,
  309. smsType: smsTypeConst.type,
  310. };
  311. await this.layout('profile/sms.ejs', renderData, 'profile/modal.ejs');
  312. }
  313. /**
  314. * 短信通知类型设置
  315. *
  316. * @param {object} ctx - egg全局变量
  317. * @return {void}
  318. */
  319. async smsType(ctx) {
  320. try {
  321. const sessionUser = ctx.session.sessionUser;
  322. const result = await ctx.service.projectAccount.noticeTypeSet(sessionUser.accountId, ctx.request.body);
  323. if (!result) {
  324. throw '修改通知类型失败!';
  325. }
  326. this.setMessage('通知类型绑定成功', this.messageType.SUCCESS);
  327. } catch (error) {
  328. console.log(error);
  329. this.setMessage(error.toString(), this.messageType.ERROR);
  330. }
  331. ctx.redirect(ctx.request.header.referer);
  332. }
  333. async dskApi(ctx) {
  334. const response = {
  335. err: 0,
  336. msg: '',
  337. };
  338. try {
  339. const sessionUser = ctx.session.sessionUser;
  340. const data = JSON.parse(ctx.request.body.data);
  341. const dskAccount = await ctx.service.projectAccount.getDataById(sessionUser.accountId);
  342. const dsk = new DSK(ctx);
  343. switch (data.type) {
  344. case 'sms':
  345. if (!data.mobile) {
  346. throw '参数有误';
  347. }
  348. if (!(/^1[3456789]\d{9}$/.test(data.mobile))) {
  349. throw '手机号码格式有误';
  350. }
  351. await dsk.sendSms(data.mobile);
  352. break;
  353. case 'bind':
  354. if (dskAccount.dsk_account) {
  355. throw '已经绑定过大司空账号';
  356. }
  357. if ((data.method !== 1 && data.method !== 2) || !data.value || !data.mobile) {
  358. throw '参数有误';
  359. }
  360. if (!(/^1[3456789]\d{9}$/.test(data.mobile))) {
  361. throw '手机号码格式有误';
  362. }
  363. const isExist = await ctx.service.projectAccount.isDskExist(ctx.session.sessionProject.id, data.mobile);
  364. if (isExist) {
  365. throw '此大司空账号已被该项目用户绑定,请重新输入';
  366. }
  367. const result = await dsk.accountAuth(data.mobile, data.method, data.value);
  368. response.data = await ctx.service.projectAccount.bindDsk(result, data.mobile, sessionUser.accountId);
  369. break;
  370. case 'unbind':
  371. if (!dskAccount.dsk_account) {
  372. throw '未绑定大司空账号';
  373. }
  374. response.data = await ctx.service.projectAccount.unbindDsk(sessionUser.accountId);
  375. break;
  376. case 'hadbind':
  377. if (!dskAccount.auth_mobile) {
  378. response.data = 1; // 未绑定手机
  379. } else if (!dskAccount.dsk_account) {
  380. response.data = 2; // 未绑定大司空账号
  381. } else {
  382. response.data = JSON.parse(dskAccount.dsk_account);
  383. }
  384. break;
  385. case 'unbindMobile':
  386. if (!dskAccount.auth_mobile) {
  387. throw '未绑定认证手机';
  388. }
  389. if (dskAccount.dsk_account) {
  390. throw '未解绑大司空账号无法解绑认证手机';
  391. }
  392. response.data = await ctx.service.projectAccount.defaultUpdate({ id: dskAccount.id, auth_mobile: '' });
  393. break;
  394. case 'compilation':
  395. if (!dskAccount.dsk_account) {
  396. throw '未绑定大司空账号';
  397. }
  398. const account = JSON.parse(dskAccount.dsk_account);
  399. const compilation = await dsk.getCompilation(data.mobile);
  400. response.data = {};
  401. response.data.compilation = compilation;
  402. if (data.getProject) {
  403. const compilationInfo = data.compilationId ? ctx.helper._.find(compilation, { ID: data.compilationId }) : null;
  404. const compilationId = compilationInfo ? compilationInfo.ID : (compilation.length > 0 ? compilation[0].ID : null);
  405. response.data.select_compilation = compilationId;
  406. const projectInfo = compilationId ? await dsk.getProjectList(account.mobile, compilationId) : [];
  407. response.data.project = projectInfo;
  408. }
  409. response.data.dskAccountData = account;
  410. break;
  411. case 'project':
  412. if (!dskAccount.dsk_account) {
  413. throw '未绑定大司空账号';
  414. }
  415. const account2 = JSON.parse(dskAccount.dsk_account);
  416. response.data = await dsk.getProjectList(account2.mobile, data.compilationId);
  417. break;
  418. case 'save_projects':
  419. if (!dskAccount.dsk_account) {
  420. throw '未绑定大司空账号';
  421. }
  422. const tender = await ctx.service.tender.getDataById(data.tid);
  423. if (!tender || tender.user_id !== sessionUser.accountId) {
  424. throw '无权限操作';
  425. }
  426. await ctx.service.projectAccount.saveDskProjects(sessionUser.accountId, data.project_list);
  427. const account3 = JSON.parse(dskAccount.dsk_account);
  428. if (data.project_id && ctx.helper._.findIndex(data.project_list, { pid: data.project_id }) !== -1) {
  429. account3.select_project = data.project_id;
  430. } else {
  431. account3.select_project = data.project_list && data.project_list.length > 0 ? data.project_list[0].pid : null;
  432. }
  433. await ctx.service.projectAccount.defaultUpdate({ id: sessionUser.accountId, dsk_account: JSON.stringify(account3) });
  434. response.data = account3.select_project;
  435. break;
  436. case 'project_tree':
  437. response.data = await dsk.getProjectTree(data.compilationId, data.projectId);
  438. const tender2 = data.tid ? await ctx.service.tender.getDataById(data.tid) : null;
  439. if (tender2 && tender2.user_id === sessionUser.accountId && dskAccount.dsk_account) {
  440. const account4 = JSON.parse(dskAccount.dsk_account);
  441. account4.select_project = data.projectId;
  442. let treeId = null;
  443. if (response.data && response.data.length > 0) {
  444. const treeInfo = ctx.helper._.find(response.data, { type: 4 });
  445. if (treeInfo && treeInfo.ID) {
  446. treeId = treeInfo.ID;
  447. }
  448. }
  449. account4.select_tree = treeId;
  450. await ctx.service.projectAccount.defaultUpdate({ id: sessionUser.accountId, dsk_account: JSON.stringify(account4) });
  451. }
  452. break;
  453. case 'project_bills':
  454. response.data = await dsk.getProjectBills(data.compilationId, data.treeId);
  455. const tender3 = data.tid ? await ctx.service.tender.getDataById(data.tid) : null;
  456. if (tender3 && tender3.user_id === sessionUser.accountId && dskAccount.dsk_account) {
  457. const account5 = JSON.parse(dskAccount.dsk_account);
  458. account5.select_tree = data.treeId;
  459. await ctx.service.projectAccount.defaultUpdate({ id: sessionUser.accountId, dsk_account: JSON.stringify(account5) });
  460. }
  461. break;
  462. default:throw '参数有误';
  463. }
  464. } catch (error) {
  465. response.err = 1;
  466. response.msg = error.toString();
  467. }
  468. ctx.body = response;
  469. }
  470. /**
  471. * 电子签名
  472. *
  473. * @param {object} ctx - egg全局变量
  474. * @return {void}
  475. */
  476. async sign(ctx) {
  477. // 获取当前用户数据
  478. const sessionUser = ctx.session.sessionUser;
  479. // 获取账号数据
  480. const accountData = await ctx.service.projectAccount.getDataByCondition({ id: sessionUser.accountId });
  481. const renderData = {
  482. accountData,
  483. fujianOssPath: ctx.app.config.fujianOssPath,
  484. };
  485. await this.layout('profile/sign.ejs', renderData);
  486. }
  487. /**
  488. * 网证通电子签名页
  489. *
  490. * @param {object} ctx - egg全局变量
  491. * @return {void}
  492. */
  493. async netcasign(ctx) {
  494. // 获取当前用户数据
  495. const sessionUser = ctx.session.sessionUser;
  496. // 获取账号数据
  497. const accountData = await ctx.service.projectAccount.getDataByCondition({ id: sessionUser.accountId });
  498. const signData = await ctx.service.netcasign.getDataByCondition({ uid: sessionUser.accountId });
  499. const renderData = {
  500. accountData,
  501. signData,
  502. };
  503. await this.layout('profile/netcasign.ejs', renderData, 'profile/sign_modal.ejs');
  504. }
  505. /**
  506. * 网证通电子签名页面操作
  507. *
  508. * @param {object} ctx - egg全局变量
  509. * @return {void}
  510. */
  511. async signSave(ctx) {
  512. const response = {
  513. err: 0,
  514. msg: '',
  515. };
  516. try {
  517. const sessionUser = ctx.session.sessionUser;
  518. const data = JSON.parse(ctx.request.body.data);
  519. let signData;
  520. switch (data.type) {
  521. case 'bind':
  522. signData = await ctx.service.netcasign.getDataByCondition({ pid: ctx.session.sessionProject.id, keyId: data.updateData.keyId });
  523. if (signData) {
  524. const accountData = await ctx.service.projectAccount.getDataByCondition({ id: signData.uid });
  525. throw '该Ukey已绑定于 ' + accountData.name + ', 不可重复绑定';
  526. }
  527. const result = await ctx.service.netcasign.add(data.updateData, sessionUser.accountId);
  528. if (!result) {
  529. throw '绑定Ukey失败';
  530. }
  531. response.data = await ctx.service.netcasign.getDataByCondition({ uid: sessionUser.accountId });
  532. break;
  533. case 'unbind':
  534. signData = await ctx.service.netcasign.getDataByCondition({ pid: ctx.session.sessionProject.id, uid: sessionUser.accountId });
  535. if (!signData) {
  536. throw '当前用户不存在绑定证书,解除绑定失败';
  537. }
  538. await ctx.service.netcasign.del(signData.id);
  539. break;
  540. case 'savesign':
  541. signData = await ctx.service.netcasign.getDataByCondition({ pid: ctx.session.sessionProject.id, uid: sessionUser.accountId });
  542. if (!signData) {
  543. throw '当前用户不存在绑定证书';
  544. }
  545. await ctx.service.netcasign.save({ sign_base64: data.sign_base64 }, signData.id);
  546. break;
  547. case 'delsign':
  548. signData = await ctx.service.netcasign.getDataByCondition({ pid: ctx.session.sessionProject.id, uid: sessionUser.accountId });
  549. if (!signData) {
  550. throw '当前用户不存在绑定证书';
  551. }
  552. if (signData && !signData.sign_base64) {
  553. throw '当前用户不存在签名,移除签名失败';
  554. }
  555. await ctx.service.netcasign.save({ sign_base64: null }, signData.id);
  556. break;
  557. default:throw '参数有误';
  558. }
  559. } catch (error) {
  560. response.err = 1;
  561. response.msg = error.toString();
  562. }
  563. ctx.body = response;
  564. }
  565. /**
  566. * 电子签名删除
  567. *
  568. * @param {object} ctx - egg全局变量
  569. * @return {void}
  570. */
  571. async signDelete(ctx) {
  572. const response = {
  573. err: 0,
  574. msg: '',
  575. };
  576. try {
  577. const sessionUser = ctx.session.sessionUser;
  578. // 获取账号数据
  579. const accountData = await ctx.service.projectAccount.getDataByCondition({ id: sessionUser.accountId });
  580. const data = JSON.parse(ctx.request.body.data);
  581. let result = false;
  582. if (data.type && data.type === 'stamp') {
  583. if (!accountData.stamp_path) {
  584. throw '不存在签章';
  585. }
  586. const stamp_path_list = accountData.stamp_path.split('!;!');
  587. const spIndex = ctx.helper._.indexOf(stamp_path_list, data.src);
  588. if (spIndex === -1) {
  589. throw '不存在此签章';
  590. }
  591. // 不删除地址,只删除数据库数据,防止已签章的报表丢失
  592. // await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + stamp_path_list[spIndex]);
  593. stamp_path_list.splice(spIndex, 1);
  594. // 删除库
  595. result = await ctx.service.projectAccount.update({ stamp_path: stamp_path_list.length === 0 ? null : stamp_path_list.join('!;!') }, { id: sessionUser.accountId });
  596. } else {
  597. if (accountData.sign_path === '') {
  598. throw '不存在签名';
  599. }
  600. result = await ctx.service.projectAccount.update({ sign_path: '' }, { id: sessionUser.accountId });
  601. }
  602. if (!result) {
  603. throw '移除签名失败';
  604. }
  605. } catch (error) {
  606. response.err = 1;
  607. response.msg = error.toString();
  608. }
  609. ctx.body = response;
  610. }
  611. /**
  612. * 生成二维码
  613. *
  614. * @param {object} ctx - egg全局变量
  615. * @return {void}
  616. */
  617. async qrCode(ctx) {
  618. const size = 5;
  619. const margin = 1;
  620. try {
  621. // 获取当前用户数据
  622. const sessionUser = ctx.session.sessionUser;
  623. let text = ctx.protocol + '://' + ctx.host + '/sign?user_id=' + sessionUser.accountId + '&app_token=' + sessionUser.sessionToken;
  624. if (ctx.query.from === 'netcasign') {
  625. text += '&from=netcasign';
  626. }
  627. // 大小默认5,二维码周围间距默认1
  628. const img = qr.image(text || '', { type: 'png', size: size || 5, margin: margin || 1 });
  629. ctx.status = 200;
  630. ctx.type = 'image/png';
  631. ctx.body = img;
  632. } catch (e) {
  633. ctx.status = 414;
  634. ctx.set('Content-Type', 'text/html');
  635. ctx.body = '<h1>414 Request-URI Too Large</h1>';
  636. }
  637. }
  638. /**
  639. * 上传签名图
  640. *
  641. * @param {object} ctx - egg全局变量
  642. * @return {void}
  643. */
  644. async signUpload(ctx) {
  645. const responseData = {
  646. err: 0, msg: '', data: null,
  647. };
  648. try {
  649. const stream = await ctx.getFileStream();
  650. const create_time = Date.parse(new Date()) / 1000;
  651. const fileInfo = path.parse(stream.filename);
  652. if (stream.fields && stream.fields.type && stream.fields.type === 'stamp') {
  653. // const dirName = 'app/public/upload/sign/profile';
  654. // const fileName = moment().format('YYYYMMDD') + '_sign_' + create_time + fileInfo.ext;
  655. const filepath = `app/public/upload/sign/profile/qianzhang_${create_time + fileInfo.ext}`;
  656. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  657. await sendToWormhole(stream);
  658. const result = await ctx.service.projectAccount.update({ stamp_path: filepath }, { id: ctx.session.sessionUser.accountId });
  659. if (result) {
  660. responseData.data = { stamp_path: filepath };
  661. } else {
  662. throw '添加数据库失败';
  663. }
  664. } else {
  665. const dirName = 'public/upload/sign';
  666. const fileName = moment().format('YYYYMMDD') + '_sign_' + create_time + fileInfo.ext;
  667. await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, 'app', dirName, fileName));
  668. await sendToWormhole(stream);
  669. const result = await ctx.service.projectAccount.update({ sign_path: fileName }, { id: ctx.session.sessionUser.accountId });
  670. if (result) {
  671. responseData.data = { sign_path: fileName };
  672. } else {
  673. throw '添加数据库失败';
  674. }
  675. }
  676. } catch (err) {
  677. this.log(err);
  678. responseData.err = 1;
  679. responseData.msg = err;
  680. }
  681. ctx.body = responseData;
  682. }
  683. /**
  684. * 上传签章图(多选)
  685. *
  686. * @param {object} ctx - egg全局变量
  687. * @return {void}
  688. */
  689. async stampUpload(ctx) {
  690. const responseData = {
  691. err: 0, msg: '', data: null,
  692. };
  693. let stream;
  694. try {
  695. const parts = ctx.multipart({ autoFields: true });
  696. const paths = [];
  697. let index = 0;
  698. stream = await parts();
  699. while (stream !== undefined) {
  700. // 判断用户是否选择上传文件
  701. if (!stream.filename) {
  702. throw '请选择上传的文件!';
  703. }
  704. const fileInfo = path.parse(stream.filename);
  705. const create_time = Date.parse(new Date()) / 1000;
  706. const filepath = `app/public/upload/sign/profile/qianzhang_${create_time + index.toString() + fileInfo.ext}`;
  707. // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, filepath));
  708. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  709. if (stream) {
  710. await sendToWormhole(stream);
  711. }
  712. paths.push(filepath);
  713. ++index;
  714. if (Array.isArray(parts.field.size) && index < parts.field.size.length) {
  715. stream = await parts();
  716. } else {
  717. stream = undefined;
  718. }
  719. }
  720. // 获取账号数据
  721. const accountData = await ctx.service.projectAccount.getDataByCondition({ id: ctx.session.sessionUser.accountId });
  722. const stamp_path = accountData.stamp_path ? accountData.stamp_path.split('!;!') : [];
  723. const stamp_path_list = ctx.helper._.concat(stamp_path, paths);
  724. const result = await ctx.service.projectAccount.update({ stamp_path: stamp_path_list.join('!;!') }, { id: ctx.session.sessionUser.accountId });
  725. if (result) {
  726. responseData.data = { stamp_path: stamp_path_list };
  727. } else {
  728. throw '添加数据库失败';
  729. }
  730. } catch (err) {
  731. this.log(err);
  732. // 失败需要消耗掉stream 以防卡死
  733. if (stream) {
  734. await sendToWormhole(stream);
  735. }
  736. responseData.err = 1;
  737. responseData.msg = err.toString();
  738. }
  739. ctx.body = responseData;
  740. }
  741. /**
  742. * 账号安全
  743. *
  744. * @param {object} ctx - egg全局变量
  745. * @return {void}
  746. */
  747. async safe(ctx) {
  748. // 获取当前用户数据
  749. const sessionUser = ctx.session.sessionUser;
  750. // 获取账号数据
  751. const accountData = await ctx.service.projectAccount.getDataByCondition({ id: sessionUser.accountId });
  752. // 获取修改密码的字段规则
  753. // const passwordRule = ctx.service.projectAccount.rule('modifyPassword');
  754. // const passwordJsValidator = await this.jsValidator.convert(passwordRule).setSelector('#password-form').build();
  755. // console.log(passwordJsValidator);
  756. // 获取登录日志
  757. const loginLogging = await ctx.service.loginLogging.getLoginLogs(ctx.session.sessionProject.id, ctx.session.sessionUser.accountId);
  758. const renderData = {
  759. accountData,
  760. // passwordJsValidator,
  761. loginLogging,
  762. loginWay,
  763. };
  764. await this.layout('profile/safe.ejs', renderData);
  765. }
  766. /**
  767. * 微信通知
  768. *
  769. * @param {object} ctx - egg全局变量
  770. * @return {void}
  771. */
  772. async wechat(ctx) {
  773. // 获取当前用户数据
  774. const sessionUser = ctx.session.sessionUser;
  775. // 获取账号数据
  776. const accountData = await ctx.service.projectAccount.getDataByCondition({ id: sessionUser.accountId });
  777. if (accountData.qywx_user_info !== null) {
  778. accountData.qywx_user_info = JSON.parse(accountData.qywx_user_info);
  779. }
  780. const renderData = {
  781. accountData,
  782. smsType: smsTypeConst.type,
  783. };
  784. await this.layout('profile/wechat.ejs', renderData, 'profile/wechat_modal.ejs');
  785. }
  786. /**
  787. * 微信解绑
  788. *
  789. * @param {object} ctx - egg全局变量
  790. * @return {void}
  791. */
  792. async removeWechat(ctx) {
  793. try {
  794. const sessionUser = ctx.session.sessionUser;
  795. // 获取账号数据
  796. const accountData = await ctx.service.projectAccount.getDataByCondition({ id: sessionUser.accountId });
  797. // 判断解绑类型
  798. if (ctx.request.body.data_type === 'wxWork') {
  799. const result = await ctx.service.projectAccount.bindWx4Work(sessionUser.accountId, null, null, null);
  800. if (!result) {
  801. throw '解绑企业微信失败!';
  802. }
  803. // 解绑成功通知
  804. const qywx = new wxWork(ctx);
  805. const desc = '您好,纵横云计量与企业微信解绑成功。';
  806. const content = [
  807. {
  808. keyname: '项目编号',
  809. value: ctx.session.sessionProject.code,
  810. },
  811. {
  812. keyname: '账号',
  813. value: sessionUser.account,
  814. },
  815. {
  816. keyname: '绑定时间',
  817. value: moment(new Date()).format('YYYY-MM-DD'),
  818. },
  819. {
  820. keyname: '备注',
  821. value: '感谢您的使用,要接收通知请重新绑定。',
  822. },
  823. ];
  824. const url = ctx.protocol + '://' + ctx.host + '/wx/tips?msg=解绑成功,感谢您的使用。';
  825. await qywx.sendTemplateCard([accountData.qywx_userid], accountData.qywx_corpid, '账号解绑成功通知', desc, content, url);
  826. this.setMessage('企业微信解绑成功', this.messageType.SUCCESS);
  827. } else {
  828. const result = await ctx.service.projectAccount.bindWx(sessionUser.accountId, null, null);
  829. if (!result) {
  830. throw '解绑微信失败!';
  831. }
  832. // 解绑成功通知
  833. const templateId = '0w0Yp65X4PHccTLeAyE5aQhS-blS-bylwxAPYEGy3CI';
  834. const url = '';
  835. const msgData = {
  836. first: {
  837. value: '您好,纵横云计量与微信解绑成功。',
  838. },
  839. keyword1: {
  840. value: ctx.session.sessionProject.code,
  841. },
  842. keyword2: {
  843. value: sessionUser.account,
  844. },
  845. keyword3: {
  846. value: moment(new Date()).format('YYYY-MM-DD'),
  847. },
  848. remark: {
  849. value: '感谢您的使用,要接收通知请重新绑定。',
  850. },
  851. };
  852. await app.wechat.api.sendTemplate(accountData.wx_openid, templateId, url, '', msgData);
  853. this.setMessage('微信解绑成功', this.messageType.SUCCESS);
  854. }
  855. } catch (error) {
  856. console.log(error);
  857. this.setMessage(error.toString(), this.messageType.ERROR);
  858. }
  859. ctx.redirect(ctx.request.header.referer);
  860. }
  861. }
  862. return ProfileController;
  863. };