sso.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. const addResult = await this.ctx.service.customer.addSSOUser(responseData[0]);
  46. if (!addResult) {
  47. console.log('sso user add error');
  48. }
  49. result = true;
  50. } catch (error) {
  51. console.log('sso:' + error);
  52. result = false;
  53. }
  54. return result;
  55. }
  56. /**
  57. * 获取SSO用户数据
  58. *
  59. * @param {Number} ssoID - sso中的id
  60. * @return {String} - 返回json数据
  61. */
  62. async getSSOUserData(ssoID) {
  63. let result = {};
  64. try {
  65. result = ssoID;
  66. } catch (error) {
  67. result = {};
  68. }
  69. return result;
  70. }
  71. }
  72. module.exports = SSO;