an_xiao/police_uniapp/pages/signature/signature.vue

290 lines
6.9 KiB
Vue
Raw Permalink Normal View History

2024-07-25 16:03:08 +08:00
<template>
<!-- 签字功能 -->
<view class="sign-box">
<canvas class="mycanvas" :style="{ height: height + 'px' }" canvas-id="mycanvas" @touchstart="touchstart"
@touchmove="touchmove" @touchend="touchend"></canvas>
<canvas canvas-id="camCacnvs" :style="{ height: width + 'px' }" class="canvsborder"></canvas>
<view class="sigh-btns">
<u-button size="small" @click="handleCancel">
<text>取消</text>
</u-button>
<u-button size="small" @click="handleReset">
<text>重写</text>
</u-button>
<u-button size="small" @click="handleConfirm">
<text>确认</text>
</u-button>
</view>
</view>
</template>
<script>
var x = 20;
var y = 20;
var tempPoint = []; //用来存放当前画纸上的轨迹点
var id = 0;
var type = '';
let that;
let canvasw;
let canvash;
import {
mapGetters
} from 'vuex';
import {
timestampFormatTime
} from '@/utils/index.js';
export default {
data() {
return {
ctx: '', //绘图图像
points: [], //路径点集合,
width: 0,
height: 300,
index: undefined,
baseURl: 'https://www.lonsungsh.com:8083',
};
},
mounted() {},
onLoad(option) {
console.log(option);
that = this;
that.index = option
console.log(option);
id = option.id;
type = option.type;
this.ctx = uni.createCanvasContext('mycanvas', this); //创建绘图对象
//设置画笔样式
this.ctx.lineWidth = 2; //画笔的粗细
this.ctx.lineCap = 'round';
this.ctx.lineJoin = 'round';
uni.getSystemInfo({
success: function(res) {
console.log(res);
that.width = res.windowWidth * 1.0;
that.height = res.windowHeight * 0.98; //控制画板的宽高
}
});
},
computed: {
// 拿取vuex数据
...mapGetters({
userInfo: 'users/getLoginInfo'
})
},
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(); //绘制路径
}
tempPoint.push(movePoint);
},
// 触摸结束,将未绘制的点清空防止对后续路径产生干扰
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);
},
handleCancel() {
uni.navigateBack({
delta: 1
});
},
//清空画布
handleReset: function() {
that.ctx.clearRect(0, 0, that.width, that.height);
that.ctx.draw(true);
tempPoint = [];
},
//将签名笔迹上传到服务器,并将返回来的地址存到本地
handleConfirm: function() {
let that = this;
if (tempPoint.length == 0) {
uni.showToast({
title: '请先签名',
icon: 'none',
duration: 2000
});
return;
}
uni.canvasToTempFilePath({
canvasId: 'mycanvas',
success: function(res) {
let tempPath = res.tempFilePath;
console.log(tempPath, '0000');
const ctx = uni.createCanvasContext('camCacnvs', that);
ctx.translate(0, that.width);
ctx.rotate((-90 * Math.PI) / 180);
ctx.drawImage(tempPath, 0, 0, that.width, that.height);
ctx.draw();
// 图片转换base64
uni.getFileSystemManager().readFile({
filePath: tempPath, //选择图片返回的相对路径
encoding: 'base64', //编码格式
success: res => {
// 成功的回调
let base64 = 'data:image/jpeg;base64,' + res.data;
if (that.index.index === '2') {
uni.showToast({
title: '签名成功',
icon: 'success',
mask: true,
success() {
setTimeout(() => {
if (that.index.name === '1') {
uni.setStorageSync('dailycolor',base64)
} else if (that.index.name ==='2') {
uni.setStorageSync('dailycolorschool',base64)
}
uni.navigateTo({
url: `/components/dailyinspection/dailyinspection`
});
that.handleReset()
}, 500)
}
});
}else if (that.index.index === '4') {
uni.showToast({
title: '签名成功',
icon: 'success',
mask: true,
success() {
setTimeout(() => {
uni.setStorageSync('mycanvas',base64)
uni.navigateTo({
url: `/pagesD/workreceipt/workreceipt`
});
that.handleReset()
}, 500)
}
});
}
}
});
// wx.uploadFile({
// url: that.baseURl + '/mdev/picture/upload', //这里就是保存到服务器的接口
// filePath: tempPath,
// timeout: 5000,
// // 参数名和接口一致
// name: 'file',
// header: {
// 'Content-Type': 'multipart/form-data;charset=utf-8'
// },
// formData: {
// jsondata: JSON.stringify(picUploadData)
// },
// success(res) {
// const resImage = JSON.parse(res.data)?.data;
//
// },
// })
}
});
}
}
};
</script>
<style lang="scss" scoped>
.sign-box {
position: relative;
width: 100%;
height: 100%;
margin: auto;
display: flex;
flex-direction: column;
text-align: center;
}
.sign-view {
height: 100%;
}
.sigh-btns {
position: absolute;
bottom: 15rpx;
right: 0;
width: 100%;
height: 100rpx;
display: flex;
justify-content: space-between;
align-items: center;
}
/deep/ .u-button {
width: 100px !important;
background-color: #4e87ff !important;
text {
color: #fff;
font-size: 12px;
}
}
.mycanvas {
margin: 10rpx 8rpx;
width: 97%;
background-color: #ececec;
}
.canvsborder {
border: 1rpx solid #333;
position: fixed;
top: 0;
left: 10000rpx;
}
</style>