project_account.js 30 KB

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