From 768d8be20d6da671d7a4c3ebad82ad6c51a27b7b Mon Sep 17 00:00:00 2001 From: yangsong13 Date: Fri, 24 Mar 2023 13:06:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AF=B7=E6=B1=82token?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 4 +++- src/api/easyTyper.ts | 40 +++++++++++++++++++++++++++++++++---- src/models/articleModels.ts | 9 +++++++++ src/store/types.ts | 2 +- vue.config.js | 3 ++- yarn.lock | 10 ++++++++++ 6 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 src/models/articleModels.ts diff --git a/package.json b/package.json index c0ab81a..7e147a8 100644 --- a/package.json +++ b/package.json @@ -18,9 +18,11 @@ "main": "electron/main.js", "dependencies": { "@types/crypto-js": "^4.1.1", + "@types/uuid": "^9.0.1", "applescript": "^1.0.0", "crypto-js": "^4.1.1", - "dayjs": "^1.11.7" + "dayjs": "^1.11.7", + "uuid": "^9.0.0" }, "devDependencies": { "@types/echarts": "^4.9.4", diff --git a/src/api/easyTyper.ts b/src/api/easyTyper.ts index c6aadf5..835c364 100644 --- a/src/api/easyTyper.ts +++ b/src/api/easyTyper.ts @@ -1,8 +1,40 @@ import * as crypto from 'crypto' +import CryptoJS from 'crypto-js' import axios, { AxiosResponse } from 'axios' +import * as uuid from 'uuid' import { LoginState } from '@/store/types' +import { NewsResponse } from '@/models/articleModels' -axios.defaults.baseURL = 'https://api.xc.cool/api' +const HASH_KEY = '3198f2e6892d5bdd0630505e20acfc849a12e03c5a1da4c5c41a180c44c67eeb85ef0bc6992d9b0c3926da22ebaa55346bcd76d8556321e044530eff3d868e2636514072' + +const axiosInstance = axios.create({ + baseURL: '/', + responseType: 'json' +}) +axiosInstance.interceptors.request.use((config) => { + const { url, method } = config + const nonce = uuid.v4() + const timestamp = Math.floor(Date.now() / 1000) + const secret = HASH_KEY + const hash = CryptoJS.SHA256(`${url}:${method}:${nonce}:${timestamp}:${secret}`.toUpperCase()) + const signature = hash.toString(CryptoJS.enc.Hex) + // 把签名添加到请求头中 + config.headers['X-Etsig'] = btoa(`${signature}:${nonce}:${timestamp}`) + // 返回新的请求配置 + return { ...config, url, method, headers: config.headers } +}) + +axiosInstance.interceptors.response.use(response => { + const { data } = response + + if (data.code === 0) { + return data.data + } else { + return Promise.reject(new Error(data.msg)) + } +}, error => Promise.reject(error)) + +axios.defaults.baseURL = '/' axios.defaults.responseType = 'json' axios.interceptors.response.use(response => { const { data } = response @@ -74,7 +106,7 @@ const matches = (guid: string): Promise => { }) } -const HASH_KEY = '3198f2e6892d5bdd0630505e20acfc849a12e03c5a1da4c5c41a180c44c67eeb85ef0bc6992d9b0c3926da22ebaa55346bcd76d8556321e044530eff3d868e2636514072' +// const HASH_KEY = '3198f2e6892d5bdd0630505e20acfc849a12e03c5a1da4c5c41a180c44c67eeb85ef0bc6992d9b0c3926da22ebaa55346bcd76d8556321e044530eff3d868e2636514072' const HASH_ALG = 'sha1' const ENCODING = 'hex' @@ -141,8 +173,8 @@ const getTodayArticle = () => { return baseRequest('/api/r/articles/today') } -const getTodayNews = () => { - return baseRequest('/api/r/news/today') +const getTodayNews = (): Promise => { + return axiosInstance.get('/api/r/news/today') } const getKataList = () => { diff --git a/src/models/articleModels.ts b/src/models/articleModels.ts new file mode 100644 index 0000000..dc8a2c1 --- /dev/null +++ b/src/models/articleModels.ts @@ -0,0 +1,9 @@ +// Generated by https://quicktype.io + +export interface NewsResponse { + id: number; + title: string; + contentList: string[]; + timestamp: number; + utime: string; +} diff --git a/src/store/types.ts b/src/store/types.ts index 0d2941e..32bb754 100644 --- a/src/store/types.ts +++ b/src/store/types.ts @@ -98,7 +98,7 @@ export interface ArticleState { /** * 标识 */ - identity: string; + identity: string|number; /** * 文章内容 */ diff --git a/vue.config.js b/vue.config.js index fa92ebd..b606bb2 100644 --- a/vue.config.js +++ b/vue.config.js @@ -102,7 +102,8 @@ module.exports = { devServer: { proxy: { '^/api': { - target: 'https://typer.owenyang.top/', + // target: 'https://typer.owenyang.top/', + target: 'http://localhost:3000/', ws: true, changeOrigin: true } diff --git a/yarn.lock b/yarn.lock index 610d727..4a247e0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1690,6 +1690,11 @@ dependencies: source-map "^0.6.1" +"@types/uuid@^9.0.1": + version "9.0.1" + resolved "https://registry.npmmirror.com/@types/uuid/-/uuid-9.0.1.tgz#98586dc36aee8dacc98cc396dbca8d0429647aa6" + integrity sha512-rFT3ak0/2trgvp4yYZo5iKFEPsET7vKydKF+VRCxlQ9bpheehyAJH89dAkaLEq/j/RZXJIqcgsmPJKUP1Z28HA== + "@types/verror@^1.10.3": version "1.10.6" resolved "https://registry.npmmirror.com/@types/verror/-/verror-1.10.6.tgz#3e600c62d210c5826460858f84bcbb65805460bb" @@ -12776,6 +12781,11 @@ uuid@^8.3.2: resolved "https://registry.npmmirror.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2" integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg== +uuid@^9.0.0: + version "9.0.0" + resolved "https://registry.npmmirror.com/uuid/-/uuid-9.0.0.tgz#592f550650024a38ceb0c562f2f6aa435761efb5" + integrity sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg== + v8-compile-cache@^2.0.3: version "2.3.0" resolved "https://registry.npmmirror.com/v8-compile-cache/-/v8-compile-cache-2.3.0.tgz#2de19618c66dc247dcfb6f99338035d8245a2cee"