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.

253 lines
6.0 KiB

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