|
|
@@ -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
|
|
|
- })
|
|
|
-}
|