import {
  apiUrl
} from '../config/config.js'
import {
  showToast
} from '../utils/tips.js'
const api = require('../api/api.js')
const {
  imgUp
} = api.url
var app = getApp()

module.exports = {
  //图片上传
  uploadImg(up_type) {
    return new Promise((resolve, reject) => {
      wx.chooseImage({
        sizeType: ['original', 'compressed'], //可选择原图或压缩后的图片
        sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
        success: res => {
          const image = res.tempFilePaths[0];
          wx.uploadFile({
            url: apiUrl + imgUp,
            filePath: image,
            name: 'file',
            header: {},
            formData: {
              up_type: up_type,
              user_id: wx.getStorageSync("user_id"), 
              user_token:  wx.getStorageSync("user_token"),
              member_id: wx.getStorageSync("member_id")
            },
            success: function(res) {
              let data = JSON.parse(res.data)
              resolve(data)
            },
            fail: function(res) {
              showToast(res.data.message)
              reject(res)
            },
          })
        }
      })
    })
  }
}