project_account.js 31 KB

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