uniapp H5实现签名

第一种:跳转签名页面

1、创建审核页面audit.vue

<template>
	<view>

		<uni-section title="">
			<view class="auditClass">
				<uni-forms :model="baseFormData" ref="baseFormRef" :rules="rules" label-position="top"
					labelWidth="80px">
					<uni-forms-item label="审核意见" required name="advice">
						<uni-easyinput type="textarea" v-model="baseFormData.advice" placeholder="请输入审核意见" />
					</uni-forms-item>
					<image :src='baseFormData.signUrl.replace(/[\r\n]/g, "")' v-if="baseFormData.signUrl"></image>
					<button type="primary" style="margin-bottom: 15px;" :disabled="isClick" @click="signBtn">签名</button>
					<button type="primary" :disabled="isClick" @click="submit('baseFormRef')">提交</button>
				</uni-forms>
			</view>
		</uni-section>

	</view>
</template>

<script>
	import config from '@/config';
	import {
		getUserProfile
	} from "@/api/system/user"
	const baseUrl = config.baseUrl;

	export default {
		data() {
			return {
				loading: false,
				user: {},
				// 基础表单数据
				baseFormData: {
					signUrl: '',
					advice: '',
				},
				isClick: false,
			}
		},
		computed: {

		},
		onLoad(options) {
			this.baseFormData.proId = options.proId;
		},
		onReady() {
			this.$refs.baseFormRef.setRules(this.rules)
		},
		onShow() {
			uni.$on("imageUrl", (data) => {
				console.log("接收事件imageUrl成功,data=", data);
				this.baseFormData.signUrl = data.imageUrl;
			});
		},
		methods: {
			getUser() {
				getUserProfile().then(response => {
					this.user = response.data
					this.getTableData();
				})
			},
			submit(ref) {
				let self = this;
				self.$refs[ref].validate().then(res => {
					console.log('success', res);
					if (self.baseFormData.signUrl == '' || self.baseFormData.signUrl == null) {
						uni.showToast({
							title: '请签名',
							icon: "error"
						});
					} else {
						self.isClick = true;
						uni.showLoading({
							title: '提交中'
						});
						uni.request({
							withCredentials: true,
							url: baseUrl + '',
							data: JSON.stringify(self.baseFormData),
							method: 'post',
							success: function(res) {
								console.log(res.data);
								self.isClick = false;
								uni.hideLoading();
								if (res.data.code == 0) {
									uni.showToast({
										title: res.data.msg,
										icon: "error"
									});
								} else {
									uni.navigateBack({
										delta: 1,
										success() {
											setTimeout(() => {
												uni.showToast({
													title: res.data.msg,
													icon: 'success'
												})
											}, 500)
										}
									});
								}
							}
						});
					}
				}).catch(err => {
					console.log('err', err);
				});
			},
			// 签名
			signBtn() {
				this.$tab.navigateTo('/pages/processAudit/sign');
			},

		}
	}
</script>

<style scoped>
	.auditClass {
		padding: 15px;
		background-color: #ffffff;
	}
</style>

2、创建签名页面sign.vue

