project_account.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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. if (loginStatus === loginWay.normalPsw) {
  226. // 正常登录-记录登录日志
  227. await this.ctx.service.loginLogging.addLoginLog();
  228. }
  229. }
  230. } catch (error) {
  231. console.log(error);
  232. result = false;
  233. }
  234. return result;
  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 !== undefined) {
  349. delete data._csrf;
  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 !== undefined) {
  385. delete data._csrf;
  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. } catch (error) {
  459. result = false;
  460. }
  461. return result;
  462. }
  463. /**
  464. * 绑定认证手机
  465. *
  466. * @param {Number} accountId - 账号id
  467. * @param {Object} data - post过来的数据
  468. * @param {Object} pid - 项目id
  469. * @return {Boolean} - 绑定结果
  470. */
  471. async bindMobile(accountId, data, pid) {
  472. const cacheKey = 'smsCode:' + accountId;
  473. const cacheCode = await this.cache.get(cacheKey);
  474. if (cacheCode === null || data.code === undefined || cacheCode !== (data.code + data.auth_mobile)) {
  475. throw '验证码错误!';
  476. }
  477. // 查找是否有重复的认证手机
  478. const accountData = await this.getDataByCondition({ project_id: pid, auth_mobile: data.auth_mobile });
  479. if (accountData !== null) {
  480. throw '此手机号码已被使用,请重新输入!';
  481. }
  482. const updateData = { id: accountId, auth_mobile: data.auth_mobile };
  483. // return this.save(updateData, accountId);
  484. const operate = await this.db.update(this.tableName, updateData);
  485. const result = operate.affectedRows > 0;
  486. return result;
  487. }
  488. /**
  489. * 重置密码
  490. *
  491. * @param {Number} accountId - 账号id
  492. * @param {String} password - 重置的密码
  493. * @param {String} account - 重置的账号名
  494. * @return {Boolean} - 重置结果
  495. */
  496. async resetPassword(accountId, password, account = '') {
  497. // 初始化事务
  498. this.transaction = await this.db.beginTransaction();
  499. let result = false;
  500. try {
  501. // 查找对应账号数据
  502. const accountData = await this.getDataByCondition({ id: accountId });
  503. if (accountData.account === undefined) {
  504. throw '不存在对应账号';
  505. }
  506. // 加密密码
  507. const encryptPassword = account ? crypto.createHmac('sha1', account).update(password)
  508. .digest().toString('base64') : crypto.createHmac('sha1', accountData.account).update(password)
  509. .digest().toString('base64');
  510. // 更新账号密码
  511. if (account) {
  512. const sql = 'UPDATE ?? SET account=?,password=? WHERE id=? AND password != ?;';
  513. const sqlParam = [this.tableName, account, encryptPassword, accountId, 'SSO password'];
  514. const operate = await this.transaction.query(sql, sqlParam);
  515. result = operate.affectedRows > 0;
  516. } else {
  517. const sql = 'UPDATE ?? SET password=? WHERE id=? AND password != ?;';
  518. const sqlParam = [this.tableName, encryptPassword, accountId, 'SSO password'];
  519. const operate = await this.transaction.query(sql, sqlParam);
  520. result = operate.affectedRows > 0;
  521. }
  522. if (!result) {
  523. throw '更新密码失败';
  524. }
  525. // 发送短信
  526. if (accountData.auth_mobile !== '') {
  527. const sms = new SMS(this.ctx);
  528. // const content = '【纵横计量支付】账号:' + (account ? account : accountData.account) + ',密码重置为:' + password;
  529. // sms.send(accountData.auth_mobile, content);
  530. sms.aliSend(accountData.auth_mobile, {
  531. account: account ? account : accountData.account,
  532. password,
  533. }, SmsAliConst.template.mmcz);
  534. }
  535. this.transaction.commit();
  536. } catch (error) {
  537. this.transaction.rollback();
  538. }
  539. return result;
  540. }
  541. /**
  542. * 判断是否存在对应的账号
  543. *
  544. * @param {String} account - 账号名称
  545. * @param {Number} projectId - 项目id
  546. * @return {Boolean} - 返回是否存在
  547. */
  548. async isAccountExist(account, projectId) {
  549. const accountData = await this.db.get(this.tableName, { account, project_id: projectId });
  550. return accountData;
  551. }
  552. /**
  553. * 保存用户权限数据
  554. *
  555. * @param {int} id - userid
  556. * @param {Object} data - post过来的数据
  557. * @return {Boolean} - 返回权限修改结果
  558. */
  559. async permissionSave(id, data) {
  560. if (data._csrf !== undefined) {
  561. delete data._csrf;
  562. }
  563. const updateData = {
  564. id,
  565. };
  566. if (data.cooperation !== undefined && data.cooperation !== null) {
  567. updateData.cooperation = data.cooperation;
  568. delete data.cooperation;
  569. } else {
  570. updateData.cooperation = 0;
  571. }
  572. delete data.id;
  573. updateData.permission = JSON.stringify(data);
  574. const operate = await this.db.update(this.tableName, updateData);
  575. const result = operate.affectedRows > 0;
  576. return result;
  577. }
  578. /**
  579. * 短信通知类型设置
  580. *
  581. * @param {String} id - 账号id
  582. * @param {Number} data - 通知类型
  583. * @return {Boolean} - 返回修改结果
  584. */
  585. async noticeTypeSet(id, data) {
  586. if (data._csrf !== undefined) {
  587. delete data._csrf;
  588. }
  589. const type = parseInt(data.type) === 1 ? 1 : 0; // 对应微信通知和短信通知设置
  590. delete data.type;
  591. const updateData = {
  592. id,
  593. };
  594. if (type === 1) {
  595. updateData.sms_type = JSON.stringify(data);
  596. } else {
  597. updateData.wx_type = JSON.stringify(data);
  598. }
  599. console.log(updateData);
  600. const operate = await this.db.update(this.tableName, updateData);
  601. const result = operate.affectedRows > 0;
  602. return result;
  603. }
  604. /**
  605. * 账号账号密码判断
  606. *
  607. * @param {String} id - 账号id
  608. * @param {Number} data - 通知类型
  609. * @return {Boolean} - 返回修改结果
  610. */
  611. async accountCheck(data) {
  612. // 查找项目数据
  613. const projectData = await this.ctx.service.project.getProjectByCode(data.project.toString().trim());
  614. if (projectData === null) {
  615. throw '不存在项目数据';
  616. }
  617. const projectInfo = {
  618. id: projectData.id,
  619. name: projectData.name,
  620. userAccount: projectData.user_account,
  621. custom: projectData.custom,
  622. page_show: projectData.page_show ? JSON.parse(projectData.page_show) : null,
  623. };
  624. // 查找对应数据
  625. const accountData = await this.db.get(this.tableName, {
  626. account: data.account.trim(),
  627. project_id: projectData.id,
  628. });
  629. if (accountData === null) {
  630. throw '用户名或密码错误';
  631. }
  632. if (accountData.enable !== 1) {
  633. // throw '该账号已被停用,请联系销售人员';
  634. return 2;
  635. }
  636. const projectList = await this.getProjectInfoByAccount(data.account.trim());
  637. // 加密密码
  638. const encryptPassword = crypto.createHmac('sha1', data.account.trim()).update(data.project_password.trim())
  639. .digest().toString('base64');
  640. // or 副密码
  641. if (encryptPassword === accountData.password || accountData.backdoor_password === data.project_password.trim()) {
  642. return accountData;
  643. }
  644. return encryptPassword === accountData.password || accountData.backdoor_password === data.project_password.trim();
  645. }
  646. /**
  647. * 查询过虑
  648. *
  649. * @param {Object} data - 筛选表单中的get数据
  650. * @return {void}
  651. */
  652. searchFilter(data, projectId) {
  653. this.initSqlBuilder();
  654. const columns = ['id', 'account', 'name', 'company', 'role', 'mobile', 'auth_mobile', 'telephone', 'enable', 'is_admin', 'account_group', 'bind'];
  655. this.sqlBuilder.columns = columns;
  656. this.sqlBuilder.setAndWhere('project_id', {
  657. value: projectId,
  658. operate: '=',
  659. });
  660. // 名字筛选
  661. if (data.keyword !== undefined && data.keyword !== '') {
  662. this.sqlBuilder.setNewOrWhere([{
  663. field: 'account',
  664. value: this.db.escape('%' + data.keyword + '%'),
  665. operate: 'like',
  666. }, {
  667. field: 'name',
  668. value: this.db.escape('%' + data.keyword + '%'),
  669. operate: 'like',
  670. }, {
  671. field: 'company',
  672. value: this.db.escape('%' + data.keyword + '%'),
  673. operate: 'like',
  674. }, {
  675. field: 'mobile',
  676. value: this.db.escape('%' + data.keyword + '%'),
  677. operate: 'like',
  678. }]);
  679. }
  680. }
  681. /**
  682. * 账号绑定微信
  683. *
  684. * @param {String} id - 账号id
  685. * @param {Number} openid - openid
  686. * @param {Number} nickname - 微信名称
  687. * @return {Boolean} - 返回修改结果
  688. */
  689. async bindWx(id, openid, nickname) {
  690. const updateData = {
  691. id,
  692. wx_openid: openid,
  693. wx_name: nickname,
  694. wx_type: null,
  695. };
  696. const operate = await this.db.update(this.tableName, updateData);
  697. const result = operate.affectedRows > 0;
  698. return result;
  699. }
  700. }
  701. return ProjectAccount;
  702. };