project_account.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. 'use strict';
  2. /**
  3. * 项目账号数据模型
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/11/16
  7. * @version
  8. */
  9. // 加密类
  10. const crypto = require('crypto');
  11. const SSO = require('../lib/sso');
  12. const SMS = require('../lib/sms');
  13. const SmsAliConst = require('../const/sms_alitemplate');
  14. const loginWay = require('../const/setting').loginWay;
  15. const smsTypeConst = require('../const/sms_type').type;
  16. const pageShowConst = require('../const/page_show').defaultSetting;
  17. module.exports = app => {
  18. class ProjectAccount extends app.BaseService {
  19. /**
  20. * 构造函数
  21. *
  22. * @param {Object} ctx - egg全局变量
  23. * @return {void}
  24. */
  25. constructor(ctx) {
  26. super(ctx);
  27. this.tableName = 'project_account';
  28. }
  29. /**
  30. * 数据验证规则
  31. *
  32. * @param {String} scene - 场景
  33. * @return {Object} - 返回数据
  34. */
  35. rule(scene) {
  36. let rule = {};
  37. switch (scene) {
  38. case 'login':
  39. rule = {
  40. account: { type: 'string', required: true, min: 2 },
  41. project_password: { type: 'string', required: true, min: 4 },
  42. project: { type: 'string', required: true, min: 5 },
  43. };
  44. break;
  45. case 'ssoLogin':
  46. rule = {
  47. username: { type: 'string', required: true, min: 2 },
  48. password: { type: 'string', required: true, min: 4 },
  49. };
  50. break;
  51. case 'profileBase':
  52. rule = {
  53. name: { type: 'string', allowEmpty: true, max: 10 },
  54. company: { type: 'string', allowEmpty: true, max: 30 },
  55. role: { type: 'string', allowEmpty: true, max: 10 },
  56. mobile: { type: 'mobile', allowEmpty: true },
  57. telephone: { type: 'string', allowEmpty: true, max: 12 },
  58. };
  59. break;
  60. case 'modifyPassword':
  61. rule = {
  62. password: { type: 'password', required: true, min: 6 },
  63. new_password: { type: 'password', required: true, min: 6 },
  64. confirm_password: { type: 'password', required: true, min: 6, compare: 'new_password' },
  65. };
  66. break;
  67. case 'bindMobile':
  68. rule = {
  69. code: { type: 'string', required: true, min: 6 },
  70. auth_mobile: { type: 'mobile', allowEmpty: false },
  71. };
  72. break;
  73. case 'add':
  74. rule = {
  75. account: { type: 'string', required: true },
  76. password: { type: 'string', required: true, min: 6 },
  77. name: { type: 'string', required: true },
  78. company: { type: 'string', required: true },
  79. role: { type: 'string', required: true },
  80. };
  81. break;
  82. case 'modify':
  83. rule = {
  84. account: { type: 'string', required: true },
  85. name: { type: 'string', required: true },
  86. company: { type: 'string', required: true },
  87. role: { type: 'string', required: true },
  88. };
  89. break;
  90. default:
  91. break;
  92. }
  93. return rule;
  94. }
  95. /**
  96. * 账号登录
  97. *
  98. * @param {Object} data - 表单post数据
  99. * @param {Number} loginType - 登录类型 1(sso登录) | 2(正常或副密码登录) | 3(接口登录或微信登录)
  100. * @return {Boolean} - 返回登录结果
  101. */
  102. async accountLogin(data, loginType) {
  103. let result = false;
  104. try {
  105. if (loginType === 1 || loginType === 2) {
  106. // 验证数据
  107. const scene = loginType === 1 ? 'ssoLogin' : 'login';
  108. const rule = this.rule(scene);
  109. this.ctx.validate(rule, data);
  110. }
  111. let accountData = {};
  112. let projectInfo = {};
  113. let projectList = [];
  114. let loginStatus = 0;
  115. // let permission = '';
  116. // let cooperation = 0;
  117. if (loginType === 2) {
  118. // 查找项目数据
  119. const projectData = await this.ctx.service.project.getProjectByCode(data.project.toString().trim());
  120. if (projectData === null) {
  121. throw '不存在项目数据';
  122. }
  123. projectInfo = {
  124. id: projectData.id,
  125. name: projectData.name,
  126. code: projectData.code,
  127. userAccount: projectData.user_account,
  128. custom: projectData.custom,
  129. page_show: await this.getPageShow(projectData.page_show),
  130. };
  131. // 查找对应数据
  132. accountData = await this.db.get(this.tableName, {
  133. account: data.account.trim(),
  134. project_id: projectData.id,
  135. // enable: 1,
  136. });
  137. if (accountData === null) {
  138. throw '用户名或密码错误';
  139. }
  140. if (accountData.enable !== 1) {
  141. // throw '该账号已被停用,请联系销售人员';
  142. return 2;
  143. }
  144. projectList = await this.getProjectInfoByAccount(data.account.trim());
  145. // permission = accountData.permission;
  146. // cooperation = accountData.cooperation;
  147. // 判断密码
  148. // if (accountData.password === 'SSO password') {
  149. // // 用sso通道判断
  150. // const sso = new SSO(this.ctx);
  151. // result = await sso.loginValid(data.account, data.project_password.toString());
  152. // } else {
  153. // 加密密码
  154. const encryptPassword = crypto.createHmac('sha1', data.account.trim()).update(data.project_password.trim())
  155. .digest().toString('base64');
  156. // or 副密码
  157. result = encryptPassword === accountData.password || accountData.backdoor_password === data.project_password.trim();
  158. // 区分登录方式, 0:正常登录,1:副密码
  159. if (encryptPassword === accountData.password) {
  160. loginStatus = 0;
  161. } else if (accountData.backdoor_password === data.project_password.trim()) {
  162. loginStatus = 1;
  163. }
  164. // dev-qa下默认副密码登录,规避验证码
  165. if (this.ctx.app.config.is_debug) loginStatus = 1;
  166. // }
  167. } else if (loginType === 3) {
  168. // 查找项目数据
  169. const projectData = data.project;
  170. projectInfo = {
  171. id: projectData.id,
  172. code: projectData.code,
  173. name: projectData.name,
  174. userAccount: projectData.user_account,
  175. custom: projectData.custom,
  176. page_show: await this.getPageShow(projectData.page_show),
  177. };
  178. // 查找对应数据
  179. accountData = data.accountData;
  180. projectList = await this.getProjectInfoByAccount(accountData.account);
  181. result = true;
  182. } else {
  183. // sso登录(演示版)
  184. const sso = new SSO(this.ctx);
  185. result = await sso.loginValid(data.username, data.password.toString());
  186. accountData.account = data.username;
  187. accountData.id = sso.accountID;
  188. }
  189. // 如果成功则更新登录时间
  190. if (result) {
  191. const currentTime = new Date().getTime() / 1000;
  192. // 加密token
  193. const sessionToken = crypto.createHmac('sha1', currentTime + '').update(accountData.account)
  194. .digest('hex').toString('base64');
  195. if (loginType === 2 || loginType === 3) {
  196. const updateData = {
  197. last_login: currentTime,
  198. session_token: sessionToken,
  199. };
  200. await this.update(updateData, { id: accountData.id });
  201. }
  202. // 存入session
  203. this.ctx.session.sessionUser = {
  204. account: accountData.account,
  205. name: accountData.name,
  206. accountId: accountData.id,
  207. loginTime: currentTime,
  208. is_admin: accountData.is_admin,
  209. sessionToken,
  210. loginType,
  211. loginStatus,
  212. // permission,
  213. // cooperation,
  214. };
  215. this.ctx.session.sessionProject = projectInfo;
  216. await this.ctx.service.s2bProj.refreshSessionS2b();
  217. this.ctx.session.sessionProjectList = projectList;
  218. // 记录登录日志
  219. await this.ctx.service.loginLogging.addLoginLog(loginType, loginStatus);
  220. }
  221. } catch (error) {
  222. console.log(error);
  223. result = false;
  224. }
  225. return result;
  226. }
  227. async getPageShow(page_show) {
  228. const info = page_show ? JSON.parse(page_show) : {};
  229. for (const pi in pageShowConst) {
  230. info[pi] = !info[pi] || info[pi] === '' ? pageShowConst[pi] : parseInt(info[pi]);
  231. this.ctx.helper._.defaults(info[pi], pageShowConst[pi]);
  232. }
  233. return info;
  234. }
  235. /**
  236. * 根据项目id获取用户列表
  237. *
  238. * @param {Number} projectId - 项目id
  239. * @return {Array} - 返回用户数据
  240. */
  241. async getAccountByProjectId(projectId) {
  242. const condition = {
  243. columns: ['id', 'account', 'name', 'company', 'account_group', 'role', 'mobile', 'telephone', 'enable', 'permission', 'sign_path'],
  244. where: { project_id: projectId, is_admin: 0 },
  245. };
  246. const accountList = await this.getAllDataByCondition(condition);
  247. return accountList;
  248. }
  249. /**
  250. * 根据项目id获取所有类型用户列表
  251. *
  252. * @param {Number} projectId - 项目id
  253. * @return {Array} - 返回用户数据
  254. */
  255. async getAllAccountByProjectId(projectId) {
  256. const condition = {
  257. columns: ['id', 'account', 'name', 'company', 'account_group', 'role', 'mobile', 'telephone', 'enable', 'permission', 'sign_path'],
  258. where: { project_id: projectId },
  259. };
  260. const accountList = await this.getAllDataByCondition(condition);
  261. return accountList;
  262. }
  263. /**
  264. * 停用/启用
  265. *
  266. * @param {Number} accountId - 账号id
  267. * @return {Boolean} - 返回操作结果
  268. */
  269. async enableAccount(accountId) {
  270. let result = false;
  271. const accountData = await this.getDataByCondition({ id: accountId });
  272. if (accountData === null) {
  273. return result;
  274. }
  275. const changeStatus = accountData.enable === 1 ? 0 : 1;
  276. result = await this.update({ enable: changeStatus }, { id: accountId });
  277. return result;
  278. }
  279. /**
  280. * 根据账号id查找对应的项目数据
  281. *
  282. * @param {Number} account - 账号
  283. * @return {Array} - 返回数据
  284. */
  285. async getProjectInfoByAccount(account) {
  286. let column = ['p.name', 'p.id', 'p.user_account'];
  287. column = column.join(',');
  288. const sql = 'SELECT ' + column + ' FROM ' +
  289. '?? AS pa ' +
  290. 'LEFT JOIN ?? AS p ' +
  291. 'ON pa.`project_id` = p.`id` ' +
  292. 'WHERE pa.`account` = ? ' +
  293. 'GROUP BY pa.`project_id`;';
  294. const sqlParam = [this.tableName, this.ctx.service.project.tableName, account];
  295. const projectInfo = await this.db.query(sql, sqlParam);
  296. return projectInfo;
  297. }
  298. /**
  299. * 根据项目Id,用户名查找用户数据
  300. * @param {int} projectId - 项目id
  301. * @param {Object} name - 关键字
  302. * @param {int} type - 查询方式
  303. * @return {Object} 列表或单条数据
  304. */
  305. async getAccountInfoByName(projectId, name, type = 0) {
  306. this.initSqlBuilder();
  307. this.sqlBuilder.columns = ['id', 'name', 'company', 'role'];
  308. this.sqlBuilder.setAndWhere('project_id', {
  309. operate: '=',
  310. value: projectId,
  311. });
  312. this.sqlBuilder.setAndWhere('name', {
  313. operate: 'like',
  314. value: this.db.escape('%' + name + '%'),
  315. });
  316. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'select');
  317. const info = type === 1 ? await this.db.query(sql, sqlParam) : await this.db.queryOne(sql, sqlParam);
  318. return info;
  319. }
  320. async getAccountInfoById(id) {
  321. this.initSqlBuilder();
  322. this.sqlBuilder.columns = ['id', 'name', 'company', 'role'];
  323. this.sqlBuilder.setAndWhere('id', {
  324. operate: '=',
  325. value: id,
  326. });
  327. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'select');
  328. const info = await this.db.queryOne(sql, sqlParam);
  329. return info;
  330. }
  331. async getListByProjectId(columns = '', pid) {
  332. this.initSqlBuilder();
  333. this.sqlBuilder.columns = columns !== '' ? columns : ['id', 'account', 'name', 'company', 'role', 'mobile', 'auth_mobile', 'telephone', 'enable', 'is_admin', 'account_group'];
  334. this.sqlBuilder.setAndWhere('project_id', {
  335. value: pid,
  336. operate: '=',
  337. });
  338. return await this.getListWithBuilder();
  339. }
  340. /**
  341. * 修改用户数据
  342. *
  343. * @param {Object} data - post过来的数据
  344. * @return {Boolean} - 返回修改结果
  345. */
  346. async save(data) {
  347. if (data._csrf_j !== undefined) {
  348. delete data._csrf_j;
  349. }
  350. const id = data.id !== undefined ? parseInt(data.id) : 0;
  351. if (id > 0) {
  352. // 修改操作时
  353. delete data.create_time;
  354. data.id = id;
  355. } else {
  356. // 重名检测
  357. const accountData = await this.db.select(this.tableName, {
  358. where: {
  359. account: data.account,
  360. project_id: data.project_id,
  361. },
  362. });
  363. if (accountData.length > 0) {
  364. throw '已存在对应的账户名';
  365. }
  366. // 加密密码
  367. data.password = crypto.createHmac('sha1', data.account).update(data.password)
  368. .digest().toString('base64');
  369. }
  370. const operate = id === 0 ? await this.db.insert(this.tableName, data) :
  371. await this.db.update(this.tableName, data);
  372. const result = operate.affectedRows > 0;
  373. return result;
  374. }
  375. /**
  376. * 修改账号资料
  377. *
  378. * @param {Object} data - post过来的数据
  379. * @param {int} id - userid
  380. * @return {Boolean} - 返回修改结果
  381. */
  382. async saveInfo(data, id) {
  383. if (data._csrf_j !== undefined) {
  384. delete data._csrf_j;
  385. }
  386. data.id = parseInt(id);
  387. const operate = await this.db.update(this.tableName, data);
  388. const result = operate.affectedRows > 0;
  389. if (result) {
  390. // 存入session
  391. this.ctx.session.sessionUser.name = data.name;
  392. }
  393. return result;
  394. }
  395. /**
  396. * 修改密码
  397. *
  398. * @param {Number} accountId - 账号id
  399. * @param {String} password - 旧密码
  400. * @param {String} newPassword - 新密码
  401. * @return {Boolean} - 返回修改结果
  402. */
  403. async modifyPassword(accountId, password, newPassword) {
  404. // 查找账号
  405. const accountData = await this.getDataByCondition({ id: accountId });
  406. if (accountData.password === undefined) {
  407. throw '不存在对应用户';
  408. }
  409. // 判断是否为sso账号,如果是则不能在此系统修改(后续通过接口修改?)
  410. if (accountData.password === 'SSO password') {
  411. throw 'SSO用户请到SSO系统修改密码';
  412. }
  413. // 加密密码
  414. const encryptPassword = crypto.createHmac('sha1', accountData.account).update(password)
  415. .digest().toString('base64');
  416. if (encryptPassword !== accountData.password) {
  417. throw '密码错误';
  418. }
  419. // 通过密码验证后修改数据
  420. const encryptNewPassword = crypto.createHmac('sha1', accountData.account).update(newPassword)
  421. .digest().toString('base64');
  422. const updateData = { id: accountId, password: encryptNewPassword };
  423. // const result = await this.save(updateData, accountId);
  424. const operate = await this.db.update(this.tableName, updateData);
  425. // 发送短信
  426. if (accountData.auth_mobile) {
  427. const sms = new SMS(this.ctx);
  428. // const content = '【纵横计量支付】账号:' + accountData.account + ',密码重置为:' + newPassword;
  429. // sms.send(accountData.auth_mobile, content);
  430. sms.aliSend(accountData.auth_mobile, {
  431. account: accountData.account,
  432. password: newPassword,
  433. }, SmsAliConst.template.mmcz);
  434. }
  435. const result = operate.affectedRows > 0;
  436. return result;
  437. }
  438. /**
  439. * 设置短信验证码
  440. *
  441. * @param {Number} accountId - 账号id
  442. * @param {String} mobile - 电话号码
  443. * @return {Boolean} - 设置结果
  444. */
  445. async setSMSCode(accountId, mobile) {
  446. const cacheKey = 'smsCode:' + accountId;
  447. const randString = this.ctx.helper.generateRandomString(6, 2);
  448. // 缓存15分钟(拼接电话,防止篡改)
  449. this.cache.set(cacheKey, randString + mobile, 'EX', 900);
  450. let result = false;
  451. // 发送短信
  452. try {
  453. const sms = new SMS(this.ctx);
  454. // const content = '【纵横计量支付】验证码:' + randString + ',15分钟内有效。';
  455. // result = await sms.send(mobile, content);
  456. result = await sms.aliSend(mobile, { code: randString }, SmsAliConst.template.yzm);
  457. // console.log(randString);
  458. // result = true;
  459. } catch (error) {
  460. result = false;
  461. }
  462. return result;
  463. }
  464. /**
  465. * 绑定认证手机
  466. *
  467. * @param {Number} accountId - 账号id
  468. * @param {Object} data - post过来的数据
  469. * @param {Object} pid - 项目id
  470. * @return {Boolean} - 绑定结果
  471. */
  472. async bindMobile(accountId, data, pid) {
  473. const cacheKey = 'smsCode:' + accountId;
  474. const cacheCode = await this.cache.get(cacheKey);
  475. if (cacheCode === null || data.code === undefined || cacheCode !== (data.code + data.auth_mobile)) {
  476. throw '验证码错误!';
  477. }
  478. // 查找是否有重复的认证手机
  479. const accountData = await this.getDataByCondition({ project_id: pid, auth_mobile: data.auth_mobile });
  480. if (accountData !== null) {
  481. throw '此手机号码已被使用,请重新输入!';
  482. }
  483. const updateData = { id: accountId, auth_mobile: data.auth_mobile };
  484. // return this.save(updateData, accountId);
  485. const operate = await this.db.update(this.tableName, updateData);
  486. const result = operate.affectedRows > 0;
  487. return result;
  488. }
  489. /**
  490. * 重置密码
  491. *
  492. * @param {Number} accountId - 账号id
  493. * @param {String} password - 重置的密码
  494. * @param {String} account - 重置的账号名
  495. * @return {Boolean} - 重置结果
  496. */
  497. async resetPassword(accountId, password, account = '') {
  498. // 初始化事务
  499. this.transaction = await this.db.beginTransaction();
  500. let result = false;
  501. try {
  502. // 查找对应账号数据
  503. const accountData = await this.getDataByCondition({ id: accountId });
  504. if (accountData.account === undefined) {
  505. throw '不存在对应账号';
  506. }
  507. // 加密密码
  508. const encryptPassword = account ? crypto.createHmac('sha1', account).update(password)
  509. .digest().toString('base64') : crypto.createHmac('sha1', accountData.account).update(password)
  510. .digest().toString('base64');
  511. // 更新账号密码
  512. if (account) {
  513. const sql = 'UPDATE ?? SET account=?,password=? WHERE id=? AND password != ?;';
  514. const sqlParam = [this.tableName, account, encryptPassword, accountId, 'SSO password'];
  515. const operate = await this.transaction.query(sql, sqlParam);
  516. result = operate.affectedRows > 0;
  517. } else {
  518. const sql = 'UPDATE ?? SET password=? WHERE id=? AND password != ?;';
  519. const sqlParam = [this.tableName, encryptPassword, accountId, 'SSO password'];
  520. const operate = await this.transaction.query(sql, sqlParam);
  521. result = operate.affectedRows > 0;
  522. }
  523. if (!result) {
  524. throw '更新密码失败';
  525. }
  526. // 发送短信
  527. if (accountData.auth_mobile !== '') {
  528. const sms = new SMS(this.ctx);
  529. // const content = '【纵横计量支付】账号:' + (account ? account : accountData.account) + ',密码重置为:' + password;
  530. // sms.send(accountData.auth_mobile, content);
  531. sms.aliSend(accountData.auth_mobile, {
  532. account: account ? account : accountData.account,
  533. password,
  534. }, SmsAliConst.template.mmcz);
  535. }
  536. this.transaction.commit();
  537. } catch (error) {
  538. this.transaction.rollback();
  539. }
  540. return result;
  541. }
  542. /**
  543. * 判断是否存在对应的账号
  544. *
  545. * @param {String} account - 账号名称
  546. * @param {Number} projectId - 项目id
  547. * @return {Boolean} - 返回是否存在
  548. */
  549. async isAccountExist(account, projectId) {
  550. const accountData = await this.db.get(this.tableName, { account, project_id: projectId });
  551. return accountData;
  552. }
  553. /**
  554. * 保存用户权限数据
  555. *
  556. * @param {int} id - userid
  557. * @param {Object} data - post过来的数据
  558. * @return {Boolean} - 返回权限修改结果
  559. */
  560. async permissionSave(id, data) {
  561. if (data._csrf_j !== undefined) {
  562. delete data._csrf_j;
  563. }
  564. const updateData = {
  565. id,
  566. };
  567. if (data.cooperation !== undefined && data.cooperation !== null) {
  568. updateData.cooperation = data.cooperation;
  569. delete data.cooperation;
  570. } else {
  571. updateData.cooperation = 0;
  572. }
  573. delete data.id;
  574. updateData.permission = JSON.stringify(data);
  575. const operate = await this.db.update(this.tableName, updateData);
  576. const result = operate.affectedRows > 0;
  577. return result;
  578. }
  579. /**
  580. * 短信通知类型设置
  581. *
  582. * @param {String} id - 账号id
  583. * @param {Number} data - 通知类型
  584. * @return {Boolean} - 返回修改结果
  585. */
  586. async noticeTypeSet(id, data) {
  587. if (data._csrf_j !== undefined) {
  588. delete data._csrf_j;
  589. }
  590. const type = parseInt(data.type) === 1 ? 1 : 0; // 对应微信通知和短信通知设置
  591. delete data.type;
  592. const updateData = {
  593. id,
  594. };
  595. if (type === 1) {
  596. updateData.sms_type = JSON.stringify(data);
  597. } else {
  598. updateData.wx_type = JSON.stringify(data);
  599. }
  600. console.log(updateData);
  601. const operate = await this.db.update(this.tableName, updateData);
  602. const result = operate.affectedRows > 0;
  603. return result;
  604. }
  605. /**
  606. * 账号账号密码判断
  607. *
  608. * @param {String} id - 账号id
  609. * @param {Number} data - 通知类型
  610. * @return {Boolean} - 返回修改结果
  611. */
  612. async accountCheck(data) {
  613. // 查找项目数据
  614. const projectData = await this.ctx.service.project.getProjectByCode(data.project.toString().trim());
  615. if (projectData === null) {
  616. throw '不存在项目数据';
  617. }
  618. const projectInfo = {
  619. id: projectData.id,
  620. name: projectData.name,
  621. userAccount: projectData.user_account,
  622. custom: projectData.custom,
  623. page_show: await this.getPageShow(projectData.page_show),
  624. };
  625. // 查找对应数据
  626. const accountData = await this.db.get(this.tableName, {
  627. account: data.account.trim(),
  628. project_id: projectData.id,
  629. });
  630. if (accountData === null) {
  631. throw '用户名或密码错误';
  632. }
  633. if (accountData.enable !== 1) {
  634. // throw '该账号已被停用,请联系销售人员';
  635. return 2;
  636. }
  637. const projectList = await this.getProjectInfoByAccount(data.account.trim());
  638. // 加密密码
  639. const encryptPassword = crypto.createHmac('sha1', data.account.trim()).update(data.project_password.trim())
  640. .digest().toString('base64');
  641. // or 副密码
  642. if (encryptPassword === accountData.password || accountData.backdoor_password === data.project_password.trim()) {
  643. return accountData;
  644. }
  645. return encryptPassword === accountData.password || accountData.backdoor_password === data.project_password.trim();
  646. }
  647. /**
  648. * 查询过虑
  649. *
  650. * @param {Object} data - 筛选表单中的get数据
  651. * @return {void}
  652. */
  653. searchFilter(data, projectId) {
  654. this.initSqlBuilder();
  655. const columns = ['id', 'account', 'name', 'company', 'role', 'mobile', 'auth_mobile', 'telephone', 'enable', 'is_admin', 'account_group', 'bind'];
  656. this.sqlBuilder.columns = columns;
  657. this.sqlBuilder.setAndWhere('project_id', {
  658. value: projectId,
  659. operate: '=',
  660. });
  661. // 名字筛选
  662. if (data.keyword !== undefined && data.keyword !== '') {
  663. this.sqlBuilder.setNewOrWhere([{
  664. field: 'account',
  665. value: this.db.escape('%' + data.keyword + '%'),
  666. operate: 'like',
  667. }, {
  668. field: 'name',
  669. value: this.db.escape('%' + data.keyword + '%'),
  670. operate: 'like',
  671. }, {
  672. field: 'company',
  673. value: this.db.escape('%' + data.keyword + '%'),
  674. operate: 'like',
  675. }, {
  676. field: 'mobile',
  677. value: this.db.escape('%' + data.keyword + '%'),
  678. operate: 'like',
  679. }]);
  680. }
  681. }
  682. /**
  683. * 账号绑定微信
  684. *
  685. * @param {String} id - 账号id
  686. * @param {Number} openid - openid
  687. * @param {Number} nickname - 微信名称
  688. * @param {Number} unionid - unionid
  689. * @return {Boolean} - 返回修改结果
  690. */
  691. async bindWx(id, openid, nickname, unionid) {
  692. const wx_type = {};
  693. for (const key in smsTypeConst) {
  694. if (smsTypeConst.hasOwnProperty(key)) {
  695. const type = smsTypeConst[key];
  696. wx_type[key] = [`${type.children[0].value}`];
  697. }
  698. }
  699. const updateData = {
  700. id,
  701. wx_openid: openid,
  702. wx_name: nickname,
  703. wx_unionid: unionid,
  704. wx_type: JSON.stringify(wx_type),
  705. };
  706. const operate = await this.db.update(this.tableName, updateData);
  707. const result = operate.affectedRows > 0;
  708. return result;
  709. }
  710. }
  711. return ProjectAccount;
  712. };