feat: Access environment variables using import.meta

This commit is contained in:
coderwei 2025-05-21 11:41:50 +08:00
parent 8abae324a5
commit 4f15cec9ea
21 changed files with 84 additions and 31 deletions

View File

@ -68,7 +68,7 @@ watch(
const handler = ref(0);
// websocket
const connect = () => {
let host = process.env.VUE_APP_WS_HOST;
let host = import.meta.env.VITE_APP_WS_HOST;
if (host === "") {
if (location.protocol === "https:") {
host = "wss://" + location.host;

View File

@ -20,8 +20,8 @@ import { showMessageError } from "@/utils/dialog";
import { getLicenseInfo, getSystemInfo } from "@/store/cache";
const title = ref("");
const version = ref(process.env.VUE_APP_VERSION);
const gitURL = ref(process.env.VUE_APP_GIT_URL);
const version = ref(import.meta.env.VITE_APP_VERSION);
const gitURL = ref(import.meta.env.VITE_APP_GIT_URL);
const copyRight = ref("");
const license = ref({});
const props = defineProps({
@ -34,7 +34,7 @@ const props = defineProps({
//
getSystemInfo()
.then((res) => {
title.value = res.data.title ?? process.env.VUE_APP_TITLE;
title.value = res.data.title ?? import.meta.env.VITE_APP_TITLE;
copyRight.value =
res.data.copyright.length > 1
? res.data.copyright

View File

@ -218,8 +218,8 @@ watch(
const login = ref(true);
const data = ref({
username: process.env.VUE_APP_USER,
password: process.env.VUE_APP_PASS,
username: import.meta.env.VITE_APP_USER,
password: import.meta.env.VITE_APP_PASS,
mobile: "",
email: "",
repass: "",

View File

@ -107,7 +107,7 @@ const animateVoice = () => {
const wavRecorder = ref(new WavRecorder({ sampleRate: 24000 }));
const wavStreamPlayer = ref(new WavStreamPlayer({ sampleRate: 24000 }));
let host = process.env.VUE_APP_WS_HOST;
let host = import.meta.env.VITE_APP_WS_HOST;
if (host === "") {
if (location.protocol === "https:") {
host = "wss://" + location.host;

View File

@ -62,8 +62,8 @@ import { onMounted, ref } from "vue";
import { ElMessage } from "element-plus";
import { getSystemInfo } from "@/store/cache";
const title = ref(process.env.VUE_APP_TITLE);
const version = ref(process.env.VUE_APP_VERSION);
const title = ref(import.meta.env.VITE_APP_TITLE);
const version = ref(import.meta.env.VITE_APP_VERSION);
const samples = ref([
"用小学生都能听懂的术语解释什么是量子纠缠",

View File

@ -52,7 +52,7 @@ import {ElMessage} from "element-plus";
import {removeAdminToken} from "@/store/session";
import {useSharedStore} from "@/store/sharedata";
const version = ref(process.env.VUE_APP_VERSION);
const version = ref(import.meta.env.VITE_APP_VERSION);
const avatar = ref("/images/user-info.jpg");
const sidebar = useSidebarStore();
const router = useRouter();

View File

@ -6,8 +6,8 @@ import {removeAdminInfo} from "@/store/cache";
* storage handler
*/
const UserTokenKey = process.env.VUE_APP_KEY_PREFIX + "Authorization";
const AdminTokenKey = process.env.VUE_APP_KEY_PREFIX + "Admin-Authorization"
const UserTokenKey = import.meta.env.VITE_APP_KEY_PREFIX + "Authorization";
const AdminTokenKey = import.meta.env.VITE_APP_KEY_PREFIX + "Admin-Authorization"
export function getSessionId() {
return randString(42)

View File

@ -46,9 +46,9 @@ export function FormatFileSize(bytes) {
}
export function setRoute(path) {
Storage.set(process.env.VUE_APP_KEY_PREFIX + 'ROUTE_',path)
Storage.set(import.meta.env.VITE_APP_KEY_PREFIX + 'ROUTE_', path)
}
export function getRoute() {
return Storage.get(process.env.VUE_APP_KEY_PREFIX + 'ROUTE_')
return Storage.get(import.meta.env.VITE_APP_KEY_PREFIX + 'ROUTE_')
}

View File

@ -9,7 +9,7 @@ import axios from 'axios'
import {getAdminToken, getUserToken, removeAdminToken, removeUserToken} from "@/store/session";
axios.defaults.timeout = 180000
axios.defaults.baseURL = process.env.VUE_APP_API_HOST
axios.defaults.baseURL = import.meta.env.VITE_APP_API_HOST
axios.defaults.withCredentials = true;
//axios.defaults.headers.post['Content-Type'] = 'application/json'

View File

@ -228,7 +228,7 @@ export const replaceImg =(img) => {
if (!img.startsWith("http")) {
img = `${location.protocol}//${location.host}/${img}`
}
const devHost = process.env.VUE_APP_API_HOST
const devHost = import.meta.env.VITE_APP_API_HOST
const localhost = "http://localhost:5678"
if (img.includes(localhost)) {
return img?.replace(localhost, devHost)

View File

@ -169,7 +169,7 @@ const loginUser = ref({});
const routerViewKey = ref(0);
const showConfigDialog = ref(false);
const license = ref({ de_copy: true });
const gitURL = ref(process.env.VUE_APP_GIT_URL);
const gitURL = ref(import.meta.env.VITE_APP_GIT_URL);
const showLoginDialog = ref(false);
/**

View File

@ -80,8 +80,8 @@ const slogan = ref("");
const license = ref({ de_copy: true });
const isLogin = ref(false);
const docsURL = ref(process.env.VUE_APP_DOCS_URL);
const gitURL = ref(process.env.VUE_APP_GIT_URL);
const docsURL = ref(import.meta.env.VITE_APP_DOCS_URL);
const gitURL = ref(import.meta.env.VITE_APP_GIT_URL);
const navs = ref([]);
const iconMap = ref({

View File

@ -62,8 +62,8 @@ const enableVerify = ref(false);
const captchaRef = ref(null);
const ruleFormRef = ref(null);
const ruleForm = reactive({
username: process.env.VUE_APP_USER,
password: process.env.VUE_APP_PASS,
username: import.meta.env.VITE_APP_USER,
password: import.meta.env.VITE_APP_PASS,
});
const rules = {
username: [{ required: true, trigger: "blur", message: "请输入账号" }],

View File

@ -171,7 +171,7 @@ onUnmounted(() => {
const download = (item) => {
const url = replaceImg(item.video_url);
const downloadURL = `${process.env.VUE_APP_API_HOST}/api/download?url=${url}`;
const downloadURL = `${import.meta.env.VITE_APP_API_HOST}/api/download?url=${url}`;
// parse filename
const urlObj = new URL(url);
const fileName = urlObj.pathname.split("/").pop();

View File

@ -209,11 +209,11 @@ const pay = (product, payWay) => {
}
loading.value = true;
loadingText.value = "正在生成支付订单...";
let host = process.env.VUE_APP_API_HOST;
let host = import.meta.env.VITE_APP_API_HOST;
if (host === "") {
host = `${location.protocol}//${location.host}`;
}
httpPost(`${process.env.VUE_APP_API_HOST}/api/payment/doPay`, {
httpPost(`${import.meta.env.VITE_APP_API_HOST}/api/payment/doPay`, {
product_id: product.id,
pay_way: payWay.pay_way,
pay_type: payWay.pay_type,

View File

@ -447,7 +447,7 @@ const merge = (item) => {
//
const download = (item) => {
const url = replaceImg(item.audio_url);
const downloadURL = `${process.env.VUE_APP_API_HOST}/api/download?url=${url}`;
const downloadURL = `${import.meta.env.VITE_APP_API_HOST}/api/download?url=${url}`;
// parse filename
const urlObj = new URL(url);
const fileName = urlObj.pathname.split("/").pop();

View File

@ -52,8 +52,8 @@ import Captcha from "@/components/Captcha.vue";
const router = useRouter();
const title = ref("Geek-AI Console");
const username = ref(process.env.VUE_APP_ADMIN_USER);
const password = ref(process.env.VUE_APP_ADMIN_PASS);
const username = ref(import.meta.env.VITE_APP_ADMIN_USER);
const password = ref(import.meta.env.VITE_APP_ADMIN_PASS);
const logo = ref("");
const enableVerify = ref(false);
const captchaRef = ref(null);

View File

@ -382,7 +382,7 @@ watch(
// const connect = function () {
// // WebSocket
// const _sessionId = getSessionId();
// let host = process.env.VUE_APP_WS_HOST
// let host = import.meta.env.VITE_APP_WS_HOST
// if (host === '') {
// if (location.protocol === 'https:') {
// host = 'wss://' + location.host;

View File

@ -74,7 +74,7 @@ import { arrayContains, removeArrayItem, showLoginDialog, substr } from "@/utils
import { showNotify } from "vant";
import { ElMessage } from "element-plus";
const title = ref(process.env.VUE_APP_TITLE);
const title = ref(import.meta.env.VITE_APP_TITLE);
const router = useRouter();
const isLogin = ref(false);
const apps = ref([]);

View File

@ -284,11 +284,11 @@ const pay = (product, payWay) => {
message: "正在创建订单",
forbidClick: true,
});
let host = process.env.VUE_APP_API_HOST;
let host = import.meta.env.VITE_APP_API_HOST;
if (host === "") {
host = `${location.protocol}//${location.host}`;
}
httpPost(`${process.env.VUE_APP_API_HOST}/api/payment/doPay`, {
httpPost(`${import.meta.env.VITE_APP_API_HOST}/api/payment/doPay`, {
product_id: product.id,
pay_way: payWay.pay_way,
pay_type: payWay.pay_type,

53
web/vite.config.js Normal file
View File

@ -0,0 +1,53 @@
import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import path from 'path';
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd());
console.log('VITE_API_HOST from .env:', env);
return {
plugins: [vue()],
resolve: {
alias: {
'@': path.resolve(__dirname, 'src'),
},
},
css: {
preprocessorOptions: {
stylus: {
paths: [path.resolve(__dirname, 'src')],
},
},
},
build: {
sourcemap: false,
minify: 'terser',
rollupOptions: {
output: {
manualChunks: {
vendor: ['vue', 'vue-router', 'pinia'],
},
},
},
},
server: {
port: 8888,
host: true,
proxy: {
'/api': {
target: env.VITE_APP_API_HOST,
changeOrigin: true,
},
'/static/upload/': {
target: env.VITE_APP_API_HOST,
changeOrigin: true,
},
},
},
esbuild: {
// 模拟 transpileDependencies: true
},
};
});