const formatTime = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
let imgs = []
const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
}
function imageUtil(e) {
    var imageSize = {};
    var originalWidth = e.detail.width;//图片原始宽 
    var originalHeight = e.detail.height;//图片原始高 
    var originalScale = originalHeight / originalWidth;//图片高宽比 
    //获取屏幕宽高
    wx.getSystemInfo({
        success: function (res) {
            var windowWidth = res.windowWidth;
            var windowHeight = res.windowHeight;
            var windowscale = windowHeight / windowWidth;//屏幕高宽比 
            if (originalScale < windowscale) {//图片高宽比小于屏幕高宽比
                //图片缩放后的宽为屏幕宽 
                imageSize.imageWidth = windowWidth;
                imageSize.imageHeight = (windowWidth * originalHeight) / originalWidth;
            } else {//图片高宽比大于屏幕高宽比 
                //图片缩放后的高为屏幕高 
                imageSize.imageHeight = windowHeight;
                imageSize.imageWidth = (windowHeight * originalWidth) / originalHeight;
            }

        }
    })
    return imageSize;
}

function getSearchMusic(keyword, pageindex, callback) {
    wx.request({
        url: 'https://leemoral.wx.bronet.cn/index.php/Api/Index/ajax_goods_search',
        data: {
            p: pageindex,
            keyword: keyword,
        },
        method: 'POST',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: function (res) {
            if (res.data.state == 'success') {
                callback(res.data);
            }
        }
    })
}

//多张图片上传
function uploadimg(data) {
    var that = this,
        i = data.i ? data.i : 0,//当前上传的哪张图片
        success = data.success ? data.success : 0,//上传成功的个数
        fail = data.fail ? data.fail : 0;//上传失败的个数
    wx.uploadFile({
        url: data.url,
        filePath: data.path[i],
        name: 'fileinfo',//这里根据自己的实际情况改
        formData: null,
        success: (resp) => {
            success++;//图片上传成功,图片上传成功的变量+1
            var data = JSON.parse(resp.data);
            imgs.push(data.fileinfo)
            //这里可能有BUG,失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时,这里的success才+1
        },
        fail: (res) => {
            fail++;//图片上传失败,图片上传失败的变量+1
        },
        complete: () => {
            i++;//这个图片执行完上传后,开始上传下一张
            if (i == data.path.length) {   //当图片传完时,停止调用          
            } else {//若图片还没有传完,则继续调用函数
                data.i = i;
                data.success = success;
                data.fail = fail;
                that.uploadimg(data);
            }
        }
    });
}
function isEmptyObject(e) {
    var t;
    for (t in e)
        return !1;
    return !0
}
module.exports = {
    formatTime: formatTime,
    imageUtil: imageUtil,
    getSearchMusic: getSearchMusic,
    uploadimg: uploadimg,
    imgs: imgs,
    isEmptyObject: isEmptyObject
}