You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
2.5 KiB

7 months ago
  1. import store from '@/store';
  2. const install = (Vue, vm) => {
  3. // 打开地图
  4. const openMap = (lat, lng) => {
  5. uni.openLocation({
  6. latitude: lat,
  7. longitude: lng
  8. })
  9. }
  10. // 距离换算
  11. const distanceFn = (val) => {
  12. if (val * 1 < 1000) {
  13. return val + '米'
  14. } else {
  15. return (val / 1000).toFixed(2) + '公里'
  16. }
  17. }
  18. // 价格计算
  19. const priceTo = (price = 0) => {
  20. // return (price / 100).toFixed(2)
  21. return (parseInt(price * 100) / 100 / 100).toFixed(2)
  22. }
  23. const distanceLatLng = (lat1, lng1) => {
  24. var that = this;
  25. let lat2 = store.state.latLng.lat;
  26. let lng2 = store.state.latLng.lng;
  27. let rad1 = lat1 * Math.PI / 180.0;
  28. let rad2 = lat2 * Math.PI / 180.0;
  29. let a = rad1 - rad2;
  30. let b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
  31. let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) *
  32. Math.cos(
  33. rad2) * Math.pow(
  34. Math.sin(b / 2), 2)));
  35. s = s * 6378.137;
  36. s = Math.round(s * 10000) / 10000;
  37. s = s.toString();
  38. s = s.substring(0, s.indexOf('.') + 2);
  39. return s
  40. }
  41. const getLocation = () => {
  42. return new Promise((resolve, reject) => {
  43. uni.getLocation({
  44. type: 'wgs84',
  45. success: function(res) {
  46. console.log('当前位置的经度:' + res.longitude);
  47. console.log('当前位置的纬度:' + res.latitude);
  48. let obj = {
  49. lat: res.latitude,
  50. lng: res.longitude
  51. }
  52. store.commit('updateLatLng', obj)
  53. resolve(obj)
  54. }
  55. });
  56. }).catch((e) => {})
  57. }
  58. function addZeroPrefix(number) {
  59. return number < 10 ? `0${number}` : number
  60. }
  61. let getDate = (date, splitor = '-') => {
  62. const year = date.getFullYear()
  63. const month = date.getMonth() + 1
  64. const day = date.getDate()
  65. return `${year}${splitor}${addZeroPrefix(month)}${splitor}${addZeroPrefix(day)}`
  66. }
  67. function doHandleMonth(month){
  68.   var m = month;
  69.   if(month.toString().length == 1){
  70.     m = "0" + month;
  71.   }
  72.   return m;
  73. }
  74. function getCustomDay(day){
  75.   var today = new Date();
  76.   var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;
  77.   today.setTime(targetday_milliseconds); //注意,这行是关键代码
  78.   var tYear = today.getFullYear();
  79.   var tMonth = today.getMonth();
  80.   var tDate = today.getDate();
  81.   tMonth = doHandleMonth(tMonth + 1);
  82.   tDate = doHandleMonth(tDate);
  83.   return tYear+"-"+tMonth+"-"+tDate;
  84. }
  85. vm.$u.utils = {
  86. openMap,
  87. getLocation,
  88. priceTo,
  89. distanceLatLng,
  90. distanceFn,
  91. getDate,
  92. getCustomDay
  93. }
  94. }
  95. export default {
  96. install
  97. }