From 5bdb29994d8b8cf24f4557900b2f72aeee764e35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8F=B2=E9=B8=BD?= <1020103647@qq.com> Date: Wed, 15 May 2024 10:06:51 +0800 Subject: [PATCH 1/9] =?UTF-8?q?refactor:=20=E8=AF=B7=E6=B1=82=E7=AD=89?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useUpload.ts | 60 ++++++-------- src/hooks/useUpload2.ts | 82 ------------------- src/manifest.json | 2 +- src/pages.json | 32 -------- src/pages/index/about.vue | 33 ++------ src/pages/index/components/request.vue | 43 ++++++++++ .../{upload2.vue => components/upload.vue} | 9 +- src/pages/index/request.vue | 67 --------------- src/pages/index/request2.vue | 35 -------- src/pages/index/upload.vue | 75 ----------------- src/service/index/foo.ts | 18 ---- src/types/auto-import.d.ts | 2 - src/types/uni-pages.d.ts | 6 +- 13 files changed, 79 insertions(+), 385 deletions(-) delete mode 100644 src/hooks/useUpload2.ts create mode 100644 src/pages/index/components/request.vue rename src/pages/index/{upload2.vue => components/upload.vue} (59%) delete mode 100644 src/pages/index/request.vue delete mode 100644 src/pages/index/request2.vue delete mode 100644 src/pages/index/upload.vue diff --git a/src/hooks/useUpload.ts b/src/hooks/useUpload.ts index d14e1f1..469aef0 100644 --- a/src/hooks/useUpload.ts +++ b/src/hooks/useUpload.ts @@ -1,3 +1,6 @@ +// TODO: 别忘加更改环境变量的 VITE_UPLOAD_BASEURL 地址。 +const VITE_UPLOAD_BASEURL = import.meta.env.VITE_UPLOAD_BASEURL + /** * useUpload 是一个定制化的请求钩子,用于处理上传图片。 * @param formData 额外传递给后台的数据,如{name: '菲鸽'}。 @@ -7,7 +10,6 @@ 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 代替。 @@ -19,23 +21,7 @@ export default function useUpload(formData: Record = {}) { 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) @@ -50,23 +36,7 @@ export default function useUpload(formData: Record = {}) { 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 +48,23 @@ 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) => { + console.log(uploadFileRes, typeof uploadFileRes.data) + data.value = JSON.parse(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/manifest.json b/src/manifest.json index 0999421..bbd76ff 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -1,5 +1,5 @@ { - "name": "unibest-base", + "name": "unibest", "appid": "H57F2ACE4", "description": "", "versionName": "1.0.0", diff --git a/src/pages.json b/src/pages.json index d62bd4a..4f69a5f 100644 --- a/src/pages.json +++ b/src/pages.json @@ -52,38 +52,6 @@ "style": { "navigationBarTitleText": "关于" } - }, - { - "path": "pages/index/request", - "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": "上传-状态一体化" - } } ], "subPackages": [] diff --git a/src/pages/index/about.vue b/src/pages/index/about.vue index fe28eed..2ba2e3c 100644 --- a/src/pages/index/about.vue +++ b/src/pages/index/about.vue @@ -11,44 +11,21 @@ class="bg-white overflow-hidden pt-2 px-4" :style="{ marginTop: safeAreaInsets?.top + 'px' }" > - 关于页面 鸽友们好,我是 菲鸽 - - 去请求页 - - 去请求页2 (状态一体) - - - - - 上传demo - - 上传demo2(状态一体) - - - - iconfont: - - - - - - + + 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/uni-pages.d.ts b/src/types/uni-pages.d.ts index aea295e..3cf23ca 100644 --- a/src/types/uni-pages.d.ts +++ b/src/types/uni-pages.d.ts @@ -5,11 +5,7 @@ interface NavigateToOptions { url: "/pages/index/index" | - "/pages/index/about" | - "/pages/index/request" | - "/pages/index/request2" | - "/pages/index/upload" | - "/pages/index/upload2"; + "/pages/index/about"; } interface RedirectToOptions extends NavigateToOptions {} From 534d2cb12f43b9a43ad17e0fb556aba21cb0474b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8F=B2=E9=B8=BD?= <1020103647@qq.com> Date: Wed, 15 May 2024 10:08:14 +0800 Subject: [PATCH 2/9] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8=E7=8B=AC?= =?UTF-8?q?=E7=AB=8Babout=E6=96=87=E4=BB=B6=E5=A4=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages.config.ts | 2 +- src/pages.json | 4 ++-- src/pages/{index => about}/about.vue | 0 src/pages/{index => about}/components/request.vue | 0 src/pages/{index => about}/components/upload.vue | 0 src/types/uni-pages.d.ts | 4 ++-- 6 files changed, 5 insertions(+), 5 deletions(-) rename src/pages/{index => about}/about.vue (100%) rename src/pages/{index => about}/components/request.vue (100%) rename src/pages/{index => about}/components/upload.vue (100%) diff --git a/pages.config.ts b/pages.config.ts index 9e3499f..1745eb0 100644 --- a/pages.config.ts +++ b/pages.config.ts @@ -35,7 +35,7 @@ export default defineUniPages({ { iconPath: 'static/tabbar/example.png', selectedIconPath: 'static/tabbar/exampleHL.png', - pagePath: 'pages/index/about', + pagePath: 'pages/about/about', text: '关于', }, ], diff --git a/src/pages.json b/src/pages.json index 4f69a5f..075e6aa 100644 --- a/src/pages.json +++ b/src/pages.json @@ -32,7 +32,7 @@ { "iconPath": "static/tabbar/example.png", "selectedIconPath": "static/tabbar/exampleHL.png", - "pagePath": "pages/index/about", + "pagePath": "pages/about/about", "text": "关于" } ] @@ -47,7 +47,7 @@ } }, { - "path": "pages/index/about", + "path": "pages/about/about", "type": "page", "style": { "navigationBarTitleText": "关于" diff --git a/src/pages/index/about.vue b/src/pages/about/about.vue similarity index 100% rename from src/pages/index/about.vue rename to src/pages/about/about.vue diff --git a/src/pages/index/components/request.vue b/src/pages/about/components/request.vue similarity index 100% rename from src/pages/index/components/request.vue rename to src/pages/about/components/request.vue diff --git a/src/pages/index/components/upload.vue b/src/pages/about/components/upload.vue similarity index 100% rename from src/pages/index/components/upload.vue rename to src/pages/about/components/upload.vue diff --git a/src/types/uni-pages.d.ts b/src/types/uni-pages.d.ts index 3cf23ca..1644db7 100644 --- a/src/types/uni-pages.d.ts +++ b/src/types/uni-pages.d.ts @@ -5,12 +5,12 @@ interface NavigateToOptions { url: "/pages/index/index" | - "/pages/index/about"; + "/pages/about/about"; } interface RedirectToOptions extends NavigateToOptions {} interface SwitchTabOptions { - url: "/pages/index/index" | "/pages/index/about" + url: "/pages/index/index" | "/pages/about/about" } type ReLaunchOptions = NavigateToOptions | SwitchTabOptions; From 5b16ebe1fe29deb01b393335d0e3f8ec0a9bb9ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8F=B2=E9=B8=BD?= <1020103647@qq.com> Date: Wed, 15 May 2024 10:09:38 +0800 Subject: [PATCH 3/9] =?UTF-8?q?refine:=20=E5=85=BC=E5=AE=B9=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E6=8E=A5=E5=8F=A3=E6=98=AFurl=E5=92=8Cobj?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/about/components/upload.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/about/components/upload.vue b/src/pages/about/components/upload.vue index fc76e04..e2de4be 100644 --- a/src/pages/about/components/upload.vue +++ b/src/pages/about/components/upload.vue @@ -15,7 +15,7 @@ 上传后返回的接口数据: {{ JSON.stringify(data) }} - + From ef83c31b8bcc44cbb400606470ffb7144e544fb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8F=B2=E9=B8=BD?= <1020103647@qq.com> Date: Wed, 15 May 2024 10:12:40 +0800 Subject: [PATCH 4/9] =?UTF-8?q?refine:=20upload=20=E8=BF=98=E6=98=AF?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E4=B8=80=E4=B8=AAurl=E5=90=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useUpload.ts | 7 ++----- src/pages/about/components/upload.vue | 6 +++--- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/hooks/useUpload.ts b/src/hooks/useUpload.ts index 469aef0..4870a9c 100644 --- a/src/hooks/useUpload.ts +++ b/src/hooks/useUpload.ts @@ -6,7 +6,7 @@ const VITE_UPLOAD_BASEURL = import.meta.env.VITE_UPLOAD_BASEURL * @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() @@ -18,7 +18,6 @@ export default function useUpload(formData: Record = {}) { count: 1, mediaType: ['image'], success: (res) => { - console.log(res) loading.value = true const tempFilePath = res.tempFiles[0].tempFilePath uploadFile({ tempFilePath, formData, data, error, loading }) @@ -33,7 +32,6 @@ export default function useUpload(formData: Record = {}) { uni.chooseImage({ count: 1, success: (res) => { - console.log(res) loading.value = true const tempFilePath = res.tempFilePaths[0] uploadFile({ tempFilePath, formData, data, error, loading }) @@ -56,8 +54,7 @@ function uploadFile({ tempFilePath, formData, data, error, loading }) { name: 'file', formData, success: (uploadFileRes) => { - console.log(uploadFileRes, typeof uploadFileRes.data) - data.value = JSON.parse(uploadFileRes.data) as T + data.value = uploadFileRes.data as T }, fail: (err) => { console.log('uni.uploadFile err->', err) diff --git a/src/pages/about/components/upload.vue b/src/pages/about/components/upload.vue index e2de4be..07f81f5 100644 --- a/src/pages/about/components/upload.vue +++ b/src/pages/about/components/upload.vue @@ -13,16 +13,16 @@ 上传...