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

127 lines
2.5 KiB

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