project_account.js 30 KB

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