import { API_URL, BASE_URL } from '@/env' // #ifdef APP-VUE import permision from "@/common/permission.js" // #endif import { setStorage, getStorage } from "@/util/index.js" const tools = { async chooseLocation() { return new Promise((resolve,reject) => { uni.chooseLocation({ keyword: '', success: function (res) { console.log('位置名称:res.name' + res.name); console.log('详细地址:res.address' + res.address); console.log('纬度:res.latitude' + res.latitude); console.log('经度:res.longitude' + res.longitude); resolve(res) } }); }) }, async getLocation() { const that = this; return new Promise((resolve,reject) => { uni.getLocation({type:'gcj02',geocode: true,success: (data)=> { let form = { latitude: data.latitude, longitude: data.longitude, }; // const {province, city, district, street, streetNum} = data.address; // form.address = province + city + district + street + streetNum; // form.lat = data.latitude + ''; // form.lon = data.longitude + ''; // form.province = province + ''; // form.city = city + ''; // form.county = district + ''; // that.coords({lat:form.lat,lon:form.lon}).then(res => { // const latitude = res.result[0].x; // const longitude = res.result[0].y; // form.lat = latitude; // form.lon = longitude; // resolve(form) // }) resolve(form) }, fail: (err) => { console.log(err) }}) }) }, // 图片处理-上传图片 coords(coords = {lat: '',lon: ''}, loading = true) { if(loading) { uni.showLoading({ title: '获取坐标请稍后' }); } return new Promise((resolve, reject) => { uni.request({ url: 'http://api.map.baidu.com/geoconv/v1/?coords=' + coords.lat + ',' + coords.lon + '&from=3&to=5&ak=YB8Sm61mr99t7RyZ2kUWvTAorO8b5dGV' + '&mcode=AB:80:5D:71:E3:31:EC:CA:DE:F1:60:67:13:E2:03:8C:CE:90:6B:1B;uni.UNIF3301E3', data: {}, header: { }, success: (res) => { uni.hideLoading() // console.log(res.data); resolve(res.data) }, fail: (res) => { uni.hideLoading() // console.log(res.data); resolve(res.data) }, }); }) }, /** * fn:检测图片协议,主要用于检测海报图片协议。 * param(imgPath): 图片地址。 */ checkImgHttp(imgPath) { let newPath = ''; let pathArr = imgPath.split('://'); // #ifdef H5 let ishttps = 'https:' == window.location.protocol ? true : false; ishttps ? (pathArr[0] = 'https') : (pathArr[0] = 'http'); // #endif // #ifdef MP-WEIXIN pathArr[0] = 'https' // #endif newPath = pathArr.join('://'); return newPath; }, // 打电话 callPhone(phoneNumber = '') { let num = phoneNumber.toString() uni.makePhoneCall({ phoneNumber: num, fail(err) { console.log('makePhoneCall出错', err) }, }); }, // 图片处理-选择图片 async chooseImage(count = 1, type = 'all') { if(type == 'album'){ let checkPermission = await this.checkAppAlbum(); if (checkPermission) { return new Promise((resolve, reject) => { uni.chooseImage({ count: count, //默认9 sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有 sourceType: [type], //从相册选择 success: res => { resolve(res.tempFilePaths); } }); }).catch(e => { reject(e) }) } } else if(type == 'camera'){ return new Promise((resolve, reject) => { uni.chooseImage({ count: count, //默认9 sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有 sourceType: [type], //从相册选择 success: res => { resolve(res.tempFilePaths); } }); }).catch(e => { reject(e) }) }else if(type == 'all'){ return new Promise((resolve, reject) => { uni.chooseImage({ count: count, //默认9 sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], //从相册选择 success: res => { resolve(res.tempFilePaths); } }); }).catch(e => { reject(e) }) } }, // 图片处理-上传图片 uploadImage(url, loading = true) { let config_url = API_URL; let api = '/upload_file/upload_img' if(loading) { uni.showLoading({ title: '上传中' }); } return new Promise((resolve, reject) => { uni.uploadFile({ url: config_url + api, filePath: url, name: 'file', header: { token: getStorage('token') || '', }, success: res => { res = JSON.parse(res.data); if (res.code === 1) { if(loading) { uni.hideLoading() uni.showToast({ title: '上传成功', icon: 'none' }); } resolve(res.data.src) } else { if(loading) { uni.hideLoading() uni.showModal({ title: '上传失败', content: res.msg }); } reject('上传失败') } }, fail: err => { if(loading) { uni.hideLoading() uni.showModal({ title: '上传失败', content: err.msg }); } reject(err) }, }); }).catch(e => { reject(e) }) }, // 图片处理-上传图片 uploadImageAll(urls) { uni.showLoading({ title: '上传中' }); return new Promise((resolve, reject) => { // 多图上传 const promiseList = urls.map(url => { return tools.uploadImage(url, false) }) Promise.all(promiseList).then(res => { uni.hideLoading() uni.showToast({ title: '上传成功', icon: 'none' }); resolve(res) }).catch(err =>{ uni.hideLoading() uni.showToast({ title: '上传失败', icon: 'none' }); }) }).catch(e => { uni.hideLoading() reject(e) }) }, // 图片处理-预览图片 previewImage(urls = [], current = 0) { uni.previewImage({ urls: urls, current: current, indicator: 'default', loop: true, fail(err) { console.log('previewImage出错', urls, err) }, }) }, // 图片处理-获取图片信息 getImageInfo(src = '') { return new Promise((resolve, reject) => { uni.getImageInfo({ src: src, success: (image) => { resolve(image) }, fail(err) { console.log('getImageInfo出错', src, err) }, }) }).catch(e => { reject(e) }) }, /** * 格式化时间 */ //时间格式化 天时分秒 format(t) { let format = { d: '00', h: '00', m: '00', s: '00', } if (t > 0) { let d = Math.floor(t / 86400) let h = Math.floor((t / 3600) % 24) let m = Math.floor((t / 60) % 60) let s = Math.floor(t % 60) format.d = d < 10 ? '0' + d : d format.h = h < 10 ? '0' + h : h format.m = m < 10 ? '0' + m : m format.s = s < 10 ? '0' + s : s } return format }, //时间格式化(格式化最大为小时) formatToHours(t) { let format = { d: '00', h: '00', m: '00', s: '00', } if (t > 0) { let h = Math.floor(t / 3600) let m = Math.floor((t / 60) % 60) let s = Math.floor(t % 60) format.h = h < 10 ? '0' + h : h format.m = m < 10 ? '0' + m : m format.s = s < 10 ? '0' + s : s } return format }, // 年月日 timestamp(timestamp) { let date = new Date(timestamp * 1000); //根据时间戳生成的时间对象 let y = date.getFullYear(); let m = date.getMonth() + 1; let d = date.getDate(); m = m < 10 ? '0' + m : m; d = d < 10 ? '0' + d : d let dateText = y + "-" + m + "-" + d return dateText }, // 年月日,时分秒 // "YYYY-mm-dd HH:MM" dateFormat(fmt, date) { let ret; const opt = { "Y+": date.getFullYear().toString(), // 年 "m+": (date.getMonth() + 1).toString(), // 月 "d+": date.getDate().toString(), // 日 "H+": date.getHours().toString(), // 时 "M+": date.getMinutes().toString(), // 分 "S+": date.getSeconds().toString() // 秒 // 有其他格式化字符需求可以继续添加,必须转化成字符串 }; for (let k in opt) { ret = new RegExp("(" + k + ")").exec(fmt); if (ret) { fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0"))) }; }; return fmt; }, /** * @fn 时间间隔格式化 * @param {*} startTime 开始时间的时间戳 * @param {*} endTime 结束时间的时间戳 * @return {string} str 返回时间字符串 */ getTimeInterval(startTime, endTime) { let runTime = parseInt((endTime - startTime) / 1000); let year = Math.floor(runTime / 86400 / 365); runTime = runTime % (86400 * 365); let month = Math.floor(runTime / 86400 / 30); runTime = runTime % (86400 * 30); let day = Math.floor(runTime / 86400); runTime = runTime % 86400; let hour = Math.floor(runTime / 3600); runTime = runTime % 3600; let minute = Math.floor(runTime / 60); runTime = runTime % 60; let second = runTime; let str = ''; if (year > 0) { str = year + '年'; } if (year <= 0 && month > 0) { str = month + '月'; } if (year <= 0 && month <= 0 && day > 0) { str = day + '天'; } if (year <= 0 && month <= 0 && day <= 0 && hour > 0) { str = hour + '小时'; } if (year <= 0 && month <= 0 && day <= 0 && hour <= 0 && minute > 0) { str = minute + '分钟'; } if (year <= 0 && month <= 0 && day <= 0 && hour <= 0 && minute <= 0 && second > 0) { str += second + '秒'; } str += '前'; return str; }, /** * @fn 时间间隔格式化 * @param {*} startTime 开始时间的时间戳 * @param {*} endTime 结束时间的时间戳 * @return {string} str 返回时间字符串 */ getDayInterval(startTime, endTime) { let runTime = parseInt((endTime - startTime)/1000); //计算出相差天数 var days = Math.floor(runTime / (24 * 3600)); if(days == 0) days = 1; return days; }, /**提示框 *title(标题) *icon(图标): success,loading,none *duration(延时): 0为不关闭, 毫秒数 *options(其它参数) */ toast(title, icon = 'none', options) { uni.showToast({ title: title || '', icon: icon, duration: (options && options.duration) || 1500, image: (options && options.image) || '', mask: (options && options.mask) || true, }); }, /** * app检测获取相册权限 * @return {String} status 1: 有权限。 */ async checkAppAlbum() { // #ifdef APP-VUE let status = permision.isIOS ? await permision.requestIOS('album') : await permision.requestAndroid([ 'android.permission.READ_EXTERNAL_STORAGE', 'android.permission.WRITE_EXTERNAL_STORAGE' ]); if (status !== 1) { uni.showModal({ content: '需要相册读写权限', confirmText: '设置', success: function(res) { if (res.confirm) { permision.gotoAppSetting(); } } }); return false } else { return true } // #endif // #ifndef APP-VUE return true // #endif }, // 简单处理一下 parse html parseHtml(htmlStr){ function formatUrl(url){ if(url.indexOf('http') >= 0){ return url }else{ return BASE_URL + url; } } var newContent= htmlStr.replace(/]*src=['"]([^'"]+)[^>]*>/gi,function(match,capture){   var newStr='';   return newStr; }); return newContent } } export default tools