profile_controller.js 39 KB

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