洛阳学员端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
5.4 KiB

8 months ago
  1. <template>
  2. <view class="pageBg">
  3. <view class="signature-box">
  4. <view class="topTps">需要您签名确认学时</view>
  5. <!-- 签名 -->
  6. <view class="signature">
  7. <canvas ref="mycanvas" class="mycanvas" canvas-id="mycanvas" @touchstart="touchstart" @touchmove="touchmove"
  8. @touchend="touchend"></canvas>
  9. </view>
  10. <view class="footBox">
  11. <view class="checkCon">
  12. <u-checkbox-group>
  13. <!-- <u-checkbox v-model="checked">本人 {{realName}} 承诺以上签名真实有效</u-checkbox> -->
  14. <u-checkbox :checked="checked" :label="`本人【 ${realName} 】承诺以上签名真实有效`" :labelSize="12" @change="checkedchange" ></u-checkbox>
  15. </u-checkbox-group>
  16. </view>
  17. <view class="footBtn">
  18. <view class="btn border" @click="clear">重签</view>
  19. <view class="btn" @click="finish">提交</view>
  20. </view>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. // import { pathToBase64 } from '@/common/js/jssdk_image_tools.js'
  27. var x = 20;
  28. var y = 20;
  29. export default {
  30. data() {
  31. return {
  32. //绘图图像
  33. ctx: '',
  34. //路径点集合
  35. points: [],
  36. //签名图片
  37. SignatureImg: '',
  38. hasSign: false,
  39. realName: '',
  40. checked: false,
  41. };
  42. },
  43. mounted() {
  44. this.createCanvas();
  45. this.realName = this.vuex_userInfo.name
  46. console.log(this.vuex_userInfo.name)
  47. },
  48. methods: {
  49. checkedchange(val) {
  50. this.checked = val
  51. },
  52. //关闭并清空画布
  53. close() {
  54. this.$emit('closeCanvas');
  55. this.clear();
  56. },
  57. //创建并显示画布
  58. createCanvas() {
  59. this.ctx = uni.createCanvasContext('mycanvas', this); //创建绘图对象
  60. this.ctx.setFillStyle('#000000')
  61. this.ctx.fillStyle = '#000000'
  62. //设置画笔样式
  63. this.ctx.lineWidth = 6;
  64. this.ctx.lineCap = 'round';
  65. this.ctx.lineJoin = 'round';
  66. },
  67. //触摸开始,获取到起点
  68. touchstart(e) {
  69. let startX = e.changedTouches[0].x;
  70. let startY = e.changedTouches[0].y;
  71. let startPoint = {
  72. X: startX,
  73. Y: startY
  74. };
  75. this.points.push(startPoint);
  76. //每次触摸开始,开启新的路径
  77. this.ctx.beginPath();
  78. },
  79. //触摸移动,获取到路径点
  80. touchmove(e) {
  81. let moveX = e.changedTouches[0].x;
  82. let moveY = e.changedTouches[0].y;
  83. let movePoint = {
  84. X: moveX,
  85. Y: moveY
  86. };
  87. this.points.push(movePoint); //存点
  88. let len = this.points.length;
  89. if (len >= 2) {
  90. this.draw(); //绘制路径
  91. }
  92. },
  93. // 触摸结束,将未绘制的点清空防止对后续路径产生干扰
  94. touchend() {
  95. this.points = [];
  96. },
  97. //绘制笔迹
  98. draw() {
  99. let point1 = this.points[0];
  100. let point2 = this.points[1];
  101. this.points.shift();
  102. this.ctx.moveTo(point1.X, point1.Y);
  103. this.ctx.lineTo(point2.X, point2.Y);
  104. this.ctx.stroke();
  105. this.ctx.draw(true);
  106. this.hasSign = true
  107. },
  108. //清空画布
  109. clear() {
  110. this.hasSign = false
  111. let that = this;
  112. this.createCanvas();
  113. uni.getSystemInfo({
  114. success: function(res) {
  115. let canvasw = res.windowWidth;
  116. let canvash = res.windowHeight;
  117. that.ctx.clearRect(0, 0, canvasw, canvash);
  118. that.ctx.draw(true);
  119. }
  120. });
  121. },
  122. //完成绘画并保存到本地
  123. finish() {
  124. if(!this.checked) {
  125. return this.$u.toast('请勾选承诺')
  126. }
  127. if (!this.hasSign) {
  128. uni.showToast({
  129. title: '签名为空不能保存',
  130. icon: 'none',
  131. duration: 2000
  132. })
  133. return
  134. }
  135. let that = this;
  136. uni.canvasToTempFilePath({
  137. canvasId: 'mycanvas',
  138. destWidth:160,
  139. success: function(res) {
  140. console.log('图片上传结果')
  141. console.log(res)
  142. if(!res || !res.tempFilePath) {
  143. console.log('没来这里?')
  144. that.SignatureImg = res.tempFilePath;
  145. console.log(that.SignatureImg)
  146. // that.$emit('closeCanvas', that.SignatureImg);
  147. }else{
  148. that.$emit('closeCanvas', res.tempFilePath);
  149. console.log('这是临时路径?')
  150. console.log(res.tempFilePath)
  151. console.log(that.SignatureImg)
  152. // //用来解决安卓真机获取到的是canvas图片的临时路径,转成base64格式
  153. // pathToBase64(res.tempFilePath).then(re => {
  154. // that.SignatureImg = re;
  155. // that.$emit('getCanvasImg', that.SignatureImg);
  156. // })
  157. }
  158. },
  159. });
  160. }
  161. }
  162. };
  163. </script>
  164. <style lang="scss" scoped>
  165. .signature-box {
  166. padding: 0 32rpx;
  167. display: flex;
  168. flex-direction: column;
  169. width: 100%;
  170. // height: 98%;
  171. height: calc(100vh - 150rpx);
  172. background: #fff;
  173. .topTps {
  174. width: 100%;
  175. border-radius: 8rpx;
  176. background-color: #f1f1f1;
  177. height: 110rpx;
  178. line-height: 110rpx;
  179. text-align: center;
  180. font-size: 34rpx;
  181. color: #333;
  182. }
  183. .footBox {
  184. .checkCon {
  185. width: 100%;
  186. height: 100rpx;
  187. display: flex;
  188. align-items: center;
  189. }
  190. .footBtn {
  191. display: flex;
  192. justify-content: space-between;
  193. .btn {
  194. width: 49%;
  195. font-size: 32rpx;
  196. line-height: 90rpx;
  197. height: 90rpx;
  198. color: #fff;
  199. border-radius: 14rpx;
  200. background: linear-gradient(180deg, #3593FB 0%, #53D3E5 100%);;
  201. text-align: center;
  202. &.border {
  203. border: 1rpx solid #218dff ;
  204. color: #218dff;
  205. background: #fff;
  206. }
  207. }
  208. }
  209. }
  210. //签名模块
  211. .signature {
  212. flex: 1;
  213. // height: 0;
  214. // width: 0;
  215. //canvas
  216. .mycanvas {
  217. width:100%;
  218. height:100%;
  219. background-color: #fff;
  220. border-radius: 10px 10px 0 0;
  221. }
  222. }
  223. }
  224. </style>