let tools = {}
/**
 * 大小判断
 * @param a
 * @param b
 * @returns {number}
 */
tools.sortNumber = function (a, b) {
    return a - b;
}


/**
 * 保留两位小数
 * @param num
 * @returns {string}
 */
tools.twoFloating = function (num) {
    // 获取两位小数
    let price = "";
    price = num * 1;
    price = String(price).split(".")[1];
    if (price !== undefined && price.length === 1) {
        price = `.${price}0`;
    } else {
        price === undefined ? (price = ".00") : (price = `.${price}`);
    }
    return price;
}
tools.formatDecimal = function (num, decimal) {
    num = num.toString()
    let index = num.indexOf('.')
    if (index !== -1) {
        num = num.substring(0, decimal + index + 1)
    } else {
        num = num.substring(0)
    }
    return parseFloat(num).toFixed(decimal)
}

/**
 * 错误提示
 * @param msg
 */
tools.error = function (msg) {
    uni.showToast({
        'title': msg,
        'icon': 'error',
        'mask': true,
        'duration': 1500
    })
}

/**
 * 成功提示
 * @param msg
 */
tools.success = function (msg) {
    uni.showToast({
        'title': msg,
        'icon': 'success',
        'mask': true,
        'duration': 1500
    })
}

/**
 * 显示Loading
 */
tools.showLoading = function () {
    uni.showLoading({
        title: '加载中...',
        mask: true
    });
}

/**
 * 关闭Loading
 */
tools.hideLoading = function () {
    uni.hideLoading();
}

/**
 * 获取时间戳（毫秒）
 * @returns {number}
 */
tools.getTime = function () {
    return new Date().getTime();
}


tools.updateVersion = function (sysVersion, appUrl) {
    let app_version = plus.runtime.version;
    console.log('版本号信息对比------------------------------' + app_version + '---------' + sysVersion)
    console.log(app_version < sysVersion)
    if (app_version < sysVersion) {
        uni.showLoading({
            title: '更新中……'
        })
        uni.downloadFile({//执行下载
            url: appUrl, //下载地址
            success: (downloadResult) => {//下载成功
                uni.hideLoading();
                if (downloadResult.statusCode === 200) {
                    uni.showModal({
                        title: '',
                        content: '更新成功，确定现在重启吗？',
                        confirmText: '重启',
                        confirmColor: '#EE8F57',
                        success: function (res) {
                            if (res.confirm === true) {
                                plus.runtime.install(//安装
                                    downloadResult.tempFilePath, {
                                    force: true
                                },
                                    function (res) {
                                        tools.success('更新成功，重启中')
                                        plus.runtime.restart();
                                    }
                                );
                            }
                        }
                    });
                }
            }
        });
    } else {
        // tools.success('你已是最新版本')
    }
}

/**
 * uniapp  html图片显示控制
 * @param str
 * @returns {*}
 */
tools.imgDeal = function (str) {
    console.log(str)
    if(str===null || str===undefined){
        return '';
    }else {
        return str.replace(/\<img/gi, '<img style="width:100%;height:auto;display:block;"');
    }

}


export default tools
