const config = require('../config.js');
const app = getApp();

function POST(url, params, boo) {
  if (arguments.length == 3) {
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
  }
  let promise = new Promise(function (resolve, reject) {
    wx.request({
      url: `${config.host}${url}`,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: params,
      method: 'POST',
      success: function (res) {
        if (arguments.length == 3) {
          wx.hideLoading();
        }
        if (res.data.code === 1) {
          resolve(res.data);
        } else {
          reject(res.data);
        }
      },
      fail: function (err) {
        if (arguments.length == 3) {
          wx.hideLoading();
        }
        wx.showToast({
          title: err.errMsg,
          icon: 'none',
          duration: 2000
        });
      }
    });
  });
  return promise
}

function GET(url, obj, boo) {
  if (arguments.length == 3) {
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
  }
  let promise = new Promise(function (resolve, reject) {
    wx.request({
      url: `${config.host}${url}`,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        "xx-token": wx.getStorageSync("token") ? wx.getStorageSync("token") : ""
      },
      method: 'GET',
      data: obj,
      success: function (res) {
        if (arguments.length == 3) {
          wx.hideLoading();
        }
        if (res.data.code === 1) {
          resolve(res.data);
        } else {
          reject(res.data);
        }
      },
      fail: function (err) {
        if (arguments.length == 3) {
          wx.hideLoading();
        }
        wx.showToast({
          title: err.errMsg,
          icon: 'none',
          duration: 2000
        });
      }
    })
  });
  return promise
}

function showToast(msg, fn) {
  let args = arguments.length;
  wx.showToast({
    title: msg,
    icon: "none",
    duration: 1500,
    mask: true,
    success: function () {
      if (args == 2) {
        setTimeout(function () {
          fn();
        }, 1500)
      }
    }
  })
}
//显示提示框
function showmodel(text) {
  wx.showModal({
    title: '提示',
    content: text,
    showCancel: false
  })
}
//将秒数变成想要的日期格式
function format(time, format) {
  var t = new Date(time);
  var tf = function (i) {
    return (i < 10 ? '0' : '') + i
  };
  return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function (a) {
    switch (a) {
      case 'yyyy':
        return tf(t.getFullYear());
        break;
      case 'MM':
        return tf(t.getMonth() + 1);
        break;
      case 'mm':
        return tf(t.getMinutes());
        break;
      case 'dd':
        return tf(t.getDate());
        break;
      case 'HH':
        return tf(t.getHours());
        break;
      case 'ss':
        return tf(t.getSeconds());
        break;
    }
  })
}

function ChooseImage(num) {
  var promise = new Promise(function (resolve, reject) {
    wx.chooseImage({
      count: num,
      success: function (res) {
        resolve(res.tempFilePaths);
      },
    })
  });
  return promise;
}

function upload(num, fn) {
  ChooseImage(num).then(res => {
    wx.showLoading({
      title: '',
    });
    if (res.length != 0) {
      let promiseList = [];
      for (let value of res) {
        let promise = new Promise((resolve, reject) => {
          wx.uploadFile({
            url: `${config.host}common/upWxappPic`,
            filePath: value,
            name: 'file',
            success: function (res) {
              var data = JSON.parse(res.data);
              if (data.code == 1) {
                resolve(data.data);
              }
            },
            fail: function (err) {
              wx.hideLoading();
              console.log(err);
            }
          });
        });
        promiseList.push(promise);
      }
      Promise.all(promiseList).then(res => {
        fn(res);
      })
    }
  });
}
//预览图片
function previewImage(one, urls) {
  wx.previewImage({
    current: one,
    urls: urls,
  });
}
module.exports = {
  post: POST,
  get: GET,
  ChooseImage: ChooseImage,
  showmodel: showmodel,
  upload: upload,
  format: format,
  showToast: showToast,
  previewImage: previewImage
}