tool.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import $Config from "@/config/index.js"
  2. import { uploadFile_Api } from "@/api/feedback.js"
  3. // 计算点位距离
  4. export const distanceCalculate = (num) => {
  5. const num_ = Number(num);
  6. if (typeof num_ === 'number') {
  7. if (num_ < 1000) {
  8. return `${num_.toFixed(1)}米`
  9. } else {
  10. const n = (num_ / 1000).toFixed(1);
  11. return `${n}公里`
  12. };
  13. }
  14. return num_ || num
  15. }
  16. // 拨打电话
  17. export const PhoneCall = (tel) => {
  18. return new Promise((resolve, reject) => {
  19. if (tel) {
  20. uni.makePhoneCall({
  21. phoneNumber: tel + '',
  22. success: res => {
  23. resolve()
  24. },
  25. fail: () => {
  26. reject()
  27. }
  28. });
  29. } else {
  30. uni.showToast({
  31. title: '暂未设置电话'
  32. });
  33. }
  34. })
  35. }
  36. // 导航
  37. export const getMapLocation = (parmas = {}) => {
  38. const { latitude, longitude, name, detailedAddress } = parmas
  39. uni.openLocation({
  40. latitude: parseFloat(latitude), // 要去的地址经度,浮点数
  41. longitude: parseFloat(longitude), // 要去的地址纬度,浮点数
  42. name: name, // 位置名
  43. address: detailedAddress, // 要去的地址详情说明
  44. scale: 16, // 地图缩放级别,整形值,范围从1~28。默认为最大
  45. });
  46. }
  47. export const getLocation = () => {
  48. return new Promise((resolve, reject) => {
  49. try {
  50. uni.getLocation({
  51. type: 'wgs84', // 返回可以用于uni.openLocation的经纬度
  52. success: (res) => {
  53. console.log("getLocation 1111111111111111111= ", res)
  54. // this.selfLatitude = res.longitude;
  55. // this.selfLongitude = res.latitude;
  56. resolve(res)
  57. },
  58. fail: (err) => {
  59. console.log('获取位置失败2 :', err);
  60. reject()
  61. }
  62. });
  63. } catch (error) {
  64. console.log('获取位置失败 1:', error);
  65. //TODO handle the exception
  66. }
  67. })
  68. }
  69. export const uploadImage = (num = 1, type = 'img') => {
  70. return new Promise(async (resolve, reject) => {
  71. uni.chooseImage({
  72. count: num,
  73. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  74. sourceType: ['album'], //从相册选择
  75. // crop: {
  76. // quality: 100, // 图片裁剪质量
  77. // width: 100, // 裁剪的宽度
  78. // height: 100, //裁剪的高度
  79. // resize: false //是否将width和height作为裁剪保存图片真实的像素值。默认值为true。注:设置为false时在裁剪编辑界面显示图片的像素值,设置为true时不显示
  80. // },
  81. success: (res) => {
  82. console.log("res = ", res)
  83. // 选中的文件对象
  84. const FilePaths = res.tempFilePaths;
  85. const Names = []
  86. res.tempFiles.forEach(el => {
  87. Names.push(el.name)
  88. })
  89. uni.showLoading({
  90. title: '上传中',
  91. mask: true
  92. });
  93. const UploadList = FilePaths.map(el => SingleFileUpload(el))
  94. Promise.all(UploadList).then(res => {
  95. const Files = [];
  96. res.forEach((el, index) => {
  97. if (el) {
  98. Files.push({
  99. annexUrl: el,
  100. annexName: Names[index]
  101. })
  102. }
  103. });
  104. if (Files && Files.length) {
  105. uni.hideLoading();
  106. uni.showToast({
  107. title: '上传成功',
  108. icon: 'none',
  109. duration: 1000
  110. })
  111. resolve(Files)
  112. } else {
  113. uni.showToast({
  114. title: '上传失败',
  115. icon: 'none',
  116. duration: 1000
  117. })
  118. resolve([])
  119. }
  120. });
  121. },
  122. fail: () => {
  123. uni.hideLoading();
  124. },
  125. complete: () => {
  126. // uni.hideLoading();
  127. }
  128. })
  129. })
  130. }
  131. // uploadFile_Api
  132. export const SingleFileUpload = (file, showLoading = false) => {
  133. return new Promise((resolve, reject) => {
  134. if (showLoading) {
  135. uni.showLoading({
  136. title: '上传中',
  137. mask: true
  138. });
  139. }
  140. uni.uploadFile({
  141. url: $Config.baseURL + '/dev/file/uploadHuaweiYunReturnUrl',
  142. filePath: file,
  143. name: 'file',
  144. success: (uploadFileRes) => {
  145. const { code, data, msg } = JSON.parse(uploadFileRes.data)
  146. if (code === 200) {
  147. resolve(data)
  148. } else {
  149. resolve()
  150. }
  151. },
  152. fail(fail) {
  153. resolve()
  154. },
  155. complete: () => {
  156. if (showLoading) {
  157. uni.hideLoading();
  158. }
  159. }
  160. })
  161. })
  162. }