| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- // 工具函数集合
- /**
- * 显示加载提示
- * @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 const storage = {
- /**
- * 设置缓存
- * @param {string} key - 缓存键
- * @param {any} value - 缓存值
- */
- set(key, value) {
- if (typeof value === 'object') {
- value = JSON.stringify(value)
- }
- uni.setStorageSync(key, value)
- },
- /**
- * 获取缓存
- * @param {string} key - 缓存键
- * @returns {any} 缓存值
- */
- get(key) {
- const value = uni.getStorageSync(key)
- try {
- return JSON.parse(value)
- } catch (e) {
- return value
- }
- },
- /**
- * 删除缓存
- * @param {string} key - 缓存键
- */
- remove(key) {
- uni.removeStorageSync(key)
- },
- /**
- * 清空缓存
- */
- clear() {
- uni.clearStorageSync()
- }
- }
- /**
- * 时间格式化
- * @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()
- const month = String(date.getMonth() + 1).padStart(2, '0')
- const day = String(date.getDate()).padStart(2, '0')
- const hour = String(date.getHours()).padStart(2, '0')
- const minute = String(date.getMinutes()).padStart(2, '0')
- const second = String(date.getSeconds()).padStart(2, '0')
- return format
- .replace('YYYY', year)
- .replace('MM', month)
- .replace('DD', day)
- .replace('HH', hour)
- .replace('mm', minute)
- .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'
- }
- // 将金额转换为数字
- const numAmount = parseFloat(amount)
- // 根据单位转换为元
- const yuan = isCent ? numAmount / 100 : numAmount
- // 保留两位小数
- return yuan.toFixed(2)
- }
- /**
- * 获取请求头
- * @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()
- }
- // 根据admin-web配置,基础API地址为后台服务地址
- // 请根据实际部署情况修改此地址
- const baseApi = 'https://dev-wash-cloud.kuaiyuman.cn/admin' // admin-web的后台服务地址
- 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
- })
- }
|