tool.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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' , extension = ['.png','.jpg']) => {
  70. return new Promise(async (resolve, reject) => {
  71. uni.chooseImage({
  72. count: num,
  73. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  74. sourceType: ['album'], //从相册选择
  75. extension:extension,
  76. // crop: {
  77. // quality: 100, // 图片裁剪质量
  78. // width: 100, // 裁剪的宽度
  79. // height: 100, //裁剪的高度
  80. // resize: false //是否将width和height作为裁剪保存图片真实的像素值。默认值为true。注:设置为false时在裁剪编辑界面显示图片的像素值,设置为true时不显示
  81. // },
  82. success: (res) => {
  83. console.log("res = ", res)
  84. // 选中的文件对象
  85. const FilePaths = res.tempFilePaths;
  86. const Names = [];
  87. res.tempFiles.forEach(el => {
  88. Names.push(el.name)
  89. })
  90. uni.showLoading({
  91. title: '上传中',
  92. mask: true
  93. });
  94. const UploadList = FilePaths.map(el => SingleFileUpload(el))
  95. Promise.all(UploadList).then(res => {
  96. const Files = [];
  97. res.forEach((el, index) => {
  98. if (el) {
  99. Files.push({
  100. annexUrl: el,
  101. annexName: Names[index]
  102. })
  103. }
  104. });
  105. if (Files && Files.length) {
  106. uni.hideLoading();
  107. uni.showToast({
  108. title: '上传成功',
  109. icon: 'none',
  110. duration: 1000
  111. })
  112. resolve(Files)
  113. } else {
  114. uni.showToast({
  115. title: '上传失败',
  116. icon: 'none',
  117. duration: 1000
  118. })
  119. resolve([])
  120. }
  121. });
  122. },
  123. fail: () => {
  124. uni.hideLoading();
  125. },
  126. complete: () => {
  127. // uni.hideLoading();
  128. }
  129. })
  130. })
  131. }
  132. // uploadFile_Api
  133. export const SingleFileUpload = (file, showLoading = false) => {
  134. return new Promise((resolve, reject) => {
  135. if (showLoading) {
  136. uni.showLoading({
  137. title: '上传中',
  138. mask: true
  139. });
  140. }
  141. uni.uploadFile({
  142. url: $Config.baseURL + '/dev/file/uploadHuaweiYunReturnUrl',
  143. filePath: file,
  144. name: 'file',
  145. success: (uploadFileRes) => {
  146. const { code, data, msg } = JSON.parse(uploadFileRes.data)
  147. if (code === 200) {
  148. resolve(data)
  149. } else {
  150. resolve()
  151. }
  152. },
  153. fail(fail) {
  154. resolve()
  155. },
  156. complete: () => {
  157. if (showLoading) {
  158. uni.hideLoading();
  159. }
  160. }
  161. })
  162. })
  163. }