profile_controller.js 40 KB

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