// pages/beiyinmei/beiyinmei.js
import qrcode, {
  height
} from '../../utils/llqrcodefrankyin.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    src: 'https://res.wx.qq.com/wxdoc/dist/assets/img/draw-image.22401fa6.png',
    height: 0,
    width: 0,
    showPage:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.cameraContext = wx.createCameraContext()
    this.cameraContext.setZoom({
      zoom: 2
    })
    this.cameraFrame()
    // this.cameraFrameApi()
  },

  // 视频流（后端校验二维码）
  cameraFrameApi() {
    let flag = true
    const that = this
    const listener = this.cameraContext.onCameraFrame((frame) => {
      if (flag) {
        flag = false
        that.setData({
          width: frame.width,
          height: frame.height,
        })
        wx.canvasPutImageData({
          canvasId: 'myCanvas',
          width: frame.width,
          height: frame.height,
          data: new Uint8ClampedArray(frame.data),
          success(rew) {
            console.log(rew, 99);
            wx.canvasToTempFilePath({
              canvasId: 'myCanvas',
              x: frame.width / 2 - 150,
              y: frame.height / 2 - 150,
              width: 300,
              height: 300,
              destWidth: 300,
              destHeight: 300,
              quality: 'high',
              fileType: 'jpg',
              success(path) {
                console.log(path.tempFilePath, 66);
                const base = wx.getFileSystemManager().readFileSync(path.tempFilePath, 'base64')
                that.setData({
                  src: 'data:image/jpeg;base64,' + base
                })
                wx.request({
                  url: 'https://ai.tj5.cc:35437/object/',
                  method: "POST",
                  data: {
                    base
                  },
                  success(g) {
                    console.log(g, 'cccc');
                    flag = true
                  }
                })
              },
              fail(rr) {
                console.log(rr, 5555);
              }
            })
          },
          fail(err) {
            console.log(err);
          }
        })
      }
    })
    listener.start()
  },

  // 视频流(前端判断二维码)
  cameraFrame() {
    let flag = true
    const that = this
    const listener = this.cameraContext.onCameraFrame((frame) => {
      if (flag) {
        flag = false
        const data = new Uint8ClampedArray(frame.data);
        const d = qrcode.decodefrank(frame.width, frame.height, data)
        if (d) {
          this.cameraContext.takePhoto({
            quality: 'high',
            selfieMirror: false,
            success(res) {
              // flag = true
              const base = wx.getFileSystemManager().readFileSync(res.tempImagePath, 'base64')
              wx.request({
                url: 'https://www.7856.work:4433/api/Bym_classfily_post/',
                method: 'POST',
                data: {
                  device_id: '50',
                  base: base
                },
                success(rs) {
                  // console.log(rs,9999,rs.data.result,555);
                  // wx.showToast({
                  //   title: 'dddd过',
                  // })
                  if (rs.data.result == 2) {
                    wx.showToast({
                      title: '通过',
                      icon:'success'
                    })
                    that.setData({
                      showPage:true
                    })
                  } else {
                    wx.showToast({
                      title: '未通过',
                      icon:'error'
                    })
                    flag = true
                  }
                },
                fail(e) {
                  console.log(e);
                }
              })
            }
          })
        } else {
          flag = true
        }
      }
    })
    listener.start()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})