This commit is contained in:
qiuyuan 2025-08-04 15:57:13 +08:00
commit 11e2900c47
2 changed files with 118 additions and 27 deletions

View File

@ -96,6 +96,7 @@
wx.canvasToTempFilePath({ wx.canvasToTempFilePath({
canvasId: 'signatureCanvas', canvasId: 'signatureCanvas',
success: (res) => { success: (res) => {
console.log(res.tempFilePath)
this.setData({ this.setData({
signatureImage: res.tempFilePath signatureImage: res.tempFilePath
}); });

View File

@ -34,6 +34,7 @@
<script> <script>
import pickerColor from "./pickerColor.vue" import pickerColor from "./pickerColor.vue"
import {IMAGE_BASE_URL,BASE_URL} from '@/utils/config';
export default { export default {
components: { components: {
pickerColor pickerColor
@ -394,34 +395,123 @@
this.ctx.setStrokeStyle(this.lineColor) this.ctx.setStrokeStyle(this.lineColor)
}, },
// //
//
subCanvas() { subCanvas() {
if (this.isEmpty()) { if (this.isEmpty()) {
uni.showToast({ uni.showToast({
title: '没有任何绘制内容哦', title: '没有任何绘制内容哦',
icon: 'none', icon: 'none',
}); });
return return;
} }
uni.canvasToTempFilePath({
canvasId: 'handWriting', uni.showLoading({
fileType: 'png', title: '正在生成图片...'
quality: 1, // });
success(res) {
uni.showToast({ uni.canvasToTempFilePath({
title: '以保存' canvasId: 'handWriting',
}); fileType: 'png',
// quality: 1,
uni.saveImageToPhotosAlbum({ success: async (res) => {
filePath: res.tempFilePath, uni.hideLoading();
success(res) {
uni.showToast({ // 1.
title: '已成功保存到相册', try {
duration: 2000 await this.saveToAlbum(res.tempFilePath);
});
} // 2.
}); uni.showLoading({
} title: '正在上传签名...'
}); });
const uploadRes = await this.uploadSignature(res.tempFilePath);
uni.hideLoading();
uni.showToast({
title: '签名上传成功',
icon: 'success'
});
// 3.
// this.$emit('upload-success', {
// tempFilePath: res.tempFilePath,
// serverPath: uploadRes //
// });
} catch (error) {
uni.hideLoading();
uni.showToast({
title: error.message || '上传失败',
icon: 'none'
});
}
},
fail: (err) => {
uni.hideLoading();
uni.showToast({
title: '生成图片失败',
icon: 'none'
});
console.error(err);
}
});
},
//
saveToAlbum(tempFilePath) {
return new Promise((resolve, reject) => {
uni.saveImageToPhotosAlbum({
filePath: tempFilePath,
success: () => {
uni.showToast({
title: '已保存到相册',
duration: 2000
});
resolve();
},
fail: (err) => {
reject(new Error('保存到相册失败'));
console.error(err);
}
});
});
},
//
uploadSignature(tempFilePath) {
return new Promise((resolve, reject) => {
//
const uploadUrl = IMAGE_BASE_URL+'/api/v1/upload';
uni.uploadFile({
url: uploadUrl,
filePath: tempFilePath,
name: 'file',
// formData: {
// //
// userId: '123', // ID
// type: 'signature'
// },
success: (uploadRes) => {
console.log(uploadRes);
console.log(JSON.parse(uploadRes.data))
try {
const {success,data} = JSON.parse(uploadRes.data);
if (success) {
resolve(data); // URL
} else {
reject(new Error(success || '上传失败'));
}
} catch (e) {
reject(new Error('解析服务器响应失败'));
}
},
fail: (err) => {
reject(new Error('上传请求失败'));
console.error(err);
}
});
});
}, },
// //
saveCanvasAsImg() { saveCanvasAsImg() {