index.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <template>
  2. <view style="width: 750rpx ;height: 500rpx;">
  3. <jp-signature ref="signatureRef"></jp-signature>
  4. </view>
  5. <image v-show="false" :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. // H5 生成的是base64
  29. // this.url = res.tempFilePath
  30. }
  31. })
  32. },
  33. // 转换 app
  34. base64(tempFilePath) {
  35. let base64Code;
  36. let path = plus.io.convertLocalFileSystemURL(tempFilePath);
  37. console.log('tempFilePath',tempFilePath);
  38. console.log('path',path);
  39. let fileReader = new plus.io.FileReader();
  40. fileReader.readAsDataURL(path);
  41. fileReader.onloadend = res => {
  42. console.log('res',res);
  43. base64Code = res.target.result;
  44. this.url = res.target.result
  45. this.$emit('getBase64',this.url)
  46. console.log('base64Code',base64Code);
  47. }
  48. },
  49. // miniProgramConvertToBase64(tempFilePath) {
  50. // uni.getFileSystemManager().readFile({
  51. // filePath: tempFilePath,
  52. // encoding: 'base64',
  53. // success: (res) => {
  54. // let base64 = 'data:image/png;base64,' + res.data
  55. // console.log('小程序base64转换成功,长度:', base64.length)
  56. // this.url = base64
  57. // },
  58. // fail: (err) => {
  59. // console.error('小程序转换失败:', err)
  60. // // 转换失败时保持使用临时路径
  61. // uni.showToast({
  62. // title: '转换失败,使用原图',
  63. // icon: 'none'
  64. // })
  65. // }
  66. // })
  67. // },
  68. clear() {
  69. this.$refs.signatureRef.clear()
  70. },
  71. undo() {
  72. this.$refs.signatureRef.undo()
  73. },
  74. }
  75. }
  76. </script>