jxdnsong
2022-10-25 bf5ba56e8a82f0ef699f2eae413d0dc2c4e4f67d
src/store/modules/mobilePosition.js
@@ -1,323 +1,61 @@
import {
  Message
} from "element-ui";
// import BMap from "./mobilePositionLID";
import RemoGeoLocation from "./mobilePositionLID1";
/*
 * @Author: shuishen 1109946754@qq.com
 * @Date: 2022-09-09 17:54:08
 * @LastEditors: shuishen 1109946754@qq.com
 * @LastEditTime: 2022-09-14 16:05:41
 * @FilePath: \smart-campus\src\store\modules\mobilePosition.js
 * @Description:
 *
 * Copyright (c) 2022 by shuishen 1109946754@qq.com, All Rights Reserved.
 */
const mobilePosition = {
  state: {
    getSPosition: null
  },
  mutations: {
    GET_NOWPOSITION(state, val) {
      let outData =
        val ||
        function (res) {
          console.log(res);
        };
      let startTime = new Date().getTime(),
        endTime;
      console.log(startTime, "startTime");
      let times = (dateBegin) => {
        var dateDiff = new Date().getTime() - dateBegin; //时间差的毫秒数
        var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000)); //计算出相差天数
        var leave1 = dateDiff % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
        var hours = Math.floor(leave1 / (3600 * 1000)); //计算出小时数
        //计算相差分钟数
        var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
        var minutes = Math.floor(leave2 / (60 * 1000)); //计算相差分钟数
        //计算相差秒数
        var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
        var seconds = Math.round(leave3 / 1000);
        var leave4 = leave3 % (60 * 1000); //计算分钟数后剩余的毫秒数
        var minseconds = Math.round(leave4 / 1000);
        var timeFn =
          "耗时:" +
          dayDiff +
          "天 " +
          hours +
          "小时 " +
          minutes +
          " 分钟" +
          seconds +
          " 秒" +
          minseconds +
          "毫秒";
        console.log(timeFn);
        return timeFn;
      };
      if (navigator.geolocation) {
        // let success = function (position) {
        //   console.log(position);
        //   times(startTime);
        //   var lat = position.coords.latitude;
        //   var lon = position.coords.longitude;
        //   console.log("成功返回经纬度信息");
        //   // Message({
        //   //   showClose: true,
        //   //   message:
        //   //     "成功返回经纬度信息(1)" +
        //   //     lon +
        //   //     "_" +
        //   //     lat +
        //   //     "--反应时间:" +
        //   //     times(startTime),
        //   //   type: "success",
        //   //   duration: 0,
        //   // });
        //   console.log(lon, lat, "成功");
        //   outData([lon, lat]);
        // };
        // let error = function (error) {
        //   // console.log(error, "失败");
        //   outData(["失败", "失败"]);
        //   let msg = "none";
        //   switch (error.code) {
        //     case error.PERMISSION_DENIED:
        //       msg = "User denied the request for Geolocation.";
        //       break;
        //     case error.POSITION_UNAVAILABLE:
        //       msg = "Location information is unavailable.";
        //       break;
        //     case error.TIMEOUT:
        //       msg = "The request to get user location timed out.";
        //       break;
        //     case error.UNKNOWN_ERROR:
        //       msg = "An unknown error occurred.";
        //       break;
        //   }
        //   // Message({
        //   //   showClose: true,
        //   //   message:
        //   //     "定位获取失败-msg:" +
        //   //     msg +
        //   //     "------" +
        //   //     error.message +
        //   //     "--反应时间:" +
        //   //     times(startTime),
        //   //   type: "error",
        //   //   duration: 0,
        //   // });
        //   Message({
        //     showClose: true,
        //     message: "未开启位置权限",
        //     type: "error",
        //     duration: 2000,
        //   });
        // };
        // let options = {
        //   enableHighAccuracy: true,
        //   // timeout: 5000,
        //   timeout: Infinity,
        //   maximumAge: 0,
        // };
        // console.log(navigator);
        // console.log(navigator.platform);
        // Message({
        //   showClose: true,
        //   message: navigator.platform,
        //   type: "error",
        //   duration: 0,
        // });
        // console.log(CLLocationManager);
        // return;
        // if (navigator.platform === "iPhone") {
        //   //iphone请求权限
        //   // let remoGeo = new RemoGeoLocation();
        //   // navigator.geolocation.getCurrentPosition = function () {
        //   //   return remoGeo.getCurrentPosition.apply(remoGeo, arguments);
        //   // };
        //   // navigator.geolocation.watchPosition = function () {
        //   //   return remoGeo.watchPosition.apply(remoGeo, arguments);
        //   // };
        //   // Message({
        //   //   showClose: true,
        //   //   message: "ipone",
        //   //   type: "error",
        //   //   duration: 0,
        //   // });
        //   // console.log(CLLocationManager);
        //   // Message({
        //   //   showClose: true,
        //   //   message: CLLocationManager,
        //   //   type: "error",
        //   //   duration: 0,
        //   // });
        //   // return;
        // }
        // return
        // console.log(BMap);
        //百度定位
        //       <script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=zTNaMXHcfGsG0nFvwd6G72aMOo98uzH2">
        // </script>
        // var geolocation = new BMap.Geolocation();
        // geolocation.getCurrentPosition(function (r) {
        //   if (this.getStatus() == BMAP_STATUS_SUCCESS) {
        //     // var mk = new BMap.Marker(r.point);
        //     // map.addOverlay(mk);
        //     // map.panTo(r.point);
        //     console.log("您的位置:" + r.point.lng + "," + r.point.lat);
        //     outData([r.point.lng, r.point.lat]);
        //     Message({
        //       showClose: true,
        //       message:
        //         "成功返回经纬度信息" +
        //         r.point.lng +
        //         "_" +
        //         r.point.lat +
        //         "--反应时间:" +
        //         times(startTime),
        //       type: "success",
        //       duration: 0,
        //     });
        //   } else {
        //     console.log("failed" + this.getStatus());
        //     Message({
        //       showClose: true,
        //       message:
        //         "定位获取失败-msg:" +
        //         "failed" +
        //         this.getStatus() +
        //         "------" +
        //         "--反应时间:" +
        //         times(startTime),
        //       type: "error",
        //       duration: 0,
        //     });
        //   }
        // });
        // return;
        // //高德定位
        // AMap.plugin("AMap.Geolocation", function () {
        //   var geolocation = new AMap.Geolocation({
        //     // 是否使用高精度定位,默认:true
        //     enableHighAccuracy: true,
        //     // 设置定位超时时间,默认:无穷大
        //     // timeout: 10000,
        //     // 定位按钮的停靠位置的偏移量,默认:Pixel(10, 20)
        //     // buttonOffset: new AMap.Pixel(10, 20),
        //     //  定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
        //     // zoomToAccuracy: true,
        //     //  定位按钮的排放位置,  RB表示右下
        //     // buttonPosition: "RB",
        //   });
        //   geolocation.getCurrentPosition();
        //   AMap.event.addListener(geolocation, "complete", onComplete);
        //   AMap.event.addListener(geolocation, "error", onError);
        //   function onComplete(data) {
        //     // data是具体的定位信息
        //     console.log(data);
        //     var lat = data.position.lat;
        //     var lon = data.position.lng;
        //     console.log("成功返回经纬度信息");
        //     Message({
        //       showClose: true,
        //       message: "成功返回经纬度信息" +
        //         lon +
        //         "_" +
        //         lat +
        //         "--反应时间:" +
        //         times(startTime),
        //       type: "success",
        //       duration: 0,
        //     });
        //     console.log(lon, lat, "成功");
        //     outData([lon, lat]);
        //   }
        //   function onError(data) {
        //     // 定位出错
        //     console.log(data);
        //     Message({
        //       showClose: true,
        //       message: "定位获取失败-msg:" +
        //         data.message +
        //         "------" +
        //         "--反应时间:" +
        //         times(startTime),
        //       type: "error",
        //       duration: 0,
        //     });
        //   }
        // });
        window.AMap.plugin('AMap.Geolocation', function () {
          var geolocation = new AMap.Geolocation({
            enableHighAccuracy: true, //是否使用高精度定位,默认:true
            // timeout: 10000, //超过10秒后停止定位,默认:5s
            // buttonPosition: 'RB', //定位按钮的停靠位置
            // buttonOffset: new AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
            // zoomToAccuracy: true, //定位成功后是否自动调整地图视野到定位点
          });
          // map.addControl(geolocation);
          geolocation.getCurrentPosition(function (status, result) {
            if (status == 'complete') {
              onComplete(result)
            } else {
              onError(result)
            }
          });
        });
        //解析定位结果
        function onComplete(data) {
          // let lngLat = [data.position.lng, data.position.lat]
          // console.log(position);
          // times(startTime);
          var lat = data.position.lat;
          var lon = data.position.lng;
          // console.log("成功返回经纬度信息");
          // Message({
          //   showClose: true,
          //   message: "成功返回经纬度信息(1)" +
          //     lon +
          //     "_" +
          //     lat +
          //     "--反应时间:" +
          //     times(startTime),
          //   type: "success",
          //   duration: 2000,
          // });
          console.log(lon, lat, "成功");
          outData([lon, lat]);
        }
        //解析定位错误信息
        function onError(data) {
          // console.log(data, "see")
          Message({
            showClose: true,
            message: "未开启位置权限",
            type: "error",
            duration: 2000,
          });
        }
        return;
        state.getSPosition = navigator.geolocation.getCurrentPosition(
          success,
          error,
          options
        );
      } else {
        console.log("不支持navigator.geolocation");
        Message({
          showClose: true,
          message: "不支持定位",
          type: "warning",
          duration: 0,
        });
        return "不支持navigator.geolocation";
      }
    state: {
        getSPosition: null
    },
    CLOSE_NOWPOSITION(state) {
      // if (state.getSPosition && navigator.geolocation) {
      //   navigator.geolocation.clearWatch(state.getSPosition);
      //   state.getSPosition = null;
      // }
    },
  },
  actions: {},
};
    mutations: {
        GET_NOWPOSITION (state, val) {
            var outData =
                val ||
                function (res) {
                    console.log(res)
                }
export default mobilePosition;
            console.log(state, 222)
            global.Geolocation.getLocation(function success (result) {
                const position = global.DC.CoordTransform.GCJ02ToWGS84(result.lng, result.lat)
                if (position[0] > 115.78815057 && position[0] < 115.80346610 && position[1] > 28.63771847 && position[1] < 28.64981682) {
                    outData([position[0], position[1]])
                } else {
                    Message({
                        message: '您当前位置超出学校范围',
                        type: 'warning',
                        duration: 2000
                    })
                    outData(['失败'])
                }
            }, function error (result) {
                if (result.code == 1) {
                    confirm('您拒绝了定位功能')
                } else if (result.code == 3 || result.code == 5) {
                    confirm('请求超时')
                }
            }, {
                timeout: 8000, // 默认值为10s;
                failTipFlag: true
            })
        },
        CLOSE_NOWPOSITION (state) {
            // if (state.getSPosition && navigator.geolocation) {
            //   navigator.geolocation.clearWatch(state.getSPosition);
            //   state.getSPosition = null;
            // }
        }
    },
    actions: {}
}
export default mobilePosition