public.js 12.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
// var baseurl = 'http://kindapp.w.bronet.cn/api/';//测试地址
var baseurl='http://wx.cijievip.com/api/' //正式地址
var imgurl = 'http://wx.cijievip.com/';
var debug=true;
// 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(data) {

    for(var item in data.pageParam){
        localStorage.setItem(item,data.pageParam[item])
    }
    window.location.href = data.url
}

function openView(fmName, fmUrl, winTitle, winName, winUrl, fmParams, winParams) {
    if(fmParams){
        for (var item in fmParams) {
            console.log(item)
            console.log(fmParams[item])
            localStorage.setItem(item,fmParams[item])
        }
    }
    if(winParams){
        for (var item in winParams) {
            console.log(item)
            console.log(winParams[item])
            localStorage.setItem(item,winParams[item])
        }
    }
    window.location.href = fmUrl+'.html'
    // 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) {
    window.location.href = document.referrer;
    window.history.back(-1);
}

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('')
    }
    setTimeout(function(){

    })
}

// 双击退出
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 (localStorage.getItem('token')) {
        return localStorage.getItem('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 (localStorage.getItem('city')) {
        return localStorage.getItem('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 formatTen(num) {
    return num > 9 ? (num + "") : ("0" + num);
}

function formatDate(date, type) {
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    if (type == 1) {
        return year + "-" + formatTen(month) + "-" + formatTen(day)
    } else {
        return year + "-" + formatTen(month) + "-" + formatTen(day) + ' ' + formatTen(hour) + ':' + formatTen(minute)
    }
}

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 || dates > 171) {
                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();
    if ((date.getHours() + num) > 24) {
        h = h < 10 ? ('0' + (date.getHours() + num) % 24) : ((date.getHours() + num) % 24);
        if (D < 30) {
            D = parseInt(D) + Math.floor((date.getHours() + num) / 24)
        } else {
            D = '0' + 1;
            M = M < 10 ? '0' + (parseInt(M) + 1) : parseInt(M) + 1;
        }
    }
    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) {
            if (res.data.code == '502') {
                setTimeout(function(){
                    window.location.href = '../../html/login/login_index.html'
                }, 2000);
                // 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; //返回参数值
}

/**
 * 跳转url转义
 * @param str
 * @returns {void|string|XML}
 */
function escape2Html(str) {
    var arrEntities = {'lt': '<', 'gt': '>', 'nbsp': ' ', 'amp': '&', 'quot': '"'};
    return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function (all, t) {
        return arrEntities[t];
    });
}