diff --git a/env/.env b/env/.env index 8f89c8f..d068df7 100644 --- a/env/.env +++ b/env/.env @@ -1,14 +1,15 @@ VITE_APP_TITLE = 'unibest' VITE_APP_PORT = 9000 -# h5部署网站的base,配置到 manifest.config.ts 里的 h5.router.base -VITE_APP_PUBLIC_BASE=/ - VITE_UNI_APPID = 'H57F2ACE4' VITE_WX_APPID = 'wxa2abb91f64032a2b' -# 非h5端只能使用完整的baseurl,否则无法请求,本地proxy只支持h5端 -# VITE_SERVER_BASEURL = '/api' -VITE_SERVER_BASEURL = 'https://ukw0y1.laf.run' +# h5部署网站的base,配置到 manifest.config.ts 里的 h5.router.base +VITE_APP_PUBLIC_BASE=/ +VITE_SERVER_BASEURL = 'https://ukw0y1.laf.run' VITE_UPLOAD_BASEURL = 'https://ukw0y1.laf.run/upload' + +# h5是否需要配置代理 +VITE_APP_PROXY=true +VITE_APP_PROXY_PREFIX = '/api' diff --git a/manifest.config.ts b/manifest.config.ts index 75720a2..90becaf 100644 --- a/manifest.config.ts +++ b/manifest.config.ts @@ -29,12 +29,12 @@ export default defineManifestConfig({ }, /* 5+App特有相关 */ 'app-plus': { - compatible: { - ignoreVersion: true, - }, usingComponents: true, nvueStyleCompiler: 'uni-app', compilerVersion: 3, + compatible: { + ignoreVersion: true, + }, splashscreen: { alwaysShowBeforeRender: true, waiting: true, diff --git a/package.json b/package.json index 0c66f81..08c536c 100644 --- a/package.json +++ b/package.json @@ -109,7 +109,10 @@ "@dcloudio/uni-cli-shared": "3.0.0-4010420240430001", "@dcloudio/uni-stacktracey": "3.0.0-4010420240430001", "@dcloudio/vite-plugin-uni": "3.0.0-4010420240430001", + "@esbuild/darwin-arm64": "0.20.2", + "@esbuild/darwin-x64": "0.20.2", "@iconify-json/carbon": "^1.1.27", + "@rollup/rollup-darwin-x64": "^4.17.2", "@types/node": "^20.11.5", "@types/wechat-miniprogram": "^3.4.7", "@typescript-eslint/eslint-plugin": "^6.15.0", diff --git a/src/env.d.ts b/src/env.d.ts index f19eda8..daea211 100644 --- a/src/env.d.ts +++ b/src/env.d.ts @@ -15,6 +15,10 @@ interface ImportMetaEnv { readonly VITE_SERVER_PORT: string /** 后台接口地址 */ readonly VITE_SERVER_BASEURL: string + /** H5是否需要代理 */ + readonly VITE_APP_PROXY: 'true' | 'false' + /** H5是否需要代理,需要的话有个前缀 */ + readonly VITE_APP_PROXY_PREFIX: string // 一般是/api /** 上传图片地址 */ readonly VITE_UPLOAD_BASEURL: string /** 是否清除console */ diff --git a/src/hooks/useUpload.ts b/src/hooks/useUpload.ts index d14e1f1..4870a9c 100644 --- a/src/hooks/useUpload.ts +++ b/src/hooks/useUpload.ts @@ -1,13 +1,15 @@ +// TODO: 别忘加更改环境变量的 VITE_UPLOAD_BASEURL 地址。 +const VITE_UPLOAD_BASEURL = import.meta.env.VITE_UPLOAD_BASEURL + /** * useUpload 是一个定制化的请求钩子,用于处理上传图片。 * @param formData 额外传递给后台的数据,如{name: '菲鸽'}。 * @returns 返回一个对象{loading, error, data, run},包含请求的加载状态、错误信息、响应数据和手动触发请求的函数。 */ -export default function useUpload(formData: Record = {}) { +export default function useUpload(formData: Record = {}) { const loading = ref(false) const error = ref(false) const data = ref() - const url = import.meta.env.VITE_UPLOAD_BASEURL const run = () => { // #ifdef MP-WEIXIN // 微信小程序从基础库 2.21.0 开始, wx.chooseImage 停止维护,请使用 uni.chooseMedia 代替。 @@ -16,26 +18,9 @@ export default function useUpload(formData: Record = {}) { count: 1, mediaType: ['image'], success: (res) => { - console.log(res) loading.value = true const tempFilePath = res.tempFiles[0].tempFilePath - uni.uploadFile({ - url, - filePath: tempFilePath, - name: 'file', - formData, - success: (uploadFileRes) => { - console.log(uploadFileRes.data) - data.value = uploadFileRes.data as T - }, - fail: (err) => { - console.log('uni.uploadFile err->', err) - error.value = true - }, - complete: () => { - loading.value = false - }, - }) + uploadFile({ tempFilePath, formData, data, error, loading }) }, fail: (err) => { console.log('uni.chooseMedia err->', err) @@ -47,26 +32,9 @@ export default function useUpload(formData: Record = {}) { uni.chooseImage({ count: 1, success: (res) => { - console.log(res) loading.value = true const tempFilePath = res.tempFilePaths[0] - uni.uploadFile({ - url, - filePath: tempFilePath, - name: 'file', - formData, - success: (uploadFileRes) => { - console.log(uploadFileRes.data) - data.value = uploadFileRes.data as T - }, - fail: (err) => { - console.log('uni.uploadFile err->', err) - error.value = true - }, - complete: () => { - loading.value = false - }, - }) + uploadFile({ tempFilePath, formData, data, error, loading }) }, fail: (err) => { console.log('uni.chooseImage err->', err) @@ -78,3 +46,22 @@ export default function useUpload(formData: Record = {}) { return { loading, error, data, run } } + +function uploadFile({ tempFilePath, formData, data, error, loading }) { + uni.uploadFile({ + url: VITE_UPLOAD_BASEURL, + filePath: tempFilePath, + name: 'file', + formData, + success: (uploadFileRes) => { + data.value = uploadFileRes.data as T + }, + fail: (err) => { + console.log('uni.uploadFile err->', err) + error.value = true + }, + complete: () => { + loading.value = false + }, + }) +} diff --git a/src/hooks/useUpload2.ts b/src/hooks/useUpload2.ts deleted file mode 100644 index b99e374..0000000 --- a/src/hooks/useUpload2.ts +++ /dev/null @@ -1,82 +0,0 @@ -/** - * useUpload 是一个定制化的请求钩子,用于处理上传图片。 - * @param url 上传图片的后台地址,如 https://ukw0y1.laf.run/upload。 - * 如果上传地址是固定的,那就可以配置到 .env 里面,函数里面不需要再传了。 - * @param formData 额外传递给后台的数据,如{name: '菲鸽'}。 - * @returns 返回一个对象{loading, error, data, run},包含请求的加载状态、错误信息、响应数据和手动触发请求的函数。 - */ -export default function useUpload(url: string, formData: Record = {}) { - const loading = ref(false) - const error = ref(false) - const data = ref() - - const run = () => { - // #ifdef MP-WEIXIN - // 微信小程序从基础库 2.21.0 开始, wx.chooseImage 停止维护,请使用 uni.chooseMedia 代替。 - // 微信小程序在2023年10月17日之后,使用本API需要配置隐私协议 - uni.chooseMedia({ - count: 1, - mediaType: ['image'], - success: (res) => { - console.log(res) - loading.value = true - const tempFilePath = res.tempFiles[0].tempFilePath - uni.uploadFile({ - url, - filePath: tempFilePath, - name: 'file', - formData, - success: (uploadFileRes) => { - console.log(uploadFileRes.data) - data.value = uploadFileRes.data as T - }, - fail: (err) => { - console.log('uni.uploadFile err->', err) - error.value = true - }, - complete: () => { - loading.value = false - }, - }) - }, - fail: (err) => { - console.log('uni.chooseMedia err->', err) - error.value = true - }, - }) - // #endif - // #ifndef MP-WEIXIN - uni.chooseImage({ - count: 1, - success: (res) => { - console.log(res) - loading.value = true - const tempFilePath = res.tempFilePaths[0] - uni.uploadFile({ - url, - filePath: tempFilePath, - name: 'file', - formData, - success: (uploadFileRes) => { - console.log(uploadFileRes.data) - data.value = uploadFileRes.data as T - }, - fail: (err) => { - console.log('uni.uploadFile err->', err) - error.value = true - }, - complete: () => { - loading.value = false - }, - }) - }, - fail: (err) => { - console.log('uni.chooseImage err->', err) - error.value = true - }, - }) - // #endif - } - - return { loading, error, data, run } -} diff --git a/src/interceptors/request.ts b/src/interceptors/request.ts index 5ae62f3..499285e 100644 --- a/src/interceptors/request.ts +++ b/src/interceptors/request.ts @@ -27,7 +27,18 @@ const httpInterceptor = { } // 非 http 开头需拼接地址 if (!options.url.startsWith('http')) { + // #ifdef H5 + console.log(__VITE_APP_PROXY__) + if (JSON.parse(__VITE_APP_PROXY__)) { + // 啥都不需要做 + } else { + options.url = baseUrl + options.url + } + // #endif + // 非H5正常拼接 + // #ifndef H5 options.url = baseUrl + options.url + // #endif // TIPS: 如果需要对接多个后端服务,也可以在这里处理,拼接成所需要的地址 } // 1. 请求超时 diff --git a/src/pages.json b/src/pages.json index c9da109..9698203 100644 --- a/src/pages.json +++ b/src/pages.json @@ -55,42 +55,17 @@ } }, { - "path": "pages/index/about", + "path": "pages/about/about", "type": "page", "style": { "navigationBarTitleText": "关于" } }, { - "path": "pages/index/request", + "path": "pages/index/about", "type": "page", - "layout": "demo", "style": { - "navigationBarTitleText": "请求" - } - }, - { - "path": "pages/index/request2", - "type": "page", - "layout": "demo", - "style": { - "navigationBarTitleText": "请求-状态一体化" - } - }, - { - "path": "pages/index/upload", - "type": "page", - "layout": "default", - "style": { - "navigationBarTitleText": "上传" - } - }, - { - "path": "pages/index/upload2", - "type": "page", - "layout": "default", - "style": { - "navigationBarTitleText": "上传-状态一体化" + "navigationBarTitleText": "关于" } }, { diff --git a/src/pages/about/about.vue b/src/pages/about/about.vue new file mode 100644 index 0000000..2ba2e3c --- /dev/null +++ b/src/pages/about/about.vue @@ -0,0 +1,36 @@ + +{ + style: { + navigationBarTitleText: '关于', + }, +} + + + + + + + diff --git a/src/pages/about/components/request.vue b/src/pages/about/components/request.vue new file mode 100644 index 0000000..d01be83 --- /dev/null +++ b/src/pages/about/components/request.vue @@ -0,0 +1,43 @@ + +{ + layout: 'demo', + style: { + navigationBarTitleText: '请求', + }, +} + + + + + diff --git a/src/pages/index/upload2.vue b/src/pages/about/components/upload.vue similarity index 64% rename from src/pages/index/upload2.vue rename to src/pages/about/components/upload.vue index 42c54f4..07f81f5 100644 --- a/src/pages/index/upload2.vue +++ b/src/pages/about/components/upload.vue @@ -12,18 +12,17 @@ 选择图片并上传 上传... diff --git a/src/service/index/foo.ts b/src/service/index/foo.ts index dac3519..acf5aa8 100644 --- a/src/service/index/foo.ts +++ b/src/service/index/foo.ts @@ -6,28 +6,10 @@ export interface IFooItem { /** GET 请求 */ export const getFooAPI = (name: string) => { - return http({ - url: `/foo`, - method: 'GET', - query: { name }, - }) -} - -/** GET 请求 - 再次简化,看大家是否喜欢这种简化 */ -export const getFooAPI2 = (name: string) => { return http.get('/foo', { name }) } /** POST 请求 */ export const postFooAPI = (name: string) => { - return http({ - url: `/foo`, - method: 'POST', - query: { name }, // post 请求也支持 query - data: { name }, - }) -} -/** POST 请求 - 再次简化,看大家是否喜欢这种简化 */ -export const postFooAPI2 = (name: string) => { return http.post('/foo', { name }, { name }) } diff --git a/src/types/auto-import.d.ts b/src/types/auto-import.d.ts index a822be5..6351212 100644 --- a/src/types/auto-import.d.ts +++ b/src/types/auto-import.d.ts @@ -170,7 +170,6 @@ declare module 'vue' { readonly useCssVars: UnwrapRef readonly useRequest: UnwrapRef readonly useSlots: UnwrapRef - readonly useUpload2: UnwrapRef readonly useUpload: UnwrapRef readonly watch: UnwrapRef readonly watchEffect: UnwrapRef @@ -254,7 +253,6 @@ declare module '@vue/runtime-core' { readonly useCssVars: UnwrapRef readonly useRequest: UnwrapRef readonly useSlots: UnwrapRef - readonly useUpload2: UnwrapRef readonly useUpload: UnwrapRef readonly watch: UnwrapRef readonly watchEffect: UnwrapRef diff --git a/src/types/global.d.ts b/src/types/global.d.ts index 2e67ed8..c243ef0 100644 --- a/src/types/global.d.ts +++ b/src/types/global.d.ts @@ -12,3 +12,5 @@ declare const __UNI_PLATFORM__: | 'quickapp-webview' | 'quickapp-webview-huawei' | 'quickapp-webview-union' + +declare const __VITE_APP_PROXY__: 'true' | 'false' diff --git a/src/types/uni-pages.d.ts b/src/types/uni-pages.d.ts index f809bcc..1964130 100644 --- a/src/types/uni-pages.d.ts +++ b/src/types/uni-pages.d.ts @@ -5,11 +5,8 @@ interface NavigateToOptions { url: "/pages/index/index" | + "/pages/about/about" | "/pages/index/about" | - "/pages/index/request" | - "/pages/index/request2" | - "/pages/index/upload" | - "/pages/index/upload2" | "/pages/my/index"; } interface RedirectToOptions extends NavigateToOptions {} diff --git a/vite.config.ts b/vite.config.ts index 24acf1c..265f752 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -35,7 +35,14 @@ export default ({ command, mode }) => { console.log('UNI_PLATFORM -> ', UNI_PLATFORM) // 得到 mp-weixin, h5, app 等 const env = loadEnv(mode, path.resolve(process.cwd(), 'env')) - const { VITE_APP_PORT, VITE_SERVER_BASEURL, VITE_DELETE_CONSOLE, VITE_SHOW_SOURCEMAP } = env + const { + VITE_APP_PORT, + VITE_SERVER_BASEURL, + VITE_DELETE_CONSOLE, + VITE_SHOW_SOURCEMAP, + VITE_APP_PROXY, + VITE_APP_PROXY_PREFIX, + } = env console.log('环境变量 env -> ', env) return defineConfig({ @@ -87,6 +94,7 @@ export default ({ command, mode }) => { ], define: { __UNI_PLATFORM__: JSON.stringify(UNI_PLATFORM), + __VITE_APP_PROXY__: JSON.stringify(VITE_APP_PROXY), }, css: { postcss: { @@ -110,13 +118,15 @@ export default ({ command, mode }) => { hmr: true, port: Number.parseInt(VITE_APP_PORT, 10), // 仅 H5 端生效,其他端不生效(其他端走build,不走devServer) - // proxy: { - // '/api': { - // target: VITE_SERVER_BASEURL, - // changeOrigin: true, - // rewrite: (path) => path.replace(/^\/api/, ''), - // }, - // }, + proxy: JSON.parse(VITE_APP_PROXY) + ? { + [VITE_APP_PROXY_PREFIX]: { + target: VITE_SERVER_BASEURL, + changeOrigin: true, + rewrite: (path) => path.replace(new RegExp(`^${VITE_APP_PROXY_PREFIX}`), ''), + }, + } + : undefined, }, build: { // 方便非h5端调试