Skip to content

Commit

Permalink
添加请求token
Browse files Browse the repository at this point in the history
  • Loading branch information
owenyang0 committed Mar 24, 2023
1 parent 4b0cb88 commit 768d8be
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 7 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
40 changes: 36 additions & 4 deletions src/api/easyTyper.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -74,7 +106,7 @@ const matches = (guid: string): Promise<Match> => {
})
}

const HASH_KEY = '3198f2e6892d5bdd0630505e20acfc849a12e03c5a1da4c5c41a180c44c67eeb85ef0bc6992d9b0c3926da22ebaa55346bcd76d8556321e044530eff3d868e2636514072'
// const HASH_KEY = '3198f2e6892d5bdd0630505e20acfc849a12e03c5a1da4c5c41a180c44c67eeb85ef0bc6992d9b0c3926da22ebaa55346bcd76d8556321e044530eff3d868e2636514072'
const HASH_ALG = 'sha1'
const ENCODING = 'hex'

Expand Down Expand Up @@ -141,8 +173,8 @@ const getTodayArticle = () => {
return baseRequest('/api/r/articles/today')
}

const getTodayNews = () => {
return baseRequest('/api/r/news/today')
const getTodayNews = (): Promise<NewsResponse> => {
return axiosInstance.get('/api/r/news/today')
}

const getKataList = () => {
Expand Down
9 changes: 9 additions & 0 deletions src/models/articleModels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Generated by https://quicktype.io

export interface NewsResponse {
id: number;
title: string;
contentList: string[];
timestamp: number;
utime: string;
}
2 changes: 1 addition & 1 deletion src/store/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export interface ArticleState {
/**
* 标识
*/
identity: string;
identity: string|number;
/**
* 文章内容
*/
Expand Down
3 changes: 2 additions & 1 deletion vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 768d8be

Please sign in to comment.