import SparkMD5 from 'spark-md5' import $mConfig from '@/config/global.config.js'; export const getPhotograph = () => { uni.chooseImage({ count: 6, //默认9 sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有 sourceType: ['camera ', 'album'], //从相册选择 success: function(res) { console.log(JSON.stringify(res.tempFilePaths)); } }); // // #ifdef APP-PLUS // var appid = plus.runtime.appid; // console.log('应用的 appid 为:' + appid); // // #endif // // #ifdef APP // var cmr = plus.camera.getCamera(); // // 属性: // // supportedImageResolutions: 字符串数组,摄像头支持的拍照分辨率 // // supportedVideoResolutions: 字符串数组,摄像头支持的摄像分辨率 // // supportedImageFormats: 字符串数组,摄像头支持的拍照文件格式 // // supportedVideoFormats: 字符串数组,摄像头支持的摄像文件格式 // // 方法: // // captureImage: 进行拍照操作 // // startVideoCapture: 调用摄像头进行摄像操作 // // stopVideoCapture: 结束摄像操作 // // 拍照 captureImage // var res = cmr.supportedImageResolutions[0]; // var fmt = cmr.supportedImageFormats[0]; // console.log("Resolution: " + res + ", Format: " + fmt); // cmr.captureImage(function(path) { // console.log("Capture image success: " + path); // }, // function(error) { // console.log("Capture image failed: " + error.message); // }, { // resolution: res, // format: fmt // } // ) // // 摄像 // // function videoCapture(){ // // } // // var cmr = plus.camera.getCamera(); // // var res = cmr.supportedVideoResolutions[0]; // // var fmt = cmr.supportedVideoFormats[0]; // // console.log("Resolution: " + res + ", Format: " + fmt); // // cmr.startVideoCapture(function(path) { // // console.log("Capture video success: " + path); // // }, // // function(error) { // // console.log("Capture video failed: " + error.message); // // }, { // // resolution: res, // // format: fmt // // } // // ); // // #endif } export const distanceCalculate = (num) => { const num_ = Number(num); if (typeof num_ === 'number') { if (num_ < 1000) { return `${num_.toFixed(1)}米` } else { const n = (num_ / 1000).toFixed(1); return `${n}公里` }; } return num_ || num } export const uploadImage = (num = 1, type = 'img') => { return new Promise(async (resolve, reject) => { console.log('uploadImage = ', num) // // #ifdef APP-PLUS // let flag = await permision.showAuthTipModal("android.permission.READ_EXTERNAL_STORAGE") // reject() // if (!flag) return // // #endif // console.log('uploadImage = 2 ', num) uni.chooseImage({ count: num, sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有 sourceType: ['album'], //从相册选择 crop: { quality: 100, // 图片裁剪质量 width: 100, // 裁剪的宽度 height: 100, //裁剪的高度 resize: false //是否将width和height作为裁剪保存图片真实的像素值。默认值为true。注:设置为false时在裁剪编辑界面显示图片的像素值,设置为true时不显示 }, success: (res) => { // 选中的文件对象 const FilePaths = res.tempFilePaths; // 记录失败的个数 let failNum = 0; // 上传成功返回的媒体资源 const successArr = [] uni.showLoading({ title: '上传中', mask: true }); FilePaths.forEach(el => { SingleFileUpload(el).then(res => { successArr.push(res.url) if ((successArr.length + failNum) >= FilePaths .length) { uni.hideLoading(); uni.showToast({ title: '上传成功', icon: 'success', duration: 1000 }) resolve(successArr) } }).catch(err => { failNum++ }) }) }, complete: () => { uni.hideLoading(); } }) }) } export const SingleFileUpload = (file, showLoading = false) => { return new Promise((resolve, reject) => { if (showLoading) { uni.showLoading({ title: '上传中', mask: true }); } uni.uploadFile({ url: $mConfig.baseUrl + '/common/upload', filePath: file, name: 'file', success: (uploadFileRes) => { const data = JSON.parse(uploadFileRes.data) .data; resolve(data) }, fail(fail) { reject() // if ((successArr.length + failNum) >= FilePaths // .length) { // uni.hideLoading(); // } }, complete: () => { if (showLoading) { uni.hideLoading(); } } }) }) } export const getLocation = async () => { // // #ifdef APP-PLUS // let flag = await permision.showAuthTipModal("android.permission.ACCESS_FINE_LOCATION") // if (!flag) return // // #endif return new Promise((resolve, reject) => { uni.getLocation({ type: 'gcj02', geocode: true, cacheTimeout: 30 * 60, success: res => { resolve(res) }, fail: err => { permision.showManualAuth("android.permission.ACCESS_FINE_LOCATION") return resolve(null) }, }); }) } /** * 格式化日期 * @prama t 时间戳 * @return str MM-dd HH:mm */ export function formatDate(t) { t = t || Date.now(); let time = new Date(t); let str = time.getMonth() < 9 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1; str += '-'; str += time.getDate() < 10 ? '0' + time.getDate() : time.getDate(); str += ' '; str += time.getHours(); str += ':'; str += time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes(); return str; }; /** * 距当前时间点的时长 * @prama time 13位时间戳 * @return str x秒 / x分钟 / x小时 */ export function formateTime(time) { const second = 1000; const minute = second * 60; const hour = minute * 60; const day = hour * 24; const now = new Date().getTime(); const diffValue = now - time; // 计算差异时间的量级 const secondC = diffValue / second; const minC = diffValue / minute; const hourC = diffValue / hour; const dayC = diffValue / day; if (dayC >= 1) { return parseInt(dayC) + "天"; } else if (hourC >= 1) { return parseInt(hourC) + "小时"; } else if (minC >= 1) { return parseInt(minC) + "分钟"; } else if (secondC >= 1) { return parseInt(secondC) + "秒"; } else { return '0秒'; } } // 获取文件的唯一MD5标识码 export function getFileMd5 (file) { return new Promise((resolve, reject) => { const fileReader = new FileReader() const spark = new SparkMD5.ArrayBuffer() fileReader.readAsArrayBuffer(file) fileReader.onload = e => { spark.append(e.target.result) let md5 = spark.end() resolve(md5) } }) }