<template>
	<view>
		<view class="main-content" @touchmove.stop.prevent="">
			<!-- 签字canvas -->
			<canvas class="mycanvas" id="mycanvas" canvas-id="mycanvas" @touchstart="touchstart" @touchmove="touchmove"
				@touchend="touchend"></canvas>
			<canvas class="mycanvas"
				:style="{ 'z-index': -1, width: `${screenWidth}px`, height: `${(screenWidth * screenWidth) / screenHeight}px` }"
				id="rotatCanvas" canvas-id="rotatCanvas"></canvas>

			<cover-view class="button-line">
				<cover-view class="save-button" @tap="finish">保存</cover-view>
				<cover-view class="clear-button" @tap="clear">清空</cover-view>
			</cover-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				ctx: '', //绘图图像
				points: [], //路径点集合
				screenWidth: '',
				screenHeight: '',
				img: '',
				isDraw: false,
			};
		},
		mounted() {
			this.createCanvas();
			uni.getSystemInfo({
				success: e => {
					this.screenWidth = e.screenWidth;
					this.screenHeight = e.screenHeight;
				}
			});
		},
		methods: {
			//创建并显示画布
			createCanvas() {
				this.showCanvas = true;
				this.ctx = uni.createCanvasContext('mycanvas', this);
				//设置画笔样式
				this.ctx.lineWidth = 2;
				this.ctx.lineCap = 'round';
				this.ctx.lineJoin = 'round';
			},
			//触摸开始,获取到起点
			touchstart(e) {
				this.isDraw = true
				let startX = e.changedTouches[0].x;
				let startY = e.changedTouches[0].y;
				let startPoint = {
					X: startX,
					Y: startY
				};
				this.points.push(startPoint);
				//每次触摸开始,开启新的路径
				this.ctx.beginPath();
			},
			//触摸移动,获取到路径点
			touchmove(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() {
				this.points = [];
			},
			/* ***********************************************
				#   绘制笔迹
				#	1.为保证笔迹实时显示,必须在移动的同时绘制笔迹
				#	2.为保证笔迹连续,每次从路径集合中区两个点作为起点(moveTo)和终点(lineTo)
				#	3.将上一次的终点作为下一次绘制的起点(即清除第一个点)
				************************************************ */
			draw() {
				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() {
				this.isDraw = false
				this.ctx.clearRect(0, 0, this.screenWidth, this.screenHeight);
				this.ctx.draw(true);
			},
			//完成绘画并保存到本地
			finish() {
				if (this.isDraw) {
					uni.canvasToTempFilePath({
						canvasId: 'mycanvas',
						fileType: 'png',
						quality: 1, //图片质量
						success: res => {
							console.log("sign" + res.tempFilePath);
							this.rotate(res.tempFilePath);
						},
						complete: com => {}
					});
				} else {
					uni.$emit("imageUrl", {
						imageUrl: ''
					});
					uni.navigateBack();
				}
			},
			rotate(tempFilePath) {
				let that = this
				wx.getImageInfo({
					src: tempFilePath,
					success: (res1) => {
						let canvasContext = wx.createCanvasContext('rotatCanvas')
						let rate = res1.height / res1.width
						let width = 300 / rate
						let height = 300
						canvasContext.translate(height / 2, width / 2)
						canvasContext.rotate((270 * Math.PI) / 180)
						canvasContext.drawImage(tempFilePath, -width / 2, -height / 2, width, height)
						canvasContext.draw(false, () => {
							wx.canvasToTempFilePath({
								canvasId: 'rotatCanvas',
								fileType: 'png',
								quality: 1, //图片质量
								success(res) {
									console.log("====="+JSON.stringify(res))
									
									uni.$emit("imageUrl", {
										imageUrl: res.tempFilePath,
									});
									uni.navigateBack();
								}
							})
						})
					}
				})
			},

		}
	};
</script>
<style lang="scss" scoped>
	.main-content {
		width: 100vw;
		height: 100vh;
		background-color: red;
		position: fixed;
		top: 0rpx;
		left: 0rpx;
		z-index: 9999;
	}

	.mycanvas {
		width: 100vw;
		height: 100vh;
		background-color: #fafafa;
		position: fixed;
		left: 0rpx;
		top: 0rpx;
		z-index: 2;
	}

	.button-line {
		transform: rotate(90deg);
		position: fixed;
		bottom: 170rpx;
		left: -100rpx;
		width: 340rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		z-index: 999;
		font-size: 34rpx;
		font-weight: 600;
	}

	.save-button {
		color: #ffffff;
		width: 150rpx;
		height: 80rpx;
		text-align: center;
		line-height: 80rpx;
		border-radius: 10rpx;
		background-color: #007aff;
	}

	.clear-button {
		color: #ffffff;
		width: 150rpx;
		height: 80rpx;
		text-align: center;
		line-height: 80rpx;
		border-radius: 10rpx;
		background-color: #aaaaaa;
	}
</style>

3、签名页面效果展示

第二种:签名页面嵌入

1、创建审核页面audit.vue

<template>
	<view>

		<uni-section title="">
			<view class="auditClass">
				<uni-forms :model="baseFormData" ref="baseFormRef" :rules="rules" label-position="top"
					labelWidth="80px">
					<uni-forms-item label="审核意见" required name="advice">
						<uni-easyinput type="textarea" v-model="baseFormData.advice" placeholder="请输入审核意见" />
					</uni-forms-item>
					<button type="primary" style="margin-bottom: 15px;" @click="signBtn">签名</button>
					<sign v-if='signShow' @closeCanvas="closeCanvas" @change="change"></sign>
					<button type="primary" @click="submit('baseFormRef')">提交</button>
				</uni-forms>
			</view>
		</uni-section>

	</view>
</template>

<script>
	import config from '@/config';
	import {
		getUserProfile
	} from "@/api/system/user"
	const baseUrl = config.baseUrl;

	import sign from './sign.vue'

	export default {
		components: {
			sign
		},
		data() {
			return {
				loading: false,
				user: {},
				// 基础表单数据
				baseFormData: {
					signUrl: '',
					advice: '',
				},
				signShow: false,
			}
		},
		computed: {

		},
		onLoad(options) {
			console.log("proId====" + options.proId)
			this.baseFormData.proId = options.proId;
		},
		onReady() {
			// 设置自定义表单校验规则,必须在节点渲染完毕后执行
			this.$refs.baseFormRef.setRules(this.rules)
		},
		methods: {
			submit(ref) {
				let self = this;
				self.$refs[ref].validate().then(res => {
					console.log('success', res);
					if (self.baseFormData.signUrl == '' || self.baseFormData.signUrl == null) {
						uni.showToast({
							title: '请签名',
							icon: "error"
						});
					} else {
						//获取表单数据并发送请求
						self.baseFormData.userId = self.user.userId;
						uni.request({
							withCredentials: true, // 加入这一行即可
							url: baseUrl + '/api/saveProcessAuditInfo',
							data: JSON.stringify(self.baseFormData),
							method: 'post',
							success: function(res) {
								console.log(res.data);
								if (res.data.code == 0) {
									uni.showToast({
										title: res.data.msg,
										icon: "error"
									});
								} else {
									uni.navigateBack({
										delta: 1,
										success() {
											setTimeout(() => {
												uni.showToast({
													title: res.data.msg,
													icon: 'success'
												})
											}, 500)
										}
									});
								}
							}
						});
					}
				}).catch(err => {
					console.log('err', err);
				});
			},
			// 签名
			signBtn() {
				this.signShow = true
			},
			// 关闭画布
			closeCanvas(e) {
				this.signShow = false
			},
			// 用户签名数据
			change(e) {
				console.log(e) //返回的base64地址
				this.baseFormData.signUrl = e;
			},

		}
	}
</script>

<style scoped>
	.auditClass {
		padding: 15px;
		background-color: #ffffff;
	}
</style>

2、创建签名页面sign.vue

<template>
	<view>
		<view class="box" :style="{height:height}">
			<view class="top">
				<canvas class="canvas-box" @touchstart='touchstart' @touchmove="touchmove" @touchend="touchend"
					canvas-id="myCanvas">
				</canvas>
				<view>请在此处签名</view>
			</view>
			<view class=" bottom">
				<uni-tag text="完成" mode="dark" @click="finish" />
				<uni-tag text="重签" mode="dark" @click="clear" />
				<uni-tag text="取消" mode="dark" @click="close" />
			</view>
		</view>
	</view>
</template>
 
<script>
	export default {
		data() {
			return {
				ctx: '', //绘图图像
				points: [], //路径点集合
				height: '200px', //高度
				canvasShoww: false, //提示
				isDraw: false,
			}
		},
		created() {
			let that = this
			// uni.getSystemInfo({
			// 	success: function(res) {
			// 		that.height = res.windowHeight + 'px';
			// 	},
			// });
		},
		mounted() {
			this.createCanvas()
		},
		methods: {
			//创建并显示画布
			createCanvas() {
				this.ctx = uni.createCanvasContext('myCanvas', this); //创建绘图对象
				//设置画笔样式
				this.ctx.lineWidth = 4;
				this.ctx.lineCap = 'round';
				this.ctx.lineJoin = 'round';
			},
 
			// 触摸开始
			touchstart(e) {
				// 在签名前调用禁用滚动
				this.disableScroll();
				this.isDraw = true
				let startX = e.changedTouches[0].x;
				let startY = e.changedTouches[0].y;
				let startPoint = {
					X: startX,
					Y: startY
				};
				this.points.push(startPoint);
				//每次触摸开始,开启新的路径
				this.ctx.beginPath();
			},
			// 移动
			touchmove(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(e) {
				// 在签名完成后调用启用滚动
				this.enableScroll();
				this.points = [];
			},
			//绘制笔迹
			draw() {
				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() {
				let that = this;
				if (this.imageShow) {
					if (this.imageUrl) {
						this.imageUrl = '';
						this.imageShow = false;
 
					}
				} else {
					uni.getSystemInfo({
						success: function(res) {
							// 设置画笔样式
							let canvasw = res.windowWidth;
							let canvash = res.windowHeight;
							that.ctx.clearRect(0, 0, canvasw, canvash);
							that.ctx.draw(true);
						}
					});
				}
				this.createCanvas();
			},
			//关闭并清空画布
			close() {
				this.$emit('closeCanvas');
				this.createCanvas();
				this.clear();
			},
 
			//完成绘画并保存到本地
			finish() {
				if (this.isDraw) {
					let that = this;
					uni.canvasToTempFilePath({
							canvasId: 'myCanvas',
							success: function(res) {
								that.imageShow = true;
								that.imageUrl = res.tempFilePath;
								that.$emit('closeCanvas');
								that.$emit('change', res.tempFilePath);
								that.close();
							}
						},
						// this
					);
					return
				}
				this.$u.func.showToast({
					title: '请签名',
				})
			},
			// 禁用页面滚动
			disableScroll() {
			   var box=function(e){passive: false ;};
			                  document.body.style.overflow='hidden';
			                  document.addEventListener("touchmove",box,false);
			},
			// 启用页面滚动
			enableScroll() {
			  var box=function(e){passive: false };
			                  document.body.style.overflow=''; // 出现滚动条
			                  document.removeEventListener("touchmove",box,false);
			},
		}
	}
</script>
 
<style lang="scss" scoped>
	.box {
		width: 100%;
		margin-bottom: 50px;
		display: flex;
		flex-direction: column;
		background-color: #fff;
 
		.top {
			height: 95%;
			margin: 50rpx;
			border: 1px solid #000;
			position: relative;
 
 
 
			.canvas-box {
				width: 100%;
				height: 100%;
			}
		}
 
		.bottom {
			height: 5%;
			display: flex;
			align-items: flex-start;
			justify-content: space-around;
		}
	}
</style>

3、签名页面效果展示

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/574849.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

数据结构初阶——树和二叉树

数据结构初阶——树和二叉树 1. 树的概念和结构1.1 树的概念1.2 树的表示 2. 二叉树2.1 二叉树的概念和结构2.2 二叉树的存储结构2.2.1 顺序存储2.2.2 链式存储 3. 二叉树的顺序结构及实现——堆3.1 堆的概念和结构3.2 堆的实现3.2.1 堆的定义3.2.2 堆的向上调整3.2.3 堆的向下…

【网络安全】安全事件管理处置 — 事件分级分类

专栏文章索引&#xff1a;网络安全 有问题可私聊&#xff1a;QQ&#xff1a;3375119339 目录 一、安全事件分级 二、应急事件分级 三、安全事件分类 四、常见安全事件原因分析 1.web入侵 2.漏洞攻击 3.网络攻击 一、安全事件分级 在对安全事件的应急响应过程中&#xf…

【Hadoop】-Apache Hive概述 Hive架构[11]

目录 Apache Hive概述 一、分布式SQL计算-Hive 二、为什么使用Hive Hive架构 一、Hive组件 Apache Hive概述 Apache Hive是一个在Hadoop上构建的数据仓库基础设施&#xff0c;它提供了一个SQL-Like查询语言来分析和查询大规模的数据集。Hive将结构化查询语言&#xff08;…

LT8711UXD助力新款Swtich游戏机底座《4K/60HZ投屏方案》

Nintendo Switch&#xff08;OLED版&#xff09;正面搭载了一块分辨率为720P的7.0英寸OLED屏幕&#xff1b;具有白色和电光蓝电光红2种颜色&#xff1b;机身长度102毫米&#xff0c;宽度242毫米&#xff0c;厚度13.9毫米&#xff0c;重量约420克。 [2]Nintendo Switch&#xff…

明天报名!!济宁教师招聘报名照片及常见问题

明天报名!!济宁教师招聘报名照片及常见问题 山东济宁教师招聘1000多人 报名时间: 2024年4月25日9:00-4月28日16:00 缴费时间: 2024年4月25日11:00-4月30日16:00 打印准考证:2024年5月23日9:00-5月26日9:30 初审时间: 2024年4月25日11:00-4月29日16:00 查询时间: 2024年4月…

10、了解JVM判断对象可回收的神秘法则!

10.1、垃圾回收触发时机? 在我们之前的学习中,我们已经了解到,当我们的系统在运行过程中创建对象时,这些对象通常会被优先分配在所谓的“新生代”内存区域,如下图所示。 在新生代中,当对象数量逐渐增多,接近填满整个空间时,会触发垃圾回收机制。这个机制的作用是回收…

人工智能(pytorch)搭建模型28-基于Transformer的端到端目标检测DETR模型的实际应用,DETR的原理与结构

大家好&#xff0c;我是微学AI&#xff0c;今天给大家介绍一下人工智能(pytorch)搭建模型28-基于Transformer的端到端目标检测DETR模型的实际应用&#xff0c;DETR的原理与结构。DETR&#xff08;Detected Transformers&#xff09;是一种基于Transformer的端到端目标检测模型&…

IPV4报文格式和IP分片及计算

目录 1.IPV4报文格式 2.IP分片及计算 1.IPV4报文格式 版本&#xff1a;四位&#xff0c;IPV4 0100 4 ;IPV6 0110 6头部长度(IHL):最小值是5&#xff0c;最大值为15&#xff0c;单位4字节。IPV6固定头部长度40字节TOS:为区分服务字段&#xff0c;用区分服务类型&#xff0c;即…

半导体晶圆厂内外网数据单向导出,什么样的方案才安全又便捷?

半导体晶圆厂企业为了隔绝外部⽹络有害攻击、保护⽹络和数据安全&#xff0c;通常采⽤物理隔离的⽅式&#xff0c;将企业内⽹与互联⽹隔离。⽹络隔离后&#xff0c;基于业务开展需求&#xff0c;部分重要数据仍需由内⽹导⼊及导出⾄外部⽹络区域。为保障数据的安全合规性&#…

VMware配置centos虚拟机实现内网互通

VMware配置centos虚拟机实现内网互通 一、安装无桌面模式 环境说明&#xff1a; VMWare版本&#xff1a;VMware Workstation 17 Pro Centos版本&#xff1a;CentOS-7.9-x86_64-DVD-2009.iso 一键下载本文资源包 1. 安装虚拟机 下面是创建具体步骤,其中需要注意的是&#xff1…

如何优雅的实现 iframe 多层级嵌套通讯

前言 在前端开发项目中&#xff0c;不可避免的总会和 iframe 进行打交道&#xff0c;我们通常会使用 postMessage 实现消息通讯。 如果存在下面情况&#xff1a; iframe 父子通讯iframe 同层级通讯iframe 嵌套层级通讯 当面对这种复杂的情况的时候&#xff0c;通讯不可避免…

uniapp制作安卓原生插件踩坑

1.uniapp和Android工程互相引用讲解 uniapp原生Android插件开发入门教程 &#xff08;最新版&#xff09;_uniapp android 插件开发-CSDN博客 2.uniapp引用原生aar目录结构 详细尝试步骤1完成后生成的aar使用&#xff0c;需要新建nativeplugins然后丢进去 3.package.json示例…

机器学习——过拟合

一、过拟合得表现 模型在训练过程中&#xff0c;除了会出现过拟合现象&#xff0c;还有可能出现欠拟合的情况。相比而言&#xff0c;后者通常发生在建模前期&#xff0c;只要做好特征工程一般可以解决模型欠拟合问题。下图描述了模型在训练数据集上的三种情况&#xff1a; 其…

二阶响应曲面分析

文章目录 一、二阶响应曲面介绍1.1 什么时候用二阶响应曲面1. 非线性关系2. 探寻极值&#xff08;最大化或最小化&#xff09;3. 复杂的交互作用4. 精度要求高5. 探索性分析阶段 1.2响应曲面的特征 二、实例说明2.1 二阶模型求解 参考自《实验设计与数据处理》一书 一、二阶响应…

HTML5 服务器发送事件(Server-Sent Events)

参考&#xff1a;HTML5 服务器发送事件(Server-Sent Events) | 菜鸟教程 一&#xff0c;sse介绍 Server-Sent 事件 - 单向消息传递 Server-Sent 事件指的是网页自动获取来自服务器的更新。 以前也可能做到这一点&#xff0c;前提是网页不得不询问是否有可用的更新。通过服务…

Verilog基础语法——parameter、localparam与`define

Verilog基础语法——parameter、localparam与define 写在前面一、localparam二、parameter三、define写在最后 写在前面 在使用Verilog编写RTL代码时&#xff0c;如果需要定义一个常量&#xff0c;可以使用define、parameter和localparam三种进行定义与赋值。 一、localparam …

【Linux深造日志】运维工程师必会Linux常见命令以及周边知识!

&#x1f3ac; 鸽芷咕&#xff1a;个人主页 &#x1f525; 个人专栏: 《linux深造日志》《粉丝福利》 ⛺️生活的理想&#xff0c;就是为了理想的生活! 引入 哈喽各位宝子们好啊&#xff01;我是博主鸽芷咕。日志这个东西我相信大家都不陌生&#xff0c;在 linxu/Windows 系统…

新媒体运营-----短视频运营-----PR视频剪辑----字幕

新媒体运营-----短视频运营-----PR视频剪辑-----持续更新(进不去说明我没写完)&#xff1a;https://blog.csdn.net/grd_java/article/details/138079659 文章目录 1. PR创建字幕2. 通过剪映来智能添加字幕3. 如何像文本对象一样&#xff0c;给字幕做特效4. 写字特效 1. PR创建字…

ssm079基于SSM框架云趣科技客户管理系统+jsp

客户管理系统设计与实现 摘 要 现代经济快节奏发展以及不断完善升级的信息化技术&#xff0c;让传统数据信息的管理升级为软件存储&#xff0c;归纳&#xff0c;集中处理数据信息的管理方式。本客户管理系统就是在这样的大环境下诞生&#xff0c;其可以帮助管理者在短时间内处…

Gateway基础知识

文章目录 Spring Cloud GateWay 用法核心概念请求流程两种配置方式设置日志&#xff08;建议设置&#xff09;路由的各种断言断言The After Route Predicate FactoryThe Before Route Predicate FactoryThe Between Route Predicate FactoryThe Cookie Route Predicate Factory…
最新文章