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

128 lines
2.6 KiB

1 year ago
12 months ago
1 year ago
11 months ago
1 year ago
1 year ago
1 year ago
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) return
  13. if (val * 1 < 1000) {
  14. return val + '米'
  15. } else {
  16. return (val / 1000).toFixed(2) + '公里'
  17. }
  18. }
  19. // 价格计算
  20. const priceTo = (price = 0) => {
  21. if(!price) return 0
  22. // return (price / 100).toFixed(2)
  23. return (parseInt(price * 100) / 100 / 100).toFixed(2)
  24. }
  25. const distanceLatLng = (lat1, lng1) => {
  26. var that = this;
  27. let lat2 = store.state.latLng.lat;
  28. let lng2 = store.state.latLng.lng;
  29. let rad1 = lat1 * Math.PI / 180.0;
  30. let rad2 = lat2 * Math.PI / 180.0;
  31. let a = rad1 - rad2;
  32. let b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
  33. let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) *
  34. Math.cos(
  35. rad2) * Math.pow(
  36. Math.sin(b / 2), 2)));
  37. s = s * 6378.137;
  38. s = Math.round(s * 10000) / 10000;
  39. s = s.toString();
  40. s = s.substring(0, s.indexOf('.') + 2);
  41. return s
  42. }
  43. const getLocation = () => {
  44. return new Promise((resolve, reject) => {
  45. uni.getLocation({
  46. type: 'wgs84',
  47. success: function(res) {
  48. console.log('当前位置的经度:' + res.longitude);
  49. console.log('当前位置的纬度:' + res.latitude);
  50. let obj = {
  51. lat: res.latitude,
  52. lng: res.longitude
  53. }
  54. store.commit('updateLatLng', obj)
  55. resolve(obj)
  56. }
  57. });
  58. }).catch((e) => {})
  59. }
  60. function addZeroPrefix(number) {
  61. return number < 10 ? `0${number}` : number
  62. }
  63. let getDate = (date, splitor = '-') => {
  64. const year = date.getFullYear()
  65. const month = date.getMonth() + 1
  66. const day = date.getDate()
  67. return `${year}${splitor}${addZeroPrefix(month)}${splitor}${addZeroPrefix(day)}`
  68. }
  69. const callPhone = (phone)=> {
  70. phone = phone.trim()
  71. if(!phone) {
  72. uni.showToast({
  73. title: '暂无联系方式',
  74. icon: 'none'
  75. });
  76. return
  77. }
  78. uni.showModal({
  79. content: '确定要拨打:'+phone+'?' ,
  80. success: function (res) {
  81. if (res.confirm) {
  82. uni.makePhoneCall({
  83. phoneNumber: phone //仅为示例
  84. });
  85. } else if (res.cancel) {
  86. console.log('用户点击取消');
  87. }
  88. }
  89. });
  90. }
  91. let truncateText = (text, maxLength)=> {
  92. if(!text) return
  93. if (text.length > maxLength) {
  94. return text.substring(0, maxLength) + "...";
  95. }
  96. return text;
  97. }
  98. vm.$u.utils = {
  99. openMap,
  100. getLocation,
  101. priceTo,
  102. distanceLatLng,
  103. distanceFn,
  104. getDate,
  105. callPhone,
  106. truncateText
  107. }
  108. }
  109. export default {
  110. install
  111. }