signatureComponent.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <template>
  2. <view class="signatureComponent">
  3. <view class="text-position-box">签字区,请签写清晰易辨识的姓名,不可使用草书</view>
  4. <!-- 签名画布 -->
  5. <canvas canvas-id="signCanvas" class="sign-canvas" disable-scroll="true" @touchstart="handleTouchStart"
  6. @touchmove="handleTouchMove" @touchend="handleTouchEnd"></canvas>
  7. <view class="agreement-box">
  8. <view class="agreement-min-box" @click="agreementButton">
  9. <img class="agreement-img" v-if="!agreementType" :src="imagesUrl('commonality/icon_12.png')">
  10. <img class="agreement-img" v-if="agreementType" :src="imagesUrl('commonality/icon_13.png')">
  11. <view>* 本人为具备资质的危废回收人员,且已与报备人核实本次回收的危废种类与称重登记重量,签名即表示确认无误。</view>
  12. </view>
  13. </view>
  14. <!-- 操作按钮 -->
  15. <view class="button-group">
  16. <view @click="clearCanvas">重签</view>
  17. <view @click="saveSignature">完成</view>
  18. </view>
  19. </view>
  20. </template>
  21. <script>
  22. import {
  23. config
  24. } from '@/api/request/config.js'
  25. export default {
  26. data() {
  27. return {
  28. baseUrl: config.base_url,
  29. //签名确认状态
  30. agreementType:false,
  31. ctx: null, // Canvas上下文
  32. points: [], // 存储笔画点
  33. isDrawing: false, // 是否正在绘制
  34. lastPoint: null // 上一个点(用于笔锋计算)
  35. };
  36. },
  37. mounted() {
  38. // 获取Canvas上下文
  39. this.ctx = uni.createCanvasContext('signCanvas', this);
  40. },
  41. methods: {
  42. //条款确认
  43. agreementButton(){
  44. this.$set(this,'agreementType',!this.agreementType);
  45. },
  46. // 1. 触摸开始
  47. handleTouchStart(e) {
  48. const touch = e.touches[0];
  49. this.startDrawing(touch.x, touch.y);
  50. },
  51. startDrawing(x, y) {
  52. this.isDrawing = true;
  53. this.points = [{
  54. X: x,
  55. Y: y,
  56. T: Date.now()
  57. }];
  58. this.ctx.beginPath();
  59. this.ctx.moveTo(x, y);
  60. },
  61. // 2. 触摸移动 (实现笔锋效果的关键)
  62. handleTouchMove(e) {
  63. if (!this.isDrawing) return;
  64. const touch = e.touches[0];
  65. const currentPoint = {
  66. X: touch.x,
  67. Y: touch.y,
  68. T: Date.now()
  69. };
  70. if (this.lastPoint) {
  71. // 计算两点间的速度和距离,动态决定线条粗细[citation:8]
  72. const speed = this.calcSpeed(currentPoint, this.lastPoint);
  73. const lineWidth = this.calcLineWidth(speed);
  74. // 绘制路径
  75. this.ctx.lineWidth = lineWidth;
  76. this.ctx.lineCap = 'round';
  77. this.ctx.lineJoin = 'round';
  78. this.ctx.strokeStyle = '#000000';
  79. this.ctx.moveTo(this.lastPoint.X, this.lastPoint.Y);
  80. this.ctx.lineTo(currentPoint.X, currentPoint.Y);
  81. this.ctx.stroke();
  82. this.ctx.draw(true); // 实时绘制
  83. }
  84. this.lastPoint = currentPoint;
  85. this.points.push(currentPoint);
  86. },
  87. // 计算两点间速度(简化版)
  88. calcSpeed(current, last) {
  89. const distance = Math.sqrt(Math.pow(current.X - last.X, 2) + Math.pow(current.Y - last.Y, 2));
  90. const timeDiff = current.T - last.T;
  91. return timeDiff > 0 ? distance / timeDiff : 0;
  92. },
  93. // 根据速度计算线宽(速度越快,线条越细)
  94. calcLineWidth(speed) {
  95. const MAX_WIDTH = 8;
  96. const MIN_WIDTH = 2;
  97. const MAX_SPEED = 5; // 可调整的阈值
  98. let width = MAX_WIDTH - (speed / MAX_SPEED) * (MAX_WIDTH - MIN_WIDTH);
  99. return Math.max(MIN_WIDTH, Math.min(MAX_WIDTH, width));
  100. },
  101. // 3. 触摸结束
  102. handleTouchEnd() {
  103. this.isDrawing = false;
  104. this.lastPoint = null;
  105. },
  106. // 4. 清空画布
  107. clearCanvas() {
  108. this.ctx.clearRect(0, 0, 750, 400); // 尺寸需与canvas实际宽高匹配
  109. this.ctx.draw(true);
  110. this.points = [];
  111. },
  112. // 5. 保存为图片
  113. saveSignature() {
  114. let self = this;
  115. if(!this.agreementType){
  116. uni.showToast({
  117. title: "请确认相关条款",
  118. icon: "none",
  119. mask: true,
  120. duration: 2000
  121. });
  122. return
  123. }
  124. uni.canvasToTempFilePath({
  125. canvasId: 'signCanvas',
  126. success: (res) => {
  127. const tempFilePath = res.tempFilePath; // 小程序/APP端为临时路径
  128. // H5端可能需要特殊处理,部分平台返回base64[citation:1]
  129. uni.showToast({
  130. title: '保存成功'
  131. });
  132. console.log('tempFilePath',tempFilePath);
  133. self.uploadImg(tempFilePath);
  134. // 这里可以将 tempFilePath 上传至服务器或展示预览
  135. },
  136. fail: (err) => {
  137. console.error('保存失败', err);
  138. }
  139. }, this);
  140. },
  141. async uploadImg(tempFilePaths) {
  142. var self = this;
  143. uni.showLoading({
  144. title: '上传中',
  145. mask: true
  146. });
  147. uni.uploadFile({
  148. url: config.base_url + '/system/file/upload', //仅为示例,非真实的接口地址
  149. header: {
  150. 'Authorization': uni.getStorageSync('token')
  151. },
  152. filePath: tempFilePaths,
  153. name: 'file',
  154. formData: {
  155. 'user': 'test'
  156. },
  157. success: (uploadFileRes) => {
  158. let res = JSON.parse(uploadFileRes.data);
  159. if (res.code == 200) {
  160. } else {
  161. uni.showToast({
  162. title: res.msg,
  163. icon: "none",
  164. mask: true,
  165. duration: 2000
  166. });
  167. }
  168. },
  169. fail: err => {},
  170. complete: () => {
  171. uni.hideLoading()
  172. }
  173. });
  174. },
  175. }
  176. }
  177. </script>
  178. <style lang="stylus" scoped>
  179. .signatureComponent {
  180. height: 100%;
  181. display flex;
  182. flex: 1;
  183. flex-direction column;
  184. overflow: hidden;
  185. position: relative;
  186. .text-position-box{
  187. width:650rpx;
  188. height:200rpx;
  189. line-height:200rpx;
  190. text-align: center;
  191. font-size:14rpx;
  192. color:#dedede;
  193. margin:0 50rpx;
  194. z-index:1;
  195. margin-top:10rpx;
  196. background-color: #fff;
  197. border:1rpx solid #dedede;
  198. }
  199. .sign-canvas{
  200. position: absolute
  201. top:10rpx;
  202. left:0;
  203. width:650rpx;
  204. height:200rpx;
  205. line-height:200rpx;
  206. text-align: center;
  207. font-size:14rpx;
  208. color:#dedede;
  209. margin:0 50rpx;
  210. z-index:10;
  211. }
  212. .agreement-box{
  213. flex:1;
  214. margin:0 60rpx;
  215. .agreement-min-box{
  216. display: flex;
  217. margin-top:15rpx;
  218. .agreement-img{
  219. width:20rpx;
  220. height:20rpx;
  221. margin:0 10rpx 0 0;
  222. }
  223. view{
  224. line-height:20rpx;
  225. font-size:12rpx;
  226. }
  227. }
  228. }
  229. .button-group{
  230. display: flex;
  231. margin:0 auto;
  232. margin-bottom:20rpx;
  233. view{
  234. font-size:14rpx;
  235. width:100rpx;
  236. height:30rpx;
  237. line-height:30rpx;
  238. text-align: center;
  239. background-color: #fff
  240. border-radius:6rpx;
  241. }
  242. view:nth-child(1){
  243. margin-right:40rpx;
  244. border:1px solid #999;
  245. background-color: #fff;
  246. color:#666;
  247. }
  248. view:nth-child(2){
  249. border:1px solid #0183FA;
  250. background-color: #0183FA;
  251. color:#fff;
  252. }
  253. }
  254. }
  255. </style>