tool.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // import SparkMD5, {
  2. // hash
  3. // } from 'spark-md5'
  4. import $mConfig from '@/config/global.config.js';
  5. export const getPhotograph = () => {
  6. uni.chooseImage({
  7. count: 6, //默认9
  8. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  9. sourceType: ['camera ', 'album'], //从相册选择
  10. success: function(res) {
  11. console.log(JSON.stringify(res.tempFilePaths));
  12. }
  13. });
  14. // // #ifdef APP-PLUS
  15. // var appid = plus.runtime.appid;
  16. // console.log('应用的 appid 为:' + appid);
  17. // // #endif
  18. // // #ifdef APP
  19. // var cmr = plus.camera.getCamera();
  20. // // 属性:
  21. // // supportedImageResolutions: 字符串数组,摄像头支持的拍照分辨率
  22. // // supportedVideoResolutions: 字符串数组,摄像头支持的摄像分辨率
  23. // // supportedImageFormats: 字符串数组,摄像头支持的拍照文件格式
  24. // // supportedVideoFormats: 字符串数组,摄像头支持的摄像文件格式
  25. // // 方法:
  26. // // captureImage: 进行拍照操作
  27. // // startVideoCapture: 调用摄像头进行摄像操作
  28. // // stopVideoCapture: 结束摄像操作
  29. // // 拍照 captureImage
  30. // var res = cmr.supportedImageResolutions[0];
  31. // var fmt = cmr.supportedImageFormats[0];
  32. // console.log("Resolution: " + res + ", Format: " + fmt);
  33. // cmr.captureImage(function(path) {
  34. // console.log("Capture image success: " + path);
  35. // },
  36. // function(error) {
  37. // console.log("Capture image failed: " + error.message);
  38. // }, {
  39. // resolution: res,
  40. // format: fmt
  41. // }
  42. // )
  43. // // 摄像
  44. // // function videoCapture(){
  45. // // }
  46. // // var cmr = plus.camera.getCamera();
  47. // // var res = cmr.supportedVideoResolutions[0];
  48. // // var fmt = cmr.supportedVideoFormats[0];
  49. // // console.log("Resolution: " + res + ", Format: " + fmt);
  50. // // cmr.startVideoCapture(function(path) {
  51. // // console.log("Capture video success: " + path);
  52. // // },
  53. // // function(error) {
  54. // // console.log("Capture video failed: " + error.message);
  55. // // }, {
  56. // // resolution: res,
  57. // // format: fmt
  58. // // }
  59. // // );
  60. // // #endif
  61. }
  62. export const distanceCalculate = (num) => {
  63. if (num < 1000) {
  64. return `${num.toFixed(1)}米`
  65. } else {
  66. const n = (num / 1000).toFixed(1);
  67. return `${n}公里`
  68. };
  69. }
  70. export const uploadImage = (num = 1, type = 'img') => {
  71. return new Promise(async (resolve, reject) => {
  72. console.log('uploadImage = ', num)
  73. // // #ifdef APP-PLUS
  74. // let flag = await permision.showAuthTipModal("android.permission.READ_EXTERNAL_STORAGE")
  75. // reject()
  76. // if (!flag) return
  77. // // #endif
  78. // console.log('uploadImage = 2 ', num)
  79. uni.chooseImage({
  80. count: num,
  81. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  82. sourceType: ['album'], //从相册选择
  83. crop: {
  84. quality: 100, // 图片裁剪质量
  85. width: 100, // 裁剪的宽度
  86. height: 100, //裁剪的高度
  87. resize: false //是否将width和height作为裁剪保存图片真实的像素值。默认值为true。注:设置为false时在裁剪编辑界面显示图片的像素值,设置为true时不显示
  88. },
  89. success: (res) => {
  90. // 选中的文件对象
  91. const FilePaths = res.tempFilePaths;
  92. // 记录失败的个数
  93. let failNum = 0;
  94. // 上传成功返回的媒体资源
  95. const successArr = []
  96. uni.showLoading({
  97. title: '上传中',
  98. mask: true
  99. });
  100. FilePaths.forEach(el => {
  101. SingleFileUpload(el).then(res => {
  102. successArr.push(res.url)
  103. if ((successArr.length + failNum) >=
  104. FilePaths
  105. .length) {
  106. uni.hideLoading();
  107. uni.showToast({
  108. title: '上传成功',
  109. icon: 'success',
  110. duration: 1000
  111. })
  112. resolve(successArr)
  113. }
  114. }).catch(err => {
  115. failNum++
  116. })
  117. })
  118. },
  119. complete: () => {
  120. uni.hideLoading();
  121. }
  122. })
  123. })
  124. }
  125. export const SingleFileUpload = (file, showLoading = false) => {
  126. return new Promise((resolve, reject) => {
  127. if (showLoading) {
  128. uni.showLoading({
  129. title: '上传中',
  130. mask: true
  131. });
  132. }
  133. uni.uploadFile({
  134. url: $mConfig.baseUrl + '/common/upload',
  135. filePath: file,
  136. name: 'file',
  137. success: (uploadFileRes) => {
  138. const data = JSON.parse(uploadFileRes.data)
  139. .data;
  140. resolve(data)
  141. },
  142. fail(fail) {
  143. reject()
  144. // if ((successArr.length + failNum) >= FilePaths
  145. // .length) {
  146. // uni.hideLoading();
  147. // }
  148. },
  149. complete: () => {
  150. if (showLoading) {
  151. uni.hideLoading();
  152. }
  153. }
  154. })
  155. })
  156. }
  157. export const getLocation = async () => {
  158. // // #ifdef APP-PLUS
  159. // let flag = await permision.showAuthTipModal("android.permission.ACCESS_FINE_LOCATION")
  160. // if (!flag) return
  161. // // #endif
  162. return new Promise((resolve, reject) => {
  163. uni.getLocation({
  164. type: 'gcj02',
  165. geocode: true,
  166. cacheTimeout: 30 * 60,
  167. success: res => {
  168. console.log('getLocation 666 = ', res)
  169. resolve(res)
  170. },
  171. fail: err => {
  172. permision.showManualAuth("android.permission.ACCESS_FINE_LOCATION")
  173. return resolve(null)
  174. },
  175. });
  176. })
  177. }