profile_controller.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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 'compilation':
  386. if (!dskAccount.dsk_account) {
  387. throw '未绑定大司空账号';
  388. }
  389. const account = JSON.parse(dskAccount.dsk_account);
  390. const compilation = await dsk.getCompilation(data.mobile);
  391. response.data = {};
  392. response.data.compilation = compilation;
  393. if (data.getProject) {
  394. const compilationInfo = data.compilationId ? ctx.helper._.find(compilation, { ID: data.compilationId }) : null;
  395. const compilationId = compilationInfo ? compilationInfo.ID : (compilation.length > 0 ? compilation[0].ID : null);
  396. response.data.select_compilation = compilationId;
  397. const projectInfo = compilationId ? await dsk.getProjectList(account.mobile, compilationId) : [];
  398. response.data.project = projectInfo;
  399. }
  400. response.data.dskAccountData = account;
  401. break;
  402. case 'project':
  403. if (!dskAccount.dsk_account) {
  404. throw '未绑定大司空账号';
  405. }
  406. const account2 = JSON.parse(dskAccount.dsk_account);
  407. response.data = await dsk.getProjectList(account2.mobile, data.compilationId);
  408. break;
  409. case 'save_projects':
  410. if (!dskAccount.dsk_account) {
  411. throw '未绑定大司空账号';
  412. }
  413. const tender = await ctx.service.tender.getDataById(data.tid);
  414. if (!tender || tender.user_id !== sessionUser.accountId) {
  415. throw '无权限操作';
  416. }
  417. await ctx.service.projectAccount.saveDskProjects(sessionUser.accountId, data.project_list);
  418. const account3 = JSON.parse(dskAccount.dsk_account);
  419. if (data.project_id && ctx.helper._.findIndex(data.project_list, { pid: data.project_id }) !== -1) {
  420. account3.select_project = data.project_id;
  421. } else {
  422. account3.select_project = data.project_list && data.project_list.length > 0 ? data.project_list[0].pid : null;
  423. }
  424. await ctx.service.projectAccount.defaultUpdate({ id: sessionUser.accountId, dsk_account: JSON.stringify(account3) });
  425. response.data = account3.select_project;
  426. break;
  427. case 'project_tree':
  428. response.data = await dsk.getProjectTree(data.compilationId, data.projectId);
  429. const tender2 = data.tid ? await ctx.service.tender.getDataById(data.tid) : null;
  430. if (tender2 && tender2.user_id === sessionUser.accountId && dskAccount.dsk_account) {
  431. const account4 = JSON.parse(dskAccount.dsk_account);
  432. account4.select_project = data.projectId;
  433. let treeId = null;
  434. if (response.data && response.data.length > 0) {
  435. const treeInfo = ctx.helper._.find(response.data, { type: 4 });
  436. if (treeInfo && treeInfo.ID) {
  437. treeId = treeInfo.ID;
  438. }
  439. }
  440. account4.select_tree = treeId;
  441. await ctx.service.projectAccount.defaultUpdate({ id: sessionUser.accountId, dsk_account: JSON.stringify(account4) });
  442. }
  443. break;
  444. case 'project_bills':
  445. response.data = await dsk.getProjectBills(data.compilationId, data.treeId);
  446. const tender3 = data.tid ? await ctx.service.tender.getDataById(data.tid) : null;
  447. if (tender3 && tender3.user_id === sessionUser.accountId && dskAccount.dsk_account) {
  448. const account5 = JSON.parse(dskAccount.dsk_account);
  449. account5.select_tree = data.treeId;
  450. await ctx.service.projectAccount.defaultUpdate({ id: sessionUser.accountId, dsk_account: JSON.stringify(account5) });
  451. }
  452. break;
  453. default:throw '参数有误';
  454. }
  455. } catch (error) {
  456. response.err = 1;
  457. response.msg = error.toString();
  458. }
  459. ctx.body = response;
  460. }
  461. /**
  462. * 电子签名
  463. *
  464. * @param {object} ctx - egg全局变量
  465. * @return {void}
  466. */
  467. async sign(ctx) {
  468. // 获取当前用户数据
  469. const sessionUser = ctx.session.sessionUser;
  470. // 获取账号数据
  471. const accountData = await ctx.service.projectAccount.getDataByCondition({ id: sessionUser.accountId });
  472. const renderData = {
  473. accountData,
  474. fujianOssPath: ctx.app.config.fujianOssPath,
  475. };
  476. await this.layout('profile/sign.ejs', renderData);
  477. }
  478. /**
  479. * 网证通电子签名页
  480. *
  481. * @param {object} ctx - egg全局变量
  482. * @return {void}
  483. */
  484. async netcasign(ctx) {
  485. // 获取当前用户数据
  486. const sessionUser = ctx.session.sessionUser;
  487. // 获取账号数据
  488. const accountData = await ctx.service.projectAccount.getDataByCondition({ id: sessionUser.accountId });
  489. const signData = await ctx.service.netcasign.getDataByCondition({ uid: sessionUser.accountId });
  490. const renderData = {
  491. accountData,
  492. signData,
  493. };
  494. await this.layout('profile/netcasign.ejs', renderData, 'profile/sign_modal.ejs');
  495. }
  496. /**
  497. * 网证通电子签名页面操作
  498. *
  499. * @param {object} ctx - egg全局变量
  500. * @return {void}
  501. */
  502. async signSave(ctx) {
  503. const response = {
  504. err: 0,
  505. msg: '',
  506. };
  507. try {
  508. const sessionUser = ctx.session.sessionUser;
  509. const data = JSON.parse(ctx.request.body.data);
  510. let signData;
  511. switch (data.type) {
  512. case 'bind':
  513. signData = await ctx.service.netcasign.getDataByCondition({ pid: ctx.session.sessionProject.id, keyId: data.updateData.keyId });
  514. if (signData) {
  515. const accountData = await ctx.service.projectAccount.getDataByCondition({ id: signData.uid });
  516. throw '该Ukey已绑定于 ' + accountData.name + ', 不可重复绑定';
  517. }
  518. const result = await ctx.service.netcasign.add(data.updateData, sessionUser.accountId);
  519. if (!result) {
  520. throw '绑定Ukey失败';
  521. }
  522. response.data = await ctx.service.netcasign.getDataByCondition({ uid: sessionUser.accountId });
  523. break;
  524. case 'unbind':
  525. signData = await ctx.service.netcasign.getDataByCondition({ pid: ctx.session.sessionProject.id, uid: sessionUser.accountId });
  526. if (!signData) {
  527. throw '当前用户不存在绑定证书,解除绑定失败';
  528. }
  529. await ctx.service.netcasign.del(signData.id);
  530. break;
  531. case 'savesign':
  532. signData = await ctx.service.netcasign.getDataByCondition({ pid: ctx.session.sessionProject.id, uid: sessionUser.accountId });
  533. if (!signData) {
  534. throw '当前用户不存在绑定证书';
  535. }
  536. await ctx.service.netcasign.save({ sign_base64: data.sign_base64 }, signData.id);
  537. break;
  538. case 'delsign':
  539. signData = await ctx.service.netcasign.getDataByCondition({ pid: ctx.session.sessionProject.id, uid: sessionUser.accountId });
  540. if (!signData) {
  541. throw '当前用户不存在绑定证书';
  542. }
  543. if (signData && !signData.sign_base64) {
  544. throw '当前用户不存在签名,移除签名失败';
  545. }
  546. await ctx.service.netcasign.save({ sign_base64: null }, signData.id);
  547. break;
  548. default:throw '参数有误';
  549. }
  550. } catch (error) {
  551. response.err = 1;
  552. response.msg = error.toString();
  553. }
  554. ctx.body = response;
  555. }
  556. /**
  557. * 电子签名删除
  558. *
  559. * @param {object} ctx - egg全局变量
  560. * @return {void}
  561. */
  562. async signDelete(ctx) {
  563. const response = {
  564. err: 0,
  565. msg: '',
  566. };
  567. try {
  568. const sessionUser = ctx.session.sessionUser;
  569. // 获取账号数据
  570. const accountData = await ctx.service.projectAccount.getDataByCondition({ id: sessionUser.accountId });
  571. const data = JSON.parse(ctx.request.body.data);
  572. let result = false;
  573. if (data.type && data.type === 'stamp') {
  574. if (!accountData.stamp_path) {
  575. throw '不存在签章';
  576. }
  577. const stamp_path_list = accountData.stamp_path.split('!;!');
  578. const spIndex = ctx.helper._.indexOf(stamp_path_list, data.src);
  579. if (spIndex === -1) {
  580. throw '不存在此签章';
  581. }
  582. // 不删除地址,只删除数据库数据,防止已签章的报表丢失
  583. // await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + stamp_path_list[spIndex]);
  584. stamp_path_list.splice(spIndex, 1);
  585. // 删除库
  586. result = await ctx.service.projectAccount.update({ stamp_path: stamp_path_list.length === 0 ? null : stamp_path_list.join('!;!') }, { id: sessionUser.accountId });
  587. } else {
  588. if (accountData.sign_path === '') {
  589. throw '不存在签名';
  590. }
  591. result = await ctx.service.projectAccount.update({ sign_path: '' }, { id: sessionUser.accountId });
  592. }
  593. if (!result) {
  594. throw '移除签名失败';
  595. }
  596. } catch (error) {
  597. response.err = 1;
  598. response.msg = error.toString();
  599. }
  600. ctx.body = response;
  601. }
  602. /**
  603. * 生成二维码
  604. *
  605. * @param {object} ctx - egg全局变量
  606. * @return {void}
  607. */
  608. async qrCode(ctx) {
  609. const size = 5;
  610. const margin = 1;
  611. try {
  612. // 获取当前用户数据
  613. const sessionUser = ctx.session.sessionUser;
  614. let text = ctx.protocol + '://' + ctx.host + '/sign?user_id=' + sessionUser.accountId + '&app_token=' + sessionUser.sessionToken;
  615. if (ctx.query.from === 'netcasign') {
  616. text += '&from=netcasign';
  617. }
  618. // 大小默认5,二维码周围间距默认1
  619. const img = qr.image(text || '', { type: 'png', size: size || 5, margin: margin || 1 });
  620. ctx.status = 200;
  621. ctx.type = 'image/png';
  622. ctx.body = img;
  623. } catch (e) {
  624. ctx.status = 414;
  625. ctx.set('Content-Type', 'text/html');
  626. ctx.body = '<h1>414 Request-URI Too Large</h1>';
  627. }
  628. }
  629. /**
  630. * 上传签名图
  631. *
  632. * @param {object} ctx - egg全局变量
  633. * @return {void}
  634. */
  635. async signUpload(ctx) {
  636. const responseData = {
  637. err: 0, msg: '', data: null,
  638. };
  639. try {
  640. const stream = await ctx.getFileStream();
  641. const create_time = Date.parse(new Date()) / 1000;
  642. const fileInfo = path.parse(stream.filename);
  643. if (stream.fields && stream.fields.type && stream.fields.type === 'stamp') {
  644. // const dirName = 'app/public/upload/sign/profile';
  645. // const fileName = moment().format('YYYYMMDD') + '_sign_' + create_time + fileInfo.ext;
  646. const filepath = `app/public/upload/sign/profile/qianzhang_${create_time + fileInfo.ext}`;
  647. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  648. await sendToWormhole(stream);
  649. const result = await ctx.service.projectAccount.update({ stamp_path: filepath }, { id: ctx.session.sessionUser.accountId });
  650. if (result) {
  651. responseData.data = { stamp_path: filepath };
  652. } else {
  653. throw '添加数据库失败';
  654. }
  655. } else {
  656. const dirName = 'public/upload/sign';
  657. const fileName = moment().format('YYYYMMDD') + '_sign_' + create_time + fileInfo.ext;
  658. await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, 'app', dirName, fileName));
  659. await sendToWormhole(stream);
  660. const result = await ctx.service.projectAccount.update({ sign_path: fileName }, { id: ctx.session.sessionUser.accountId });
  661. if (result) {
  662. responseData.data = { sign_path: fileName };
  663. } else {
  664. throw '添加数据库失败';
  665. }
  666. }
  667. } catch (err) {
  668. this.log(err);
  669. responseData.err = 1;
  670. responseData.msg = err;
  671. }
  672. ctx.body = responseData;
  673. }
  674. /**
  675. * 上传签章图(多选)
  676. *
  677. * @param {object} ctx - egg全局变量
  678. * @return {void}
  679. */
  680. async stampUpload(ctx) {
  681. const responseData = {
  682. err: 0, msg: '', data: null,
  683. };
  684. let stream;
  685. try {
  686. const parts = ctx.multipart({ autoFields: true });
  687. const paths = [];
  688. let index = 0;
  689. stream = await parts();
  690. while (stream !== undefined) {
  691. // 判断用户是否选择上传文件
  692. if (!stream.filename) {
  693. throw '请选择上传的文件!';
  694. }
  695. const fileInfo = path.parse(stream.filename);
  696. const create_time = Date.parse(new Date()) / 1000;
  697. const filepath = `app/public/upload/sign/profile/qianzhang_${create_time + index.toString() + fileInfo.ext}`;
  698. // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, filepath));
  699. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  700. if (stream) {
  701. await sendToWormhole(stream);
  702. }
  703. paths.push(filepath);
  704. ++index;
  705. if (Array.isArray(parts.field.size) && index < parts.field.size.length) {
  706. stream = await parts();
  707. } else {
  708. stream = undefined;
  709. }
  710. }
  711. // 获取账号数据
  712. const accountData = await ctx.service.projectAccount.getDataByCondition({ id: ctx.session.sessionUser.accountId });
  713. const stamp_path = accountData.stamp_path ? accountData.stamp_path.split('!;!') : [];
  714. const stamp_path_list = ctx.helper._.concat(stamp_path, paths);
  715. const result = await ctx.service.projectAccount.update({ stamp_path: stamp_path_list.join('!;!') }, { id: ctx.session.sessionUser.accountId });
  716. if (result) {
  717. responseData.data = { stamp_path: stamp_path_list };
  718. } else {
  719. throw '添加数据库失败';
  720. }
  721. } catch (err) {
  722. this.log(err);
  723. // 失败需要消耗掉stream 以防卡死
  724. if (stream) {
  725. await sendToWormhole(stream);
  726. }
  727. responseData.err = 1;
  728. responseData.msg = err.toString();
  729. }
  730. ctx.body = responseData;
  731. }
  732. /**
  733. * 账号安全
  734. *
  735. * @param {object} ctx - egg全局变量
  736. * @return {void}
  737. */
  738. async safe(ctx) {
  739. // 获取当前用户数据
  740. const sessionUser = ctx.session.sessionUser;
  741. // 获取账号数据
  742. const accountData = await ctx.service.projectAccount.getDataByCondition({ id: sessionUser.accountId });
  743. // 获取修改密码的字段规则
  744. // const passwordRule = ctx.service.projectAccount.rule('modifyPassword');
  745. // const passwordJsValidator = await this.jsValidator.convert(passwordRule).setSelector('#password-form').build();
  746. // console.log(passwordJsValidator);
  747. // 获取登录日志
  748. const loginLogging = await ctx.service.loginLogging.getLoginLogs(ctx.session.sessionProject.id, ctx.session.sessionUser.accountId);
  749. const renderData = {
  750. accountData,
  751. // passwordJsValidator,
  752. loginLogging,
  753. loginWay,
  754. };
  755. await this.layout('profile/safe.ejs', renderData);
  756. }
  757. /**
  758. * 微信通知
  759. *
  760. * @param {object} ctx - egg全局变量
  761. * @return {void}
  762. */
  763. async wechat(ctx) {
  764. // 获取当前用户数据
  765. const sessionUser = ctx.session.sessionUser;
  766. // 获取账号数据
  767. const accountData = await ctx.service.projectAccount.getDataByCondition({ id: sessionUser.accountId });
  768. if (accountData.qywx_user_info !== null) {
  769. accountData.qywx_user_info = JSON.parse(accountData.qywx_user_info);
  770. }
  771. const renderData = {
  772. accountData,
  773. smsType: smsTypeConst.type,
  774. };
  775. await this.layout('profile/wechat.ejs', renderData, 'profile/wechat_modal.ejs');
  776. }
  777. /**
  778. * 微信解绑
  779. *
  780. * @param {object} ctx - egg全局变量
  781. * @return {void}
  782. */
  783. async removeWechat(ctx) {
  784. try {
  785. const sessionUser = ctx.session.sessionUser;
  786. // 获取账号数据
  787. const accountData = await ctx.service.projectAccount.getDataByCondition({ id: sessionUser.accountId });
  788. // 判断解绑类型
  789. if (ctx.request.body.data_type === 'wxWork') {
  790. const result = await ctx.service.projectAccount.bindWx4Work(sessionUser.accountId, null, null, null);
  791. if (!result) {
  792. throw '解绑企业微信失败!';
  793. }
  794. // 解绑成功通知
  795. const qywx = new wxWork(ctx);
  796. const desc = '您好,纵横云计量与企业微信解绑成功。';
  797. const content = [
  798. {
  799. keyname: '项目编号',
  800. value: ctx.session.sessionProject.code,
  801. },
  802. {
  803. keyname: '账号',
  804. value: sessionUser.account,
  805. },
  806. {
  807. keyname: '绑定时间',
  808. value: moment(new Date()).format('YYYY-MM-DD'),
  809. },
  810. {
  811. keyname: '备注',
  812. value: '感谢您的使用,要接收通知请重新绑定。',
  813. },
  814. ];
  815. const url = ctx.protocol + '://' + ctx.host + '/wx/tips?msg=解绑成功,感谢您的使用。';
  816. await qywx.sendTemplateCard([accountData.qywx_userid], accountData.qywx_corpid, '账号解绑成功通知', desc, content, url);
  817. this.setMessage('企业微信解绑成功', this.messageType.SUCCESS);
  818. } else {
  819. const result = await ctx.service.projectAccount.bindWx(sessionUser.accountId, null, null);
  820. if (!result) {
  821. throw '解绑微信失败!';
  822. }
  823. // 解绑成功通知
  824. const templateId = '0w0Yp65X4PHccTLeAyE5aQhS-blS-bylwxAPYEGy3CI';
  825. const url = '';
  826. const msgData = {
  827. first: {
  828. value: '您好,纵横云计量与微信解绑成功。',
  829. },
  830. keyword1: {
  831. value: ctx.session.sessionProject.code,
  832. },
  833. keyword2: {
  834. value: sessionUser.account,
  835. },
  836. keyword3: {
  837. value: moment(new Date()).format('YYYY-MM-DD'),
  838. },
  839. remark: {
  840. value: '感谢您的使用,要接收通知请重新绑定。',
  841. },
  842. };
  843. await app.wechat.api.sendTemplate(accountData.wx_openid, templateId, url, '', msgData);
  844. this.setMessage('微信解绑成功', this.messageType.SUCCESS);
  845. }
  846. } catch (error) {
  847. console.log(error);
  848. this.setMessage(error.toString(), this.messageType.ERROR);
  849. }
  850. ctx.redirect(ctx.request.header.referer);
  851. }
  852. }
  853. return ProfileController;
  854. };