password.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import JSEncrypt from 'jsencrypt';
  2. import { LocalScheme } from '~auth/runtime';
  3. import { sha256 } from 'js-sha256';
  4. import encryption from '@/utils/encryption.js';
  5. let publicKey;
  6. const encrypt = new JSEncrypt();
  7. function getProp(holder, propName) {
  8. if (!propName || !holder || typeof holder !== 'object') {
  9. return holder;
  10. }
  11. if (propName in holder) {
  12. return holder[propName];
  13. }
  14. const propParts = Array.isArray(propName)
  15. ? propName
  16. : (propName + '').split('.');
  17. let result = holder;
  18. while (propParts.length && result) {
  19. result = result[propParts.shift()];
  20. }
  21. return result;
  22. }
  23. export default class CustomScheme extends LocalScheme {
  24. async login(data, { reset = true } = {}) {
  25. const endpoint = { data: Object.assign({}, data) };
  26. // endpoint.data = Object.assign({}, endpoint.data);
  27. // endpoint.data = endpoint.data;
  28. if (!this.options.endpoints.login) {
  29. return;
  30. }
  31. if (reset) {
  32. this.$auth.reset({ resetInterceptor: false });
  33. }
  34. if (this.options.clientId) {
  35. endpoint.data.client_id = this.options.clientId;
  36. }
  37. if (this.options.grantType) {
  38. endpoint.data.grant_type = this.options.grantType;
  39. }
  40. if (this.options.scope) {
  41. endpoint.data.scope = this.options.scope;
  42. }
  43. if (!publicKey) {
  44. publicKey = (
  45. await this.$auth.ctx.$axios.$post('/user/v1/client/login/getPbKey')
  46. ).data.publicKey;
  47. encrypt.setPublicKey(publicKey);
  48. }
  49. endpoint.data.password = await encryption(endpoint.data.password, publicKey);
  50. // endpoint.data.uuid = '132514568481654';
  51. // endpoint.data.client = 7;
  52. // 获取服务器时间
  53. let requestTime = await this.$auth.ctx.$axios.$get('pay/v1/order/getSystemTime');
  54. // 转化
  55. let registerTime = new Date(requestTime.data * 1000).$formatTime();
  56. endpoint.data.registerTime = registerTime;
  57. // 加密服务器时间
  58. const requestTimeSign = sha256("Register_SZX_2023:" + registerTime);
  59. // 设置请求头
  60. endpoint.headers = {devicesId: '', registerSign: requestTimeSign};
  61. const response = await this.$auth.request(
  62. endpoint,
  63. this.options.endpoints.login,
  64. );
  65. this.updateTokens(response);
  66. // localStorage.setItem('auth.username', response.data.data.username);
  67. if (!this.requestHandler.interceptor) {
  68. this.initializeRequestInterceptor();
  69. }
  70. if (this.options.user.autoFetch) {
  71. await this.fetchUser();
  72. }
  73. return response;
  74. }
  75. // fetchUser(endpoint) {
  76. // console.log(
  77. // '🚀 ~ file: password.js ~ line 83 ~ CustomScheme ~ fetchUser ~ endpoint',
  78. // endpoint,
  79. // );
  80. // if (!this.check().valid) {
  81. // return Promise.resolve();
  82. // }
  83. // if (!this.options.endpoints.user) {
  84. // this.$auth.setUser({});
  85. // return Promise.resolve();
  86. // }
  87. // return this.$auth
  88. // .requestWith(this.name, endpoint, this.options.endpoints.user)
  89. // .then((response) => {
  90. // const userData = getProp(response.data, this.options.user.property);
  91. // userData.username = localStorage.getItem('auth.username');
  92. // if (!userData) {
  93. // const error = new Error(
  94. // `User Data response does not contain field ${this.options.user.property}`,
  95. // );
  96. // return Promise.reject(error);
  97. // }
  98. // this.$auth.setUser(userData);
  99. // return response;
  100. // })
  101. // .catch((error) => {
  102. // this.$auth.callOnError(error, { method: 'fetchUser' });
  103. // return Promise.reject(error);
  104. // });
  105. // }
  106. }