/* 常用工具函数 */
export const msg = (title, duration = 1500, mask = true, icon = 'none') => {
    //统一提示方便全局修改
    if (Boolean(title) === false) { return }
    uni.showToast({
        title,
        duration,
        mask,
        icon
    })
}
export const modal = (title, content, confirmText, cancelText, showCancel = false, callBack) => {
    uni.showModal({
        title,
        content,
        confirmText,
        cancelText,
        confirmColor: '#F3B574',
        showCancel,
        success: function(res) {
            if (res.confirm) {
                callBack()
            }
        }
    })
}
export const json = type => {
    //模拟异步请求数据
    return new Promise(resolve => {
        setTimeout(() => {
            // console.log(resolve+'======='+type);
            resolve(Json[type])
        }, 500)
    })
}

export const prePage = () => {
    let pages = getCurrentPages()
    let prePage = pages[pages.length - 2]
    // #ifdef H5
    return prePage
    // #endif
    return prePage.$vm
}

// 获取节点元素信息
export const boundingClientRect = (component, selector, flag = false) => {
    return new Promise((resolve, reject) => {
        const query = uni.createSelectorQuery().in(component)
        if (!flag) {
            query
                .select(selector)
                .boundingClientRect(data => {
                    resolve(data)
                })
                .exec()
        } else {
            query
                .selectAll(selector)
                .boundingClientRect(data => {
                    resolve(data)
                })
                .exec()
        }
    })
}

/** 
 * @description 时间日期格式化
 * @param {dateTime} date 标准时间格式 -> new Date()
 * @param {string} format 时间格式化的格式 'yyyy-MM-dd hh:mm:ss'
 * @returns {string} 格式化后的时间  '2017-01-01 01:00:00'
 */
export function dateFormat(date = new Date(), format = 'yyyy-MM-dd hh:mm:ss') {
    var o = {
        'M+': date.getMonth() + 1, // month
        'd+': date.getDate(), // day
        'h+': date.getHours(), // hour
        'm+': date.getMinutes(), // minute
        's+': date.getSeconds(), // second
        'q+': Math.floor((date.getMonth() + 3) / 3), // quarter
        S: date.getMilliseconds(), // millisecond
    }
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    for (var k in o) {
        if (new RegExp('(' + k + ')').test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k])
                .length))
        }
    }
    return format
}