import { lang_en } from "../static/language/en.js"; // #ifdef H5 var jweixin = require('../jweixin'); //引入微信浏览器分享的jssdk // #endif /* * 判断分页是否还有数据 */ export function checkPaginationHasMore({ current, pageSize, total }) { return current * pageSize < total * 1; } // 检查字符串是否全是空格 export function checkSpace(str) { return str.trim() == "" ? true : false; } /* * 替换指定位置的字符 * str * startIndex 要替换的字符串的开始位置 * stopIndex 要替换的字符串的结束位置 * replacetext 指定位置要替换成的内容 */ export function replaceConByPosition(str, startIndex, stopIndex, replacetext) { let target_str = str.substring(0, startIndex - 1) + replacetext + str.substring(stopIndex + 1); return target_str; } /* * 返回一个数字的整数和小数 * number 需要处理的数据 * type: 要获取的数据 int 整数 decimal 小数 */ export function getPartNumber(number, type) { let target = ''; if (number == undefined) { return false; } number = number.toString(); if (type == 'int') { target = number.split('.')[0]; } else if (type == 'decimal') { target = number.split('.')[1] != undefined ? ('.' + number.split('.')[1]) : '.00'; if (target.length < 3) { target += '0'; } } return target; } // 手机号的验证 export function checkMobile(mobile) { let reg = /(1[3-9]\d{9}$)/; if (!mobile) { this.$api.msg('请输入手机号!'); return false; } else if (!reg.test(mobile)) { this.$api.msg('请输入正确的手机号!'); return false; } else { return true; } } // 6~20位,由英文、数字或符号组成的验证 export function checkPwd(pwd) { if (pwd.length < 6) { this.$api.msg('密码最少6位哦~'); return false; } else if (pwd.length > 20) { this.$api.msg('密码最多20位哦~'); return false; } else if (/[\u4E00-\u9FA5]/g.test(pwd)) { this.$api.msg('密码不可以有中文哦~'); return false; } else if (!(/^\S*$/.test(pwd))) { this.$api.msg('密码中不可以有空格哦~'); return false; } else { return true; } } //设置cookie,判断首页是否弹出开屏 export function setCookie() { uni.setStorage({ key: 'cookie', data: 'cookie' }); } //设置cookie,判断店铺首页是否弹出开屏 export function setStoreIsCookie(vid) { uni.setStorage({ key: 'storeIsCookie'+vid, data: 'storeIsCookie'+vid }); } //设置cookie,判断积分商城首页是否弹出开屏 export function setPointIsCookie() { uni.setStorage({ key: 'pointIsCookie', data: 'pointIsCookie' }); } // 登录成功的页面跳转 export function loginGoPage() { const pages = getCurrentPages(); let fromurl = uni.getStorageSync('fromurl'); if (fromurl) { uni.removeStorage({ key: 'fromurl', success: function(res) {} }); //#ifdef H5 uni.reLaunch({ url: fromurl }); return; //#endif if (fromurl.indexOf("pages/user/user") > -1) { uni.switchTab({ url: fromurl }); } else { if (pages.length > 1) { uni.navigateBack(); } else { uni.navigateTo({ url: fromurl }); } } return } //#ifdef H5 uni.reLaunch({ url: '/pages/user/user' }); return; //#endif if (pages.length > 1) { uni.navigateBack(); } else { uni.switchTab({ url: `/pages/user/user` }); } } // 数字格式化为以w为单位,保留2为小数 export function formatW(num) { return num > 10000 ? (num / 10000).toFixed(1) * 1 + 'w' : num; } // 邮箱的验证 export function checkEmail(email) { let reg = /^([a-zA-Z0-9]+[-_.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[-_.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,6}$/; if (!email) { this.$api.msg('请输入邮箱!'); return false; } else if (!reg.test(email)) { this.$api.msg('请输入正确的邮箱!'); return false; } else { return true; } } // 装修地址跳转 export function diyNavTo(val) { if (val.link_type == 'url') { //链接地址,只有h5可以跳转外部链接,其他端都不可以 //ifdef H5 window.location.href = val.link_value; //endif // #ifdef APP-PLUS plus.runtime.openURL(url) //这里默认使用外部浏览器打开而不是内部web-view组件打开 // #endif // #ifdef MP uni.navigateTo({ url: '/pages/index/skip_to?url=' + url }) // #endif } else if (val.link_type == 'keyword') { //关键词 uni.navigateTo({ url: `/pages/product/list?keyword=${encodeURIComponent(val.link_value)}&source=search` }); } else if (val.link_type == 'goods') { //商品 uni.navigateTo({ url: '/pages/product/detail?productId=' + val.info.defaultProductId + '&goodsId=' + val.info.goodsId }) } else if (val.link_type == 'category') { //商品分类 let url = `/pages/product/list?cateId=${val.info.categoryId}`; if (val.info.grade == 3) { url += '&pid=' + val.info.pid; } uni.navigateTo({ url: url }) } else if (val.link_type == 'topic') { //专题 uni.navigateTo({ url: `/pages/index/topic?id=${val.info.id}` }); } } // 装修类型判断 export function decoType(info) { let deco_obj = {} deco_obj.tupianzuhe = [] // deco_obj.gonggao = [] info.map(item => { if (item.type == 'more_tab') { //tab切换 deco_obj.more_tab = { border_radius: item.border_radius, data: item.data, } } else if (item.type == 'lunbo') { //轮播图 deco_obj.lunbo = { is_show: item.is_show, data: item.data } } else if (item.type == 'gonggao') { //公告 if (item.show_style == 'one') { deco_obj.gonggao1 = { show_style: item.show_style, text: item.text, is_show: item.is_show, } } else { deco_obj.gonggao2 = { show_style: item.show_style, text: item.text, is_show: item.is_show, } } } else if (item.type == 'top_cat_nav') { //顶部轮播图 deco_obj.top_cat_nav = { swiper_bg_style: item.swiper_bg_style, data: item.data } } else if (item.type == 'nav') { //导航 deco_obj.nav = { is_show: item.is_show, data: item.data, icon_set: item.icon_set } } else if (item.type == 'kefu') { //客服 deco_obj.kefu = { is_show: item.is_show, tel: item.tel, text: item.text } } else if (item.type == 'fuwenben') { //富文本 deco_obj.fuwenben = { is_show: item.is_show, text: item.text } } else if (item.type == 'tupianzuhe') { //图片组合 deco_obj.tupianzuhe.push({ is_show: item.is_show, sele_style: item.sele_style, width: item.width, height: item.height, data: item.data, }) } else if (item.type == 'dapei') { //搭配 deco_obj.dapei = { is_show: item.is_show, dapei_desc: item.dapei_desc, dapei_img: item.dapei_img, data: item.data, width: item.width, height: item.height } } else if (item.type == 'fzx') { //辅助线 deco_obj.fzx = { color: item.color, is_show: item.is_show, lrmargin: item.lrmargin, tbmargin: item.tbmargin, type: item.val } } else if (item.type == 'tuijianshangpin') { //推荐商品 deco_obj.recommond_goods = { border_style: item.border_style, data: item.data, goods_margin: item.goods_margin, is_show: item.is_show, page_margin: item.page_margin, show_style: item.small, text_align: item.text_align, text_style: item.normal, isshow_sales: item.isshow_sales } } else if (item.type == 'fzkb') { //辅助空白 deco_obj.fzkb = { color: item.color, is_show: item.is_show, text: item.text, } } }) return deco_obj } /** * 判断是否是微信浏览器 * * @zjf-2020-11-06 */ export function isWeiXinBrower() { //#ifdef H5 let ua = window.navigator.userAgent.toLowerCase(); if (ua.match(/MicroMessenger/i) == 'micromessenger') { return true; // 微信中打开 } else { return false; // 普通浏览器中打开 } //#endif //#ifndef H5 return false; //#endif } /** * 微信浏览器里面的分享功能 * type 分享类型 1 为微信好友分享 2为微信朋友圈分享 * shareData 分享数据数组 里面的参数分别如下: * title: '', // 分享标题 * desc: '', // 分享描述 * link: '', // 分享链接 * imgUrl: '', // 分享图片 * type: '', // 分享类型,music、video或link,不填默认为link * dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空 * isTs 是否推手模块,默认false * @zjf-2020-11-06 */ export function weiXinBrowerShare(type, shareData) { let tar_url = getApp().globalData.apiUrl + 'v3/member/front/login/wxjsConf?source=1'; uni.request({ url: tar_url, method: 'GET', data: { url: getApp().globalData.apiUrl }, success(res) { let data = res.data; // #ifdef H5 jweixin.config({ debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 appId: data.data.appId, // 必填,公众号的唯一标识 timestamp: data.data.timestamp, // 必填,生成签名的时间戳 nonceStr: data.data.nonceStr, // 必填,生成签名的随机串 signature: data.data.signature, // 必填,签名 jsApiList: ["updateAppMessageShareData", "updateTimelineShareData"] // 必填,需要使用的JS接口列表 }); jweixin.ready(function() { if (type == 1) { //获取“分享给朋友”按钮点击状态及自定义分享内容接口 jweixin.updateAppMessageShareData({ title: shareData.title != undefined ? shareData.title : '', // 分享标题 desc: shareData.desc != undefined ? shareData.desc : '', // 分享描述 link: shareData.link != undefined ? shareData.link : '', // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致 imgUrl: shareData.imgUrl, // 分享图标 type: '', // 分享类型,music、video或link,不填默认为link dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空 success: function() { } }) } else if (type == 2) { //获取“分享到朋友圈”按钮点击状态及自定义分享内容接口 wx.updateTimelineShareData({ title: shareData.title != undefined ? shareData.title : '', // 分享标题 link: shareData.link != undefined ? shareData.link : '', // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致 imgUrl: shareData.imgUrl, // 分享图标 success: function() { // 设置成功 } }) } }) //#endif } }) } /** * APP微信分享功能 * type 分享类型 0 图文 2 图片 * scene 场景 WXSceneSession 分享朋友 WXSenceTimeline 分享朋友圈 * shareData 分享数据数组 里面的参数分别如下: * 1、图文数据 * href: '', // 分享链接 * title: '', // 分享标题 * summary: '', // 分享描述 * imageUrl: '', // 分享图片 * 2、图片数据 * imageUrl: '', // 分享图片 * * @zjf-2020-11-12 */ export function weiXinAppShare(type, scene, shareData) { if (type == 0) { //分享图文 uni.share({ provider: "weixin", scene: scene, type: type, //0为图文 href: shareData.href, title: shareData.title, summary: shareData.summary, imageUrl: shareData.imageUrl, //图片,图片过大的话不展示,建议小于20kb success: function(res) { }, fail: function(err) { } }); } else if (type == 2) { //分享图片 uni.share({ provider: "weixin", scene: scene, type: type, //2为图片 imageUrl: shareData.imageUrl, //图片,图片过大的话不展示,建议小于20kb success: function(res) { console.log(res,'res') }, fail: function(err) { console.log(err,'err') } }); } } /** * 获取浏览器地址参数 * variable 为参数名,存在的话返回具体值,否则返回false * * @zjf-2020-11-17 */ export function getQueryVariable(variable) { var query = window.location.search.substring(1); var vars = query.split("&"); for (var i = 0; i < vars.length; i++) { var pair = vars[i].split("="); if (pair[0] == variable) { return pair[1]; } } return false; } /** * 微信浏览器里面的支付 * payData 支付数据数组 里面的参数分别如下: * timestamp: '', // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符 * nonceStr: '', // 支付签名随机串,不长于 32 位 * package: '', // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***) * signType: '', // 签名方式,默认为'SHA1',使用新版支付需传入'MD5' * paySign: '', // 支付签名 * appId: '', * success: function (res) { // 支付成功后的回调函数 } * fail: function (res) { // 失败时执行的回调函数 } * complete: function (res) { // 接口调用完成时执行的回调函数,无论成功或失败都会执行 } * cancel: function (res) { // 用户点击取消时的回调函数,仅部分有用户取消操作的api才会用到 } * trigger: function (res) { // 监听Menu中的按钮点击时触发的方法,该方法仅支持Menu中的相关接口 } * * @zjf-2020-11-06 */ export function weiXinBrowerPay(payData) { let tar_url = getApp().globalData.apiUrl + 'v3/member/front/login/wxjsConf?source=1'; uni.request({ url: tar_url, method: 'GET', data: { url: location.href }, success(res) { let data = res.data; // #ifdef H5 jweixin.config({ debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 appId: data.data.appId, // 必填,公众号的唯一标识 timestamp: data.data.timestamp, // 必填,生成签名的时间戳 nonceStr: data.data.nonceStr, // 必填,生成签名的随机串 signature: data.data.signature, // 必填,签名 jsApiList: ['chooseWXPay', 'scanQRCode'] // 必填,需要使用的JS接口列表 }); jweixin.ready(function() { jweixin.chooseWXPay(payData); }) //#endif } }) } /** * 通用提示 * con String 提示的内容,无特殊要求的话可不传 * * @zjf-2020-11-18 */ export function sldCommonTip(con = '该功能在升级中~') { uni.showToast({ title: con, icon: 'none', }); } export function formatPercent(val) { return val.substring(0, val.length - 1) } /** * 获取用户登录模块的终端类型 * * @zjf-2020-11-23 */ export function getLoginClient() { let client = 1; //终端类型, 1、H5(微信内部浏览器) 2、H5(微信小程序);3、app //#ifdef APP-PLUS client = 3; //#endif //#ifdef MP-WEIXIN client = 2; //#endif //#ifdef H5 client = 1; //#endif return client; } /** * 防止用户多此点击触发事件 * @ljp - 2021-2-7 * */ export function frequentyleClick(fn) { let that = this; if (that.onOff) { that.onOff = false; fn(); setTimeout(() => { that.onOff = true; }, 1500) } else { //如果一直走else,可能是你没有页面的data下面挂载onOff = true; 不然一直会走else } } /** * h5端页面返回处理,刷新后返回到首页 * @ww - 2021-2-21 * */ export function back(fn) { // #ifdef H5 const pages = getCurrentPages() if (pages.length > 1) { uni.navigateBack(1) return; } //使用vue-router返回上一级 let a = this.$router.go(-1) if (a == undefined) { //重新定向跳转页面 uni.reLaunch({ url: '/pages/index/index' }) } return; // #endif // #ifndef H5 const pages = getCurrentPages() if (pages.length > 1) { uni.navigateBack(1) return; } else { uni.reLaunch({ url: '/pages/index/index' }) } // #endif H5 } /* * 获取当前语言下的数据 —— Object类型 * 返回对象 语言数据对象 * @zjf-2020-12-28 * */ export function getCurLanguage(key) { let curLang = getApp().globalData.curLang; if (curLang == 'zh') { return key; } else { const language = { 'en': lang_en, } let curData = language[curLang][key]; return curData != undefined && curData ? curData : '语言包中缺少该数据'; //此处不要翻译 } } /* * 判断是否显示聊天页面的时间,2条消息之间间隔超过3分钟显示 * 返回Boolean类型 * preMsgTime 上一条消息的发送时间,curMsgTime该条消息的发送时间 * @zjf-2021-03-05 * */ export function isShowTime(preMsgTime, curMsgTime) { let res = false; // #ifdef APP-PLUS if(uni.getSystemInfoSync().platform==='ios'&&preMsgTime!=undefined&&curMsgTime!=undefined){//ios系统不识别该时间格式,进行专门格式化 let arr = [preMsgTime, curMsgTime].map(item=>item.toString().split(/[- :]/)) let newDate = arr.map(item=>item=new Date(item[0],item[1]-1,item[2],item[3],item[4],item[5])) if (Date.parse(newDate[1]) * 1 - Date.parse(newDate[0]) * 1 > 3 * 60 * 1000) { res = true; } }else if(uni.getSystemInfoSync().platform==='android'&&preMsgTime!=undefined&&curMsgTime!=undefined){ if (Date.parse(new Date(curMsgTime)) * 1 - Date.parse(new Date(preMsgTime)) * 1 > 3 * 60 * 1000) { res = true; } } // #endif // #ifndef APP-PLUS if (Date.parse(new Date(curMsgTime.replace(/-/g, '/'))) * 1 - Date.parse(new Date(preMsgTime.replace(/-/g, '/'))) * 1 > 3 * 60 * 1000) { res = true; } // #endif return res; } /* * 格式化聊天时间 * 返回格式化后的数据,字符串类型 * time 时间戳 13位 * @zjf-2021-03-05 * */ export function formatChatTime(time) { // #ifdef APP-PLUS if(uni.getSystemInfoSync().platform==='ios'){//ios系统不识别该时间格式,进行专门格式化 let arr = time.split(/[- :]/) let newDate = new Date( arr[0], arr[1]-1, arr[2], arr[3], arr[4], arr[5] ) return format(newDate,'yyyy年MM月dd日 hh:mm') }else{ return format(new Date(time), 'yyyy年MM月dd日 hh:mm'); } // #endif // #ifndef APP-PLUS return format(new Date(time.replace(/-/g, '/')), 'yyyy年MM月dd日 hh:mm'); // #endif } export function format(date, fmt) { let o = { "y+": date.getFullYear() , //年 "M+": date.getMonth() + 1, //月份 "d+": date.getDate(), //日 "h+": date.getHours(), //小时 "m+": date.getMinutes(), //分 "s+": date.getSeconds(), //秒 "q+": Math.floor((date.getMonth() + 3) / 3), //季度 "S": date.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length)); } for (let k in o) { if (new RegExp("(" + k + ")").test(fmt)) { fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); } } return fmt; } /** * 基于uniapp.uploadFile的多文件上传,支持 跨端 * fileArray:Array * params:Object * success:Function * @lijm-2021-06-15 */ export function multifileUpload({fileArray,params,success,fail}){ if(!fileArray || !params){ return ''; } let promiseArray=[]; fileArray.forEach((item,index)=>{ params.filePath=item; params.formData.file=item; //将每个请求封装成promise promiseArray[index]=uni.uploadFile(params) }) if(success && typeof success ==='function'){ //当所有请求完成后,调用success回调,返回请求后的reponse Promise.all(promiseArray).then((res)=>{ let data=[] res.forEach((item,index)=>{ if(JSON.parse(item[1].data).state===200){ data.push(JSON.parse(item[1].data)) } }) if(data.length){ success(data); } //如果有失败回调,则返回失败信息 if(fail && typeof fail==='function'){ let errData=''; let tempStatus=res.some((item)=>{ if(JSON.parse(item[1].data).state != 200){ errData=JSON.parse(item[1].data); return true; } return false; }) fail(errData || {status:200,msg:'no errors'}) } }) } }