From 59a8f83a916cf35dc7a3ffe56caab57d9ed8d457 Mon Sep 17 00:00:00 2001 From: zk Date: Mon, 4 Dec 2023 13:22:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=EF=BC=8C=E4=BF=AE=E6=94=B9request=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.development | 2 ++ .env.production | 3 +++ src/api/request.ts | 39 ++++++++++++++++++++++---------------- src/utils/env.ts | 7 +++++++ vite.config.ts | 47 +++++++++++++++++++++++++--------------------- 5 files changed, 61 insertions(+), 37 deletions(-) create mode 100644 .env.development create mode 100644 .env.production create mode 100644 src/utils/env.ts diff --git a/.env.development b/.env.development new file mode 100644 index 0000000..b69634e --- /dev/null +++ b/.env.development @@ -0,0 +1,2 @@ +// 王昊地址 +VITE_GLOB_API_URL=http://192.168.1.16 \ No newline at end of file diff --git a/.env.production b/.env.production new file mode 100644 index 0000000..844ff49 --- /dev/null +++ b/.env.production @@ -0,0 +1,3 @@ + + +VITE_GLOB_API_URL=http://cqyt.dev.yg.dt.io \ No newline at end of file diff --git a/src/api/request.ts b/src/api/request.ts index bac29b8..58390ed 100644 --- a/src/api/request.ts +++ b/src/api/request.ts @@ -1,25 +1,32 @@ -import axios from "axios"; +import axios from 'axios'; import requestURL from './url'; -let token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWRlbnRpZmllciI6ImU5Y2EyM2Q2OGQ4ODRkNGViYjE5ZDA3ODg5NzI3ZGFlIiwiZXhwIjoxNzAyMDA1MDQ4fQ.M1Xlecj3eUOgZUSofOlyBMoxJ5F01KH-tgJxaV2Femc\n' +import { getAppEnvConfig } from '../utils/env.ts'; + +const { VITE_GLOB_API_URL } = getAppEnvConfig(); +let token = + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWRlbnRpZmllciI6ImU5Y2EyM2Q2OGQ4ODRkNGViYjE5ZDA3ODg5NzI3ZGFlIiwiZXhwIjoxNzAyMDA1MDQ4fQ.M1Xlecj3eUOgZUSofOlyBMoxJ5F01KH-tgJxaV2Femc\n'; const service = axios.create({ - baseURL: requestURL, + baseURL: VITE_GLOB_API_URL, timeout: 50000, headers: { // 设置后端需要的传参类型 'Content-Type': 'application/json;charset=UTF-8', - 'X-Access-Token':token - } -}) + 'X-Access-Token': token, + }, +}); export const get = (url, params) => { return new Promise((resolve, reject) => { - service.get(url, {params: params}).then(res => { - if (res.data.code === 200) { - resolve(res.data); - } else { - reject(res.data); - } - }).catch(err => { - reject(err.data); - }) + service + .get(url, { params: params }) + .then((res) => { + if (res.data.code === 200) { + resolve(res.data); + } else { + reject(res.data); + } + }) + .catch((err) => { + reject(err.data); + }); }); -} \ No newline at end of file +}; \ No newline at end of file diff --git a/src/utils/env.ts b/src/utils/env.ts new file mode 100644 index 0000000..e0d4103 --- /dev/null +++ b/src/utils/env.ts @@ -0,0 +1,7 @@ +export function getAppEnvConfig() { + const ENV = import.meta.env; + const { VITE_GLOB_API_URL } = ENV; + return { + VITE_GLOB_API_URL, + }; +} \ No newline at end of file diff --git a/vite.config.ts b/vite.config.ts index 6e0bdae..acc8327 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,26 +1,31 @@ -import { defineConfig } from 'vite' -import vue from '@vitejs/plugin-vue' -import {resolve} from 'path'; +import { defineConfig } from 'vite'; +import vue from '@vitejs/plugin-vue'; +import { resolve } from 'path'; function pathResolve(dir: string) { - return resolve(process.cwd(), '.', dir); + return resolve(process.cwd(), '.', dir); } + // https://vitejs.dev/config/ export default defineConfig({ - plugins: [vue()], - css: { - preprocessorOptions:{ - less: { - javascriptEnabled: true - } - } - }, - resolve: { - alias: [ - { - find: /\/@\//, - replacement: pathResolve('src') + '/', - }, - ], - }, -}) + plugins: [vue()], + css: { + preprocessorOptions: { + less: { + javascriptEnabled: true, + }, + }, + }, + resolve: { + alias: [ + { + find: /\/@\//, + replacement: pathResolve('src') + '/', + }, + ], + }, + server: { + host: true, + https: false, + }, +});