fullSignture.js 5.96 KB
import { commonApi, equityApi } from '../../../api/api'
Page({
  data: {
    tempFilePath: '',
    hideModal: true,
    hasDraw: false,
    canvasName: '#handWriting',
    ctx: '',
    canvasWidth: 0,
    canvasHeight: 0,
    startPoint: {
      x: 0,
      y: 0,
    },
    selectColor: 'black',
    lineColor: '#282829', // 颜色
    lineSize: 3, // 笔记倍数
    radius: 1.5, //画圆的半径
    showPlaceHoder: true,
    signImage: '',
    showSuccessMask:false,
    maskDate:''
  },
  onLoad(options) {
    if(options.writeOffCode){
      this.writeOffCode = options.writeOffCode
      this.httpInfo = JSON.parse(decodeURIComponent(options.http))
      this.contractNo = options.contractNo
      this.merchantName = options.merchantName
    }
    this.getSettingDay()
    let that = this
    let query = wx.createSelectorQuery().in(this); //获取自定义组件的SelectQuery对象
    this.canvasCtx = wx.createCanvasContext('signature', that)
    // 设置线的样式
    let { windowWidth, windowHeight } = wx.getSystemInfoSync()
    this.canvasCtx.setFillStyle("white");
    this.canvasCtx.fillRect(0, 0, windowWidth, windowHeight);
    this.canvasCtx.setLineCap("round");
    this.canvasCtx.setLineJoin("round");
    // 初始化颜色
    this.canvasCtx.setStrokeStyle(that.data.selectColor);
    query.select('.modal-canvas').boundingClientRect(rect => {
      this.setData({
        canvasWidth: rect.width,
        canvasHeight: rect.height,
      });
    }).exec();
  },
  getSettingDay() {
    equityApi.getSettingDay({}).then(res => {
        this.setData({
            settingDay: res || 0
        })
    })
  },
  scaleStart(event) {
    if (event.type != 'touchstart') return false;
    let currentPoint = {
      x: event.touches[0].x,
      y: event.touches[0].y
    }
    this.drawCircle(currentPoint);
    this.setData({
      startPoint: currentPoint,
      hasDraw: true,
      showPlaceHoder: false
    });
  },
  scaleMove(event) {
    if (event.type != "touchmove") return false;

    let {
      startPoint
    } = this.data
    let currentPoint = {
      x: event.touches[0].x,
      y: event.touches[0].y
    }

    this.drawLine(startPoint, currentPoint)
    this.setData({
      startPoint: currentPoint
    })
  },
  scaleEnd(e) {
    this.setData({
      isStart: true
    })
  },
  drawCircle(point) { //这里负责点
    let ctx = this.canvasCtx;
    ctx.beginPath();
    // ctx.fillStyle = this.data.lineColor;
    ctx.setFillStyle(this.data.lineColor)
    //笔迹粗细由圆的大小决定
    ctx.arc(point.x, point.y, this.data.radius, 0, 2 * Math.PI);
    ctx.fill();
    ctx.closePath();
    ctx.draw(true)
  },
  drawLine(sourcePoint, targetPoint) {
    let ctx = this.canvasCtx;
    this.drawCircle(targetPoint);
    ctx.beginPath();
    // ctx.strokeStyle = this.data.lineColor;
    ctx.setStrokeStyle(this.data.lineColor)
    // ctx.lineWidth = this.data.radius * 2; //这里乘2是因为线条的粗细要和圆的直径相等
    ctx.setLineWidth(this.data.radius * 2)
    ctx.moveTo(sourcePoint.x, sourcePoint.y);
    ctx.lineTo(targetPoint.x, targetPoint.y);
    ctx.stroke();
    ctx.closePath();
  },
  clearCanvas() { //清空画布
    let ctx = this.canvasCtx;
    ctx.clearRect(0, 0, this.data.canvasWidth, this.data.canvasHeight);
    ctx.fillStyle = 'rgba(0, 0, 0, .1)';
    ctx.draw()
    this.setData({
      hasDraw: false, //未签字
      showPlaceHoder: true,
      signImage: ''
    })
  },
  getTempFile() {
    return {
      tempFilePath: this.data.tempFilePath
    }
  },
  saveToImage() {
    let {
      hasDraw,
      canvasHeight,
      canvasWidth
    } = this.data
    let that = this
    if (!hasDraw) {
      wx.showToast({
        title: '请签字确认',
        icon: 'none',
        mask: true
      })
      return
    }
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: canvasWidth,
      height: canvasHeight,
      canvasId: 'signature',
      fileType:'jpg',
      success(res) {
        if (res.tempFilePath) {
          that.toUploadImage(res.tempFilePath)
        }
      },
      fail(err) {
        console.log(err);
      }
    }, that)
  },
  toBack() {
    wx.navigateBack({
      delta: 1
    });
  },
  // 上传图片
  toUploadImage(tempFilePaths) {
    let _this = this
    const tempFiles = tempFilePaths
    let imgType = tempFiles.split('.')[1]
    let type = imgType == 'unknown' ? 'jpg' : imgType
    wx.getFileSystemManager().readFile({
      filePath: tempFiles, //选择图片返回的相对路径
      encoding: 'base64', //编码格式
      success(_res) {
        wx.$util.showLoading('上传中...')
        commonApi.uploadImg({
          file: `${type}@${_res.data}`
        }).then(img => {
          _this.saveToHttp(img)
        })
      },
      fail() {
        wx.$util.hideLoading()
      }
    })
  },
  // 上传签名到后端
  saveToHttp(signUrl) {
    const fromDetail = this.fromDetail
    const  writeOffCode  = this.writeOffCode
    if(fromDetail) {
        wx.$util.showLoading('提交中')
        equityApi.signSumbit({signUrl,writeOffCode }).then(res => {
            wx.hideLoading();
            if(res) {
                this.maskQuery = {writeOffCode}
                this.toConfirmOK()
            }
        })
        return
    }
    let para = {
      // writeOffCode: this.writeOffCode,
      signUrl,
      ...this.httpInfo
    }
    wx.$util.showLoading('提交中')
    equityApi.claimsSubmit(para).then(res => {
      wx.hideLoading();
      if (res) {
        this.maskQuery = para
        const currentDate = new Date()
        const showMaskDate = currentDate.setDate(currentDate.getDate() + Number(this.data.settingDay));
        this.setData({
          showSuccessMask:true,
          maskDate:new Date(showMaskDate).format('yyyy年MM月dd日'),
          maskMerchant:this.merchantName
        })
       
      }
    })
  },
  toConfirmOK(){
    wx.reLaunch({
      url: '/packageEquity/pages/claimsNote/claimsNote?formPage=apply&hxcode=' + this.maskQuery.writeOffCode + `&contractNo=${this.contractNo}`,
    });
  }
})