-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
白唯
committed
Sep 3, 2020
1 parent
d4e8514
commit f3da208
Showing
12 changed files
with
499 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# 0.1.0 (2020-09-03) | ||
|
||
|
||
### Features | ||
|
||
* **基础代码架构:** 项目基础代码初始化,目前只有vue 全家桶 d4e8514 | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { createStore } from 'vuex' | ||
declare interface Book { | ||
author?: string | ||
pageName: string | ||
} | ||
|
||
declare interface StoreInstance extends ReturnType<typeof createStore> { | ||
save?: (type: string, val: any) => any | ||
get?: (type: string, val: any) => any | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** 所有的网络请求地址列表,方便集中维护 */ | ||
|
||
import { Method, ResponseType } from 'axios' | ||
|
||
interface ApiListItem { | ||
[key: string]: Array<{ url: string; method?: Method; resType?: ResponseType }> | ||
} | ||
const ApiList: ApiListItem = { | ||
user: [ | ||
{ | ||
url: '/login', | ||
method: 'post', | ||
resType: 'json' | ||
}, | ||
{ | ||
url: '/login', | ||
method: 'get', | ||
resType: 'json' | ||
} | ||
], | ||
other: [ | ||
{ | ||
url: 'fd' | ||
} | ||
] | ||
} | ||
|
||
export default ApiList |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import Axios, { AxiosResponse, AxiosRequestConfig, AxiosError } from 'axios' | ||
|
||
/** | ||
* get status code | ||
* @param {AxiosResponse} response Axios response object | ||
*/ | ||
const getErrorCode2text = (response: AxiosResponse) => { | ||
/** http status code */ | ||
const code = response.status | ||
/** notice text */ | ||
let message = 'Request Error' | ||
switch (code) { | ||
case 400: | ||
message = 'Request Error' | ||
break | ||
case 401: | ||
message = 'Unauthorized, please login' | ||
break | ||
case 403: | ||
message = 'Access denied' | ||
break | ||
case 404: | ||
message = 'Request address error' | ||
break | ||
case 408: | ||
message = 'Request timeout' | ||
break | ||
case 500: | ||
message = 'Internal server error' | ||
break | ||
case 501: | ||
message = 'Service not implemented' | ||
break | ||
case 502: | ||
message = 'Gateway error' | ||
break | ||
case 503: | ||
message = 'Service is not available' | ||
break | ||
case 504: | ||
message = 'Gateway timeout' | ||
break | ||
case 505: | ||
message = 'HTTP version is not supported' | ||
break | ||
default: | ||
} | ||
return message | ||
} | ||
|
||
/** | ||
* @returns {AxiosResponse} result | ||
* @tutorial see more:https://github.com/onlyling/some-demo/tree/master/typescript-width-axios | ||
* @example | ||
* service.get<{data: string; code: number}>('/test').then(({data}) => { console.log(data.code) }) | ||
*/ | ||
const service = Axios.create({ | ||
baseURL: process.env.VUE_APP_BASE_URL, | ||
timeout: 10000 | ||
}) | ||
|
||
// request interceptors | ||
|
||
interface ApiResponse { | ||
err_code: number | ||
err_msg?: string | ||
data?: any | ||
} | ||
|
||
/** | ||
* @description 请求发起前的拦截器 | ||
* @returns {AxiosRequestConfig} config | ||
*/ | ||
service.interceptors.request.use(async <AxiosRequestConfig>(config: AxiosRequestConfig) => { | ||
// check network | ||
/* TODO add http headers | ||
const token = window.localStorage.getItem('token') | ||
config.headers = { | ||
...config.headers, | ||
Authorization: token | ||
} */ | ||
|
||
return config | ||
}) | ||
|
||
/** | ||
* @description 请求发起前的拦截器 | ||
* @returns {} config | ||
*/ | ||
service.interceptors.response.use( | ||
/** 请求有响应 */ | ||
async (response: AxiosResponse) => { | ||
if (response.status === 200) { | ||
return Promise.resolve(response) | ||
} else { | ||
const __text = getErrorCode2text(response) | ||
return Promise.reject(new Error(__text)) | ||
} | ||
}, | ||
/** 请求无响应 */ | ||
(error: AxiosError) => { | ||
let __emsg: string = error.message || '' | ||
|
||
if (error.message) { | ||
__emsg = error.message | ||
} | ||
|
||
if (error.response) { | ||
__emsg = error.response.data.msg | ||
} | ||
// timeout | ||
if (__emsg.indexOf('timeout') >= 0) { | ||
__emsg = 'timeout' | ||
} | ||
|
||
if (error?.response?.data?.code === 401) { | ||
return Promise.reject(new Error('401')) | ||
} | ||
return Promise.reject(new Error(__emsg)) | ||
} | ||
) | ||
|
||
export default service |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import API from './request' | ||
interface HttpParams { | ||
coinName: string | ||
cashName: string | ||
} | ||
|
||
export interface UserApi { | ||
coin2cash(param: HttpParams): Promise<any> | ||
} | ||
|
||
/** | ||
* @example Axios.get(`https://api.abckey.com/market/${this.c_switchCashName}/${this.cash.toLowerCase()}&t=${new Date().getTime()}`) | ||
* @todo Get the exchange rate of the current currency | ||
*/ | ||
class User { | ||
register() { | ||
/* return API.get<{ data: string | { error: string } }>(`/market/${targetCoin}/${param.cashName.toLowerCase()}`, { | ||
params: { | ||
t: new Date().getTime() | ||
} | ||
}).then(({ data }) => data) */ | ||
} | ||
} | ||
|
||
export default User |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +0,0 @@ | ||
declare interface Book { | ||
author?: string | ||
pageName: string | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.