Selaa lähdekoodia

彻底修复 utils 模块循环依赖,消除 TDZ 初始化错误

将 request/get/post 等网络请求函数从 index.js 抽离到独立的 request.js,
storage 已抽到 storage.js。所有模块依赖单向流动,彻底消除循环:
  index.js → dict.js → request.js → storage.js
  api/*.js → utils/index.js (单向)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
skyline 1 päivä sitten
vanhempi
säilyke
d0f541adab
3 muutettua tiedostoa jossa 85 lisäystä ja 192 poistoa
  1. 2 2
      admin-h5/src/utils/dict.js
  2. 4 190
      admin-h5/src/utils/index.js
  3. 79 0
      admin-h5/src/utils/request.js

+ 2 - 2
admin-h5/src/utils/dict.js

@@ -1,4 +1,4 @@
-import { getDataDictList } from '../api/dict.js'
+import { post } from './request.js'
 import { storage } from './storage.js'
 
 const DICT_KEY = 'dicts'
@@ -13,7 +13,7 @@ const dictUtil = {
    */
   loadDicts: async () => {
     try {
-      const res = await getDataDictList()
+      const res = await post('/dataDict/list', { pageSize: 1024 })
       if (res && res.code === 200) {
         const list = res.data
         const dictGroup = dictUtil.groupByKey(list, 'code')

+ 4 - 190
admin-h5/src/utils/index.js

@@ -1,45 +1,9 @@
-// 工具函数集合
-
-/**
- * 显示加载提示
- * @param {string} title - 提示文字
- */
-export const showLoading = (title = '加载中') => {
-  uni.showLoading({
-    title,
-    mask: true
-  })
-}
-
-/**
- * 隐藏加载提示
- */
-export const hideLoading = () => {
-  uni.hideLoading()
-}
-
-/**
- * 显示Toast提示
- * @param {string} title - 提示文字
- * @param {string} icon - 图标类型 success | error | loading | none
- * @param {number} duration - 显示时长
- */
-export const showToast = (title, icon = 'none', duration = 2000) => {
-  uni.showToast({
-    title,
-    icon,
-    duration
-  })
-}
+// 工具函数集合 — 统一导出
 
+export { showLoading, hideLoading, showToast, request, get, post } from './request.js'
 export { storage } from './storage.js'
+export { fmtDictName, getDictColor } from './dict.js'
 
-/**
- * 时间格式化
- * @param {number|string} time - 时间戳或日期字符串
- * @param {string} format - 格式化模板
- * @returns {string} 格式化后的时间字符串
- */
 export const formatTime = (time, format = 'YYYY-MM-DD HH:mm:ss') => {
   const date = new Date(time)
   const year = date.getFullYear()
@@ -58,159 +22,9 @@ export const formatTime = (time, format = 'YYYY-MM-DD HH:mm:ss') => {
     .replace('ss', second)
 }
 
-/**
- * 金额格式化(支持元或分,保留两位小数)
- * @param {number|string} amount - 金额(元或分)
- * @param {boolean} isCent - 是否为分单位(默认为true,因为项目中金额单位均为分)
- * @returns {string} 格式化后的金额(元)
- */
 export const formatAmount = (amount, isCent = true) => {
-  if (amount === null || amount === undefined || isNaN(amount)) {
-    return '0.00'
-  }
-
-  // 将金额转换为数字
+  if (amount === null || amount === undefined || isNaN(amount)) return '0.00'
   const numAmount = parseFloat(amount)
-
-  // 根据单位转换为元
   const yuan = isCent ? numAmount / 100 : numAmount
-
-  // 保留两位小数
   return yuan.toFixed(2)
 }
-
-// 字典工具函数 — 委托到 utils/dict.js
-import { fmtDictName, getDictColor } from './dict.js'
-export { fmtDictName, getDictColor }
-
-/**
- * 获取请求头
- * @returns {Object} 请求头
- */
-export const getHeaders = () => {
-  const token = storage.get('token')
-  const headers = {
-    'Content-Type': 'application/json'
-  }
-
-  if (token) {
-    headers['accessToken'] = token
-  }
-
-  return headers
-}
-
-/**
- * 网络请求工具
- * @param {Object} config - 请求配置
- * @returns {Promise} 请求结果
- */
-export const request = (config) => {
-  const {
-    url,
-    method = 'GET',
-    data = {},
-    showLoad = true
-  } = config
-
-  if (showLoad) {
-    showLoading()
-  }
-
-  const baseApi = import.meta.env.VITE_BASE_API || 'https://cloud.yeswash.cn/admin'
-
-  const fullUrl = `${baseApi}${url}`
-  console.log(`发起${method}请求:`, fullUrl)
-
-  // 对于GET请求,如果data不为空,将其作为查询参数
-  const requestConfig = {
-    url: fullUrl,
-    method,
-    header: getHeaders()
-  }
-
-  // GET请求使用查询参数,POST/PUT/DELETE使用请求体
-  if (method === 'GET' && Object.keys(data).length > 0) {
-    requestConfig.data = data
-  } else if (method !== 'GET') {
-    requestConfig.data = data
-  }
-
-  return new Promise((resolve, reject) => {
-    uni.request({
-      ...requestConfig,
-      success: (res) => {
-        if (showLoad) {
-          hideLoading()
-        }
-
-        const { statusCode, data } = res
-        console.log(`请求响应 [${statusCode}]:`, fullUrl, data)
-
-        if (statusCode === 200) {
-          // 登录成功后保存token
-          if (url === '/admin-user/login' && data.code === 200) {
-            storage.set('token', data.data.accessToken)
-          }
-
-          if (data.code === 200) {
-            resolve(data)
-          } else {
-            showToast(data.msg || '请求失败')
-            reject(data)
-          }
-        } else if (statusCode === 401) {
-          showToast('登录已过期,请重新登录')
-          storage.remove('token')
-          uni.navigateTo({
-            url: '/pages/login/login'
-          })
-          reject(data)
-        } else {
-          showToast(`请求失败(${statusCode})`)
-          reject(data)
-        }
-      },
-      fail: (err) => {
-        if (showLoad) {
-          hideLoading()
-        }
-        console.error('网络请求失败:', fullUrl, err)
-        showToast('网络请求失败,请检查网络连接')
-        reject(err)
-      }
-    })
-  })
-}
-
-/**
- * GET请求
- * @param {string} url - 请求地址
- * @param {Object} data - 请求参数
- * @param {boolean} showLoad - 是否显示加载提示
- * @returns {Promise} 请求结果
- */
-export const get = (url, data = {}, showLoad = true) => {
-  return request({
-    url,
-    method: 'GET',
-    data,
-    showLoad
-  })
-}
-
-/**
- * POST请求
- * @param {string} url - 请求地址
- * @param {Object} data - 请求参数
- * @param {boolean} showLoad - 是否显示加载提示
- * @returns {Promise} 请求结果
- */
-export const post = (url, data = {}, showLoad = true) => {
-  return request({
-    url,
-    method: 'POST',
-    data,
-    showLoad
-  })
-}

+ 79 - 0
admin-h5/src/utils/request.js

@@ -0,0 +1,79 @@
+import { storage } from './storage.js'
+
+export const showLoading = (title = '加载中') => {
+  uni.showLoading({ title, mask: true })
+}
+
+export const hideLoading = () => {
+  uni.hideLoading()
+}
+
+export const showToast = (title, icon = 'none', duration = 2000) => {
+  uni.showToast({ title, icon, duration })
+}
+
+export const getHeaders = () => {
+  const token = storage.get('token')
+  const headers = { 'Content-Type': 'application/json' }
+  if (token) headers['accessToken'] = token
+  return headers
+}
+
+export const request = (config) => {
+  const { url, method = 'GET', data = {}, showLoad = true } = config
+
+  if (showLoad) showLoading()
+
+  const baseApi = import.meta.env.VITE_BASE_API || 'https://cloud.yeswash.cn/admin'
+  const fullUrl = `${baseApi}${url}`
+
+  const requestConfig = { url: fullUrl, method, header: getHeaders() }
+
+  if (method === 'GET' && Object.keys(data).length > 0) {
+    requestConfig.data = data
+  } else if (method !== 'GET') {
+    requestConfig.data = data
+  }
+
+  return new Promise((resolve, reject) => {
+    uni.request({
+      ...requestConfig,
+      success: (res) => {
+        if (showLoad) hideLoading()
+        const { statusCode, data } = res
+        if (statusCode === 200) {
+          if (url === '/admin-user/login' && data.code === 200) {
+            storage.set('token', data.data.accessToken)
+          }
+          if (data.code === 200) {
+            resolve(data)
+          } else {
+            showToast(data.msg || '请求失败')
+            reject(data)
+          }
+        } else if (statusCode === 401) {
+          showToast('登录已过期,请重新登录')
+          storage.remove('token')
+          uni.navigateTo({ url: '/pages/login/login' })
+          reject(data)
+        } else {
+          showToast(`请求失败(${statusCode})`)
+          reject(data)
+        }
+      },
+      fail: (err) => {
+        if (showLoad) hideLoading()
+        showToast('网络请求失败,请检查网络连接')
+        reject(err)
+      }
+    })
+  })
+}
+
+export const get = (url, data = {}, showLoad = true) => {
+  return request({ url, method: 'GET', data, showLoad })
+}
+
+export const post = (url, data = {}, showLoad = true) => {
+  return request({ url, method: 'POST', data, showLoad })
+}