Skip to content

Commit

Permalink
feat: 剔除auto-libs
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason.Chiu committed Jul 17, 2023
1 parent 5a04e70 commit e68ba53
Show file tree
Hide file tree
Showing 11 changed files with 387 additions and 73 deletions.
8 changes: 4 additions & 4 deletions components/aside/footer.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React, { FC, useMemo, useContext } from 'react'
import { Popconfirm } from 'antd'
import React, { FC, useContext, useMemo } from 'react'
import getLoginInfo from '../utils/getLoginInfo'
import { toConsoleLogin } from '../utils/token'
import WrapperContext from '../wrapper/wrapperContext'
import {
PoweroffOutlined,
DoubleRightOutlined,
DoubleLeftOutlined,
AppstoreOutlined,
} from '@ant-design/icons'
import { toConsoleLogin } from 'auto-libs'
import getLoginInfo from '../utils/getLoginInfo'
import WrapperContext from '../wrapper/wrapperContext'

const Footer: FC<{
collapsed: boolean
Expand Down
15 changes: 6 additions & 9 deletions components/dev-login/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Button, Form, Input, message, Spin } from 'antd'
import React from 'react'
import { Input, Form, Button, message, Spin } from 'antd'
import { httpConsole, setConsoleToken } from 'auto-libs'
import httpConsole from '../utils/httpConsole'
import { setConsoleToken } from '../utils/token'

interface IProps {
history: any
Expand Down Expand Up @@ -38,13 +39,9 @@ class View extends React.PureComponent<IProps, IState> {
})
const { username, password } = values

const res: any = await httpConsole({
url: '/casService/login',
method: 'POST',
data: {
username,
password,
},
const res: any = await httpConsole.post('/casService/login', {
username,
password,
})

const { token, ...userInfo } = res
Expand Down
8 changes: 3 additions & 5 deletions components/header/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/* eslint-disable prefer-template,react/destructuring-assignment */
import React from 'react'
import { clearConsoleToken } from 'auto-libs'
import { Layout } from 'antd'
import Icon from '@ant-design/compatible/lib/icon'
// @ts-ignore
import { Layout } from 'antd'
import cn from 'classnames'
import React from 'react'
import { clearConsoleToken } from '../utils/token'

interface IProps {
breakpoint: boolean
Expand Down
3 changes: 1 addition & 2 deletions components/upload/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { UploadOutlined } from '@ant-design/icons'
import { Button, message, Upload as AntdUpload } from 'antd'
import { UploadProps as AntdUploadProps } from 'antd/lib/upload'
import { httpConsole } from 'auto-libs'
import React, { FC, useEffect, useState } from 'react'
/* eslint-disable no-param-reassign */
import httpConsole from '../utils/httpConsole'

export interface UploadProps extends AntdUploadProps {
ticket: string
Expand Down
103 changes: 103 additions & 0 deletions components/utils/desensitize.ts
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
196 changes: 196 additions & 0 deletions components/utils/httpConsole.ts
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
2 changes: 1 addition & 1 deletion components/utils/showDesensitize.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Desensitize } from 'auto-libs'
import { ReactNode } from 'react'
import Desensitize from '../utils/desensitize'
import { isFunc } from './is'

export type DesensitizeType =
Expand Down
Loading

0 comments on commit e68ba53

Please sign in to comment.