index.js 7.99 KB
var common = function() {
    return {
        // httpUrl: 'https://zlyx.shunzhi.net/', //正式
        httpUrl: 'https://ocp.sxsedu.net/sxyx/api/', //正式
        // httpUrl:'http://yanxue.shunzhi.net:8037/', //测试
        //存cookie
        setCookie: function(name, value) {
            //设置名称为name,值为value的Cookie
            var expdate = new Date(); //初始化时间
            expdate.setTime(expdate.getTime() + 30 * 60 * 1000); //时间单位毫秒
            document.cookie = name + "=" + value + ";expires=" + expdate.toGMTString() + ";path=/";
            //即document.cookie= name+"="+value+";path=/";  时间默认为当前会话可以不要,但路径要填写,因为JS的默认路径是当前页,如果不填,此cookie只在当前页面生效!
        },
        //取cookie
        getCookie: function(c_name) {
            //判断document.cookie对象里面是否存有cookie
            if (document.cookie.length > 0) {
                let c_start = document.cookie.indexOf(c_name + "=")
                    //如果document.cookie对象里面有cookie则查找是否有指定的cookie,如果有则返回指定的cookie值,如果没有则返回空字符串
                if (c_start != -1) {
                    c_start = c_start + c_name.length + 1
                    let c_end = document.cookie.indexOf(";", c_start)
                    if (c_end == -1) c_end = document.cookie.length
                    return decodeURI(document.cookie.substring(c_start, c_end))
                }
            }
            return ""
        },
        //删除cookie
        deleCookie: function(name) {
            this.setCookie(name, '');
        },
        //获取当前时间 或 转换时间格式 
        getDateFormat: function(d, s) {
            var time = d ? new Date(d) : new Date();
            var year = time.getFullYear();
            var month = time.getMonth() + 1 > 9 ? time.getMonth() + 1 : '0' + (time.getMonth() + 1);
            var day = time.getDate() > 9 ? time.getDate() : '0' + time.getDate();
            var Hours = time.getHours() > 9 ? time.getHours() : '0' + time.getHours();
            var Minutes = time.getMinutes() > 9 ? time.getMinutes() : '0' + time.getMinutes();
            if (s) {
                return year + '-' + month + '-' + day + ' ' + Hours + ':' + Minutes;
            } else {
                return year + '-' + month + '-' + day;
            }
        },
        getPosition: function(callback) {
            // if (navigator.geolocation) {
            //      navigator.geolocation.getCurrentPosition(position => {
            //        let latitude = position.coords.latitude;//获取纬度
            //        let longitude = position.coords.longitude;//获取经度
            let mapObj = new AMap.Map('iCenter');
            mapObj.plugin('AMap.Geolocation', function() {
                    let geolocation = new AMap.Geolocation({
                        enableHighAccuracy: true, //是否使用高精度定位,默认:true
                        timeout: 10000, //超过10秒后停止定位,默认:无穷大
                        maximumAge: 0, //定位结果缓存0毫秒,默认:0
                        convert: true, //自动偏移坐标,偏移后的坐标为高德坐标,默认:true
                        showButton: true, //显示定位按钮,默认:true
                        buttonPosition: 'LB', //定位按钮停靠位置,默认:'LB',左下角
                        buttonOffset: new AMap.Pixel(0, 0), //定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
                        showMarker: true, //定位成功后在定位到的位置显示点标记,默认:true
                        showCircle: true, //定位成功后用圆圈表示定位精度范围,默认:true
                        panToLocation: true, //定位成功后将定位到的位置作为地图中心点,默认:true
                        zoomToAccuracy: true //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
                    });
                    mapObj.addControl(geolocation);
                    geolocation.getCurrentPosition();
                    AMap.event.addListener(geolocation, 'complete', function(res) {
                        console.log(res)
                        callback(res.addressComponent.city, 1);
                    }); //返回定位信息
                    AMap.event.addListener(geolocation, 'error', function(res) {
                        callback('杭州市', 0);
                    }); //返回定位出错信息
                })
                //      }, error => {
                //        // switch (error.code) {
                //        //   case error.PERMISSION_DENIED:
                //        //     alert("定位失败,用户拒绝请求地理定位");
                //        //     break;
                //        //   case error.POSITION_UNAVAILABLE:
                //        //     alert("定位失败,位置信息是不可用");
                //        //     break;
                //        //   case error.TIMEOUT:
                //        //     alert("定位失败,请求获取用户位置超时");
                //        //     break;
                //        //   case error.UNKNOWN_ERROR:
                //        //     alert("定位失败,定位系统失效");
                //        //     break;
                //        // }
                // 	   callback('杭州',0);
                //     });
                // } else {
                // 	callback('杭州',0);
                // }
        },
        getNowPosition: function(callback) {
            let mapObj = new AMap.Map('iCenter');
            mapObj.plugin('AMap.Geolocation', function() {
                let geolocation = new AMap.Geolocation({
                    enableHighAccuracy: true, //是否使用高精度定位,默认:true
                    timeout: 10000, //超过10秒后停止定位,默认:无穷大
                    maximumAge: 0, //定位结果缓存0毫秒,默认:0
                    convert: true, //自动偏移坐标,偏移后的坐标为高德坐标,默认:true
                    showButton: true, //显示定位按钮,默认:true
                    buttonPosition: 'LB', //定位按钮停靠位置,默认:'LB',左下角
                    buttonOffset: new AMap.Pixel(0, 0), //定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
                    showMarker: true, //定位成功后在定位到的位置显示点标记,默认:true
                    showCircle: true, //定位成功后用圆圈表示定位精度范围,默认:true
                    panToLocation: true, //定位成功后将定位到的位置作为地图中心点,默认:true
                    zoomToAccuracy: true //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
                });
                mapObj.addControl(geolocation);
                geolocation.getCurrentPosition();
                AMap.event.addListener(geolocation, 'complete', function(res) {
                    callback(res);
                }); //返回定位信息
                AMap.event.addListener(geolocation, 'error', function(res) {
                    callback();
                }); //返回定位出错信息
            })

        },
        //取地址参数方法
        getUrlParam: function(name) {
            var url = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var newUrl = window.location.search.substr(1).match(url);
            if (newUrl != null) {
                return unescape(newUrl[2]);
            } else {
                return '';
            }
        },
        isWeiXin: function() {
            var ua = window.navigator.userAgent.toLowerCase();
            if (ua.match(/MicroMessenger/i) == 'micromessenger') {
                return true;
            } else {
                return false;
            }
        }
    }
}();
export default common;