index.vue 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <template>
  2. <view style="width: 750rpx ;height: 500rpx;">
  3. <jp-signature ref="signatureRef"></jp-signature>
  4. </view>
  5. <image :src="url" alt="" />
  6. <view>
  7. <button @click="clear">清空</button>
  8. <button @click="undo">撤消</button>
  9. <button @click="save">保存</button>
  10. </view>
  11. </template>
  12. <script>
  13. export default {
  14. data() {
  15. return {
  16. url: '',
  17. }
  18. },
  19. methods: {
  20. save() {
  21. this.$refs.signatureRef.canvasToTempFilePath({
  22. success: (res) => {
  23. // 是否为空画板 无签名
  24. console.log(res.isEmpty)
  25. console.log(res)
  26. this.base64(res.tempFilePath)
  27. }
  28. })
  29. },
  30. base64(tempFilePath) {
  31. uni.getFileSystemManager().readFile({
  32. filePath: tempFilePath,
  33. encoding: 'base64',
  34. success: (res) => {
  35. console.log('res111',res);
  36. let base64 = 'data:image/png;base64,' + res.data
  37. console.log('小程序base64转换成功,长度:', base64.length)
  38. this.url = base64
  39. this.$emit("getBase64",this.url)
  40. },
  41. fail: (err) => {
  42. console.error('小程序转换失败:', err)
  43. // 转换失败时保持使用临时路径
  44. uni.showToast({
  45. title: '转换失败,使用原图',
  46. icon: 'none'
  47. })
  48. }
  49. })
  50. },
  51. clear() {
  52. this.$refs.signatureRef.clear()
  53. },
  54. undo() {
  55. this.$refs.signatureRef.undo()
  56. },
  57. }
  58. }
  59. </script>