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

119 lines
2.3 KiB

1 year ago
1 year ago
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. const callPhone = (phone)=> {
  68. phone = phone.trim()
  69. if(!phone) {
  70. uni.showToast({
  71. title: '暂无联系方式',
  72. icon: 'none'
  73. });
  74. return
  75. }
  76. uni.showModal({
  77. content: '确定要拨打:'+phone+'?' ,
  78. success: function (res) {
  79. if (res.confirm) {
  80. uni.makePhoneCall({
  81. phoneNumber: phone //仅为示例
  82. });
  83. } else if (res.cancel) {
  84. console.log('用户点击取消');
  85. }
  86. }
  87. });
  88. }
  89. vm.$u.utils = {
  90. openMap,
  91. getLocation,
  92. priceTo,
  93. distanceLatLng,
  94. distanceFn,
  95. getDate,
  96. callPhone
  97. }
  98. }
  99. export default {
  100. install
  101. }