This commit is contained in:
Zy
2026-10-14 09:28:49 +08:00
parent ace7d969ac
commit 27d9fddccc
18 changed files with 4164 additions and 3890 deletions

View File

@@ -1,5 +1,9 @@
// 服务器地址(与 request.js 保持一致)
const baseUrl = 'http://192.168.0.239:8080';
// const baseUrl = 'http://192.168.0.239:8080';
// const baseUrl = 'http://192.168.1.222:8080';
import {
baseUrl
} from './request.js';
/**
* 图片上传 Hook
@@ -7,7 +11,9 @@ const baseUrl = 'http://192.168.0.239:8080';
* 解决编辑回显后重新上传的问题
*/
import { reactive } from 'vue'
import {
reactive
} from 'vue'
/**
* @param {Object} options 配置
@@ -18,7 +24,7 @@ import { reactive } from 'vue'
export function useImageUpload(options = {}) {
const {
uploadUrl = '/api/resident/file/uploadImage',
fileKey = 'file'
fileKey = 'file'
} = options
// 使用 reactive 对象存储所有状态
@@ -87,12 +93,16 @@ export function useImageUpload(options = {}) {
const uploadImage = () => {
// 如果没有新选择临时文件,直接返回已有的服务器URL
if (!state.tempFilePath) {
return Promise.resolve({ url: state.serverUrl })
return Promise.resolve({
url: state.serverUrl
})
}
// 如果已经有服务器URL且没有重新选择,也直接返回
if (state.serverUrl && !state.tempFilePath) {
return Promise.resolve({ url: state.serverUrl })
return Promise.resolve({
url: state.serverUrl
})
}
state.uploading = true
@@ -118,11 +128,14 @@ export function useImageUpload(options = {}) {
const data = JSON.parse(res.data)
if (data.code === 200 && data.data) {
// 更新服务器URL(确保存的是字符串)
const url = typeof data.data === 'string' ? data.data : (data.data?.url || '')
const url = typeof data.data === 'string' ? data.data : (data.data
?.url || '')
state.serverUrl = url
// 清空临时文件
state.tempFilePath = null
resolve({ url })
resolve({
url
})
} else {
uni.showToast({
title: data.msg || '上传失败',
@@ -164,7 +177,9 @@ export function useImageUpload(options = {}) {
return await uploadImage()
}
// 否则返回已有的服务器URL
return { url: state.serverUrl }
return {
url: state.serverUrl
}
}
/**
@@ -187,4 +202,4 @@ export function useImageUpload(options = {}) {
getSubmitUrl,
reset
}
}
}