generated from duanshuwen/webapp-vue-frontend
feat: 上传图片和保存图片
This commit is contained in:
@@ -59,7 +59,7 @@ import AgreementTip from '../components/AgreementTip.vue';
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import { getAigcSceneList, AigcSceneListItem, AigcSceneStyleItem } from '@api';
|
import { getAigcSceneList, AigcSceneListItem, AigcSceneStyleItem } from '@api';
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import { createGeneratorPhotoTask, updateImageFile } from '@api';
|
import { createGeneratorPhotoTask } from '@api';
|
||||||
import { showFailToast, showSuccessToast } from 'vant';
|
import { showFailToast, showSuccessToast } from 'vant';
|
||||||
|
|
||||||
// --- 测试数据 ---
|
// --- 测试数据 ---
|
||||||
@@ -115,27 +115,32 @@ const onStartSelect = () => {
|
|||||||
|
|
||||||
// 调用上传
|
// 调用上传
|
||||||
const uploadImage = () => {
|
const uploadImage = () => {
|
||||||
wx.miniProgram.postMessage({
|
// 测试保存图片逻辑
|
||||||
data: { action: 'chooseAndUpload' }
|
// if (window.wx && wx.miniProgram) {
|
||||||
});
|
// wx.miniProgram.navigateTo({
|
||||||
|
// url: `/pages-bridge/SaveImage?imageUrl=${encodeURIComponent(activeScene.value.sceneCoverUrl)}`
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
// return
|
||||||
|
|
||||||
|
/// 调用小程序上传接口
|
||||||
|
if (window.wx && wx.miniProgram) {
|
||||||
|
wx.miniProgram.navigateTo({
|
||||||
|
url: '/pages-bridge/UploadImage'
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 监听 hash 变化,获取结果
|
window.onUploadResult = function(imageUrl) {
|
||||||
window.addEventListener('hashchange', function() {
|
console.log('上传结果:', imageUrl);
|
||||||
const hash = window.location.hash;
|
|
||||||
if (hash.startsWith('#uploadResult=')) {
|
if (!imageUrl || imageUrl === 'error') {
|
||||||
const imageUrl = decodeURIComponent(hash.substring('#uploadResult='.length));
|
showFailToast('获取图片地址失败,请重试。');
|
||||||
console.log('上传成功,图片地址:', imageUrl);
|
return;
|
||||||
// 这里就可以使用 imageUrl 了,比如赋值给某个变量或调用业务逻辑
|
|
||||||
if (!imageUrl || imageUrl === 'error') {
|
|
||||||
showFailToast('获取图片地址失败,请重试。');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
sendGeneratorPhotoTask(imageUrl);
|
|
||||||
// 可选:清除 hash,避免重复触发
|
|
||||||
history.replaceState(null, null, window.location.pathname + window.location.search);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
sendGeneratorPhotoTask(imageUrl);
|
||||||
|
};
|
||||||
|
|
||||||
/// 发送生成任务
|
/// 发送生成任务
|
||||||
const sendGeneratorPhotoTask = async (photoUrl: string) => {
|
const sendGeneratorPhotoTask = async (photoUrl: string) => {
|
||||||
|
|||||||
@@ -79,13 +79,11 @@ const saveImage = () => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 发送消息给小程序
|
if (window.wx && wx.miniProgram) {
|
||||||
wx.miniProgram.postMessage({
|
wx.miniProgram.navigateTo({
|
||||||
data: {
|
url: `/pages-bridge/SaveImage?imageUrl=${encodeURIComponent(imageUrl.value)}`
|
||||||
action: 'saveImage',
|
});
|
||||||
imageUrl: imageUrl
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user