学员端小程序
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.

95 lines
1.9 KiB

1 year 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. vm.$u.utils = {
  68. openMap,
  69. getLocation,
  70. priceTo,
  71. distanceLatLng,
  72. distanceFn,
  73. getDate,
  74. }
  75. }
  76. export default {
  77. install
  78. }