dev
This commit is contained in:
@@ -91,6 +91,7 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted } from 'vue';
|
||||
import { createMerchantRoom, updateMerchantRoom, getMerchantRoom } from '@/api/room';
|
||||
import { chooseAndUpload } from '@/utils/upload';
|
||||
|
||||
const loading = ref(false);
|
||||
const roomId = ref<number | null>(null);
|
||||
@@ -178,16 +179,21 @@ function toggleFacility(f: string) {
|
||||
}
|
||||
}
|
||||
|
||||
function chooseImage() {
|
||||
uni.chooseImage({
|
||||
count: 20 - imageList.value.length,
|
||||
sizeType: ['compressed'],
|
||||
sourceType: ['album', 'camera'],
|
||||
success: (res) => {
|
||||
// 暂用本地路径,实际需上传到服务器
|
||||
imageList.value.push(...res.tempFilePaths);
|
||||
},
|
||||
});
|
||||
async function chooseImage() {
|
||||
const remaining = 20 - imageList.value.length;
|
||||
if (remaining <= 0) {
|
||||
uni.showToast({ title: '最多上传20张图片', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
try {
|
||||
uni.showLoading({ title: '上传中...' });
|
||||
const urls = await chooseAndUpload({ count: remaining, useSellerToken: true });
|
||||
imageList.value.push(...urls);
|
||||
} catch (err: any) {
|
||||
uni.showToast({ title: err.message || '上传失败', icon: 'none' });
|
||||
} finally {
|
||||
uni.hideLoading();
|
||||
}
|
||||
}
|
||||
|
||||
function removeImage(index: number) {
|
||||
|
||||
@@ -117,6 +117,7 @@ import { ref } from 'vue';
|
||||
import { applyMerchant } from '@/api/merchant';
|
||||
import { useSellerStore } from '@/store/seller';
|
||||
import RegionSelector from '@/components/RegionSelector.vue';
|
||||
import { chooseAndUpload } from '@/utils/upload';
|
||||
|
||||
const sellerStore = useSellerStore();
|
||||
|
||||
@@ -145,32 +146,17 @@ function deleteLicense() {
|
||||
form.value.businessLicense = '';
|
||||
}
|
||||
|
||||
function uploadLicense() {
|
||||
uni.chooseImage({
|
||||
count: 1,
|
||||
sizeType: ['compressed'],
|
||||
sourceType: ['album', 'camera'],
|
||||
success: (res) => {
|
||||
const tempFilePath = res.tempFilePaths[0];
|
||||
// 模拟上传,实际需调用后端上传接口
|
||||
// 这里暂时用本地路径模拟,实际项目需要上传到服务器获取URL
|
||||
uploadImageToServer(tempFilePath);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function uploadImageToServer(filePath: string) {
|
||||
uni.showLoading({ title: '上传中...' });
|
||||
|
||||
// 实际项目中需要调用后端上传接口
|
||||
// 这里模拟上传成功,使用临时路径作为演示
|
||||
// TODO: 实现真实的文件上传接口
|
||||
setTimeout(() => {
|
||||
uni.hideLoading();
|
||||
// 模拟上传成功后的URL
|
||||
form.value.businessLicense = filePath;
|
||||
async function uploadLicense() {
|
||||
try {
|
||||
uni.showLoading({ title: '上传中...' });
|
||||
const urls = await chooseAndUpload({ count: 1, useSellerToken: true });
|
||||
form.value.businessLicense = urls[0];
|
||||
uni.showToast({ title: '上传成功', icon: 'success' });
|
||||
}, 1000);
|
||||
} catch (err: any) {
|
||||
uni.showToast({ title: err.message || '上传失败', icon: 'none' });
|
||||
} finally {
|
||||
uni.hideLoading();
|
||||
}
|
||||
}
|
||||
|
||||
function validateForm(): boolean {
|
||||
|
||||
@@ -116,6 +116,7 @@ import { ref, onMounted } from 'vue';
|
||||
import { updateMerchant, getMyMerchant } from '@/api/merchant';
|
||||
import { useSellerStore } from '@/store/seller';
|
||||
import RegionSelector from '@/components/RegionSelector.vue';
|
||||
import { chooseAndUpload } from '@/utils/upload';
|
||||
|
||||
const sellerStore = useSellerStore();
|
||||
|
||||
@@ -166,26 +167,17 @@ function deleteLicense() {
|
||||
form.value.businessLicense = '';
|
||||
}
|
||||
|
||||
function uploadLicense() {
|
||||
uni.chooseImage({
|
||||
count: 1,
|
||||
sizeType: ['compressed'],
|
||||
sourceType: ['album', 'camera'],
|
||||
success: (res) => {
|
||||
const tempFilePath = res.tempFilePaths[0];
|
||||
uploadImageToServer(tempFilePath);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function uploadImageToServer(filePath: string) {
|
||||
uni.showLoading({ title: '上传中...' });
|
||||
// TODO: 实现真实的文件上传接口
|
||||
setTimeout(() => {
|
||||
uni.hideLoading();
|
||||
form.value.businessLicense = filePath;
|
||||
async function uploadLicense() {
|
||||
try {
|
||||
uni.showLoading({ title: '上传中...' });
|
||||
const urls = await chooseAndUpload({ count: 1, useSellerToken: true });
|
||||
form.value.businessLicense = urls[0];
|
||||
uni.showToast({ title: '上传成功', icon: 'success' });
|
||||
}, 1000);
|
||||
} catch (err: any) {
|
||||
uni.showToast({ title: err.message || '上传失败', icon: 'none' });
|
||||
} finally {
|
||||
uni.hideLoading();
|
||||
}
|
||||
}
|
||||
|
||||
function validateForm(): boolean {
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
const BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:3000/api';
|
||||
|
||||
export interface UploadResult {
|
||||
url: string;
|
||||
filename: string;
|
||||
originalname: string;
|
||||
size: number;
|
||||
}
|
||||
|
||||
export function uploadFile(filePath: string, options?: { useSellerToken?: boolean }): Promise<string> {
|
||||
const token = options?.useSellerToken
|
||||
? uni.getStorageSync('sellerToken') || ''
|
||||
: uni.getStorageSync('token') || '';
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.uploadFile({
|
||||
url: `${BASE_URL}/${options?.useSellerToken ? 'seller/' : ''}upload`,
|
||||
filePath,
|
||||
name: 'file',
|
||||
header: token ? { Authorization: `Bearer ${token}` } : {},
|
||||
success: (res) => {
|
||||
if (res.statusCode === 200) {
|
||||
try {
|
||||
const data = JSON.parse(res.data);
|
||||
if (data.code >= 200 && data.code < 300 && data.data?.url) {
|
||||
resolve(data.data.url);
|
||||
} else {
|
||||
reject(new Error(data.message || '上传失败'));
|
||||
}
|
||||
} catch {
|
||||
reject(new Error('解析响应失败'));
|
||||
}
|
||||
} else if (res.statusCode === 401) {
|
||||
reject(new Error('登录已过期'));
|
||||
} else {
|
||||
reject(new Error(`上传失败 (${res.statusCode})`));
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
reject(new Error(err.errMsg || '网络异常'));
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function chooseAndUpload(options?: {
|
||||
count?: number;
|
||||
useSellerToken?: boolean;
|
||||
}): Promise<string[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.chooseImage({
|
||||
count: options?.count || 1,
|
||||
sizeType: ['compressed'],
|
||||
sourceType: ['album', 'camera'],
|
||||
success: async (res) => {
|
||||
const urls: string[] = [];
|
||||
try {
|
||||
for (const path of res.tempFilePaths) {
|
||||
const url = await uploadFile(path, { useSellerToken: options?.useSellerToken });
|
||||
urls.push(url);
|
||||
}
|
||||
resolve(urls);
|
||||
} catch (err: any) {
|
||||
reject(err);
|
||||
}
|
||||
},
|
||||
fail: () => reject(new Error('取消选择图片')),
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user