| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- (function() {
- window.tools = {
-
- // 判断对象数组中指定属性是否有某个值的数据
- objArrHasValue(objArr, key, value) {
- return objArr.find(obj => obj[key] === value) !== undefined;
- },
-
- // 获取对象数组中指定属性为指定值的对象
- objArrGetObjByValue(objArr, key, value) {
- return objArr.find(obj => obj[key] === value);
- },
-
- // 对url追加项目版本号,用于页面更新后用户端的强制刷新
- urlAddVer(url) {
- let newUrl = url;
- // Mock version for H5
- const version_number = '1.0.0';
-
- if (newUrl.indexOf('_v=') !== -1) {
- return newUrl;
- }
-
- if (newUrl.indexOf('?') !== -1) {
- newUrl += "&_v=" + version_number;
- } else {
- newUrl += "?_v=" + version_number;
- }
- console.log("[urlAddVer] newUrl", newUrl);
- return newUrl;
- },
-
- // 导航到彩图奔跑APP内的某个页面或执行APP内部的某些功能
- appAction(url, actType = "") {
- console.log("appAction", url);
- // Safe guard for getApp audio
- try {
- if(window.getApp && window.getApp().$audio) {
- window.getApp().$audio.destroy();
- window.getApp().$audio.pause();
- }
- } catch(e) {}
-
- if (url.indexOf('http') !== -1) { // http 或 https 开头的网址
- window.location.href = this.urlAddVer(url);
- } else if (url == "reload") {
- window.location.reload();
- } else if (actType == "uni.navigateTo") {
- uni.navigateTo({
- url: this.urlAddVer(url)
- });
- } else {
- // Native bridge simulation or actual call
- if(window.appAction) {
- window.appAction(url);
- } else {
- window.location.href = url;
- }
- }
- },
-
- // 格式化赛事时间 09-09 16:48
- fmtMcTime(timestamp) {
- var date = new Date(timestamp * 1000);
- var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
- var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
- var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
- var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
- const timeStr = M + D + h + m;
- return timeStr;
- },
-
- // 获取活动时间 09-09 16:48 至 09-30 16:48
- getActtime(beginSecond, endSecond) {
- const acttime = this.fmtMcTime(beginSecond) + " 至 " + this.fmtMcTime(endSecond);
- return acttime;
- },
-
- // 格式化赛事时间 2024.9.9-30 2024.9.9-10.30 2024.9.9-2025.10.30
- fmtMcTime2(timestamp1, timestamp2) {
- const date1 = new Date(timestamp1 * 1000);
- const date2 = new Date(timestamp2 * 1000);
-
- const Y1 = date1.getFullYear();
- const Y2 = date2.getFullYear();
- const M1 = date1.getMonth() + 1;
- const M2 = date2.getMonth() + 1;
- const D1 = date1.getDate();
- const D2 = date2.getDate();
-
- var timeStr1 = Y1 + '.' + M1 + '.' + D1;
- var timeStr2 = '';
-
- if (Y2 != Y1) {
- timeStr2 += Y2 + '.' + M2 + '.' + D2;
- } else if (M2 != M1) {
- timeStr2 += M2 + '.' + D2;
- } else if (D2 != D1) {
- timeStr2 += D2;
- }
-
- var timeStr = timeStr1;
- if (timeStr2.length > 0) {
- timeStr += '-' + timeStr2;
- }
- return timeStr;
- },
-
- // 判断赛事/活动状态 0: 未开始 1: 进行中 2: 已结束
- checkMcState(beginSecond, endSecond) {
- let mcState = 0; // 未开始
- if (beginSecond > 0 && endSecond > 0) {
- const now = Date.now() / 1000;
- const dif1 = beginSecond - now;
- const dif2 = endSecond - now;
- if (dif1 > 0) {
- mcState = 0; // 未开始
- } else if (dif2 > 0) {
- mcState = 1; // 进行中
- } else {
- mcState = 2; // 已结束
- }
- }
- return mcState;
- },
-
- // 动态创建<style>标签
- loadCssCode(cssCode, styleId = "css-custom") {
- this.removeCssCode(styleId);
- var style = window.document.createElement("style");
- style.type = "text/css";
- style.id = styleId;
- if (style.styleSheet) {
- style.styleSheet.cssText = cssCode;
- } else {
- style.appendChild(document.createTextNode(cssCode));
- }
- document.getElementsByTagName("head")[0].appendChild(style);
- },
-
- // 删除之前动态创建的<style>标签
- removeCssCode(styleId = "css-custom") {
- var oldCss = document.getElementById(styleId);
- if (oldCss != null) {
- document.getElementsByTagName("head")[0].removeChild(oldCss);
- }
- },
-
- // uni-data-select 组件,根据选中的值获取对应的文本 (Mock for pure JS)
- getSelectedText(obj, value) {
- if(!obj) return '';
- const selectedOption = obj.find(option => option.value === value);
- return selectedOption ? selectedOption.text : '';
- },
-
- objectToQueryString(obj) {
- return Object.keys(obj).map(k => k + '=' + obj[k]).join('&');
- },
-
- // 秒数转换成 XX天XX小时
- convertSecondsToDHM(seconds) {
- var days = Math.floor(seconds / (3600 * 24));
- var hours = Math.floor((seconds % (3600 * 24)) / 3600);
- var minutes = Math.floor((seconds % (3600 * 24)) % 3600 / 60);
- if (days > 0)
- return `${days}天${hours.toString().padStart(2, '0')}小时`;
- else
- return `${hours.toString().padStart(2, '0')}小时${minutes.toString().padStart(2, '0')}分钟`;
- },
-
- // 秒数转换成时分秒
- convertSecondsToHMS(seconds, style = 0) {
- if (!(seconds > 0)) {
- return '--';
- }
- var hours = Math.floor(seconds / 3600);
- var minutes = Math.floor((seconds % 3600) / 60);
- var remainingSeconds = Math.floor(seconds % 60);
- if (style == 0)
- return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
- else if (style == 1) {
- if (hours > 0)
- return `${hours}h${minutes}′${remainingSeconds.toString().padStart(2, '0')}″`;
- else
- return `${minutes}′${remainingSeconds.toString().padStart(2, '0')}″`;
- } else if (style == 2) {
- if (hours > 0)
- return `${hours*60+minutes}′${remainingSeconds.toString().padStart(2, '0')}″`;
- else
- return `${minutes}′${remainingSeconds.toString().padStart(2, '0')}″`;
- }
- },
-
- // ... other simple validation helpers can be kept as is ...
- isPhone(val) {
- var patrn = /^(((1[3456789][0-9]{1})|(15[0-9]{1}))+\d{8})$/
- if (!patrn.test(val) || val === '') {
- uni.showToast({ title: '手机号格式不正确', icon: 'none' })
- return false
- } else {
- return true
- }
- }
- }
- })();
|