Commit 35cfb5c8 by huangqy

提交下axios封装与验证方式还有ts各类的配置

parent 915faa8a
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
"axios": "^0.26.1", "axios": "^0.26.1",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"vue": "^3.2.25", "vue": "^3.2.25",
"vue-ls": "^4.2.0",
"vue-router": "^4.0.14", "vue-router": "^4.0.14",
"vuex": "^4.0.2" "vuex": "^4.0.2"
}, },
......
import Vue from 'vue'
import Axios, { Method, ResponseType, AxiosResponse } from 'axios' import Axios, { Method, ResponseType, AxiosResponse } from 'axios'
import { message } from 'ant-design-vue'; import { message } from 'ant-design-vue';
...@@ -30,9 +29,8 @@ axios.defaults.headers.post['Content-Type'] = 'application/json' ...@@ -30,9 +29,8 @@ axios.defaults.headers.post['Content-Type'] = 'application/json'
// 请求拦截器 // 请求拦截器
axios.interceptors.request.use( axios.interceptors.request.use(
(config) => { (config) => {
const token = Vue.ls.get(ACCESS_TOKEN) if (sessionStorage.getItem("accessToken")) {
if (token) { config.headers!.Authorization = `${sessionStorage.getItem("accessToken")}`;
config.headers[ 'X-Access-Token' ] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
} }
return config; return config;
}, },
...@@ -67,12 +65,30 @@ export default function request(arr: axiosData) { ...@@ -67,12 +65,30 @@ export default function request(arr: axiosData) {
responseType: arr.responseType || 'json' responseType: arr.responseType || 'json'
}) })
.then((response: AxiosResponse<any>) => { .then((response: AxiosResponse<any>) => {
if (response.data.code == 99200) { /**
message.error(response.data) * response格式
*
* {
data:{},
status:200,
statusText:'OK',//从服务器返回的http状态文本
headers: {},//响应头信息
config: {} //`config`是在请求的时候的一些配置信息
}
*/
const responseStatus = `${response.status}`
// 状态码为2开头的
if (responseStatus.charAt(0) === '2') {
if (response.data.code !== '99200') {
message.error(response.data.message)
reject(response.data)
return
}
resolve(response.data)
} else {
message.error(response.data.message)
reject(response.data) reject(response.data)
return
} }
resolve(response.data)
}) })
.catch((err) => { .catch((err) => {
message.error(err.message) message.error(err.message)
......
/** /**
* 邮箱 * 邮箱
* @param {*} s * @param {*} str
*/ */
export function isEmail (str: string) { export function isEmail (str: string) {
return /^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(str) return /^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(str)
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
/** /**
* 手机号码 * 手机号码
* @param {*} s * @param {*} str
*/ */
export function isMobile (str: string) { export function isMobile (str: string) {
return /^1[0-9]{10}$/.test(str) return /^1[0-9]{10}$/.test(str)
...@@ -16,7 +16,7 @@ export function isMobile (str: string) { ...@@ -16,7 +16,7 @@ export function isMobile (str: string) {
/** /**
* 电话号码 * 电话号码
* @param {*} s * @param {*} str
*/ */
export function isPhone (str: string) { export function isPhone (str: string) {
return /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(str) return /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(str)
...@@ -24,7 +24,7 @@ export function isPhone (str: string) { ...@@ -24,7 +24,7 @@ export function isPhone (str: string) {
/** /**
* URL地址 * URL地址
* @param {*} s * @param {*} str
*/ */
export function isURL (str: string) { export function isURL (str: string) {
return /^http[s]?:\/\/.*/.test(str) return /^http[s]?:\/\/.*/.test(str)
......
...@@ -10,7 +10,11 @@ ...@@ -10,7 +10,11 @@
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true, "isolatedModules": true,
"esModuleInterop": true, "esModuleInterop": true,
"lib": ["esnext", "dom"] "lib": ["esnext", "dom"],
"baseUrl": "./",
"paths": {
"@/*": ["src/*"]
}
}, },
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"], "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
"references": [{ "path": "./tsconfig.node.json" }] "references": [{ "path": "./tsconfig.node.json" }]
......
...@@ -11,6 +11,7 @@ export default defineConfig({ ...@@ -11,6 +11,7 @@ export default defineConfig({
resolve: { resolve: {
alias: { alias: {
// 如果报错__dirname找不到,需要安装node,执行yarn add @types/node --save-dev // 如果报错__dirname找不到,需要安装node,执行yarn add @types/node --save-dev
'~': resolve(__dirname, './'),
'@': resolve(__dirname, 'src') '@': resolve(__dirname, 'src')
} }
}, },
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论