微信小程序签名到指定位置.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <template>
  2. <view class="h100">
  3. <view style="text-align: center;padding-bottom: 150px;">
  4. <image :src="image" v-if="image" style="width: 200px;height: 200px;border: 1px solid #ccc;"></image>
  5. <image :src="imgs" v-else style="width: 200px;height: 200px;border: 1px solid #ccc;"></image>
  6. <view class="but" style="margin: 0 25px;" @click="toPop">我要在上面签字</view>
  7. <jp-signature-popup ref="signature2" @change="setImg" popup />
  8. <canvas canvas-id="shareCanvas" class="canvas" bindlongpress="saveImg" catchtouchmove="true"
  9. style="position:fixed;left:500%"
  10. :style="{height: canvasHeight+'px',width:canvasWidth+'px'}">
  11. </canvas>
  12. </view>
  13. </view>
  14. </template>
  15. <!-- 有项目需要开发的请联系 扣 - 371524845 -->
  16. <!--
  17. 注意:需要采用线上图片且需要配置白名单,未配置手机无法签名,采用真机调试2.0不配置白名单也可以签名,正式版本需要线上图片且需要配置白名单
  18. -->
  19. <script>
  20. export default {
  21. data() {
  22. return {
  23. canvasHeight: 400,
  24. canvasWidth: 400,
  25. width:80,
  26. height: 50,
  27. left: 20,
  28. top: 20,
  29. ctx:null,
  30. image: '',
  31. imgs: 'http://mmbiz.qpic.cn/sz_mmbiz_jpg/GEWVeJPFkSGTfkSpSbg9cHUqcibBv38r8GXDIVy4W6FN7a1TMWf6RSNQLemKBwG8VqjlxUhicIzz3NTONVrD96ibg/0?wx_fmt=jpeg'
  32. }
  33. },
  34. mounted() {
  35. //初始化画布
  36. this.ctx = wx.createCanvasContext('shareCanvas', this)
  37. },
  38. methods: {
  39. setImg(val) {
  40. if (val) {
  41. this.exportPost(val).then(res => {
  42. this.image = res
  43. })
  44. }
  45. },
  46. toPop() {
  47. this.$refs.signature2.toPop()
  48. },
  49. getImageInfo(src) {
  50. return new Promise((resolve, reject) => {
  51. wx.getImageInfo({
  52. src,
  53. success: (res) => resolve(res),
  54. fail: (res) => reject(res)
  55. })
  56. });
  57. },
  58. exportPost(image2){
  59. let that = this
  60. return new Promise(function (resolve, reject) {
  61. let image = that.imgs
  62. //获取系统的基本信息,为后期的画布和底图适配宽高
  63. uni.getSystemInfo({
  64. success: function (res) {
  65. Promise.all([that.getImageInfo(image),that.getImageInfo(image2)]).then(res=>{
  66. //获取底图和二维码图片的基本信息,通常前端导出的二维码是base64格式的,所以要转成图片格式的才可以获取图片的基本信息
  67. that.ctx.drawImage(res[0].path,0 , 0,that.canvasWidth,that.canvasHeight);
  68. that.ctx.drawImage(res[1].path,that.left,that.top,that.width, that.height);
  69. that.ctx.draw(false,function(){
  70. wx.canvasToTempFilePath({
  71. x: 0,
  72. y: 0,
  73. width:that.canvasWidth,
  74. height:that.canvasHeight,
  75. destWidth:that.canvasWidth*2,//这里乘以2是为了保证合成图片的清晰度
  76. destHeight:that.canvasHeight*2,
  77. canvasId: 'shareCanvas',
  78. fileType:'jpg',//设置导出图片的后缀名
  79. success: function (res) {
  80. resolve(res.tempFilePath)
  81. },
  82. fail: function (res) {
  83. reject(res)
  84. },
  85. })
  86. });
  87. })
  88. }
  89. })
  90. })
  91. },
  92. }
  93. }
  94. </script>
  95. <style lang="scss">
  96. .but {
  97. margin: 25px;
  98. line-height: 40px;
  99. text-align: center;
  100. background-color: #55aaff;
  101. color: #fff;
  102. }
  103. .sssv {
  104. height: 1150px;
  105. }
  106. </style>