This commit is contained in:
aaron 2025-02-23 17:32:41 +08:00
parent f0426f1f5a
commit 90f83588fd
3 changed files with 154 additions and 36 deletions

View File

@ -52,4 +52,13 @@ export function updateCommunity(id, data) {
method: 'put',
data
})
}
}
// 上传图片
export function uploadImage(data) {
return request({
url: '/api/upload/image',
method: 'post',
data
})
}

View File

@ -1,12 +1,12 @@
const env = {
development: {
API_URL: 'http://localhost:8080'
API_URL: 'http://localhost:8000'
},
testing: {
API_URL: process.env.VUE_APP_API_URL || 'https://api-dev.beefast.co'
},
production: {
API_URL: process.env.VUE_APP_API_URL || 'http://api.example.com'
API_URL: process.env.VUE_APP_API_URL || 'http://api.beefast.co'
}
}

View File

@ -52,6 +52,7 @@
</template>
</a-table>
<a-modal
v-model:visible="mapVisible"
title="小区位置"
@ -97,19 +98,33 @@
/>
<a-form-item label="群二维码" name="qy_group_qrcode">
<a-upload
v-model:file-list="fileList"
name="file"
:action="uploadUrl"
list-type="picture-card"
:max-count="1"
@change="handleQrcodeChange"
>
<div v-if="!fileList.length">
<plus-outlined />
<div style="margin-top: 8px">上传</div>
</div>
</a-upload>
<div class="qrcode-upload-wrapper">
<a-upload
v-model:file-list="fileList"
name="file"
:customRequest="handleQRCodeUpload"
@remove="handleQRCodeRemove"
list-type="picture-card"
accept=".jpg,.jpeg,.png"
:max-count="1"
@preview="previewQRCode"
>
<div v-if="!fileList.length">
<plus-outlined />
<div style="margin-top: 8px">上传</div>
</div>
</a-upload>
<!-- 添加预览模态框 -->
<a-modal
v-model:visible="previewVisible"
:title="previewTitle"
:footer="null"
@cancel="handlePreviewCancel"
>
<img style="width: 100%" :src="previewImage" />
</a-modal>
</div>
</a-form-item>
</a-form>
</a-modal>
@ -120,13 +135,13 @@
<script>
import { defineComponent, ref, onMounted, nextTick } from 'vue'
import { message, Tag, Menu, Dropdown, Image, Upload } from 'ant-design-vue'
import { getCommunityList, createCommunity, updateCommunityStatus, updateCommunity } from '@/api/community'
import { getCommunityList, createCommunity, updateCommunityStatus, updateCommunity, uploadImage } from '@/api/community'
import { loadAMap, createMap } from '@/utils/amap.js'
import dayjs from 'dayjs'
import PageContainer from '@/components/PageContainer.vue'
import { DownOutlined, PlusOutlined } from '@ant-design/icons-vue'
import MapPicker from '@/components/MapPicker/index.vue'
import request from '../../utils/request'
export default defineComponent({
components: {
PageContainer,
@ -325,7 +340,9 @@ export default defineComponent({
//
const fileList = ref([])
const uploadUrl = '/api/upload/image'
const previewVisible = ref(false)
const previewImage = ref('')
const previewTitle = ref('')
//
const handleEdit = (record) => {
@ -376,6 +393,85 @@ export default defineComponent({
fileList.value = []
}
//
const previewQRCode = async (file) => {
// 使 url
if (file.url) {
previewImage.value = file.url
previewVisible.value = true
previewTitle.value = file.name || '群二维码预览'
return
}
// File
if (file.originFileObj) {
try {
const preview = await getBase64(file.originFileObj)
previewImage.value = preview
previewVisible.value = true
previewTitle.value = file.name || '群二维码预览'
} catch (error) {
console.error('预览图片失败:', error)
message.error('预览失败')
}
}
}
//
const handlePreviewCancel = () => {
previewVisible.value = false
}
// base64
const getBase64 = (file) => {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.readAsDataURL(file)
reader.onload = () => resolve(reader.result)
reader.onerror = error => reject(error)
})
}
//
const handleQRCodeUpload = async ({ file, onSuccess, onError }) => {
const formData = new FormData()
formData.append('file', file)
try {
const res = await request.post('/api/upload/image', formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
})
if (res.code === 200) {
formState.value.qy_group_qrcode = res.data.url
//
fileList.value = [{
uid: file.uid,
name: file.name,
status: 'done',
url: res.data.url
}]
message.success('上传成功')
onSuccess(res)
} else {
message.error(res.message || '上传失败')
onError(new Error(res.message || '上传失败'))
}
} catch (error) {
console.error('上传图片失败:', error)
message.error('上传失败')
onError(error)
}
}
//
const handleQRCodeRemove = (file) => {
formState.value.qy_group_qrcode = ''
fileList.value = []
}
//
const handleSubmit = () => {
formRef.value.validate().then(async () => {
@ -440,20 +536,6 @@ export default defineComponent({
}
}
//
const handleQrcodeChange = (info) => {
if (info.file.status === 'done') {
if (info.file.response.code === 200) {
formState.value.qy_group_qrcode = info.file.response.data.url
message.success('上传成功')
} else {
message.error(info.file.response.message || '上传失败')
}
} else if (info.file.status === 'error') {
message.error('上传失败')
}
}
onMounted(() => {
fetchData()
})
@ -481,9 +563,14 @@ export default defineComponent({
handleCancel,
handleStatusChange,
fileList,
uploadUrl,
handleQrcodeChange,
handleEdit
handleQRCodeUpload,
handleQRCodeRemove,
handleEdit,
previewVisible,
previewImage,
previewTitle,
handlePreviewCancel,
previewQRCode
}
}
})
@ -797,4 +884,26 @@ export default defineComponent({
width: 104px;
height: 104px;
}
.qrcode-upload-wrapper {
:deep(.ant-upload-list-picture-card-container) {
width: 104px;
height: 104px;
}
:deep(.ant-upload.ant-upload-select-picture-card) {
width: 104px;
height: 104px;
}
:deep(.ant-upload-list-picture-card .ant-upload-list-item) {
padding: 4px;
}
:deep(.ant-upload-list-picture-card .ant-upload-list-item-thumbnail) {
width: 100%;
height: 100%;
object-fit: cover;
}
}
</style>