util.js 3.54 KB

(function(){
    'use strict';

    let utils=function(){}
    let blackId = ''
    let origin = window.location.origin
    if(origin.includes('localhost')>0 || origin=='null' || origin.includes('file:')>0){
        origin = 'https://testznzl.lgyzpt.com'
    }
    utils.prototype={
        /**
         * 获取地址栏后边的参数
         */
        getUrlParam:function(m) {
            let reg = new RegExp('(^|&)' + m + '=([^&]*)(&|$)')
            let r = window.location.href.split('?')
            r = r.length === 1 ? null : r[1].match(reg)
            if (r == null) return ''
            return decodeURIComponent(r[2]).replace(/(#|wechat_redirect|\/[0-9a-zA-Z]*)*/g, '')
        },
        /**
         * 判断设备是Android还是iPhone
         */
        androidOrIphone: function () {
            var system = window.navigator.userAgent.toLowerCase();
            if (system.indexOf("iphone") >= 0 || system.indexOf("ipad") >= 0 || system.indexOf("mac") >= 0 || system.indexOf("ipod") >= 0) {
                return false;
            }

            return true;
        },
         /**
         * 黑框提示
         */ 
        toast: function (str) {
            if (document.getElementById("blackDiv")) {
                document.getElementById("blackSpan").innerHTML = str;
            } else {
                var blackDiv = document.createElement("div");

                blackDiv.id = "blackDiv";
                blackDiv.className = "blackts";
                blackDiv.style.position = "fixed";
                blackDiv.style.left = "0";
                blackDiv.style.bottom = "20%";
                blackDiv.style.width = "100%";
                blackDiv.style.textAlign = "center";
                blackDiv.style.zIndex = "999999";
                blackDiv.style.display = 'flex'
                blackDiv.style.justifyContent = 'center'

                var html = '<div id="blackSpan" style="background: rgba(42,45,50,.94);color: white;';
                html += 'border-radius: .1rem;font-size: 0.32rem;padding: .2rem .6rem;max-width: 80%;line-height: 1.5;">';
                html += str + '</div>';
                blackDiv.innerHTML = html;

                document.getElementsByTagName("body")[0].appendChild(blackDiv);
            }

            if (blackId && blackId != "") {
                clearTimeout(blackId);
            }
            blackId = setTimeout(function () {
                if (document.getElementById("blackDiv")) {
                    document.getElementsByTagName("body")[0].removeChild(document.getElementById("blackDiv"));
                }
            }, 3000);
        },
        /**
         * zhijian/ha
         * 
         */ 
        httpRequest:function(param){
            let middle = param.middleUrl || '/zhijian-trial/ha'
            let pa = localStorage.getItem('appLoginInfo')
            if(pa){
                pa = JSON.parse(pa)
            }else{
                pa = {}
            }

            return new Promise(function(resolve, reject){
                axios ({
                    method: 'post',
                    url: origin + middle + param.url,
                    timeout: param.time||15000,
                    data:param.data,
                    headers: {
                        'Content-Type': 'application/json',
                        'x-access-token': pa.tokenValue
                    }
                }).then((res) => {
                    resolve(res.data)
                }).catch(res => {
                    reject(res)
                })
            })
        },
    }
    window.publicMethod = utils
})()