tool.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 (typeof num === 'number') {
  64. if (num < 1000) {
  65. return `${num.toFixed(1)}米`
  66. } else {
  67. const n = (num / 1000).toFixed(1);
  68. return `${n}公里`
  69. };
  70. }
  71. return num
  72. }
  73. export const uploadImage = (num = 1, type = 'img') => {
  74. return new Promise(async (resolve, reject) => {
  75. console.log('uploadImage = ', num)
  76. // // #ifdef APP-PLUS
  77. // let flag = await permision.showAuthTipModal("android.permission.READ_EXTERNAL_STORAGE")
  78. // reject()
  79. // if (!flag) return
  80. // // #endif
  81. // console.log('uploadImage = 2 ', num)
  82. uni.chooseImage({
  83. count: num,
  84. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  85. sourceType: ['album'], //从相册选择
  86. crop: {
  87. quality: 100, // 图片裁剪质量
  88. width: 100, // 裁剪的宽度
  89. height: 100, //裁剪的高度
  90. resize: false //是否将width和height作为裁剪保存图片真实的像素值。默认值为true。注:设置为false时在裁剪编辑界面显示图片的像素值,设置为true时不显示
  91. },
  92. success: (res) => {
  93. // 选中的文件对象
  94. const FilePaths = res.tempFilePaths;
  95. // 记录失败的个数
  96. let failNum = 0;
  97. // 上传成功返回的媒体资源
  98. const successArr = []
  99. uni.showLoading({
  100. title: '上传中',
  101. mask: true
  102. });
  103. FilePaths.forEach(el => {
  104. SingleFileUpload(el).then(res => {
  105. successArr.push(res.url)
  106. if ((successArr.length + failNum) >=
  107. FilePaths
  108. .length) {
  109. uni.hideLoading();
  110. uni.showToast({
  111. title: '上传成功',
  112. icon: 'success',
  113. duration: 1000
  114. })
  115. resolve(successArr)
  116. }
  117. }).catch(err => {
  118. failNum++
  119. })
  120. })
  121. },
  122. complete: () => {
  123. uni.hideLoading();
  124. }
  125. })
  126. })
  127. }
  128. export const SingleFileUpload = (file, showLoading = false) => {
  129. return new Promise((resolve, reject) => {
  130. if (showLoading) {
  131. uni.showLoading({
  132. title: '上传中',
  133. mask: true
  134. });
  135. }
  136. uni.uploadFile({
  137. url: $mConfig.baseUrl + '/common/upload',
  138. filePath: file,
  139. name: 'file',
  140. success: (uploadFileRes) => {
  141. const data = JSON.parse(uploadFileRes.data)
  142. .data;
  143. resolve(data)
  144. },
  145. fail(fail) {
  146. reject()
  147. // if ((successArr.length + failNum) >= FilePaths
  148. // .length) {
  149. // uni.hideLoading();
  150. // }
  151. },
  152. complete: () => {
  153. if (showLoading) {
  154. uni.hideLoading();
  155. }
  156. }
  157. })
  158. })
  159. }
  160. export const getLocation = async () => {
  161. // // #ifdef APP-PLUS
  162. // let flag = await permision.showAuthTipModal("android.permission.ACCESS_FINE_LOCATION")
  163. // if (!flag) return
  164. // // #endif
  165. return new Promise((resolve, reject) => {
  166. uni.getLocation({
  167. type: 'gcj02',
  168. geocode: true,
  169. cacheTimeout: 30 * 60,
  170. success: res => {
  171. console.log('getLocation 666 = ', res)
  172. resolve(res)
  173. },
  174. fail: err => {
  175. permision.showManualAuth("android.permission.ACCESS_FINE_LOCATION")
  176. return resolve(null)
  177. },
  178. });
  179. })
  180. }