sso.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. 'use strict';
  2. /**
  3. * sso相关接口
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/11/15
  7. * @version
  8. */
  9. // 加密类
  10. const crypto = require('crypto');
  11. class SSO {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. this.authUrl = 'http://sso.smartcost.com.cn/api/jzlogin';
  20. this.ctx = ctx;
  21. }
  22. /**
  23. * SSO登录验证
  24. *
  25. * @param {String} username - cld用户名
  26. * @param {String} password - cld密码
  27. * @return {boolean} - 验证结果
  28. */
  29. async loginValid(username, password) {
  30. let result = false;
  31. try {
  32. if (username === '' || password === '') {
  33. throw '用户名或密码错误';
  34. }
  35. // 生成加密token
  36. const postData = {
  37. username,
  38. userpasswd: password,
  39. };
  40. const responseData = await this.ctx.helper.sendRequest(this.authUrl, postData);
  41. if (responseData.length <= 0) {
  42. throw '接口返回错误:' + responseData.err;
  43. }
  44. // 如果验证成功,则新增SSO数据到数据库
  45. if (responseData.data !== '') {
  46. const addResult = await this.ctx.service.customer.addSSOUser(responseData[0]);
  47. if (!addResult) {
  48. console.log('sso user add error');
  49. }
  50. }
  51. result = true;
  52. } catch (error) {
  53. console.log('sso:' + error);
  54. result = false;
  55. }
  56. return result;
  57. }
  58. /**
  59. * 获取SSO用户数据
  60. *
  61. * @param {Number} ssoID - sso中的id
  62. * @return {String} - 返回json数据
  63. */
  64. async getSSOUserData(ssoID) {
  65. let result = {};
  66. try {
  67. result = ssoID;
  68. } catch (error) {
  69. result = {};
  70. }
  71. return result;
  72. }
  73. }
  74. module.exports = SSO;