public.js 10.4 KB
var baseurl = 'http://kindapp.w.bronet.cn/api/';//测试地址
var imgurl='http://kindapp.w.bronet.cn/'
// var baseurl = 'http://192.168.1.3/api/';//本地测试
var mobileReg = /^1(3[0-9]|4[57]|5[0-35-9]|7[0135678]|8[0-9])\d{8}$/; //手机正则
var id_card = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;//身份证正则


function openWin(winName, url) {
    api.openWin({
        name: winName,
        url: url
    });
}

function openView(fmName, fmUrl, winTitle, winName, winUrl, fmParams, winParams) {
    var options = {
        name: '',
        url: '',
        pageParam: {
            winTitle: '',
            win: {},
            fmName: '',
            fmUrl: '',
            fm: {},
        },
        slidBackEnabled: false
    };
    options.pageParam.fmName = fmName;
    options.pageParam.fmUrl = 'widget://html/' + fmUrl + '.html';
    if (winName) {
        options.name = winName;
    } else {
        options.name = fmName;
    }
    if (winUrl) {
        options.url = 'widget://html/' + winUrl + '.html';
    } else {
        options.url = 'widget://html/common/win/win.html';
    }
    options.pageParam.winTitle = winTitle || '';
    options.pageParam.winTitle = winTitle;

    if (winParams != undefined && winParams != null && winParams != {} && winParams != 'undefined') {
        options.pageParam.win = JSON.stringify(winParams);
    }
    if (fmParams != undefined && fmParams != null && fmParams != {} && fmParams != 'undefined') {
        options.pageParam.fm = JSON.stringify(fmParams);
    }
    api.openWin(options);
}

function closeWindow(winName) {
    if (winName) {
        api.closeWin({
            name: winName,
            animation: "reveal"
        });
    } else {
        api.closeWin({
            animation: "reveal"
        });
    }
}

function getHeight(id) {
    return document.getElementById(id).offsetHeight;
}


function openFm(fmName, url) {
    var posY = $api.offset($api.dom("header")).h;
    var fmUrl = url ? url : fmName + "_fm.html";
    api.openFrame({
        name: fmName,
        url: fmUrl,
        rect: {
            x: 0,
            y: posY
        },
        bounces: true,
        hScrollBarEnabled: false,
        vScrollBarEnabled: false
    });
}

function toastMsg(msg) {
    if (msg) {
        weui.toast(msg, '')
    } else {
        weui.toast('')
    }
}

// 双击退出
function exit_app() {
    var appId = api.appId;
    api.addEventListener({
        name: 'keyback'
    }, function (ret, err) {
        api.toast({
            msg: '再按一次返回键退出' + api.appName,
            duration: 2000,
            location: 'bottom'
        });
        api.addEventListener({
            name: 'keyback'
        }, function (ret, err) {
            api.closeWidget({
                id: appId,     //应用ID
                retData: {name: 'closeWidget'},
                silent: true
            });
        });
    });
}

function fixStatusBar(t) {
    var e = api.systemType;
    if ("ios" == e) fixIos7Bar(t);
    else if ("android" == e) {
        var a = api.systemVersion;
        a = parseFloat(a), a >= 4.4 && (t.style.paddingTop = "1.36rem", t.style.height = "3.56rem", t.style.lineHeight = "2.2rem");
    }
}

function fixIos7Bar(t) {
    var e = api.systemType;
    if ("ios" == e) {
        var a = api.systemVersion,
            n = parseInt(a, 10),
            i = api.fullScreen,
            o = api.iOS7StatusBarAppearance;
        n >= 7 && !i && o && (t.style.paddingTop = "1rem", t.style.height = "3.2rem")
    }
}

function getToken() {
    if ($api.getStorage('token')) {
        return $api.getStorage('token')
    } else {
        return null;
    }
}


function isLogin(_this, callback) {
    Vue.http.options.emulateJSON = true;
    var logpost = {
        cmd: 'u_checkTokenIsRight',
        token: getToken()
    };
    _this.$http.post(baseUrl + 'Api/Init/', logpost).then(function (res) {
        if (res.data.code == 1) {
            callback(true);
        } else {
            callback(false);
        }
    }, function () {
        toastMsg('网络小短腿跑不动了...');
        api.refreshHeaderLoadDone();
        // api.addEventListener({
        //   name:'loadfailed'
        // });
        losenet(function () {
            location.reload();
        });
    });
}


function loadEnd() {
    var ele = document.getElementById('loadStart');
    if (ele) {
        ele.style.opacity = 0;
        setTimeout(function () {
            ele.style.display = "none";
        }, 1000);
    }
}


function getcity() {
    if ($api.getStorage('city')) {
        return $api.getStorage('city')
    } else {
        return null;
    }
}

