project_account.js 31 KB

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