pad-app/pages/receipt/sign.vue

330 lines
8.1 KiB
Vue
Raw Permalink Normal View History

2023-11-07 18:02:20 +08:00
<template>
<view class="sign">
<head-view title="签名"></head-view>
<view class="containe contentFixedr">
<view class="sign-box">
<canvas class="mycanvas" :style="{width:width +'px',height:height +'px'}" canvas-id="mycanvas"
@touchstart="touchstart" @touchmove="touchmove" @touchend="touchend"></canvas>
<canvas canvas-id="camCacnvs" :style="{width:height +'px',height:width +'px'}"
class="canvsborder"></canvas>
</view>
<view class="sigh-btns">
2023-12-22 17:48:52 +08:00
<view class="btn qx" @click="handleCancel"></view>
<view class="btn qc" @click="handleReset"></view>
<view class="btn bc" @click="handleConfirm"></view>
2023-11-07 18:02:20 +08:00
</view>
</view>
</view>
</template>
<script>
import {
pathToBase64,
base64ToPath
} from '../../js_sdk/mmmm-image-tools/index.js'
import {
compressImgBySize
} from '@/common/compressImg'
import utils from '@/common/util.js';
var x = 20;
var y = 20;
var tempPoint = []; //用来存放当前画纸上的轨迹点
var id = 0;
var type = '';
let that;
let canvasw;
let canvash;
export default {
data() {
return {
ctx: '', //绘图图像
points: [], //路径点集合,
width: 0,
height: 0,
loginObj: {},
signImg: "",
shipInfo: "",
};
},
onLoad(option) {
that = this;
// id = option.id;
// type = option.type;
this.shipInfo = JSON.parse(decodeURIComponent(option.params))
console.log(this.shipInfo)
this.ctx = uni.createCanvasContext('mycanvas', this); //创建绘图对象
//设置画笔样式
this.ctx.lineWidth = 4;
this.ctx.lineCap = 'round';
this.ctx.lineJoin = 'round';
uni.getSystemInfo({
success: function(res) {
console.log(res);
that.width = res.windowWidth * 0.8;
that.height = res.windowHeight * 0.85;
}
});
this.loginObj = uni.getStorageSync('loginObj')
},
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() {
2024-03-22 19:00:41 +08:00
var that = this
2023-11-07 18:02:20 +08:00
if (tempPoint.length == 0) {
uni.showToast({
title: '请先签名',
icon: 'none',
duration: 2000
});
return;
} else {
2024-03-22 19:00:41 +08:00
// var that = this
2023-11-07 18:02:20 +08:00
uni.canvasToTempFilePath({
canvasId: 'mycanvas',
success: function(e) {
2024-03-22 19:00:41 +08:00
pathToBase64(e.tempFilePath).then(path => {
// let timestamp = new Date().getTime();
// let sunumber = Math.floor(Math.random() * 999);
// var file = that.base64ToFile(e.tempFilePath, timestamp + sunumber)
let data = {
"fileName": "abc",
"base64Str": path
}
uni.request({
url: `${that.$local}/pub/file/uploadRaw`,
data: data,
header: {
'Content-Type': 'application/json', //自定义请求头信息
'Authorization': `Bearer ${that.loginObj.access_token}`
},
method: 'POST', //请求方式,必须为大写
success: (res) => {
var file = String(res.data)
2023-11-07 18:02:20 +08:00
uni.uploadFile({
url: `${that.$local}/api/file/upload`, //上传图片api
header: {
'Authorization': `Bearer ${that.loginObj.access_token}`
},
2024-03-22 19:00:41 +08:00
filePath: e.tempFilePath,
2023-11-07 18:02:20 +08:00
name: 'file',
2024-03-22 19:00:41 +08:00
formData: {
'file': file
},
2023-11-07 18:02:20 +08:00
success: (res) => {
that.signImg = JSON.parse(res.data).data.filePath
let reqDTO = {}
if (that.shipInfo.type == '0') {
reqDTO = {
yardSignFg: 1,
yardSignUrl: that.signImg,
vvyId: that.shipInfo.vvyId,
}
} else if (that.shipInfo.type == '1') {
reqDTO = {
tallySignFg: 1,
tallySignUrl: that.signImg,
vvyId: that.shipInfo.vvyId,
}
} else {
reqDTO = {
shipSignFg: 1,
shipSignUrl: that.signImg,
vvyId: that.shipInfo.vvyId,
}
}
console.log(reqDTO)
uni.request({
url: `${that.$local}/shp/vesselVoyages/writeNameUrl`,
data: reqDTO,
header: {
'Content-Type': 'application/json', //自定义请求头信息
'Authorization': `Bearer ${that.loginObj.access_token}`
},
method: 'POST', //请求方式,必须为大写
success: (res) => {
console.log(res)
let obj = {
vvyId: that.shipInfo.vvyId,
}
const params = encodeURIComponent(JSON
.stringify(obj));
2024-03-15 17:34:17 +08:00
uni.redirectTo({
url: '/pages/receipt/details?params=' +
params
})
2023-11-07 18:02:20 +08:00
}
})
},
fail: (err) => {
console.log(err)
}
})
2024-03-22 19:00:41 +08:00
}
})
})
2023-11-07 18:02:20 +08:00
}
});
}
},
base64ToFile(base64, name) {
if (typeof base64 != 'string') {
return;
}
var arr = base64.split(',')
var type = arr[0].match(/:(.*?);/)[1]
var fileExt = type.split('/')[1]
var bstr = atob(arr[1])
var n = bstr.length
var u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new File([u8arr], `${name}.` + fileExt, {
type: type
})
},
}
};
</script>
<style lang="scss" scoped>
2023-12-22 17:48:52 +08:00
page {
background-color: #fff;
}
2023-11-07 18:02:20 +08:00
.container {
height: calc(100vh - 68px);
padding-bottom: 30px;
background-color: #fff;
}
.sign-box {
height: 90%;
margin-left: 10%;
display: flex;
flex-direction: column;
text-align: center;
}
.sign-view {
height: 100%;
}
.sigh-btns {
display: flex;
justify-content: center;
2023-12-22 17:48:52 +08:00
width: 100%;
height: 80px;
background: #FFFFFF;
box-shadow: 0 -3px 7px 0 rgba(0, 0, 0, 0.10);
position: fixed;
bottom: 0;
2023-11-07 18:02:20 +08:00
}
.btn {
2023-12-22 17:48:52 +08:00
margin: 15.5px 17px;
width: 200px;
height: 49px;
border-radius: 2px;
font-size: 18px;
text-align: center;
line-height: 49px;
2023-11-07 18:02:20 +08:00
}
2023-12-22 17:48:52 +08:00
.qx {
background-color: #eee;
2023-11-07 18:02:20 +08:00
}
2023-12-22 17:48:52 +08:00
.qc {
color: #0067CF;
background-color: rgba(0, 103, 207, 0.10);
}
.bc {
color: #fff;
background: #0067CF;
}
2023-11-07 18:02:20 +08:00
.mycanvas {
margin: auto 0rpx;
2023-12-22 17:48:52 +08:00
background-color: #fbfbfb;
border: 1px dashed #eee;
2023-11-07 18:02:20 +08:00
}
.canvsborder {
border: 1rpx solid #333;
position: fixed;
top: 0;
left: 10000rpx;
}
</style>