import IDCard from "../vendor/IDCard.js"; const dialog=uni.requireNativePlugin("CL-Dialog"); /** * @param {string} text 提示内容 * @param {int} time 多少时间消失 * @return {function} fn 函数 * */ const Tip = (text, time = 2000, fn = null) => { uni.showToast({ 'title' : text, duration : time, icon:'none' }); if (fn != null) setTimeout(() => {fn();}, time); } /** * 加载数据 */ const loadIng = (title = false,mask = false) => { uni.showLoading({ title: title ? title : "加载中...", mask : mask }); } //询问框 const showModal = (title,success = null,fail = null) =>{ if(dialog != null){ let options = { title: "系统提示", //标题 (可选) con: title, //内容(可选)但是标题和内容至少选择一个 okTitle: "确认", //确认按钮文字(可选) cancleTitle: "取消", //取消按钮文字(可选) okTextColor: "#f1851e", //确认按钮颜色(可选) cancleTextColor: "#999999", //取消按钮颜色(可选) singer: false, //是否只显示确认按钮,默认false(可选 textAlign: "center", //对齐方式 //left居左,center居中 ,right 居右 默认居中 conColor: "", bgColor: "#ffffff", //自定义弹框颜色 titleColor: "#3d3d3d" //自定义title颜色 //提示框内容颜色; } dialog.show(options,()=>{ if(success != null) success(); },()=>{ if(fail != null) fail(); }); } else { uni.showModal({ title: "系统提示", content: title, confirmText: "确定", cancelText: "取消", success:function(res){ if(res.confirm && success != null){ success(); return; } if(!res.confirm && fail != null){ fail(); return; } } }) } } /** * 提示框 */ const showAlert = (ob,success = null) =>{ var oAr = {}; if(ob instanceof Object){ oAr = ob; if(oAr['title'] == null || oAr['title'] == '') { oAr['title'] = "系统提示"; } } else { oAr['title'] = "系统提示"; oAr['content'] = ob; } if(dialog != null){ let options = { title: oAr['title'], //标题 (可选) con: oAr['content'], //内容(可选)但是标题和内容至少选择一个 okTitle: oAr['btn'] == null ? '确认' : oAr['btn'], //确认按钮文字(可选) okTextColor: "#f1851e", //确认按钮颜色(可选) singer: true, //是否只显示确认按钮,默认false(可选 textAlign: "center", //对齐方 式 //left居左,center居中 ,right 居右 默认居中 conColor: "", bgColor: "#ffffff", //自定义弹框颜色 titleColor: "#3d3d3d" //自定义title颜色 //提示框内容颜色; } console.log(dialog); dialog.show(options,()=>{ if(success != null) success(); }); } else { uni.showModal({ title: oAr['title'], content: oAr['content'], confirmText:oAr['btn'] == null ? '确认' : oAr['btn'], showCancel: false, success:function(){ if(success != null) success(); } }); } } /** * 判断是否微信 * @return {bool} 是否微信 */ function isWeiXin(){ var ua = window.navigator.userAgent.toLowerCase(); if(ua.match(/MicroMessenger/i) == 'micromessenger'){ return true; }else{ return false; } } /** * 判断是否存在 * @param {Object} value */ function isDefine(value) { if (value == null || value == "" || value == "undefined" || value == undefined || value == "null" || value == "(null)" || value == 'NULL' || typeof (value) == 'undefined') { return false; } else { value = value + ""; value = value.replace(/\s/g, ""); if (value == "") { return false; } return true; } } /** * 时间戳11位 */ function timest() { var tmp = Date.parse( new Date() ).toString(); tmp = tmp.substr(0,10); return tmp; } /** * 时间戳格式化函数 * @param {string} format 格式 * @param {int} timestamp 要格式化的时间 默认为当前时间 * @return {string} 格式化的时间字符串 */ function date(format, timestamp) { var a, jsdate = ((timestamp) ? new Date(timestamp * 1000) : new Date()); var pad = function (n, c) { n = n + ""; if (n.length < c) { var i = c - n.length; var v = ''; for (var j = 0; j < i; j++) { v += '0'; } return v + n; } else { return n; } }; var txt_weekdays = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"]; var txt_ordin = { "1": "st", "2": "nd", "3": "rd", "21": "st", "22": "nd", "23": "rd", "31": "st" }; var txt_months = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; var f = { // Day d: function () { return pad(f.j(), 2) }, D: function () { return f.l().substr(0, 3) }, j: function () { return jsdate.getDate() }, l: function () { return txt_weekdays[f.w()] }, N: function () { return f.w() + 1 }, S: function () { return txt_ordin[f.j()] ? txt_ordin[f.j()] : 'th' }, w: function () { return jsdate.getDay() } , // Week W: function () { var a = f.z(), b = 364 + f.L() - a; var nd2, nd = (new Date(jsdate.getFullYear() + "/1/1").getDay() || 7) - 1; if (b <= 2 && ((jsdate.getDay() || 7) - 1) <= 2 - b) { return 1; } else { if (a <= 2 && nd >= 4 && a >= (6 - nd)) { nd2 = new Date(jsdate.getFullYear() - 1 + "/12/31"); return date("W", Math.round(nd2.getTime() / 1000)); } else { return (1 + (nd <= 3 ? ((a + nd) / 7) : (a - (7 - nd)) / 7) >> 0); } } }, // Month F: function () { return txt_months[f.n()] }, m: function () { return pad(f.n(), 2) }, M: function () { return f.F().substr(0, 3) }, n: function () { return jsdate.getMonth() + 1 }, t: function () { var n; if ((n = jsdate.getMonth() + 1) == 2) { return 28 + f.L(); } else { if (n & 1 && n < 8 || !(n & 1) && n > 7) { return 31; } else { return 30; } } }, //o not supported yet Y: function () { return jsdate.getFullYear() }, y: function () { return (jsdate.getFullYear() + "") }, // Time a: function () { return jsdate.getHours() > 11 ? "pm" : "am" }, A: function () { return f.a().toUpperCase() }, B: function () { // peter paul koch: var off = (jsdate.getTimezoneOffset() + 60) * 60; var theSeconds = (jsdate.getHours() * 3600) + (jsdate.getMinutes() * 60) + jsdate.getSeconds() + off; var beat = Math.floor(theSeconds / 86.4); if (beat > 1000) beat -= 1000; if (beat < 0) beat += 1000; if ((String(beat)).length == 1) beat = "00" + beat; if ((String(beat)).length == 2) beat = "0" + beat; return beat; }, g: function () { return jsdate.getHours() % 12 || 12 }, G: function () { return jsdate.getHours() }, h: function () { return pad(f.g(), 2) }, H: function () { return pad(jsdate.getHours(), 2) }, i: function () { return pad(jsdate.getMinutes(), 2) }, s: function () { return pad(jsdate.getSeconds(), 2) }, //u not supported yet // Timezone //e not supported yet //I not supported yet O: function () { var t = pad(Math.abs(jsdate.getTimezoneOffset() / 60 * 100), 4); if (jsdate.getTimezoneOffset() > 0) t = "-" + t; else t = "+" + t; return t; }, P: function () { var O = f.O(); return (O.substr(0, 3) + ":" + O.substr(3, 2)) }, //T not supported yet //Z not supported yet // Full Date/Time c: function () { return f.Y() + "-" + f.m() + "-" + f.d() + "T" + f.h() + ":" + f.i() + ":" + f.s() + f.P() }, //r not supported yet U: function () { return Math.round(jsdate.getTime() / 1000) } }; return format.replace(/[\\]?([a-zA-Z])/g, function (t, s) { var ret = ''; if (t != s) { ret = s; } else if (f[s]) { ret = f[s](); } else { ret = s; } return ret; }); } /** * strtotime()函数 * strtotime('2012-07-27 12:43:43') OR strtotime('2012-07-27') * @return 时间戳 */ function strtotime(str = '') { if(str == '') { var _temp = new Date(); return parseInt(_temp.getTime() / 1000); } var _arr = str.split(' '); var _day = _arr[0].split('-'); _arr[1] = (_arr[1] == null) ? '0:0:0' : _arr[1]; var _time = _arr[1].split(':'); for (var i = _day.length - 1; i >= 0; i--) { _day[i] = isNaN(parseInt(_day[i])) ? 0 : parseInt(_day[i]); }; for (var i = _time.length - 1; i >= 0; i--) { _time[i] = isNaN(parseInt(_time[i])) ? 0 : parseInt(_time[i]); }; var _temp = new Date(_day[0], _day[1] - 1, _day[2], _time[0], _time[1], _time[2]); return parseInt(_temp.getTime() / 1000); } /** * 小数保留默认2位 */ function returnFloat(value) { var value = Math.round(parseFloat(value) * 100) / 100; var xsd = value.toString().split("."); if (xsd.length == 1) { value = value.toString() + ".00"; return value; } if (xsd.length > 1) { if (xsd[1].length < 2) { value = value.toString() + "0"; } return value; } } /** * 百分比 */ function percentage(value,value2) { return returnFloat((value / value2) * 100); } /** * 倒计时【验证码】 * @time 倒计时 秒 * @结束 */ function CountDowm(time, fe = null) { var id = setInterval(() => { time--; if (fe != null) fe(time, 0); if (time <= 0) { clearInterval(id); fe(0, 1); } }, 1000); } /** * 远程获取[GET同步返回] * @parm url 地址 * @parm data get数据 * @parm fn 异步返回 */ const httpGet = (url, data, fn, fe) => { // if (fe == null) { fe = () => { Tip("网络错误,请稍等尝试..."); wx.hideLoading() } }; if (data == null) data = {time:Math.round(new Date().getTime() / 1000)}; uni.request({ url: url, data: post, success: res => { if (res.statusCode == 200) if (fn != null) fn(res.data); else fe(); }, fail: () => { fe(); }}); } /** * 远程获取[GET异步返回] * @parm url 地址 * @parm data get数据 */ const httpGetAsy = (url, data) => { return new Promise((resolve, reject) => { if (data == null) data = {time:Math.round(new Date().getTime() / 1000)}; uni.request({ url : url, data : data, success : res => {if (res.statusCode == 200) resolve(res.data); else reject();}, fail: ()=> {reject();} }); }); }; /** * 判断手机号码是否准确 */ const isPoneAvailable = (tel) => { var myreg=/^[1][3,4,5,6,7,8,9][0-9]{9}$/; if (!myreg.test(tel)) { return false; } else { return true; } }; /** * 远程获取[POST异步操作] * @parm url 地址 * @parm post post数据 */ const httpPostAsy = (url, post) => { return new Promise((resolve, reject) => { uni.request({ url : url, method : 'POST', data : post, header : { "content-type" : "application/x-www-form-urlencoded" }, success: res => { if (res.statusCode == 200) resolve(res.data); else reject(); }, fail: () => { reject(); } }); }); }; /** * 广告跳转页面 * @param {Object} url */ function navigateTo(url) { if (url == '#' || url == '') { return; } var switchAr = [ '/pages/cart/index' ]; for(var i in switchAr) { if(switchAr[i] == url) { uni.switchTab({ url : url }); } } if(url.indexOf("http://") >= 0 || url.indexOf("https://") >= 0) { location.href = url; return; } uni.navigateTo({ url: url }); } /** * 阿拉伯数字转中文数字 * @param {Object} num */ function noToChinese(num) { if (!/^\d*(\.\d*)?$/.test(num)) { alert("Number is wrong!"); return "Number is wrong!"; } var AA = new Array("零", "一", "二", "三", "四", "五", "六", "七", "八", "九"); var BB = new Array("", "十", "百", "千", "万", "亿", "点", ""); var a = ("" + num).replace(/(^0*)/g, "").split("."), k = 0, re = ""; for (var i = a[0].length - 1; i >= 0; i--) { switch (k) { case 0: re = BB[7] + re; break; case 4: if (!new RegExp("0{4}\\d{" + (a[0].length - i - 1) + "}$").test(a[0])) re = BB[4] + re; break; case 8: re = BB[5] + re; BB[7] = BB[5]; k = 0; break; } if (k % 4 == 2 && a[0].charAt(i + 2) != 0 && a[0].charAt(i + 1) == 0) re = AA[0] + re; if (a[0].charAt(i) != 0) re = AA[a[0].charAt(i)] + BB[k % 4] + re; k++; } if (a.length > 1) //加上小数部分(如果有小数部分) { re += BB[6]; for (var i = 0; i < a[1].length; i++) re += AA[a[1].charAt(i)]; } return re; }; /** * 字节转换 */ function bytesToSize(bytes) { if (bytes === 0) return '0 B'; var k = 1024; var sizes = ['B', 'KB', 'M', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; var i = Math.floor(Math.log(bytes) / Math.log(k)); return toDecimal(bytes / Math.pow(k, i)) + ' ' + sizes[i]; } /** * 星座 */ function constellation(strBirthday) { if (strBirthday == '0') return '无'; var strBirthdayArr = strBirthday.split("-"); var month = strBirthdayArr[1]; var date = strBirthdayArr[2]; if (month == 1 && date >= 20 || month == 2 && date <= 18) { return "水瓶座"; } if (month == 2 && date >= 19 || month == 3 && date <= 20) { return "双鱼座"; } if (month == 3 && date >= 21 || month == 4 && date <= 19) { return "白羊座"; } if (month == 4 && date >= 20 || month == 5 && date <= 20) { return "金牛座"; } if (month == 5 && date >= 21 || month == 6 && date <= 21) { return "双子座"; } if (month == 6 && date >= 22 || month == 7 && date <= 22) { return "巨蟹座"; } if (month == 7 && date >= 23 || month == 8 && date <= 22) { return "狮子座"; } if (month == 8 && date >= 23 || month == 9 && date <= 22) { return "室女座"; } if (month == 9 && date >= 23 || month == 10 && date <= 22) { return "天秤座"; } if (month == 10 && date >= 23 || month == 11 && date <= 21) { return "天蝎座"; } if (month == 11 && date >= 22 || month == 12 && date <= 21) { return "人马座"; } if (month == 12 && date >= 22 || month == 1 && date <= 19) { return "摩羯座"; } } /** * 消息时间值转化 * @param {int} timestamp 要格式化的时间 默认为当前时间 * @return {string} 格式化的时间字符串 */ function timeLastdate(timestamp,isWeekTime = false){ //判断是否今天 var stoday = strtotime(date('Y-m-d 00:00:00')); if (timestamp > stoday){ var h = date('H'); if(h <= 12) return '早上' + date('H:i', timestamp); else return '下午' + date('H:i', timestamp); } //昨天 var day = new Date(); day.setTime(day.getTime() - 24 * 60 * 60 * 1000); var ytoday = day.getTime() / 1000; var s_yesterday = strtotime(date('Y-m-d 0:0:0', ytoday)); var e_yesterday = strtotime(date('Y-m-d 23:59:59', ytoday)); if (timestamp > s_yesterday && timestamp < e_yesterday){ return '昨天' + date('H:i', timestamp); } //获取周一时间 var dayInWeek = day.getDay(); dayInWeek == 0 && (dayInWeek = 7); var beginWeek = strtotime(date('Y-m-d 0:0:0',(day.getTime() - (dayInWeek - 1) * 86400000) / 1000)); if (timestamp >= beginWeek){ return date('l', timestamp) + (isWeekTime == true ? date('h:i',timestamp) : ''); } //判断是否今年 var y = day.getFullYear() ; var y1 = date('Y', timestamp); if(y == y1){ return isWeekTime ? date('m月d日 H:i', timestamp) : date('m月d日', timestamp); } return date('Y年m月d日 H:i', timestamp); } function countdown(time,isAr) { var str = ""; var day = parseInt(time / (3600 * 24)); var day_yusu = time % (3600 * 24); var housr = parseInt(day_yusu / 3600); var housr_yusu = day_yusu % 3600; var minutes = parseInt(housr_yusu / 60); var seconds = housr_yusu % 60; if(isAr) { return [day, (housr > 9 ? housr : ('0' + housr) ), (minutes > 9 ? minutes : ('0' + minutes) ), (seconds > 9 ? seconds : ('0' + seconds))]; } if(day > 0){ str = day + '天'; } console.log(seconds); str += (housr > 9 ? housr : ('0' + housr) ) + ':' + (minutes > 9 ? minutes : ('0' + minutes) ) + ':' + (seconds > 9 ? seconds : ('0' + seconds) ); return str; } /** * 金钱单位模拟 * @param {Object} str */ function moneyUnit(str){ let money = parseFloat(str); if(money < 1000) { return money + "元"; } if(money >= 1000) { return Math.floor(money / 1000) + "K"; } } /** * 返回薪水 * @param {Object} min_price 最小金额 * @param {Object} max_price 最大金额 * @param {Object} salary_type 薪水类型 */ function nxMoney(min_price,max_price,salary_type) { var tls = ""; if(min_price == max_price && min_price == 0) { return '面议'; } if(min_price == max_price && min_price > 0) { tls = (min_price) +"元"; } //以上 if(min_price > 0 && max_price == 0) { tls = (min_price) + "元以上"; } //区间 if(min_price > 0 && max_price > 0) { tls = (min_price) + "元-" + (max_price)+"元"; } //小时制 if(salary_type == 2) { tls += "/每小时"; } //天制 if(salary_type == 3) { tls += "/每天"; } return tls; } /** * 最小年龄 | 最大年龄 * @param {Object} min_age * @param {Object} max_age */ function nxAge(min_age,max_age) { if(min_age > 0 && max_age > 0 && max_age != min_age) { return min_age + "岁-" + max_age + "岁"; } if(min_age > 0 && max_age > 0 && max_age == min_age) { return min_age + "岁"; } if(min_age > 0 && max_age == 0) { return min_age + "岁以上"; } if(min_age == 0 && max_age > 0) { return max_age + "岁以下"; } if(min_age == 0 && max_age == 0) { return "不限年龄"; } } function toH5ml(str) { if(str == null || str.length == 0) return ''; //#ifdef H5 var newStr = str.replace(/\n/g, '_@').replace(/\r/g, '_#'); newStr = newStr.replace(/_#_@/g, '
');//IE7-8 newStr = newStr.replace(/_@/g, '
');//IE9、FF、chrome newStr = newStr.replace(/\s/g, ' ');//空格处理 return newStr; //#endif //#ifndef H5 str = str.replace(/ /ig, ' ');//替换HTML空格 //#endif return str; } function bankCode(code) { if(code.length <= 12) { return code; } var len = parseInt(code.length / 4) + (code.length % 4 > 0 ? 1 : 0); var reStr = ""; for(var i = 0;i= code.length ? code.length : (i * 4); reStr += code.substr((i * 4),4) + " "; } return reStr; } /** * 标题搜索突出关键词 * @param {Object} title1 * @param {Object} title2 */ function titleTag(title1,title2) { var dAr = []; for(var i in title2) { if(title1.indexOf(title2[i]) >= 0) { dAr.push({ name : title2[i], type : 's'}); } else { dAr.push({ name : title2[i], type : 'e'}); } } return dAr; } /** * 检验18位身份证号码(15位号码可以只检测生日是否正确即可) * @author wolfchen * @param cid 18为的身份证号码 * @return Boolean 是否合法 **/ function isCnNewID(cid){ return IDCard(cid); } /** * 自动计算高度 | 宽度 * @param width 原始 * @param height 原始 * @param mWidth 现在 0不参与计算 * @param mHeight 现在 */ function getAotuWidth(width,height,mWidth,mHeight = 0) { if(mWidth > 0 && mHeight == 0) { return (height / width) * mWidth; } if(mWidth == 0 && mHeight > 0) { return (width / height) * mHeight; } return 0; } function getAvatar(img,type){ if(isDefine(img) && img != "") { return img; } return type == 'chat' ? '/static/chat/user-avatar.png' : '/static/chat/group_chat.png'; } /** * 销售量 * @param {Object} count */ function sales(count){ if(isNaN(count)) return 0; if(count < 100) return count + ""; if(count > 100 && count < 1000) return parseInt(count / 100) * 100 + "+"; if(count >= 1000 && count < 10000) return parseInt(count / 1000) * 1000 + "+"; if(count >= 10000 ) return parseInt(count / 10000) + "万+"; } /** * 数字转化文字 * @param {Object} num */ function numberToChinese(num) { var cnNums = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"]; var cnIntRadice = ["", "十", "百", "千"]; var cnIntUnits = ["", "万", "亿", "兆"]; var integerNum = Math.floor(num); var decimalNum = Math.round((num - integerNum) * 100); function convertToChinese(num) { var cnStr = ""; var numStr = num.toString(); var length = numStr.length; var lastNonZero = false; // 用于处理连续的零 for (var i = 0; i < length; i++) { var digit = numStr.charAt(i); if (digit === "0") { lastNonZero = false; if (i === length - 1 || numStr.charAt(i + 1) !== "0") { cnStr += cnNums[parseInt(digit)]; } } else { cnStr += cnNums[parseInt(digit)] + cnIntRadice[length - i - 1]; lastNonZero = true; } } return cnStr; } var chineseStr = ""; var zeroCount = 0; while (integerNum > 0) { var section = integerNum % 10000; if (zeroCount > 0) { chineseStr = cnNums[0] + chineseStr; // 处理连续的零 } chineseStr = convertToChinese(section) + cnIntUnits[zeroCount] + chineseStr; integerNum = Math.floor(integerNum / 10000); zeroCount++; } chineseStr += convertToChinese(decimalNum); chineseStr = chineseStr.replace(/(零.)*零$/, "").replace(/^$/, "零"); if(chineseStr[chineseStr.length - 1] == '零'){ chineseStr = chineseStr.substring(0,chineseStr.length - 1); } return chineseStr; } /** * 取小数位0 * @param {Object} num */ function removeTrailingZeros(num) { let str = num.toString(); if (str.indexOf('.') !== -1) { let parts = str.split('.'); let decimalPart = parts[1].replace(/0+$/, ''); if (decimalPart.length === 0) { return parseInt(parts[0]); } else { return parts[0] + '.' + decimalPart; } } else { return str; } } function isJSON(str) { if (typeof str == 'string') { try { var obj=JSON.parse(str); if(typeof obj == 'object' && obj ){ return true; }else{ return false; } } catch(e) { return false; } } } export default { Tip : Tip,// sales : sales,//销售数量 loadIng : loadIng,//加载效果 showModal : showModal,//确认框 showAlert : showAlert,//提示框 date : date,//时间戳解析时间 strtotime : strtotime,//日期解析时间戳 returnFloat : returnFloat,//小数保留二位 CountDowm : CountDowm,//倒计时 isDefine : isDefine,//判断是否空 httpGet : httpGet,//同步获取GET timest : timest,//获取11位时间戳 httpGetAsy : httpGetAsy,//异步GET httpPostAsy : httpPostAsy,//异步POST isPoneAvailable : isPoneAvailable,//判断手机号码 navigateTo : navigateTo,//页面跳转 noToChinese : noToChinese,//数字转为中文数字 timeLastdate : timeLastdate, moneyUnit : moneyUnit,//金钱单位 bankCode : bankCode,//银行卡hide toH5ml : toH5ml,//格式化 nxAge : nxAge,//年龄格式化 nxMoney : nxMoney,//金钱格式化 titleTag : titleTag,//标题tag countdown : countdown, isCnNewID : isCnNewID, getAotuWidth : getAotuWidth, percentage : percentage, getAvatar : getAvatar, isJSON : isJSON, numberToChinese : numberToChinese,//数字转化中文 removeTrailingZeros : removeTrailingZeros, navigateBack : ()=>{ const pages = getCurrentPages(); pages.length > 1 ? uni.navigateBack() : uni.reLaunch({ url:"/pages/index/home" }); } }