//content转化为html
function charToHtml(str) {
    if (str) {
        str = str.replace(/&/g, '&');
        str = str.replace(/&lt;/g, '<');
        str = str.replace(/&gt;/g, '>');
        str = str.replace(/&quot;/g, "'");
        str = str.replace(/&#039;/g, ' ');
    }
    return str;
}

//我的-头像显示
refresh = function (callback) {
    api.setRefreshHeaderInfo({
        bgColor: '#eee',
        textColor: '#000',
        textDown: '下拉刷新...',
        textUp: '松开刷新...'
    }, function () {
        callback();
    });
};

refreshDone = function (mess) {
    if (mess) {
        toastMsg(mess);
    }
    setTimeout(function () {
        api.refreshHeaderLoadDone();
    }, 500)
};



function add0(num) {
    if (num) {
        num = parseInt(num);
        if (num >= 10) {
            return num
        }
        else {
            return '0' + num
        }
    } else {
        return "00"
    }
}

function timestampToTime(timestamp) {
    var date = new Date(timestamp * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() + ' ';
    var h = date.getHours() + ':';
    var m = date.getMinutes();
    var s = date.getSeconds();
    return Y + M + D + h + m;
}

function checkDate(date, type) {
    if (date == '') {
        return false;
    } else {
        var day = new Date();
        var da = new Date(date.replace(/-/g, '/'));
        var startDate = day.getTime();
        var endDate = da.getTime();
        var ms = endDate - startDate
        var dates = Math.floor(ms / 1000 / 60 / 60);
        // alert(dates)
        if (type == 1) {
            if (dates < 3) {
                return false;
            } else {
                return true;
            }
        } else if (type == 3) {
            if (dates < 72) {
                return false;
            } else {
                return true;
            }
        }
        else {
            if (startDate < endDate) {
                return false;
            } else {
                return true;
            }
        }

    }
}

function getTimeDetil(num) {
    var date = new Date();
    var Y, M, D, h, m, s;
    Y = date.getFullYear() + '-';
    M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    D = date.getDate() + ' ';
    h = date.getHours() + num + ':';
    m = date.getMinutes();
    s = date.getSeconds();
    return Y + M + D + h + m
}

function toDecimal2(x) {
    var f = parseFloat(x);
    if (isNaN(f)) {
        return false;
    }
    var f = Math.round(x * 100) / 100;
    var s = f.toString();
    var rs = s.indexOf('.');
    if (rs < 0) {
        rs = s.length;
        s += '.';
    }
    while (s.length <= rs + 2) {
        s += '0';
    }
    return s;
}

function toWan(str) {
    var str = parseFloat(str);
    if (str > 9999) {
        return (str / 1e4) + '万'
    } else {
        return str
    }
}

function getRequest(type, url, params, header) {
    var promise = new Promise(function (resolve, reject) {
        axios({
            url: baseurl + url,
            method: type,
            params: params,
            headers: header,
        }).then(function (res) {
            // alert(JSON.stringify(res))
            if (res.data.code == 502) {
                // openView('login_index', 'login/login_index', '登录', 'login_index', false, false, false)
            } else {
                resolve(res)
            }
            loadEnd();
            // resolve(res)
        }).catch(function (err) {
            toastMsg('网络错误');
            openView('wrong', 'common/wrong', '网络错误', 'wrong', false, false, false);
            alert(JSON.stringify(err))
            reject(err)
        })
    });
    return promise
}

//设备判断
function getDevice() {
    var login_device = '';
    var sUserAgent = navigator.userAgent.toLowerCase();
    var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
    var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
    var bIsMidp = sUserAgent.match(/midp/i) == "midp";
    var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
    var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
    var bIsAndroid = sUserAgent.match(/android/i) == "android";
    var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
    var wx = sUserAgent.match(/MicroMessenger/i) == "micromessenger"
    var mac = sUserAgent.match(/macintosh|mac os x/i) == "mac"
    var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
    if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
        var UA = window.navigator.userAgent;

        isAndorid = /android/i.test(UA),
            isIphone = /iphone/i.test(UA),
            //isIphone = /(?:iPhone)/.test(UA),
            isPad = /ipad/i.test(UA),
            isDolphin = typeof dolphin !== 'undefined';
        device = isAndorid ? 'android' : isIphone ? 'iphone' : isPad ? 'ipad' : mac ? 'mac' : 'mobile';
        switch (device) {
            case 'iphone':
                login_device = device;
                break;
            case 'android':
                login_device = device;
                break;
            case 'ipad':
                login_device = device;
                break;
            default:
                login_device = device;
                break;
        }
    } else {
        pc_device = wx ? 'wx' : mac ? 'mac' : 'pc'
        login_device = pc_device;
    }
    return login_device
}

function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]);
    return null; //返回参数值
}