Page({

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

  },

  //拍照后转base64图片
  async getTakePhoto() {
    let res = await this.takePhoto();
    let data = await this.readFile(res.tempImagePath, 'base64');
    this.setData({})
  },

  // 封装一个Promise的函数
  wxApiPromise(api, options) {
    return new Promise((resolve, reject) => {
      api({
        ...options,
        success: resolve,
        fail: reject
      });
    });
  },

  // 使用Promise封装wx.createCameraContext().takePhoto()
  takePhoto() {
    const ctx = wx.createCameraContext();
    return this.wxApiPromise(ctx.takePhoto.bind(ctx), { quality: 'high' });
  },

  // 使用Promise封装wx.getFileSystemManager().readFile()
  readFile(filePath, encoding) {
    const fsm = wx.getFileSystemManager();
    return this.wxApiPromise(fsm.readFile.bind(fsm), { filePath: filePath, encoding: encoding });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

})
Last Updated:
Contributors: pengrengui