signatureComponent.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. hwmsAppRegisterOrderAdd,
  24. } from '@/pages_hazardousWasteRecycling/api/index.js'
  25. import {
  26. config
  27. } from '@/api/request/config.js'
  28. export default {
  29. data() {
  30. return {
  31. newData:null,
  32. baseUrl: config.base_url,
  33. //签名确认状态
  34. agreementType:false,
  35. ctx: null, // Canvas上下文
  36. points: [], // 存储笔画点
  37. isDrawing: false, // 是否正在绘制
  38. lastPoint: null, // 上一个点(用于笔锋计算)
  39. signatureType:false,
  40. };
  41. },
  42. onLoad(option) {
  43. this.$set(this,'newData',JSON.parse(decodeURIComponent(option.data)));
  44. },
  45. mounted() {
  46. // 获取Canvas上下文
  47. this.ctx = uni.createCanvasContext('signCanvas', this);
  48. },
  49. methods: {
  50. //条款确认
  51. agreementButton(){
  52. this.$set(this,'agreementType',!this.agreementType);
  53. },
  54. // 1. 触摸开始
  55. handleTouchStart(e) {
  56. this.$set(this,'signatureType',true);
  57. const touch = e.touches[0];
  58. this.startDrawing(touch.x, touch.y);
  59. },
  60. startDrawing(x, y) {
  61. this.isDrawing = true;
  62. this.points = [{
  63. X: x,
  64. Y: y,
  65. T: Date.now()
  66. }];
  67. this.ctx.beginPath();
  68. this.ctx.moveTo(x, y);
  69. },
  70. // 2. 触摸移动 (实现笔锋效果的关键)
  71. handleTouchMove(e) {
  72. if (!this.isDrawing) return;
  73. const touch = e.touches[0];
  74. const currentPoint = {
  75. X: touch.x,
  76. Y: touch.y,
  77. T: Date.now()
  78. };
  79. if (this.lastPoint) {
  80. // 计算两点间的速度和距离,动态决定线条粗细[citation:8]
  81. const speed = this.calcSpeed(currentPoint, this.lastPoint);
  82. const lineWidth = this.calcLineWidth(speed);
  83. // 绘制路径
  84. this.ctx.lineWidth = lineWidth;
  85. this.ctx.lineCap = 'round';
  86. this.ctx.lineJoin = 'round';
  87. this.ctx.strokeStyle = '#000000';
  88. this.ctx.moveTo(this.lastPoint.X, this.lastPoint.Y);
  89. this.ctx.lineTo(currentPoint.X, currentPoint.Y);
  90. this.ctx.stroke();
  91. this.ctx.draw(true); // 实时绘制
  92. }
  93. this.lastPoint = currentPoint;
  94. this.points.push(currentPoint);
  95. },
  96. // 计算两点间速度(简化版)
  97. calcSpeed(current, last) {
  98. const distance = Math.sqrt(Math.pow(current.X - last.X, 2) + Math.pow(current.Y - last.Y, 2));
  99. const timeDiff = current.T - last.T;
  100. return timeDiff > 0 ? distance / timeDiff : 0;
  101. },
  102. // 根据速度计算线宽(速度越快,线条越细)
  103. calcLineWidth(speed) {
  104. const MAX_WIDTH = 8;
  105. const MIN_WIDTH = 2;
  106. const MAX_SPEED = 5; // 可调整的阈值
  107. let width = MAX_WIDTH - (speed / MAX_SPEED) * (MAX_WIDTH - MIN_WIDTH);
  108. return Math.max(MIN_WIDTH, Math.min(MAX_WIDTH, width));
  109. },
  110. // 3. 触摸结束
  111. handleTouchEnd() {
  112. this.isDrawing = false;
  113. this.lastPoint = null;
  114. },
  115. // 4. 清空画布
  116. clearCanvas() {
  117. this.$set(this,'signatureType',false);
  118. this.ctx.clearRect(0, 0, 750, 400); // 尺寸需与canvas实际宽高匹配
  119. this.ctx.draw(true);
  120. this.points = [];
  121. },
  122. // 5. 保存为图片
  123. saveSignature() {
  124. let self = this;
  125. if(!this.agreementType){
  126. uni.showToast({
  127. title: "请确认相关条款",
  128. icon: "none",
  129. mask: true,
  130. duration: 2000
  131. });
  132. return
  133. }
  134. if(!this.signatureType){
  135. uni.showToast({
  136. title: "请签名",
  137. icon: "none",
  138. mask: true,
  139. duration: 2000
  140. });
  141. return
  142. }
  143. uni.showModal({
  144. // title: '确认要退出吗?',
  145. content: '确认提交?',
  146. cancelColor: "#999",
  147. confirmColor: "#0183FA",
  148. success: function(res) {
  149. if (res.confirm) {
  150. uni.canvasToTempFilePath({
  151. canvasId: 'signCanvas',
  152. success: (res) => {
  153. const tempFilePath = res.tempFilePath; // 小程序/APP端为临时路径
  154. // H5端可能需要特殊处理,部分平台返回base64[citation:1]
  155. uni.showToast({
  156. title: '保存成功'
  157. });
  158. self.uploadImg(tempFilePath);
  159. // 这里可以将 tempFilePath 上传至服务器或展示预览
  160. },
  161. fail: (err) => {
  162. console.error('保存失败', err);
  163. }
  164. }, self);
  165. } else if (res.cancel) {}
  166. }
  167. });
  168. },
  169. async uploadImg(tempFilePaths) {
  170. var self = this;
  171. uni.showLoading({
  172. title: '上传中',
  173. mask: true
  174. });
  175. uni.uploadFile({
  176. url: config.base_url + '/system/file/upload', //仅为示例,非真实的接口地址
  177. header: {
  178. 'Authorization': uni.getStorageSync('token')
  179. },
  180. filePath: tempFilePaths,
  181. name: 'file',
  182. formData: {
  183. 'user': 'test'
  184. },
  185. success: (uploadFileRes) => {
  186. let res = JSON.parse(uploadFileRes.data);
  187. if (res.code == 200) {
  188. self.$set(self.newData,'collectorSign',res.data.url);
  189. self.hwmsAppRegisterOrderAdd();
  190. } else {
  191. uni.showToast({
  192. title: res.msg,
  193. icon: "none",
  194. mask: true,
  195. duration: 2000
  196. });
  197. }
  198. },
  199. fail: err => {},
  200. complete: () => {
  201. uni.hideLoading()
  202. }
  203. });
  204. },
  205. async hwmsAppRegisterOrderAdd(){
  206. const {
  207. data
  208. } = await hwmsAppRegisterOrderAdd(this.newData);
  209. if (data.code == 200) {
  210. uni.showToast({
  211. title: '提交成功',
  212. icon: "none",
  213. mask: true,
  214. duration: 2000
  215. });
  216. setTimeout(function() {
  217. uni.navigateBack({
  218. delta: 2
  219. });
  220. }, 2000);
  221. }
  222. },
  223. }
  224. }
  225. </script>
  226. <style lang="stylus" scoped>
  227. .signatureComponent {
  228. height: 100%;
  229. display flex;
  230. flex: 1;
  231. flex-direction column;
  232. overflow: hidden;
  233. position: relative;
  234. .text-position-box{
  235. width:650rpx;
  236. height:200rpx;
  237. line-height:200rpx;
  238. text-align: center;
  239. font-size:14rpx;
  240. color:#dedede;
  241. margin:0 50rpx;
  242. z-index:1;
  243. margin-top:10rpx;
  244. background-color: #fff;
  245. border:1rpx solid #dedede;
  246. }
  247. .sign-canvas{
  248. position: absolute
  249. top:10rpx;
  250. left:0;
  251. width:650rpx;
  252. height:200rpx;
  253. line-height:200rpx;
  254. text-align: center;
  255. font-size:14rpx;
  256. color:#dedede;
  257. margin:0 50rpx;
  258. z-index:10;
  259. }
  260. .agreement-box{
  261. flex:1;
  262. margin:0 60rpx;
  263. .agreement-min-box{
  264. display: flex;
  265. margin-top:15rpx;
  266. .agreement-img{
  267. width:20rpx;
  268. height:20rpx;
  269. margin:0 10rpx 0 0;
  270. }
  271. view{
  272. line-height:20rpx;
  273. font-size:12rpx;
  274. }
  275. }
  276. }
  277. .button-group{
  278. display: flex;
  279. margin:0 auto;
  280. margin-bottom:20rpx;
  281. view{
  282. font-size:14rpx;
  283. width:100rpx;
  284. height:30rpx;
  285. line-height:30rpx;
  286. text-align: center;
  287. background-color: #fff
  288. border-radius:6rpx;
  289. }
  290. view:nth-child(1){
  291. margin-right:40rpx;
  292. border:1px solid #999;
  293. background-color: #fff;
  294. color:#666;
  295. }
  296. view:nth-child(2){
  297. border:1px solid #0183FA;
  298. background-color: #0183FA;
  299. color:#fff;
  300. }
  301. }
  302. }
  303. </style>