tool.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // 计算点位距离
  2. export const distanceCalculate = (num) => {
  3. const num_ = Number(num);
  4. if (typeof num_ === 'number') {
  5. if (num_ < 1000) {
  6. return `${num_.toFixed(1)}米`
  7. } else {
  8. const n = (num_ / 1000).toFixed(1);
  9. return `${n}公里`
  10. };
  11. }
  12. return num_ || num
  13. }
  14. // 拨打电话
  15. export const PhoneCall = (tel) => {
  16. if (tel) {
  17. uni.makePhoneCall({
  18. phoneNumber: tel + ''
  19. });
  20. } else {
  21. uni.showToast({
  22. title: '暂未设置电话'
  23. });
  24. }
  25. }
  26. // 导航
  27. export const getMapLocation = (parmas = {}) => {
  28. const { latitude, longitude, name, detailedAddress } = parmas
  29. uni.openLocation({
  30. latitude: parseFloat(latitude), // 要去的地址经度,浮点数
  31. longitude: parseFloat(longitude), // 要去的地址纬度,浮点数
  32. name: name, // 位置名
  33. address: detailedAddress, // 要去的地址详情说明
  34. scale: 16, // 地图缩放级别,整形值,范围从1~28。默认为最大
  35. });
  36. }
  37. export const getLocation = () => {
  38. return new Promise((resolve, reject) => {
  39. try {
  40. uni.getLocation({
  41. type: 'wgs84', // 返回可以用于uni.openLocation的经纬度
  42. success: (res) => {
  43. console.log("getLocation = " , res)
  44. // this.selfLatitude = res.longitude;
  45. // this.selfLongitude = res.latitude;
  46. resolve(res)
  47. },
  48. fail: (err) => {
  49. console.log('获取位置失败2 :', err);
  50. },
  51. fail: () => {
  52. console.log('获取位置失败3 :');
  53. }
  54. });
  55. } catch (error) {
  56. console.log('获取位置失败 1:', error);
  57. //TODO handle the exception
  58. }
  59. })
  60. }