// pages/photo/index.js
const app = getApp();
Component({
  lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行
      this.ctx = wx.createCameraContext()
      console.log(wx.createCameraContext());
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  },

  /**
   * 页面的初始数据
   */
  data: {
    result: []

  },
  methods: {
    takePhoto() {
      if (this.data.result.length > 0) {
        this.setData({
          result: [],
          src: ''
        });
        return;
      }
      console.log(4444);
      this.ctx.takePhoto({
        quality: 'high',
        success: (res) => {
          console.log(res);
          this.setData({
            src: res.tempImagePath
          })
            var FSM = wx.getFileSystemManager();
            FSM.readFile({
              filePath: res.tempImagePath,
              encoding: "base64",
              success:(data) => {
                console.log(data);
                // 调用接口

                this.postGarbage(data.data);
              }
            });

          //console.log(this.data);
        }
      })
  },

  postGarbage(imageBase64) {
   
   let loading = wx.showLoading({
      title: '识别中...',
    })
    wx.request({
      url: app.data.api.garbage,
      dataType: 'json',
      method: 'post',
      data: {
        image: imageBase64
      },
      success: (response) => {
          wx.hideLoading({
            success: (res) => {
            },
          })
        
          if(response.statusCode == 200){
            console.log(response.data.data);
            // name data code
            this.setData({
              result:response.data.data
            });
          }
      }
    });

  }
  
  },
})