业务需求:在最近开发的项目中,需要在任务完成页面,填写任务相关信息,并签署自己的名字,完成任务。

根据uniapp的签名插件,调整封装成sign.vue组件,在页面中使用

image.png

  1. 因为任务有保存功能,增加了image标签用于展示保存之后的签名图片。点击画布清除按钮会删除保存的签名,因此需要父组件传值签名图片url给子组件 ,子组件使用props接收,并且需要改变该值。

    <image class="signImg" v-if="newValue" :src="newValue" mode="widthFix"></image>

    props是只读的,Vue底层会监测你对props的修改,如果进行了修改,就会发出警告,若业务需求确实需要修改,那么请复制props的内容到data中一份,然后去修改data中的数据。

    此方法无法实现更改,data中的值在created生命周期或mounted生命周期里打印始终为空

    解决方法:
    通过watch来监听传递过来的字段,把这个字段赋值给data中的新字段,实现更改

    watch: {
        value(val, oldVal) {
                this.newValue = val;
        },
    },
    
  2. 在页面初始化的时候,需要设置绘画图像的背景颜色为白色,不然图像的背景色是透明的

    this.ctx.setFillStyle('#fff')

  3. @touchmove.stop.prevent 在签名的时候,禁止页面滑动。

  4. 签名成功后的临时存储文件路径tempFilePath为本地路径,如果直接上传该路径,后台会阻止该上传(在微信开发者工具没有问题,在真机上签名后无法提交任务),后面将签名路径(本地资源)给后台服务器(uni.uploadFile将本地资源上传到开发者服务器),再使用后台重新返回该签名新的地址提交,才成功。一直以为是 @touchmove.stop.prevent 阻止了提交按钮,用真机调试,打印一些信息,才看到后台返回的阻止信息

    imgUpload(tempFilePaths) {
    
        new Promise((resolve, reject) => {
                const uploadTask = uni.uploadFile({
                        url: this.action, //上传图片的后台接口api
                        filePath: tempFilePaths,
                        name: 'file',
                        fileType: 'image',
                        header: {
                                'Token': uni.getStorageSync('token'),
                        },
                        success: (uploadFileRes) => {
                                resolve(uploadFileRes);
                                this.$emit("uploadSuccess", uploadFileRes);
                        },
                        fail: (err) => {
                                reject(err);
                                this.$emit("uploadFail", err);
                        },
    
                });
        })
    },
    
  5. 在页面中使用sign.vue组件。

    <sign @touchmove.stop.prevent @uploadSuccess="UploadSuccess2" v-model="signUrl" :value="signUrl" />

    
    UploadSuccess2(res) {
        // 子组件传递给父组件的签名路径
        var data = JSON.parse(res.data);
        if (data.code == 200) {
            this.signUrl = data.data
        }
    },
    
sign.vue组件
 <template>
	<view class="sign-box">
		<image class="signImg" v-if="newValue" :src="newValue" mode="widthFix"></image>
		
		<canvas class="mycanvas" canvas-id="mycanvas" @touchstart="touchstart" @touchmove="touchmove"
			@touchend="touchend" ></canvas>
		<view class="footer">
			<view class="left box-bg" @click.stop="finish">完成</view>
			<view class="right box-bg" @click.stop="clear">清除</view>
		</view>
	</view>
</template>

<script>
	var x = 20;
	var y = 20;
	import {
		config
	} from '@/utils/config.js'
	export default {
		props:{
			value:'',
		},
		data() {
			return {
				ctx: '', //绘图图像
				points: [], //路径点集合
				action: config.baseUrl + 'api/update',
				
				newValue: ''
			}
		},
		
		watch: {
			value(val, oldVal) {
				// console.log('value',val, oldVal)
				this.newValue = val;
			},
		},
		created() {
			this.ctx = uni.createCanvasContext("mycanvas", this); //创建绘图对象
			this.ctx.setFillStyle('#fff')
			//设置画笔样式
			this.ctx.lineWidth = 4;
			this.ctx.lineCap = "round"
			this.ctx.lineJoin = "round"
		},
		methods: {
			//触摸开始,获取到起点
			touchstart: function(e) {
				let startX = e.changedTouches[0].x;
				let startY = e.changedTouches[0].y;
				let startPoint = {
					X: startX,
					Y: startY
				};

				/* **************************************************
				    #由于uni对canvas的实现有所不同,这里需要把起点存起来
				 * **************************************************/
				this.points.push(startPoint);

				//每次触摸开始,开启新的路径
				this.ctx.beginPath();
			},

			//触摸移动,获取到路径点
			touchmove: function(e) {
				let moveX = e.changedTouches[0].x;
				let moveY = e.changedTouches[0].y;
				let movePoint = {
					X: moveX,
					Y: moveY
				};
				this.points.push(movePoint); //存点
				let len = this.points.length;
				if (len >= 2) {
					this.draw(); //绘制路径
				}

			},

			// 触摸结束,将未绘制的点清空防止对后续路径产生干扰
			touchend: function() {
				this.points = [];
			},

			/* ***********************************************
			#   绘制笔迹
			#   1.为保证笔迹实时显示,必须在移动的同时绘制笔迹
			#   2.为保证笔迹连续,每次从路径集合中区两个点作为起点(moveTo)和终点(lineTo)
			#   3.将上一次的终点作为下一次绘制的起点(即清除第一个点)
			************************************************ */
			draw: function() {
				let point1 = this.points[0]
				let point2 = this.points[1]
				this.points.shift()
				this.ctx.moveTo(point1.X, point1.Y)
				this.ctx.lineTo(point2.X, point2.Y)
				this.ctx.stroke()
				this.ctx.draw(true)
			},

			//清空画布
			clear: function() {
				let that = this;
				that.newValue = '';
				uni.getSystemInfo({
					success: function(res) {
						let canvasw = res.windowWidth;
						let canvash = res.windowHeight;
						that.ctx.setFillStyle('#fff')
						that.ctx.clearRect(0, 0, canvasw, canvash);
						that.ctx.draw(true);
					},
				})
			},

			//完成绘画并保存到本地
			finish: function() {
				var that = this;
				
				uni.canvasToTempFilePath({
					canvasId: 'mycanvas',
					success: function(res) {
						console.log(res)
						uni.showToast({
							'title': '签名成功'
						})
						let path = res.tempFilePath;
						
						that.imgUpload(path)
					},
					fail: function(res) {
						console.log(res)

					},
				},that)
			},
			imgUpload(tempFilePaths) {
				
				new Promise((resolve, reject) => {
					const uploadTask = uni.uploadFile({
						url: this.action, 
						filePath: tempFilePaths,
						name: 'file',
						fileType: 'image',
					
						header: {
							'Token': uni.getStorageSync('token'),
						},
						success: (uploadFileRes) => {
							
							if (typeof this.uploadSuccess == 'function') {
								if (this.uploadSuccess(uploadFileRes).success) {
							
									this.value.push(this.uploadSuccess(uploadFileRes)
										.url)
								}
							}
							resolve(uploadFileRes);
							this.$emit("uploadSuccess", uploadFileRes);
						},
						fail: (err) => {
							
							reject(err);
							this.$emit("uploadFail", err);
						},
						
					});
				})
			},
		},
	}
</script>


Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