uniapp 签名

<template>
    <view class="signature">
        <view class="wrap">
            <view class="sign-box">
                <canvas class="mycanvas" canvas-id="mycanvas" @touchstart="touchstart" @touchmove="touchmove"
                    @touchend="touchend" :style="{width:width +'px',height:height +'px'}">
                    <view class="title">
                        我已阅读全部文件
                        <text class="resinature" @click="handleReset">重签</text>
                    </view>
                    <view class="title2">
                        请在空白区域签写您的姓名:张明
                    </view>

                    <ksp-button class="next" :width="440" name="提交" @node-click="handleConfirm" />
                </canvas>

                <canvas canvas-id="camCacnvs" class="canvsborder"
                    :style="{width:height +'px',height:width +'px'}"></canvas>
            </view>
        </view>
    </view>
</template>

<script>
    let x = 20;
    let y = 20;
    let tempPoint = []; //用来存放当前画纸上的轨迹点
    let id = 0;
    let type = '';
    let that = '';
    let canvasw = '';
    let canvash = '';
    
    export default {
        onLoad(option) {
            that = this;
            id = option.id;
            type = option.type;

            this.ctx = uni.createCanvasContext('mycanvas', this); //创建绘图对象
            //设置画笔样式
            this.ctx.lineWidth = 4;
            this.ctx.lineCap = 'round';
            this.ctx.lineJoin = 'round';

            uni.getSystemInfo({
                success: res => {
                    this.width = 345;
                    this.height = 660;
                }
            });
        },
        data() {
            return {
                ctx: '', //绘图图像
                points: [], //路径点集合,
                width: 0,
                height: 0,
            };
        },
        methods: {
            //触摸开始,获取到起点
            touchstart(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(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() {
                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);
            },
            //清空画布
            handleReset() {
                console.log('handleReset');
                that.ctx.clearRect(0, 0, that.width, that.height);
                that.ctx.draw(true);
                tempPoint = [];
            },
            //将签名笔迹上传到服务器,并将返回来的地址存到本地
            handleConfirm() {
                if (tempPoint.length == 0) {
                    uni.showToast({
                        title: '请先签名',
                        icon: 'none',
                        duration: 2000
                    });
                    return;
                }
                uni.canvasToTempFilePath({
                    canvasId: 'mycanvas',
                    success(res) {
                        let tempPath = res.tempFilePath;
                        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();
                        setTimeout(() => {
                            //保存签名图片到本地
                            uni.canvasToTempFilePath({
                                    canvasId: 'camCacnvs',
                                    success: function(res) {
                                        //这是签名图片文件的本地临时地址
                                        const token = uni.getStorageSync('access_token')
                                        uni.showLoading({
                                            title: '上传中'
                                        });

                                        const fileOfBlob = base64toFile(res.tempFilePath,
                                            new Date() +
                                            ".png")

                                        uni.uploadFile({
                                            url: uni.$BaseURL +
                                                '/image/add',
                                            filePath: res.tempFilePath,
                                            name: 'file',
                                            header: {
                                                Authorization: 'Bearer ' + token
                                            },
                                            formData: {
                                                file: fileOfBlob
                                            },
                                            success(uploadFileRes) {
                                                uni.hideLoading();
                                                const url = JSON.parse(
                                                    uploadFileRes.data)
                                                if (url.retCode === '000000') {
                                                    that.$message({
                                                        type: 'success',
                                                        message: '上传成功'
                                                    })
                                                    const { data } = url
                                                }
                                            }
                                        });

                                    },
                                    fail: err => {
                                        console.log('fail', err);
                                    }
                                },
                                this
                            );
                        }, 200);
                    }
                });
            }
        }
    }
</script>

<style lang="scss">
    .signature {
        padding: 30rpx;

        .wrap {
            height: 1320rpx;
            width: 690rpx;
            position: relative;
        }

        .title {
            position: absolute;
            font-size: 30rpx;
            text-align: center;
            transform: rotate(90deg);
            right: -380rpx;
            top: 45%;

            .resinature {
                font-size: 30rpx;
                color: #2079EB;
                margin-left: 30rpx;
            }
        }

        .title2 {
            opacity: 0.5;
            font-size: 46rpx;
            color: #999999;
            text-align: center;
            transform: rotate(90deg);
            top: 45%;
            position: absolute;
            z-index: -1;
        }
    }

    .sign-box {
        border: 2rpx solid #D8D8D8;
        position: relative;
        margin: auto;
        display: flex;
        flex-direction: column;
        text-align: center;
        height: 100%;
        width: 100%;
    }

    .mycanvas {
        margin: auto 0rpx;
    }

    .canvsborder {
        border: 1rpx solid #333;
        position: fixed;
        top: 0;
        left: 10000rpx;
    }
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值