-
Notifications
You must be signed in to change notification settings - Fork 2
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
Eason.Chiu
committed
Jul 17, 2023
1 parent
5a04e70
commit e68ba53
Showing
11 changed files
with
387 additions
and
73 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
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
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,103 @@ | ||
/** | ||
* 数据脱敏 | ||
*/ | ||
const Desensitize = { | ||
/** | ||
* 身份证号脱敏 | ||
* @param str 身份证号码 | ||
* @returns 脱敏后的身份证码 | ||
*/ | ||
idCard(str: string): string { | ||
return Desensitize.custom(str, 3, 4) | ||
}, | ||
|
||
/** | ||
* 手机号脱敏 | ||
* @param str 11位手机号 | ||
* @returns 脱敏后的11位手机号 | ||
*/ | ||
mobile(str: string): string { | ||
return Desensitize.custom(str, 3, 4) | ||
}, | ||
|
||
/** | ||
* 姓名脱敏 | ||
* @param str 姓名 | ||
* @returns 脱敏后的姓名 | ||
*/ | ||
name(str: string): string { | ||
return Desensitize.custom(str, 1, 0) | ||
}, | ||
|
||
/** | ||
* 车架号 | ||
* @param str 车架号 | ||
* @returns 脱敏后的车架号 | ||
*/ | ||
vin(str: string): string { | ||
return Desensitize.custom(str, 3, 3) | ||
}, | ||
|
||
/** | ||
* 车牌号 | ||
* @param str 车牌 | ||
* @returns 脱敏后的车牌 | ||
*/ | ||
plateCode(str: string): string { | ||
return Desensitize.custom(str, 2, 2) | ||
}, | ||
|
||
/** | ||
* 银行卡 | ||
* @param str 银行卡 | ||
* @returns 脱敏后的银行卡 | ||
*/ | ||
bankCard(str: string): string { | ||
return Desensitize.custom(str, str.length - 8, 0) | ||
}, | ||
|
||
/** | ||
* 邮箱 | ||
* @param str 邮箱 | ||
* @returns 脱敏后的邮箱 | ||
*/ | ||
email(str: string): string { | ||
if (str.indexOf('@') > -1) { | ||
const [str1, str2] = str.split('@') | ||
return Desensitize.custom(str1, 1, 0) + '@' + Desensitize.custom(str2, 0, 0) | ||
} | ||
|
||
return Desensitize.custom(str, 1, 0) | ||
}, | ||
|
||
/** | ||
* 自定义脱敏 | ||
* @param str 需要脱敏的字符串 | ||
* @param showPrefix 显示前几位 | ||
* @param showSuffix 显示后几位 | ||
* @param hider 隐藏替换符,默认为 * | ||
* @returns 脱敏后的字符串 | ||
*/ | ||
custom(str: string, showPrefix: number, showSuffix: number, hider = '*'): string { | ||
const sstr = String(str) | ||
|
||
// 传参错误的情况,不处理 | ||
if (!sstr || showPrefix < 0 || showSuffix < 0 || !hider) { | ||
return sstr | ||
} | ||
|
||
// 需要显示的内容大于等于字符串长度,不处理 | ||
if (showPrefix + showSuffix >= sstr.length) { | ||
return sstr | ||
} | ||
|
||
// 进行隐藏替换 | ||
const hiderCount = Math.max(sstr.length - showPrefix - showSuffix, 0) | ||
|
||
const allHider = Array(hiderCount).fill(hider).join('') | ||
|
||
return sstr.substr(0, showPrefix) + allHider + (showSuffix > 0 ? sstr.substr(-showSuffix) : '') | ||
}, | ||
} | ||
|
||
export default Desensitize |
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,196 @@ | ||
import axios, { AxiosError, AxiosResponse } from 'axios' | ||
import Cookie from 'js-cookie' | ||
import { clearConsoleCookie, clearConsoleToken, getConsoleToken, toConsoleLogin } from './token' | ||
|
||
export type ToLoginType = ((config?: AxiosResponse) => boolean) | undefined | ||
let toLogin: ToLoginType | ||
|
||
interface HttpConfig { | ||
resCode?: string | ||
resMsg?: string | ||
data?: any | ||
} | ||
|
||
class HttpError extends Error { | ||
msg: string | ||
name = 'HttpError' | ||
data: any | ||
code = '0' | ||
constructor(message: string, data?: HttpConfig) { | ||
super(message) | ||
|
||
this.msg = message | ||
if (data) { | ||
this.data = data ? (data.data ? data.data : data) : null | ||
this.code = data.resCode || '' | ||
} | ||
} | ||
|
||
toString() { | ||
return this.message | ||
} | ||
} | ||
|
||
export interface HttpConsoleExtendParams { | ||
/** | ||
* return true 不执行下一步 | ||
*/ | ||
toLogin: ToLoginType | ||
} | ||
|
||
export function httpConsoleExtend(params: HttpConsoleExtendParams) { | ||
const { toLogin: initialToLogin } = params || {} | ||
|
||
if (initialToLogin) { | ||
toLogin = initialToLogin | ||
} | ||
} | ||
|
||
/** | ||
* 配置axios | ||
*/ | ||
|
||
const httpConsole = axios.create({ | ||
baseURL: '/', | ||
headers: { | ||
Accept: 'application/json;version=3.0;compress=false', | ||
'Content-Type': 'application/json;charset=utf-8', | ||
}, | ||
data: {}, | ||
}) | ||
|
||
/** | ||
* 请求拦截器,在发起请求之前 | ||
*/ | ||
httpConsole.interceptors.request.use(config => { | ||
const token = getConsoleToken() | ||
const utmSource = Cookie.get('utm_source') | ||
const utmMedium = Cookie.get('utm_medium') | ||
const utmCampaign = Cookie.get('utm_campaign') | ||
const utmTerm = Cookie.get('utm_term') | ||
|
||
const method = (config.method as string).toLocaleLowerCase() | ||
if (token) { | ||
config.headers.Authorization = token | ||
} | ||
if (method === 'get') { | ||
if (typeof config.params !== 'object') { | ||
config.params = {} | ||
} | ||
|
||
// 兼容appserver的接口,appserver的接口token需要带在参数中,post请求也是一样 | ||
if (token) { | ||
config.params.token = token | ||
} | ||
if (utmSource) { | ||
config.params.utmSource = utmSource | ||
} | ||
if (utmMedium) { | ||
config.params.utmMedium = utmMedium | ||
} | ||
if (utmCampaign) { | ||
config.params.utmCampaign = utmCampaign | ||
} | ||
if (utmTerm) { | ||
config.params.utmTerm = utmTerm | ||
} | ||
|
||
config.params.requestId = Number(new Date()) | ||
} | ||
|
||
const methods: string[] = ['post', 'put', 'patch', 'delete'] | ||
if (methods.indexOf(method) > -1 && typeof config.data !== 'string') { | ||
if (token) { | ||
config.data.token = token | ||
} | ||
if (utmSource) { | ||
config.data.utmSource = utmSource | ||
} | ||
if (utmMedium) { | ||
config.data.utmMedium = utmMedium | ||
} | ||
if (utmCampaign) { | ||
config.data.utmCampaign = utmCampaign | ||
} | ||
if (utmTerm) { | ||
config.data.utmTerm = utmTerm | ||
} | ||
config.data.requestId = Number(new Date()) | ||
} | ||
|
||
;(config as any).____t = new Date().valueOf() | ||
|
||
return config | ||
}) | ||
|
||
/** | ||
* 接口响应拦截器,在接口响应之后 | ||
*/ | ||
httpConsole.interceptors.response.use( | ||
config => { | ||
let strictModel = true // 严格模式 | ||
const data = config.data || {} | ||
|
||
if (toLogin && toLogin(config) === true) { | ||
return | ||
} | ||
|
||
// 目前的判断方式:因为resCode与resMsg是java端必给的字段,所以认为没有该两个字段时,走标准的http status模式 | ||
if (typeof data.resCode !== 'undefined' && typeof data.resMsg !== 'undefined') { | ||
strictModel = false | ||
} | ||
|
||
if (strictModel) { | ||
if (config.status >= 200 && config.status < 300) { | ||
return data | ||
} else { | ||
if (config.status === 401) { | ||
clearConsoleCookie() | ||
clearConsoleToken() | ||
toConsoleLogin() | ||
return false | ||
} | ||
|
||
return Promise.reject(new HttpError(data.message || '', data)) | ||
} | ||
} | ||
|
||
// atcz java端的模式 | ||
|
||
// 响应正常 | ||
if (data.resCode === '000000') { | ||
return data.data | ||
} | ||
// 需要登录(没登录或登录过期) | ||
else if (data.resCode === '200008') { | ||
clearConsoleCookie() | ||
clearConsoleToken() | ||
toConsoleLogin() | ||
return false | ||
} | ||
|
||
// reject错误处理 | ||
return Promise.reject(new HttpError(data.resMsg || data.msg || data.message, data)) | ||
}, | ||
(error: AxiosError) => { | ||
console.error('http:reject', error) | ||
|
||
if (toLogin && toLogin(error.response) === true) { | ||
return | ||
} | ||
|
||
if (error.response && error.response.status === 401) { | ||
clearConsoleCookie() | ||
clearConsoleToken() | ||
toConsoleLogin() | ||
return false | ||
} | ||
|
||
// reject错误处理 | ||
const { data } = error.response || {} | ||
const { message = '系统错误', msg, resMsg } = data || {} | ||
return Promise.reject(new HttpError(resMsg || msg || message)) | ||
}, | ||
) | ||
|
||
export default httpConsole |
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.