paint.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <template>
  2. <scroll-view scroll-y="false">
  3. <cu-custom bgColor="bg-blue" :isBack="true">
  4. <block slot="backText">手写签名</block>
  5. <block slot="content"></block>
  6. <block slot="right">
  7. <view class=" flex justify-center round" >
  8. <button class="margin-top-ssm cu-btn round line-blue text-white">
  9. <button @click="clearClick()" class="cu-btn line-blue "> <text class="text-white">清除</text></button>
  10. <text class="cuIcon-titles"></text>
  11. <button @click="finish()" class="cu-btn line-blue"> <text class="text-white">保存</text></button>
  12. </button>
  13. </view>
  14. </block>
  15. </cu-custom>
  16. <canvas class="margin-top-sm" style="border: 1px solid #39B54A;height: 1150rpx;width: 98%;" canvas-id="mycanvas" @touchmove='move' @touchstart='start($event)' @touchend='end'
  17. @touchcancel='cancel' @longtap='tap' disable-scroll='true' @error='error'>
  18. </canvas>
  19. <view class="flex justify-center margin-top-sm">
  20. <text>请在绿色区域中进行签名</text>
  21. </view>
  22. </scroll-view>
  23. </template>
  24. <script>
  25. import config from "../../../../core/api.js"
  26. export default {
  27. data() {
  28. return {
  29. qmimgList:[],
  30. ctx:'', //绘图图像
  31. points:[] ,//路径点集合
  32. userInfo:"",
  33. };
  34. },
  35. onLoad() {
  36. this.paint();
  37. var userInfo = uni.getStorageSync("userInfo");
  38. this.userInfo=userInfo;
  39. },
  40. methods: {
  41. paint(){
  42. this.modalName="Image"
  43. this.ctx= uni.createCanvasContext('mycanvas',this)
  44. //设置画笔样式
  45. this.ctx.setLineWidth(2);
  46. this.ctx.setStrokeStyle("#DD001B") //字体颜色
  47. this.ctx.setLineCap("round");
  48. this.ctx.setLineJoin("round");
  49. },
  50. // 画布的触摸移动开始手势响应
  51. start: function(event) {
  52. //获取触摸开始的 x,y
  53. let point = {
  54. x: event.changedTouches[0].x,
  55. y: event.changedTouches[0].y
  56. }
  57. this.points.push(point);
  58. },
  59. // 画布的触摸移动手势响应
  60. move: function(e) {
  61. let point = {
  62. x: e.touches[0].x,
  63. y: e.touches[0].y
  64. }
  65. // console.log(point)
  66. this.points.push(point)
  67. if (this.points.length >= 2) {
  68. this.draw(this.points)
  69. }
  70. },
  71. // 画布的触摸移动结束手势响应
  72. end: function(e) {
  73. // 清空轨迹数组
  74. for (let i = 0; i < this.points.length; i++) {
  75. this.points.pop()
  76. }
  77. },
  78. // 画布的触摸取消响应
  79. cancel: function(e) {
  80. console.log("触摸取消" + e)
  81. },
  82. // 画布的长按手势响应
  83. tap: function(e) {
  84. console.log("长按手势" + e)
  85. },
  86. error: function(e) {
  87. console.log("画布触摸错误" + e)
  88. },
  89. //绘制
  90. draw: function() {
  91. let point1 = this.points[0]
  92. let point2 = this.points[1]
  93. this.points.shift()
  94. this.ctx.moveTo(point1.x, point1.y)
  95. this.ctx.lineTo(point2.x, point2.y)
  96. this.ctx.stroke()
  97. this.ctx.draw(true)
  98. },
  99. //清除操作
  100. clearClick: function() {
  101. let that = this;
  102. uni.getSystemInfo({
  103. success: function(res) {
  104. let canvasw = res.windowWidth;
  105. let canvash = res.windowHeight;
  106. that.ctx.clearRect(0, 0, canvasw, canvash);
  107. that.ctx.draw(true);
  108. },
  109. })
  110. },
  111. //完成绘画并保存到本地
  112. finish:function(){
  113. var that =this;
  114. uni.canvasToTempFilePath({
  115. canvasId: 'mycanvas',
  116. success: function(res) {
  117. let base = res.tempFilePath;
  118. that.qmimgList=[];
  119. that.qmimgList.push(base);
  120. that.modalName="";
  121. /* that.clearClick(); */
  122. that.save();
  123. }
  124. })
  125. },
  126. save(){
  127. var url = config.api;
  128. var that =this;
  129. var token =uni.getStorageSync("token");
  130. uni.uploadFile({
  131. url: url+'/app/updateSignatureByBase',
  132. filePath:that.qmimgList[0],
  133. name: 'file',
  134. header:{"Authorization": token},
  135. formData:{
  136. "userId":that.userInfo.id,
  137. },
  138. success: function (uploadFileRes) {
  139. var data = JSON.parse(uploadFileRes.data);
  140. if(data.result=="1"){
  141. uni.setStorageSync("userInfo",data.data);
  142. that.qmimgList=[];
  143. that.qmimgList.push(data.data.signature);
  144. that.userInfo.signature =data.data.signature ;
  145. that.$prompt.none("更新成功");
  146. }else{
  147. that.$prompt.none("更新失败");
  148. }
  149. }
  150. });
  151. }
  152. }
  153. }
  154. </script>
  155. <style>
  156. .cu-form-group .title {
  157. min-width: calc(4em + 15px);
  158. }
  159. .cont{
  160. display: flex;
  161. flex-direction: row;
  162. align-items: center;
  163. justify-content: flex-end;
  164. }
  165. </style>