import { API_URL } from '@/env' import { router } from '@/common/router' // #ifdef H5 import $wxsdk from '@/common/utils/sdk-h5-weixin'; // #endif export default { /** * 跳转再封装,主要是为了兼容外链。 * @param {String} path - 跳转路径 * @param {isTabbar} isTabbar - 是否是底部导航 */ routerTo(path, isTabbar) { if (path) { // 是否跳转外部链接 if (~path.indexOf('http') || ~path.indexOf('www')) { // #ifdef H5 window.location = path; // #endif // #ifndef H5 router.push({ path: '/pages/public/webview', query: { 'webviewPath': path } }) // #endif return false } if (isTabbar) { router.replaceAll(path) } else { path.includes('/pages/index') && !path.includes('/pages/index/view') ? router.replaceAll(path) : router .push(path) } } else { console.log(`%cerr:没有填写跳转路径`, 'color:green;background:yellow'); } }, /** * 导航-打开位置 * @param {Number} longitude - 经度 * @param {Number} latitude - 纬度 * @param {String} address - 地理位置 */ openLocation(longitude, latitude, address) { uni.openLocation({ longitude: parseFloat(longitude), latitude: parseFloat(latitude), address: address }) }, /** * 图片处理-预览图片 * @param {Array} urls - 图片列表 * @param {Number} current - 首个预览下标 */ previewImage(urls = [], current = 0) { uni.previewImage({ urls: urls, current: current, indicator: 'default', loop: true, fail(err) { console.log('previewImage出错', urls, err) }, }) }, /** * 数据分组 * @param {Array} oArr - 原数组列表 * @param {Number} length - 单个数组长度 * @return {Array} arr - 分组后的新数组 */ splitData(oArr = [], length = 1) { let arr = []; let minArr = []; oArr.forEach(c => { if (minArr.length === length) { minArr = []; } if (minArr.length === 0) { arr.push(minArr); } minArr.push(c); }); return arr; }, /** * 获取指定日期 * @param {Date} date - 传入的时间Date对象 * @return {Object} days - 添加的天数,可以是正数、负数或零 */ getDaysToDate(date, days) { console.log(date) if (!(date instanceof Date)) { throw new Error('第一个参数必须是Date对象'); } if (typeof days !== 'number') { throw new Error('第二个参数必须是数字'); } const newDate = new Date(date) newDate.setDate(date.getDate() + days) console.log(newDate) return newDate; }, /** * 剩余时间格式化 * @param {Number} t - 剩余多少秒 * @return {Object} format - 格式后的天时分秒对象 */ 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 format.h = h format.m = m format.s = s } return format; }, /** * 打电话 * @param {String} phoneNumber - 数字字符串 */ callPhone(phoneNumber = '') { let num = phoneNumber.toString() uni.makePhoneCall({ phoneNumber: num, fail(err) { console.log('makePhoneCall出错', err) }, }); }, /** * 微信头像 * @param {String} url -图片地址 */ checkMPUrl(url) { // #ifdef MP if ( url.substring(0, 4) === 'http' && url.substring(0, 5) !== 'https' && url.substring(0, 12) !== 'http://store' && url.substring(0, 10) !== 'http://tmp' && url.substring(0, 10) !== 'http://usr' ) { url = 'https' + url.substring(4, url.length); } // #endif return url; }, /** * 中间部分* */ hiddenText(str, frontLen, endLen) { //str:要进行隐藏的变量 frontLen: 前面需要保留几位 endLen: 后面需要保留几位 var len = str.length - frontLen - endLen; var xing = ""; for (var i = 0; i < len; i++) { xing += "*"; } return ( str.substring(0, frontLen) + xing + str.substring(str.length - endLen) ) }, amountCN(n) { const fraction = ['角', '分'] const digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'] const unit = [ ['元', '万', '亿'], ['', '拾', '佰', '仟'] ] const head = n < 0 ? '欠' : '' n = Math.abs(n) let s = '' for (let i = 0; i < fraction.length; i++) { s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '') } s = s || '整' n = Math.floor(n) // eslint-disable-next-line no-redeclare for (let i = 0; i < unit[0].length && n > 0; i++) { let p = '' for (let j = 0; j < unit[1].length && n > 0; j++) { p = digit[n % 10] + unit[1][j] + p n = Math.floor(n / 10) } s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s } return ( head + s .replace(/(零.)*零元/, '元') .replace(/(零.)+/g, '零') .replace(/^整$/, '零元整') ) }, // 分享复制 shareCopy(page) { // #ifdef H5 const url = location.href.split('/pages')[0]; uni.setClipboardData({ data: url + page, showToast: false, success() { uni.showToast({ mask: true, title: '您已复制邀请链接,快去分享吧', icon: 'success' }) // uni.showModal({ // title: '温馨提示', // content: '您已复制邀请链接,快去分享吧', // showCancel: false // }) }, fail() { uni.$u.toast('复制失败,请重新点击') } }) // #endif }, // H5设置分享信息 shareHandle(detail) { // #ifdef H5 const url = location.href.split('/pages')[0]; $wxsdk.updateShareInfo({ title: detail.title, desc: detail.desc, link: url + detail.link, image: detail.image }); // #endif }, }