import JSEncrypt from 'jsencrypt'; import { LocalScheme } from '~auth/runtime'; import { sha256 } from 'js-sha256'; import encryption from '@/utils/encryption.js'; let publicKey; const encrypt = new JSEncrypt(); function getProp(holder, propName) { if (!propName || !holder || typeof holder !== 'object') { return holder; } if (propName in holder) { return holder[propName]; } const propParts = Array.isArray(propName) ? propName : (propName + '').split('.'); let result = holder; while (propParts.length && result) { result = result[propParts.shift()]; } return result; } export default class CustomScheme extends LocalScheme { async login(data, { reset = true } = {}) { const endpoint = { data: Object.assign({}, data) }; // endpoint.data = Object.assign({}, endpoint.data); // endpoint.data = endpoint.data; if (!this.options.endpoints.login) { return; } if (reset) { this.$auth.reset({ resetInterceptor: false }); } if (this.options.clientId) { endpoint.data.client_id = this.options.clientId; } if (this.options.grantType) { endpoint.data.grant_type = this.options.grantType; } if (this.options.scope) { endpoint.data.scope = this.options.scope; } if (!publicKey) { publicKey = ( await this.$auth.ctx.$axios.$post('/user/v1/client/login/getPbKey') ).data.publicKey; encrypt.setPublicKey(publicKey); } endpoint.data.password = await encryption(endpoint.data.password, publicKey); // endpoint.data.uuid = '132514568481654'; // endpoint.data.client = 7; // 获取服务器时间 let requestTime = await this.$auth.ctx.$axios.$get('pay/v1/order/getSystemTime'); // 转化 let registerTime = new Date(requestTime.data * 1000).$formatTime(); endpoint.data.registerTime = registerTime; // 加密服务器时间 const requestTimeSign = sha256("Register_SZX_2023:" + registerTime); // 设置请求头 endpoint.headers = {devicesId: '', registerSign: requestTimeSign}; const response = await this.$auth.request( endpoint, this.options.endpoints.login, ); this.updateTokens(response); // localStorage.setItem('auth.username', response.data.data.username); if (!this.requestHandler.interceptor) { this.initializeRequestInterceptor(); } if (this.options.user.autoFetch) { await this.fetchUser(); } return response; } // fetchUser(endpoint) { // console.log( // '🚀 ~ file: password.js ~ line 83 ~ CustomScheme ~ fetchUser ~ endpoint', // endpoint, // ); // if (!this.check().valid) { // return Promise.resolve(); // } // if (!this.options.endpoints.user) { // this.$auth.setUser({}); // return Promise.resolve(); // } // return this.$auth // .requestWith(this.name, endpoint, this.options.endpoints.user) // .then((response) => { // const userData = getProp(response.data, this.options.user.property); // userData.username = localStorage.getItem('auth.username'); // if (!userData) { // const error = new Error( // `User Data response does not contain field ${this.options.user.property}`, // ); // return Promise.reject(error); // } // this.$auth.setUser(userData); // return response; // }) // .catch((error) => { // this.$auth.callOnError(error, { method: 'fetchUser' }); // return Promise.reject(error); // }); // } }