export default {
    /** 将平面树json转为带children的json
     * @param {Object} a
     * @param {Object} idStr
     * @param {Object} pidStr
     * @param {Object} childrenStr
     */
    transData(a, idStr, pidStr, childrenStr) {
        var r = [],
            hash = {},
            id = idStr,
            pid = pidStr,
            children = childrenStr,
            i = 0,
            j = 0,
            len = a.length;
        for (; i < len; i++) {
            hash[a[i][id]] = a[i];
        }
        for (; j < len; j++) {
            var aVal = a[j],
                hashVP = hash[aVal[pid]];
            if (hashVP) {
                !hashVP[children] && (hashVP[children] = []);
                hashVP[children].push(aVal);
            } else {
                r.push(aVal);
            }
        }
        return r;
    },
    /**从字典缓存中返回数组用于页面上作下拉，
     * @param {Object} k 字典名称
     * @param {Object} v value取哪个字典属性
     * @param {Object} t
     */
    arrByDic(k, v, t) {
        var map = uni.getStorageSync("dic");
        var arr = map[k];
        var r = [];
        arr.forEach(obj => {
            r.push({
                value: obj[v],
                text: obj[t]
            });
        })
        return r;
    },

    dateFmtyyyyMMddHHmmss(target) {
        let date = new Date(target);
        let y = date.getFullYear(); // 年
        let MM = date.getMonth() + 1; // 月
        MM = MM < 10 ? ('0' + MM) : MM;
        let d = date.getDate(); // 日
        d = d < 10 ? ('0' + d) : d;
        let h = date.getHours(); // 时
        h = h < 10 ? ('0' + h) : h;
        let m = date.getMinutes(); // 分
        m = m < 10 ? ('0' + m) : m;
        let s = date.getSeconds(); // 秒
        s = s < 10 ? ('0' + s) : s;
        return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s;
    },
    // 比较时间大小
    compareDate(date1, date2) {
        var oDate1 = new Date(date1);
        var oDate2 = new Date(date2);
        if (oDate1.getTime() > oDate2.getTime()) {
            return true; //第一个大
        } else {
            return false; //第二个大
        }
    },
    uploadPreview(url, lists, name) {
        var ext = url.substring(url.lastIndexOf(".") + 1);
        ext = ext.toLowerCase();
        console.log(ext);
        if (ext == "pdf") {
            uni.navigateTo({
                url: "/pages/pdf/index?url=" + url
            })
        }
    }

}