import config from "config" // #ifdef APP-PLUS // import checkUpdateApp from "@/utils/updateapp.js" // #endif // uploadImg 上传图片方法 // openGaoDeMap 打开高德地图APP // openBaiDuMap 打开百度地图APP // authTo 判断是否登录,未登录提示登录 // isLoginCallback 判断是否登录,未登录提示登录,已登录执行回调方法 // to 跳转页面路由 // switchTab 跳转到tabbar页面 // redirectTo 关闭当前页面,跳转到下一个页面 // reLaunch 关闭所有页面,跳转到下一个页面 // goBack 页面返回 // onCall 拨打电话 // onCopy 复制内容 // isImage 是否为图片地址 // isVideo 是否为视频地址 // testEmail 检测是否是邮箱 // testMobile 检测是否是手机号 // testUrl 检测是否是url地址 // testIdCard 检测是否是身份证号 // previewImage 预览图片 // timeFormat 时间戳格式化为日期 时分秒 与uView方法一致 // isAMorPM 判断时间戳转为上午或下午 // ellipsisText 省略文本 裁剪文本 // capsulHeight 微信小程序胶囊自身高度 // capsulTop 微信小程序胶囊距离页面顶部的高度 // lightFn 匹配一段文字改为其他颜色 // throttle 节流方法 // debounce 防抖方法 // toast 弹窗 // pay 微信小程序支付或APP微信支付或APP支付宝支付 // onSaveImageAlbum // 保存照片到相册 /** * 上传图片 * @author 万世科技 * @param {String} imgurl 图片地址 */ function uploadImg(imgurl) { return new Promise((resolve, reject) => { const token = uni.getStorageSync('token') try { uni.uploadFile({ url: config.domain + '/uploadimg', header: { "authorization": token || '', }, filePath: imgurl, name: 'file', success: res => { const data = JSON.parse(res.data) console.log(data) if (data.code === 200) { resolve(data.data.url) } else { reject(data) } }, fail: err => { reject(err) // console.log(err) } }) } catch (err) { reject(err) } }) } /** * 打开高德地图APP * @author 万世科技 * @param {String} current_latitude 当前纬度 * @param {String} current_longitude 当前经度 * @param {String} current_address 当前所在地名称 * @param {String} shop_latitude 目的地纬度 * @param {String} shop_longtude 目的地经度 * @param {String} shop_address 目的地名称 */ // #ifdef APP-PLUS function openGaoDeMap(current_latitude, current_longitude, current_address, shop_latitude, shop_longtude, shop_address) { if (uni.$u.os() === 'android') { var packageName = 'com.autonavi.minimap'; var main = plus.android.runtimeMainActivity(); var packageManager = main.getPackageManager(); var PackageManager = plus.android.importClass(packageManager); var packageInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES); if (packageInfo) { var Uri = plus.android.importClass("android.net.Uri"); var url = "amapuri://route/plan?sourceApplication=maxuslife" + "&sid=A&slat=" + current_latitude + "&slon=" + current_longitude + "&sname=" + current_address + "&did=B&dlat=" + shop_latitude + "&dlon=" + shop_longtude + "&dname=" + shop_address + "D10&dev=0&t=0"; var Intent = plus.android.importClass('android.content.Intent'); var intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.addCategory(Intent.CATEGORY_DEFAULT); var uri = Uri.parse(url); //将功能Scheme以URI的方式传入 intent.setData(uri); intent.setPackage("com.autonavi.minimap"); uni.hideLoading() var main = plus.android.runtimeMainActivity(); main.startActivity(intent); } else { uni.hideLoading() this.$u.toast('未安装高德地图APP,请先安装') } } else if (uni.$u.os() === 'ios') { const url = `baidumap://map/direction?origin=${current_latitude},${current_longitude}&destination=${shop_latitude},${shop_longtude}&origin_region=${current_address}&destination_region=${shop_address}&mode=driving&coord_type=gcj02&src=ios.companyName.appName` plus.runtime.openURL(encodeURI(url), function(e) { plus.nativeUI.alert("本机未安装指定的地图应用"); }); } } // #endif /** * 打开百度地图APP * @author 万世科技 * @param {String} current_latitude 当前纬度 * @param {String} current_longitude 当前经度 * @param {String} current_address 当前所在地名称 * @param {String} shop_latitude 目的地纬度 * @param {String} shop_longtude 目的地经度 * @param {String} shop_address 目的地名称 */ // #ifdef APP-PLUS function openBaiDuMap(current_latitude, current_longitude, current_address, shop_latitude, shop_longtude, shop_address) { if (uni.$u.os() === 'android') { var packageName = 'com.baidu.BaiduMap'; var main = plus.android.runtimeMainActivity(); var packageManager = main.getPackageManager(); var PackageManager = plus.android.importClass(packageManager); var packageInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES); if (packageInfo) { var Uri = plus.android.importClass("android.net.Uri"); var url = `baidumap://map/direction?&origin=name:${current_address}|${shop_latitude},${shop_longtude}&destination=${shop_address}&coord_type=gcj02&mode=driving&src=andr.baidu.openAPIdemo`; var Intent = plus.android.importClass('android.content.Intent'); var intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.addCategory(Intent.CATEGORY_DEFAULT); var uri = Uri.parse(url); //将功能Scheme以URI的方式传入 intent.setData(uri); intent.setPackage("com.baidu.BaiduMap"); uni.hideLoading() var main = plus.android.runtimeMainActivity(); main.startActivity(intent); } else { uni.hideLoading() this.$u.toast('未安装百度地图APP,请先安装') } } else if (uni.$u.os() === 'ios') { const url = `baidumap://map/direction?origin=${current_latitude},${current_longitude}&destination=${shop_latitude},${shop_longtude}&origin_region=${current_address}&destination_region=${shop_address}&mode=driving&coord_type=gcj02&src=ios.companyName.appName` plus.runtime.openURL(encodeURI(url), function(e) { plus.nativeUI.alert("本机未安装指定的地图应用"); }); } } // #endif /** * 跳转路径 * @param {String} url 跳转的页面地址 */ function to(url) { uni.navigateTo({ url }) } /** * 判断是否登录,未登录提示 点击跳转到登录页 * @param {String} url 跳转的页面地址 */ function authTo(url) { const token = uni.getStorageSync('token') if (!token) { uni.showModal({ title: '提示', content: '您还未登录,请先登录', success(res) { if (res.confirm) { uni.reLaunch({ url: '/pages/login/wechatlogin' }) } else if (res.cancel) { } } }) } else { uni.navigateTo({ url }) } } /** * 判断是否登录,未登录提示 ,点击跳转到登录页,已登录执行回调 * @param {funciton} callback 执行的回调函数 */ function isLoginCallback(callback) { const token = uni.getStorageSync('token') if (!token) { uni.showModal({ title: '提示', content: '您还未登录,请先登录', success(res) { if (res.confirm) { uni.reLaunch({ url: '/pages/login/wechatlogin' }) } else if (res.cancel) { } } }) } else { callback() } } /** * 跳转tabbar路径 * @param {String} url 跳转的tabar地址 */ function switchTab(url) { uni.switchTab({ url }) } /** * 跳转到下一个页面,关闭当前页面 * @param {String} url 跳转的页面地址 */ function redirectTo(url) { uni.redirectTo({ url }) } /** * 关闭所有页面,跳转到下一个页面 * @param {String} url 跳转的页面地址 */ function reLaunch(url) { uni.reLaunch({ url }) } /** * 返回 * @param {Number} num 返回几层 */ function goBack(num = 1) { uni.navigateBack({ delta: Number(num) }) } /** * 预览图片 * @param {String} urls 图片数组 */ function previewImage(urls, index = 0) { uni.previewImage({ urls, current: index }) } /** * 打电话 * @param {String} phone 电话号码 */ function onCall(phone) { uni.makePhoneCall({ phoneNumber: phone, fail: (err) => { console.log(err) } }); // if(uni.$u.test.mobile(phone)){ // uni.makePhoneCall({ // phoneNumber: phone // }); // } else { // uni.$u.toast('手机号码错误') // } } /** * 复制 * @param {String} text 需复制的文本 */ function onCopy(text) { uni.setClipboardData({ data: text, success: () => { uni.showToast({ title: '复制成功', icon: "none" }) } }); } /** * 检测图片地址 * @param {String} url 图片地址 */ function isImage(url) { if (!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(url)) { return false } else { return true } } /** * 检测视频地址 * @param {String} url 视频地址 */ const isVideo = (url) => { if (!/\.(mp4|mov|rmvb|AVI)$/.test(url)) { return false } else { return true } } /** * 检测是否是邮箱 * @author 万世科技 * @param {String} email 邮箱号 */ function testEmail(email) { return uni.$u.test.email(email) } /** * 检测是否是手机号 * @author 万世科技 * @param {String} mobile 手机号 */ function testMobile(mobile) { return uni.$u.test.mobile(mobile) } /** * 检测是否是Url * @author 万世科技 * @param {String} url url地址 */ function testUrl(url) { return uni.$u.test.url(url) } /** * 检测是否是身份证号 * @param {String} idCard 身份证号 */ function testIdCard(idCard) { return uni.$u.test.idCard(idCard) } /** * 时间格式化 * @param {Number} date 时间戳 */ function timeFormat(date, format = 'yyyy-mm-dd hh:MM') { return uni.$u.timeFormat(date, format) } /** * 时间戳转上午或下午 * @param {Number} timestamp 时间戳 */ function isAMorPM(timestamp) { var date = new Date(timestamp); var hours = date.getHours(); return hours < 12 ? '上午' : '下午'; } /** * 省略文本 * @param {String} text 文本 * @param {Number} len 希望返回的长度 */ function ellipsisText(text, len) { return text.length > len ? text.substr(0, len) + '...' : text } let capsulHeight = '' // 胶囊的高度 let capsulTop = '' // 胶囊距离手机最顶部的高度 /** * 获取胶囊距离顶部高度(微信小程序) */ // #ifdef MP-WEIXIN let { height, top } = wx.getMenuButtonBoundingClientRect(); capsulHeight = height capsulTop = top // #endif // #ifdef MP-ALIPAY let { height, top } = my.getMenuButtonBoundingClientRect(); capsulHeight = height capsulTop = top // #endif /** * 修改一段文字中某写文字的颜色 * @param {String} fullText 完整的文字段落 * @param {String} text 需要改变颜色的文字段落 * @param {String} color 颜色 */ function lightFn(fullText, text, color = '#4FE4C4') { // 使用正则找到str,'ig'忽略大小写 const reg = new RegExp(text, 'ig') // 返回一个字符串,str里有return回来的字符串进行替换 return fullText.replace(reg, (match) => { return `${match}` }) } /** * 节流 * @param {String} callback 回调函数 * @param {String} time 节流间隔时间 */ function throttle(callback, time = 1000) { uni.$u.throttle(callback, time) } /** * 防抖 * @param {String} callback 回调函数 * @param {String} time 防抖间隔时间 */ function debounce(callback, time = 1000) { uni.$u.debounce(callback, time) } /** * 弹窗 * @param {String} text 弹出的文字 */ function toast(text) { uni.$u.toast(text) } /** * 微信小程序支付或APP微信支付或APP支付宝支付 * @param {Object} orderInfo 支付信息对象 * @param {function} fn1 支付成功的回调函数 * @param {function} fn2 支付失败的回调函数 */ function pay(orderInfo, fn1, fn2) { // #ifdef APP-PLUS uni.requestPayment({ provider, orderInfo, // success: (res) => { // console.log(res); fn1(res) }, fail: (err) => { fn2(err) } }); // #endif // #ifdef MP-WEIXIN uni.requestPayment({ provider, appid: orderInfo.appId, // 微信开放平台 - 应用 - AppId,注意和微信小程序、公众号 AppId 可能不一致 nonceStr: orderInfo.nonceStr, // 随机字符串 package: orderInfo.package, // 固定值 timeStamp: orderInfo.timeStamp, // 时间戳(单位:秒) paySign: orderInfo.paySign, // 签名,这里用的 MD5/RSA 签名 signType: 'MD5', success: (res) => { // console.log(res) // if(res.errMsg === 'requestPayment:ok') { // fn1(res) // } fn1(res) }, fail: (err) => { fn2(err) } }); // #endif } /** * 保存图片到相册 * @param {String} url 图片地址 */ function onSaveImageAlbum(url) { uni.getImageInfo({ src: url, success: (res) => { console.log(res) // #ifndef H5 uni.saveImageToPhotosAlbum({ filePath: res.path || res.tempFilePaths, success: () => { uni.$u.toast('保存成功') } }) // #endif } }) } /** * 微信浏览器支付(不使用SDK) * @param {Object} params 支付参数 */ function wxBrowserPay(params) { WeixinJSBridge.invoke('getBrandWCPayRequest', { "appId": params.data.appId, //公众号ID,由商户传入 "timeStamp": params.data.timeStamp, //时间戳,自1970年以来的秒数 "nonceStr": params.data.nonceStr, //随机串 "package": params.data.package, "signType": params.data.signType, //微信签名方式: "paySign": params.data.paySign //微信签名 }, function(res) { if (res.err_msg == "get_brand_wcpay_request:ok") { // 使用以上方式判断前端返回,微信团队郑重提示: //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。 params.success(res) } else { params.fail(res) } } ); } const wskj_function = { uploadImg, authTo, isLoginCallback, to, switchTab, redirectTo, reLaunch, goBack, onCall, onCopy, isImage, isVideo, testEmail, testMobile, testUrl, testIdCard, previewImage, timeFormat, isAMorPM, ellipsisText, // #ifdef MP-WEIXIN || MP-ALIPAY capsulHeight, capsulTop, // #endif // #ifdef APP-PLUS openGaoDeMap, openBaiDuMap, // checkUpdateApp, // #endif lightFn, throttle, debounce, toast, pay, onSaveImageAlbum, wxBrowserPay } export default wskj_function