project_account.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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 getAccountInfoByAccountWithPid(account, project_id) {
  335. if (!account || !project_id) throw new Error('参数错误');
  336. this.initSqlBuilder();
  337. this.sqlBuilder.columns = ['account', 'name', 'company', 'role', 'is_admin', 'enable', 'telephone', 'mobile', 'account_group'];
  338. this.sqlBuilder.setAndWhere('account', {
  339. operate: '=',
  340. value: `"${account}"`,
  341. });
  342. this.sqlBuilder.setAndWhere('project_id', {
  343. operate: '=',
  344. value: project_id,
  345. });
  346. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'select');
  347. const info = await this.db.queryOne(sql, sqlParam);
  348. return info;
  349. }
  350. async getListByProjectId(columns = '', pid) {
  351. this.initSqlBuilder();
  352. this.sqlBuilder.columns = columns !== '' ? columns : ['id', 'account', 'name', 'company', 'role', 'mobile', 'auth_mobile', 'telephone', 'enable', 'is_admin', 'account_group'];
  353. this.sqlBuilder.setAndWhere('project_id', {
  354. value: pid,
  355. operate: '=',
  356. });
  357. return await this.getListWithBuilder();
  358. }
  359. /**
  360. * 修改用户数据
  361. *
  362. * @param {Object} data - post过来的数据
  363. * @return {Boolean} - 返回修改结果
  364. */
  365. async save(data) {
  366. if (data._csrf_j !== undefined) {
  367. delete data._csrf_j;
  368. }
  369. const id = data.id !== undefined ? parseInt(data.id) : 0;
  370. if (id > 0) {
  371. // 修改操作时
  372. delete data.create_time;
  373. data.id = id;
  374. } else {
  375. // 重名检测
  376. const accountData = await this.db.select(this.tableName, {
  377. where: {
  378. account: data.account,
  379. project_id: data.project_id,
  380. },
  381. });
  382. if (accountData.length > 0) {
  383. throw '已存在对应的账户名';
  384. }
  385. // 加密密码
  386. data.password = crypto.createHmac('sha1', data.account).update(data.password)
  387. .digest().toString('base64');
  388. }
  389. const operate = id === 0 ? await this.db.insert(this.tableName, data) :
  390. await this.db.update(this.tableName, data);
  391. const result = operate.affectedRows > 0;
  392. return result;
  393. }
  394. /**
  395. * 修改账号资料
  396. *
  397. * @param {Object} data - post过来的数据
  398. * @param {int} id - userid
  399. * @return {Boolean} - 返回修改结果
  400. */
  401. async saveInfo(data, id) {
  402. if (data._csrf_j !== undefined) {
  403. delete data._csrf_j;
  404. }
  405. data.id = parseInt(id);
  406. const operate = await this.db.update(this.tableName, data);
  407. const result = operate.affectedRows > 0;
  408. if (result) {
  409. // 存入session
  410. this.ctx.session.sessionUser.name = data.name;
  411. }
  412. return result;
  413. }
  414. /**
  415. * 修改密码
  416. *
  417. * @param {Number} accountId - 账号id
  418. * @param {String} password - 旧密码
  419. * @param {String} newPassword - 新密码
  420. * @return {Boolean} - 返回修改结果
  421. */
  422. async modifyPassword(accountId, password, newPassword) {
  423. // 查找账号
  424. const accountData = await this.getDataByCondition({ id: accountId });
  425. if (accountData.password === undefined) {
  426. throw '不存在对应用户';
  427. }
  428. // 判断是否为sso账号,如果是则不能在此系统修改(后续通过接口修改?)
  429. if (accountData.password === 'SSO password') {
  430. throw 'SSO用户请到SSO系统修改密码';
  431. }
  432. // 加密密码
  433. const encryptPassword = crypto.createHmac('sha1', accountData.account).update(password)
  434. .digest().toString('base64');
  435. if (encryptPassword !== accountData.password) {
  436. throw '密码错误';
  437. }
  438. // 通过密码验证后修改数据
  439. const encryptNewPassword = crypto.createHmac('sha1', accountData.account).update(newPassword)
  440. .digest().toString('base64');
  441. const updateData = { id: accountId, password: encryptNewPassword };
  442. // const result = await this.save(updateData, accountId);
  443. const operate = await this.db.update(this.tableName, updateData);
  444. // 发送短信
  445. if (accountData.auth_mobile) {
  446. const sms = new SMS(this.ctx);
  447. // const content = '【纵横计量支付】账号:' + accountData.account + ',密码重置为:' + newPassword;
  448. // sms.send(accountData.auth_mobile, content);
  449. sms.aliSend(accountData.auth_mobile, {
  450. account: accountData.account,
  451. password: newPassword,
  452. }, SmsAliConst.template.mmcz);
  453. }
  454. const result = operate.affectedRows > 0;
  455. return result;
  456. }
  457. /**
  458. * 设置短信验证码
  459. *
  460. * @param {Number} accountId - 账号id
  461. * @param {String} mobile - 电话号码
  462. * @return {Boolean} - 设置结果
  463. */
  464. async setSMSCode(accountId, mobile) {
  465. const cacheKey = 'smsCode:' + accountId;
  466. const randString = this.ctx.helper.generateRandomString(6, 2);
  467. // 缓存15分钟(拼接电话,防止篡改)
  468. this.cache.set(cacheKey, randString + mobile, 'EX', 900);
  469. let result = false;
  470. // 发送短信
  471. try {
  472. const sms = new SMS(this.ctx);
  473. // const content = '【纵横计量支付】验证码:' + randString + ',15分钟内有效。';
  474. // result = await sms.send(mobile, content);
  475. result = await sms.aliSend(mobile, { code: randString }, SmsAliConst.template.yzm);
  476. // console.log(randString);
  477. // result = true;
  478. } catch (error) {
  479. result = false;
  480. }
  481. return result;
  482. }
  483. /**
  484. * 绑定认证手机
  485. *
  486. * @param {Number} accountId - 账号id
  487. * @param {Object} data - post过来的数据
  488. * @param {Object} pid - 项目id
  489. * @return {Boolean} - 绑定结果
  490. */
  491. async bindMobile(accountId, data, pid) {
  492. const cacheKey = 'smsCode:' + accountId;
  493. const cacheCode = await this.cache.get(cacheKey);
  494. if (cacheCode === null || data.code === undefined || cacheCode !== (data.code + data.auth_mobile)) {
  495. throw '验证码错误!';
  496. }
  497. // 查找是否有重复的认证手机
  498. const accountData = await this.getDataByCondition({ project_id: pid, auth_mobile: data.auth_mobile });
  499. if (accountData !== null) {
  500. throw '此手机号码已被使用,请重新输入!';
  501. }
  502. const updateData = { id: accountId, auth_mobile: data.auth_mobile };
  503. // return this.save(updateData, accountId);
  504. const operate = await this.db.update(this.tableName, updateData);
  505. const result = operate.affectedRows > 0;
  506. return result;
  507. }
  508. /**
  509. * 重置密码
  510. *
  511. * @param {Number} accountId - 账号id
  512. * @param {String} password - 重置的密码
  513. * @param {String} account - 重置的账号名
  514. * @return {Boolean} - 重置结果
  515. */
  516. async resetPassword(accountId, password, account = '') {
  517. // 初始化事务
  518. this.transaction = await this.db.beginTransaction();
  519. let result = false;
  520. try {
  521. // 查找对应账号数据
  522. const accountData = await this.getDataByCondition({ id: accountId });
  523. if (accountData.account === undefined) {
  524. throw '不存在对应账号';
  525. }
  526. // 加密密码
  527. const encryptPassword = account ? crypto.createHmac('sha1', account).update(password)
  528. .digest().toString('base64') : crypto.createHmac('sha1', accountData.account).update(password)
  529. .digest().toString('base64');
  530. // 更新账号密码
  531. if (account) {
  532. const sql = 'UPDATE ?? SET account=?,password=? WHERE id=? AND password != ?;';
  533. const sqlParam = [this.tableName, account, encryptPassword, accountId, 'SSO password'];
  534. const operate = await this.transaction.query(sql, sqlParam);
  535. result = operate.affectedRows > 0;
  536. } else {
  537. const sql = 'UPDATE ?? SET password=? WHERE id=? AND password != ?;';
  538. const sqlParam = [this.tableName, encryptPassword, accountId, 'SSO password'];
  539. const operate = await this.transaction.query(sql, sqlParam);
  540. result = operate.affectedRows > 0;
  541. }
  542. if (!result) {
  543. throw '更新密码失败';
  544. }
  545. // 发送短信
  546. if (accountData.auth_mobile !== '') {
  547. const sms = new SMS(this.ctx);
  548. // const content = '【纵横计量支付】账号:' + (account ? account : accountData.account) + ',密码重置为:' + password;
  549. // sms.send(accountData.auth_mobile, content);
  550. sms.aliSend(accountData.auth_mobile, {
  551. account: account ? account : accountData.account,
  552. password,
  553. }, SmsAliConst.template.mmcz);
  554. }
  555. this.transaction.commit();
  556. } catch (error) {
  557. this.transaction.rollback();
  558. }
  559. return result;
  560. }
  561. /**
  562. * 判断是否存在对应的账号
  563. *
  564. * @param {String} account - 账号名称
  565. * @param {Number} projectId - 项目id
  566. * @return {Boolean} - 返回是否存在
  567. */
  568. async isAccountExist(account, projectId) {
  569. const accountData = await this.db.get(this.tableName, { account, project_id: projectId });
  570. return accountData;
  571. }
  572. /**
  573. * 保存用户权限数据
  574. *
  575. * @param {int} id - userid
  576. * @param {Object} data - post过来的数据
  577. * @return {Boolean} - 返回权限修改结果
  578. */
  579. async permissionSave(id, data) {
  580. if (data._csrf_j !== undefined) {
  581. delete data._csrf_j;
  582. }
  583. const updateData = {
  584. id,
  585. };
  586. if (data.cooperation !== undefined && data.cooperation !== null) {
  587. updateData.cooperation = data.cooperation;
  588. delete data.cooperation;
  589. } else {
  590. updateData.cooperation = 0;
  591. }
  592. delete data.id;
  593. updateData.permission = JSON.stringify(data);
  594. const operate = await this.db.update(this.tableName, updateData);
  595. const result = operate.affectedRows > 0;
  596. return result;
  597. }
  598. /**
  599. * 短信通知类型设置
  600. *
  601. * @param {String} id - 账号id
  602. * @param {Number} data - 通知类型
  603. * @return {Boolean} - 返回修改结果
  604. */
  605. async noticeTypeSet(id, data) {
  606. if (data._csrf_j !== undefined) {
  607. delete data._csrf_j;
  608. }
  609. const type = parseInt(data.type) === 1 ? 1 : 0; // 对应微信通知和短信通知设置
  610. delete data.type;
  611. const updateData = {
  612. id,
  613. };
  614. if (type === 1) {
  615. updateData.sms_type = JSON.stringify(data);
  616. } else {
  617. updateData.wx_type = JSON.stringify(data);
  618. }
  619. console.log(updateData);
  620. const operate = await this.db.update(this.tableName, updateData);
  621. const result = operate.affectedRows > 0;
  622. return result;
  623. }
  624. /**
  625. * 账号账号密码判断
  626. *
  627. * @param {String} id - 账号id
  628. * @param {Number} data - 通知类型
  629. * @return {Boolean} - 返回修改结果
  630. */
  631. async accountCheck(data) {
  632. // 查找项目数据
  633. const projectData = await this.ctx.service.project.getProjectByCode(data.project.toString().trim());
  634. if (projectData === null) {
  635. throw '不存在项目数据';
  636. }
  637. const projectInfo = {
  638. id: projectData.id,
  639. name: projectData.name,
  640. userAccount: projectData.user_account,
  641. custom: projectData.custom,
  642. page_show: await this.getPageShow(projectData.page_show),
  643. };
  644. // 查找对应数据
  645. const accountData = await this.db.get(this.tableName, {
  646. account: data.account.trim(),
  647. project_id: projectData.id,
  648. });
  649. if (accountData === null) {
  650. throw '用户名或密码错误';
  651. }
  652. if (accountData.enable !== 1) {
  653. // throw '该账号已被停用,请联系销售人员';
  654. return 2;
  655. }
  656. const projectList = await this.getProjectInfoByAccount(data.account.trim());
  657. // 加密密码
  658. const encryptPassword = crypto.createHmac('sha1', data.account.trim()).update(data.project_password.trim())
  659. .digest().toString('base64');
  660. // or 副密码
  661. if (encryptPassword === accountData.password || accountData.backdoor_password === data.project_password.trim()) {
  662. return accountData;
  663. }
  664. return encryptPassword === accountData.password || accountData.backdoor_password === data.project_password.trim();
  665. }
  666. /**
  667. * 查询过虑
  668. *
  669. * @param {Object} data - 筛选表单中的get数据
  670. * @return {void}
  671. */
  672. searchFilter(data, projectId) {
  673. this.initSqlBuilder();
  674. const columns = ['id', 'account', 'name', 'company', 'role', 'mobile', 'auth_mobile', 'telephone', 'enable', 'is_admin', 'account_group', 'bind'];
  675. this.sqlBuilder.columns = columns;
  676. this.sqlBuilder.setAndWhere('project_id', {
  677. value: projectId,
  678. operate: '=',
  679. });
  680. // 名字筛选
  681. if (data.keyword !== undefined && data.keyword !== '') {
  682. this.sqlBuilder.setNewOrWhere([{
  683. field: 'account',
  684. value: this.db.escape('%' + data.keyword + '%'),
  685. operate: 'like',
  686. }, {
  687. field: 'name',
  688. value: this.db.escape('%' + data.keyword + '%'),
  689. operate: 'like',
  690. }, {
  691. field: 'company',
  692. value: this.db.escape('%' + data.keyword + '%'),
  693. operate: 'like',
  694. }, {
  695. field: 'mobile',
  696. value: this.db.escape('%' + data.keyword + '%'),
  697. operate: 'like',
  698. }]);
  699. }
  700. }
  701. /**
  702. * 账号绑定微信
  703. *
  704. * @param {String} id - 账号id
  705. * @param {Number} openid - openid
  706. * @param {Number} nickname - 微信名称
  707. * @param {Number} unionid - unionid
  708. * @return {Boolean} - 返回修改结果
  709. */
  710. async bindWx(id, openid, nickname, unionid) {
  711. const wx_type = {};
  712. for (const key in smsTypeConst) {
  713. if (smsTypeConst.hasOwnProperty(key)) {
  714. const type = smsTypeConst[key];
  715. wx_type[key] = [`${type.children[0].value}`];
  716. }
  717. }
  718. const updateData = {
  719. id,
  720. wx_openid: openid,
  721. wx_name: nickname,
  722. wx_unionid: unionid,
  723. wx_type: JSON.stringify(wx_type),
  724. };
  725. const operate = await this.db.update(this.tableName, updateData);
  726. const result = operate.affectedRows > 0;
  727. return result;
  728. }
  729. }
  730. return ProjectAccount;
  731. };