tool.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. complete() {
  63. console.log('获取位置失败结束1');
  64. }
  65. });
  66. } catch (error) {
  67. console.log('获取位置失败 1:', error);
  68. reject()
  69. } finally{
  70. console.log('获取位置失败结束2');
  71. }
  72. })
  73. }
  74. export const uploadImage = (num = 1, type = 'img', extension = ['.png', '.jpg']) => {
  75. return new Promise(async (resolve, reject) => {
  76. uni.chooseImage({
  77. count: num,
  78. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  79. sourceType: ['album'], //从相册选择
  80. extension: extension,
  81. // crop: {
  82. // quality: 100, // 图片裁剪质量
  83. // width: 100, // 裁剪的宽度
  84. // height: 100, //裁剪的高度
  85. // resize: false //是否将width和height作为裁剪保存图片真实的像素值。默认值为true。注:设置为false时在裁剪编辑界面显示图片的像素值,设置为true时不显示
  86. // },
  87. success: (res) => {
  88. console.log("res = ", res)
  89. // 选中的文件对象
  90. const FilePaths = res.tempFilePaths;
  91. const Names = [];
  92. res.tempFiles.forEach(el => {
  93. Names.push(el.name)
  94. })
  95. uni.showLoading({
  96. title: '上传中',
  97. mask: true
  98. });
  99. const UploadList = FilePaths.map(el => SingleFileUpload(el))
  100. Promise.all(UploadList).then(res => {
  101. const Files = [];
  102. res.forEach((el, index) => {
  103. if (el) {
  104. Files.push({
  105. annexUrl: el,
  106. annexName: Names[index]
  107. })
  108. }
  109. });
  110. if (Files && Files.length) {
  111. uni.hideLoading();
  112. uni.showToast({
  113. title: '上传成功',
  114. icon: 'none',
  115. duration: 1000
  116. })
  117. resolve(Files)
  118. } else {
  119. uni.showToast({
  120. title: '上传失败',
  121. icon: 'none',
  122. duration: 1000
  123. })
  124. resolve([])
  125. }
  126. });
  127. },
  128. fail: () => {
  129. uni.hideLoading();
  130. },
  131. complete: () => {
  132. // uni.hideLoading();
  133. }
  134. })
  135. })
  136. }
  137. // uploadFile_Api
  138. export const SingleFileUpload = (file, showLoading = false) => {
  139. return new Promise((resolve, reject) => {
  140. if (showLoading) {
  141. uni.showLoading({
  142. title: '上传中',
  143. mask: true
  144. });
  145. }
  146. uni.uploadFile({
  147. url: $Config.baseURL + '/dev/file/uploadAmazonYunReturnUrl',
  148. filePath: file,
  149. name: 'file',
  150. success: (uploadFileRes) => {
  151. const { code, data, msg } = JSON.parse(uploadFileRes.data)
  152. if (code === 200) {
  153. resolve(data)
  154. } else {
  155. resolve()
  156. }
  157. },
  158. fail(fail) {
  159. resolve()
  160. },
  161. complete: () => {
  162. if (showLoading) {
  163. uni.hideLoading();
  164. }
  165. }
  166. })
  167. })
  168. }
  169. export const getUUID = () => {
  170. return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
  171. return (c === "x" ? (Math.random() * 16) | 0 : "r&0x3" | "0x8").toString(
  172. 16
  173. );
  174. });
  175. }
  176. export const createTianditu = (key) => {
  177. console.log("key keykeykeykeykeykeykeykeykeykey= " , key)
  178. return new Promise((resolve, reject) => {
  179. if (window.T) {
  180. resolve(window.T);
  181. return;
  182. }
  183. const script = document.createElement('script');
  184. script.src = `https://api.tianditu.gov.cn/api?v=4.0&tk=${key}`;
  185. script.onload = () => resolve(window.T);
  186. script.onerror = reject;
  187. document.head.appendChild(script);
  188. });
  189. }