profile_controller.js 31 KB

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