tools.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. (function() {
  2. window.tools = {
  3. // 判断对象数组中指定属性是否有某个值的数据
  4. objArrHasValue(objArr, key, value) {
  5. return objArr.find(obj => obj[key] === value) !== undefined;
  6. },
  7. // 获取对象数组中指定属性为指定值的对象
  8. objArrGetObjByValue(objArr, key, value) {
  9. return objArr.find(obj => obj[key] === value);
  10. },
  11. // 对url追加项目版本号,用于页面更新后用户端的强制刷新
  12. urlAddVer(url) {
  13. let newUrl = url;
  14. // Mock version for H5
  15. const version_number = '1.0.0';
  16. if (newUrl.indexOf('_v=') !== -1) {
  17. return newUrl;
  18. }
  19. if (newUrl.indexOf('?') !== -1) {
  20. newUrl += "&_v=" + version_number;
  21. } else {
  22. newUrl += "?_v=" + version_number;
  23. }
  24. console.log("[urlAddVer] newUrl", newUrl);
  25. return newUrl;
  26. },
  27. // 导航到彩图奔跑APP内的某个页面或执行APP内部的某些功能
  28. appAction(url, actType = "") {
  29. console.log("appAction", url);
  30. // Safe guard for getApp audio
  31. try {
  32. if(window.getApp && window.getApp().$audio) {
  33. window.getApp().$audio.destroy();
  34. window.getApp().$audio.pause();
  35. }
  36. } catch(e) {}
  37. if (url.indexOf('http') !== -1) { // http 或 https 开头的网址
  38. window.location.href = this.urlAddVer(url);
  39. } else if (url == "reload") {
  40. window.location.reload();
  41. } else if (actType == "uni.navigateTo") {
  42. uni.navigateTo({
  43. url: this.urlAddVer(url)
  44. });
  45. } else {
  46. // Native bridge simulation or actual call
  47. if(window.appAction) {
  48. window.appAction(url);
  49. } else {
  50. window.location.href = url;
  51. }
  52. }
  53. },
  54. // 格式化赛事时间 09-09 16:48
  55. fmtMcTime(timestamp) {
  56. var date = new Date(timestamp * 1000);
  57. var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  58. var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
  59. var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  60. var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
  61. const timeStr = M + D + h + m;
  62. return timeStr;
  63. },
  64. // 获取活动时间 09-09 16:48 至 09-30 16:48
  65. getActtime(beginSecond, endSecond) {
  66. const acttime = this.fmtMcTime(beginSecond) + " 至 " + this.fmtMcTime(endSecond);
  67. return acttime;
  68. },
  69. // 格式化赛事时间 2024.9.9-30 2024.9.9-10.30 2024.9.9-2025.10.30
  70. fmtMcTime2(timestamp1, timestamp2) {
  71. const date1 = new Date(timestamp1 * 1000);
  72. const date2 = new Date(timestamp2 * 1000);
  73. const Y1 = date1.getFullYear();
  74. const Y2 = date2.getFullYear();
  75. const M1 = date1.getMonth() + 1;
  76. const M2 = date2.getMonth() + 1;
  77. const D1 = date1.getDate();
  78. const D2 = date2.getDate();
  79. var timeStr1 = Y1 + '.' + M1 + '.' + D1;
  80. var timeStr2 = '';
  81. if (Y2 != Y1) {
  82. timeStr2 += Y2 + '.' + M2 + '.' + D2;
  83. } else if (M2 != M1) {
  84. timeStr2 += M2 + '.' + D2;
  85. } else if (D2 != D1) {
  86. timeStr2 += D2;
  87. }
  88. var timeStr = timeStr1;
  89. if (timeStr2.length > 0) {
  90. timeStr += '-' + timeStr2;
  91. }
  92. return timeStr;
  93. },
  94. // 判断赛事/活动状态 0: 未开始 1: 进行中 2: 已结束
  95. checkMcState(beginSecond, endSecond) {
  96. let mcState = 0; // 未开始
  97. if (beginSecond > 0 && endSecond > 0) {
  98. const now = Date.now() / 1000;
  99. const dif1 = beginSecond - now;
  100. const dif2 = endSecond - now;
  101. if (dif1 > 0) {
  102. mcState = 0; // 未开始
  103. } else if (dif2 > 0) {
  104. mcState = 1; // 进行中
  105. } else {
  106. mcState = 2; // 已结束
  107. }
  108. }
  109. return mcState;
  110. },
  111. // 动态创建<style>标签
  112. loadCssCode(cssCode, styleId = "css-custom") {
  113. this.removeCssCode(styleId);
  114. var style = window.document.createElement("style");
  115. style.type = "text/css";
  116. style.id = styleId;
  117. if (style.styleSheet) {
  118. style.styleSheet.cssText = cssCode;
  119. } else {
  120. style.appendChild(document.createTextNode(cssCode));
  121. }
  122. document.getElementsByTagName("head")[0].appendChild(style);
  123. },
  124. // 删除之前动态创建的<style>标签
  125. removeCssCode(styleId = "css-custom") {
  126. var oldCss = document.getElementById(styleId);
  127. if (oldCss != null) {
  128. document.getElementsByTagName("head")[0].removeChild(oldCss);
  129. }
  130. },
  131. // uni-data-select 组件,根据选中的值获取对应的文本 (Mock for pure JS)
  132. getSelectedText(obj, value) {
  133. if(!obj) return '';
  134. const selectedOption = obj.find(option => option.value === value);
  135. return selectedOption ? selectedOption.text : '';
  136. },
  137. objectToQueryString(obj) {
  138. return Object.keys(obj).map(k => k + '=' + obj[k]).join('&');
  139. },
  140. // 秒数转换成 XX天XX小时
  141. convertSecondsToDHM(seconds) {
  142. var days = Math.floor(seconds / (3600 * 24));
  143. var hours = Math.floor((seconds % (3600 * 24)) / 3600);
  144. var minutes = Math.floor((seconds % (3600 * 24)) % 3600 / 60);
  145. if (days > 0)
  146. return `${days}天${hours.toString().padStart(2, '0')}小时`;
  147. else
  148. return `${hours.toString().padStart(2, '0')}小时${minutes.toString().padStart(2, '0')}分钟`;
  149. },
  150. // 秒数转换成时分秒
  151. convertSecondsToHMS(seconds, style = 0) {
  152. if (!(seconds > 0)) {
  153. return '--';
  154. }
  155. var hours = Math.floor(seconds / 3600);
  156. var minutes = Math.floor((seconds % 3600) / 60);
  157. var remainingSeconds = Math.floor(seconds % 60);
  158. if (style == 0)
  159. return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
  160. else if (style == 1) {
  161. if (hours > 0)
  162. return `${hours}h${minutes}′${remainingSeconds.toString().padStart(2, '0')}″`;
  163. else
  164. return `${minutes}′${remainingSeconds.toString().padStart(2, '0')}″`;
  165. } else if (style == 2) {
  166. if (hours > 0)
  167. return `${hours*60+minutes}′${remainingSeconds.toString().padStart(2, '0')}″`;
  168. else
  169. return `${minutes}′${remainingSeconds.toString().padStart(2, '0')}″`;
  170. }
  171. },
  172. // ... other simple validation helpers can be kept as is ...
  173. isPhone(val) {
  174. var patrn = /^(((1[3456789][0-9]{1})|(15[0-9]{1}))+\d{8})$/
  175. if (!patrn.test(val) || val === '') {
  176. uni.showToast({ title: '手机号格式不正确', icon: 'none' })
  177. return false
  178. } else {
  179. return true
  180. }
  181. }
  182. }
  183. })();