diff --git a/.drone.yml b/.drone.yml index 568739a..1cb1624 100644 --- a/.drone.yml +++ b/.drone.yml @@ -28,7 +28,7 @@ steps: - name: dev-build image: ccr.ccs.tencentyun.com/violin/node:22-bookworm commands: - - npm run build:h5 + - npm run build:h5 -- --mode development volumes: - name: node-cache path: /root/.npm @@ -85,7 +85,7 @@ steps: - name: prod-build image: ccr.ccs.tencentyun.com/violin/node:22-bookworm commands: - - npm run build:h5 + - npm run build:h5 -- --mode production volumes: - name: node-cache path: /root/.npm diff --git a/.env.development b/.env.development new file mode 100644 index 0000000..bf04d81 --- /dev/null +++ b/.env.development @@ -0,0 +1,3 @@ +# 开发环境 +VITE_API_BASE_URL=http://localhost:8080/api/v1 +VITE_H5_BASE_URL=http://localhost:8080 \ No newline at end of file diff --git a/.env.production b/.env.production new file mode 100644 index 0000000..1d7e24a --- /dev/null +++ b/.env.production @@ -0,0 +1,3 @@ +# 生产环境 +VITE_API_BASE_URL=https://sales.violin-work.online/api/v1 +VITE_H5_BASE_URL=https://sales.violin-work.online \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d2bed60 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +# 本地环境变量覆盖(不提交) +.env.local +.env.*.local + +# 构建输出 +dist/ +unpackage/ + +# IDE +.idea/ +.vscode/ +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? \ No newline at end of file diff --git a/src/api/index.js b/src/api/index.js index e5a648a..00e7767 100644 --- a/src/api/index.js +++ b/src/api/index.js @@ -1,5 +1,5 @@ // API基础配置 -const BASE_URL = 'https://sales.violin-work.online/api/v1' +const BASE_URL = import.meta.env.VITE_API_BASE_URL || 'https://sales.violin-work.online/api/v1' // 请求拦截器 const request = (url, method, query = {}, data = {}) => { diff --git a/src/pages/order/detail.vue b/src/pages/order/detail.vue index a59790a..a1140ec 100644 --- a/src/pages/order/detail.vue +++ b/src/pages/order/detail.vue @@ -217,7 +217,7 @@ export default { }, shareOrder() { // 构建分享链接(包含订单号和客户ID) - const h5BaseUrl = 'https://sales.violin-work.online/h5' + const h5BaseUrl = import.meta.env.VITE_H5_BASE_URL const customerId = this.order.customerId || '' const shareUrl = `${h5BaseUrl}/#/pages/share/order?orderNo=${this.order.orderNo}&customerId=${customerId}` diff --git a/vite.config.js b/vite.config.js index 0177f64..0b15f9a 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,14 +1,24 @@ -import { defineConfig } from 'vite' +import { defineConfig, loadEnv } from 'vite' import uni from '@dcloudio/vite-plugin-uni' -export default defineConfig({ - plugins: [ - uni() - ], - root: '.', - build: { - rollupOptions: { - input: './index.html' +export default defineConfig(({ mode }) => { + // 加载当前环境的环境变量 + const env = loadEnv(mode, process.cwd()) + + return { + plugins: [ + uni() + ], + root: '.', + build: { + rollupOptions: { + input: './index.html' + } + }, + define: { + // 注入环境变量到代码中 + 'import.meta.env.VITE_API_BASE_URL': JSON.stringify(env.VITE_API_BASE_URL || 'https://sales.violin-work.online/api/v1'), + 'import.meta.env.VITE_H5_BASE_URL': JSON.stringify(env.VITE_H5_BASE_URL || 'https://sales.violin-work.online') } } })