Bläddra i källkod

修改错误类型

yuanmingze 3 månader sedan
förälder
incheckning
ff902d240f
7 ändrade filer med 44 tillägg och 14 borttagningar
  1. 0 4
      components.d.ts
  2. BIN
      dist.zip
  3. 1 1
      package.json
  4. 1 1
      src/main.ts
  5. 41 6
      src/services/request/index.ts
  6. 0 2
      src/views/invoice-information/index.vue
  7. 1 0
      vite.config.ts

+ 0 - 4
components.d.ts

@@ -21,10 +21,6 @@ declare module 'vue' {
     VanField: typeof import('vant/es')['Field']
     VanIcon: typeof import('vant/es')['Icon']
     VanNavBar: typeof import('vant/es')['NavBar']
-    VanSkeleton: typeof import('vant/es')['Skeleton']
-    VanSkeletonParagraph: typeof import('vant/es')['SkeletonParagraph']
-    VanStep: typeof import('vant/es')['Step']
-    VanSteps: typeof import('vant/es')['Steps']
     VanUploader: typeof import('vant/es')['Uploader']
   }
 }

BIN
dist.zip


+ 1 - 1
package.json

@@ -9,7 +9,7 @@
   "scripts": {
     "dev": "vite --host",
     "build": "run-p type-check \"build-only {@}\" --",
-    "build:pre": "run-p type-check \"build-only --mode pre\" --", //
+    "build:pre": "run-p type-check \"build-only --mode pre\" --",
     "preview": "vite preview",
     "build-only": "vite build",
     "type-check": "vue-tsc --build",

+ 1 - 1
src/main.ts

@@ -1,9 +1,9 @@
 import { createApp } from 'vue'
 import 'normalize.css'
-import 'vant/lib/index.css'
 import vueDompurifyHTMLPlugin from 'vue-dompurify-html'
 import VueViewer from 'v-viewer'
 import 'viewerjs/dist/viewer.css'
+import 'vant/lib/index.css'
 import { setupStore } from '@/stores'
 import App from './App.vue'
 import router from './router'

+ 41 - 6
src/services/request/index.ts

@@ -35,15 +35,22 @@ export interface ServiceResponse<T = any> {
 }
 
 /** 自定义错误类 */
+export interface HttpErrorOptions {
+  code?: string | number
+  status?: number
+  data?: any
+  response?: any
+}
+
 export class HttpError extends Error {
   code?: string | number
   status?: number
   data?: any
-  constructor(message: string, options?: { code?: string | number; status?: number; data?: any }) {
+  response?: any
+
+  constructor(message: string, options: HttpErrorOptions = {}) {
     super(message)
-    this.code = options?.code
-    this.status = options?.status
-    this.data = options?.data
+    Object.assign(this, options)
   }
 }
 
@@ -81,8 +88,31 @@ const abortAllRequests = () => {
 
 const handleNetworkError = (response: AxiosResponse, silent?: boolean) => {
   const status = response?.status
+  const backendCode = response?.data?.code
+  const backendMsg = response?.data?.msg
+  const backendData = response?.data?.data
+
+  // 默认错误信息
   let message = '未知错误'
 
+  // ✅ 优先使用后端业务错误(即 code !== 0)
+  if (backendCode && backendCode !== 0) {
+    message = backendMsg || '业务处理失败'
+
+    if (!silent) {
+      // showToast(message)
+    }
+
+    // ✅ 抛出包含后端信息的 HttpError
+    throw new HttpError(message, {
+      code: backendCode,
+      status,
+      data: backendData,
+      response: response.data,
+    })
+  }
+
+  // 🚨 否则走 HTTP 状态码分支
   switch (status) {
     case 400:
       message = '错误的请求'
@@ -119,12 +149,17 @@ const handleNetworkError = (response: AxiosResponse, silent?: boolean) => {
       message = `其他错误:${status}`
   }
 
-  const msg = response.data?.msg || response.data?.data || message
+  const msg = backendMsg || backendData || message
   if (!silent) {
     showToast(msg)
   }
 
-  throw new HttpError(msg, { code: response.data?.code, status, data: response.data })
+  throw new HttpError(msg, {
+    code: backendCode || 'HTTP_ERROR',
+    status,
+    data: backendData,
+    response: response.data,
+  })
 }
 
 /* -------------------------------------------------------------------------- */

+ 0 - 2
src/views/invoice-information/index.vue

@@ -175,8 +175,6 @@ const getConfirmInvoiceInfo = async () => {
   try {
     loading.value = true
     const res = await getConfirmInvoiceInfoApi(params)
-    console.log('res', res)
-
     if (res.code === 0) {
       invoiceInfo.value = res.data
     }

+ 1 - 0
vite.config.ts

@@ -12,6 +12,7 @@ export default defineConfig(({ mode }): UserConfig => {
   // const isProduction = mode === 'production'
 
   return {
+    base: '/h5/',
     plugins: [
       vue(),