tool.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import SparkMD5 from 'spark-md5'
  2. import $mConfig from '@/config/global.config.js';
  3. export const getPhotograph = () => {
  4. uni.chooseImage({
  5. count: 6, //默认9
  6. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  7. sourceType: ['camera ', 'album'], //从相册选择
  8. success: function(res) {
  9. console.log(JSON.stringify(res.tempFilePaths));
  10. }
  11. });
  12. // // #ifdef APP-PLUS
  13. // var appid = plus.runtime.appid;
  14. // console.log('应用的 appid 为:' + appid);
  15. // // #endif
  16. // // #ifdef APP
  17. // var cmr = plus.camera.getCamera();
  18. // // 属性:
  19. // // supportedImageResolutions: 字符串数组,摄像头支持的拍照分辨率
  20. // // supportedVideoResolutions: 字符串数组,摄像头支持的摄像分辨率
  21. // // supportedImageFormats: 字符串数组,摄像头支持的拍照文件格式
  22. // // supportedVideoFormats: 字符串数组,摄像头支持的摄像文件格式
  23. // // 方法:
  24. // // captureImage: 进行拍照操作
  25. // // startVideoCapture: 调用摄像头进行摄像操作
  26. // // stopVideoCapture: 结束摄像操作
  27. // // 拍照 captureImage
  28. // var res = cmr.supportedImageResolutions[0];
  29. // var fmt = cmr.supportedImageFormats[0];
  30. // console.log("Resolution: " + res + ", Format: " + fmt);
  31. // cmr.captureImage(function(path) {
  32. // console.log("Capture image success: " + path);
  33. // },
  34. // function(error) {
  35. // console.log("Capture image failed: " + error.message);
  36. // }, {
  37. // resolution: res,
  38. // format: fmt
  39. // }
  40. // )
  41. // // 摄像
  42. // // function videoCapture(){
  43. // // }
  44. // // var cmr = plus.camera.getCamera();
  45. // // var res = cmr.supportedVideoResolutions[0];
  46. // // var fmt = cmr.supportedVideoFormats[0];
  47. // // console.log("Resolution: " + res + ", Format: " + fmt);
  48. // // cmr.startVideoCapture(function(path) {
  49. // // console.log("Capture video success: " + path);
  50. // // },
  51. // // function(error) {
  52. // // console.log("Capture video failed: " + error.message);
  53. // // }, {
  54. // // resolution: res,
  55. // // format: fmt
  56. // // }
  57. // // );
  58. // // #endif
  59. }
  60. export const distanceCalculate = (num) => {
  61. const num_ = Number(num);
  62. if (typeof num_ === 'number') {
  63. if (num_ < 1000) {
  64. return `${num_.toFixed(1)}米`
  65. } else {
  66. const n = (num_ / 1000).toFixed(1);
  67. return `${n}公里`
  68. };
  69. }
  70. return num_ || num
  71. }
  72. export const uploadImage = (num = 1, type = 'img') => {
  73. return new Promise(async (resolve, reject) => {
  74. console.log('uploadImage = ', num)
  75. // // #ifdef APP-PLUS
  76. // let flag = await permision.showAuthTipModal("android.permission.READ_EXTERNAL_STORAGE")
  77. // reject()
  78. // if (!flag) return
  79. // // #endif
  80. // console.log('uploadImage = 2 ', num)
  81. uni.chooseImage({
  82. count: num,
  83. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  84. sourceType: ['album'], //从相册选择
  85. crop: {
  86. quality: 100, // 图片裁剪质量
  87. width: 100, // 裁剪的宽度
  88. height: 100, //裁剪的高度
  89. resize: false //是否将width和height作为裁剪保存图片真实的像素值。默认值为true。注:设置为false时在裁剪编辑界面显示图片的像素值,设置为true时不显示
  90. },
  91. success: (res) => {
  92. // 选中的文件对象
  93. const FilePaths = res.tempFilePaths;
  94. // 记录失败的个数
  95. let failNum = 0;
  96. // 上传成功返回的媒体资源
  97. const successArr = []
  98. uni.showLoading({
  99. title: '上传中',
  100. mask: true
  101. });
  102. FilePaths.forEach(el => {
  103. SingleFileUpload(el).then(res => {
  104. successArr.push(res.url)
  105. if ((successArr.length + failNum) >=
  106. FilePaths
  107. .length) {
  108. uni.hideLoading();
  109. uni.showToast({
  110. title: '上传成功',
  111. icon: 'success',
  112. duration: 1000
  113. })
  114. resolve(successArr)
  115. }
  116. }).catch(err => {
  117. failNum++
  118. })
  119. })
  120. },
  121. complete: () => {
  122. uni.hideLoading();
  123. }
  124. })
  125. })
  126. }
  127. export const SingleFileUpload = (file, showLoading = false) => {
  128. return new Promise((resolve, reject) => {
  129. if (showLoading) {
  130. uni.showLoading({
  131. title: '上传中',
  132. mask: true
  133. });
  134. }
  135. uni.uploadFile({
  136. url: $mConfig.baseUrl + '/common/upload',
  137. filePath: file,
  138. name: 'file',
  139. success: (uploadFileRes) => {
  140. const data = JSON.parse(uploadFileRes.data)
  141. .data;
  142. resolve(data)
  143. },
  144. fail(fail) {
  145. reject()
  146. // if ((successArr.length + failNum) >= FilePaths
  147. // .length) {
  148. // uni.hideLoading();
  149. // }
  150. },
  151. complete: () => {
  152. if (showLoading) {
  153. uni.hideLoading();
  154. }
  155. }
  156. })
  157. })
  158. }
  159. export const getLocation = async () => {
  160. // // #ifdef APP-PLUS
  161. // let flag = await permision.showAuthTipModal("android.permission.ACCESS_FINE_LOCATION")
  162. // if (!flag) return
  163. // // #endif
  164. return new Promise((resolve, reject) => {
  165. uni.getLocation({
  166. type: 'gcj02',
  167. geocode: true,
  168. cacheTimeout: 30 * 60,
  169. success: res => {
  170. resolve(res)
  171. },
  172. fail: err => {
  173. permision.showManualAuth("android.permission.ACCESS_FINE_LOCATION")
  174. return resolve(null)
  175. },
  176. });
  177. })
  178. }
  179. /**
  180. * 格式化日期
  181. * @prama t 时间戳
  182. * @return str MM-dd HH:mm
  183. */
  184. export function formatDate(t) {
  185. t = t || Date.now();
  186. let time = new Date(t);
  187. let str = time.getMonth() < 9 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1;
  188. str += '-';
  189. str += time.getDate() < 10 ? '0' + time.getDate() : time.getDate();
  190. str += ' ';
  191. str += time.getHours();
  192. str += ':';
  193. str += time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes();
  194. return str;
  195. };
  196. /**
  197. * 距当前时间点的时长
  198. * @prama time 13位时间戳
  199. * @return str x秒 / x分钟 / x小时
  200. */
  201. export function formateTime(time) {
  202. const second = 1000;
  203. const minute = second * 60;
  204. const hour = minute * 60;
  205. const day = hour * 24;
  206. const now = new Date().getTime();
  207. const diffValue = now - time;
  208. // 计算差异时间的量级
  209. const secondC = diffValue / second;
  210. const minC = diffValue / minute;
  211. const hourC = diffValue / hour;
  212. const dayC = diffValue / day;
  213. if (dayC >= 1) {
  214. return parseInt(dayC) + "天";
  215. } else if (hourC >= 1) {
  216. return parseInt(hourC) + "小时";
  217. } else if (minC >= 1) {
  218. return parseInt(minC) + "分钟";
  219. } else if (secondC >= 1) {
  220. return parseInt(secondC) + "秒";
  221. } else {
  222. return '0秒';
  223. }
  224. }
  225. // 获取文件的唯一MD5标识码
  226. export function getFileMd5 (file) {
  227. return new Promise((resolve, reject) => {
  228. const fileReader = new FileReader()
  229. const spark = new SparkMD5.ArrayBuffer()
  230. fileReader.readAsArrayBuffer(file)
  231. fileReader.onload = e => {
  232. spark.append(e.target.result)
  233. let md5 = spark.end()
  234. resolve(md5)
  235. }
  236. })
  237. }