PageSourceSearch

https://takungpao.com.hk/static/tkwwtemplate/js/utils/util.js?template=20230313

js takungpao.com.hk collected 2026-10-01 11:40:19 UTC 8,072 bytes, 260 lines download raw bytes

1class Util {
2  static isNull(data){
3      switch (data){
4        case null: return true
5  			case undefined: return true
6  			case '': return true
7  			default: return !data
8      }
9  }
10  
11  static testStr({str,type}){
12    var reg = ""
13    switch(type){
14      case "email":{
15        reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/
16        break;
17      }
18      case "idCard":{
19        reg = /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
20        break;
21      }
22    }
23    if(this.isNull(str)||!reg.test(str))return false
24    return true
25  }
26  static dateFormat(fmt, str) {
27    //var date = new Date(str.replace(/-/g, '/').replace('T', ' ').replace('.000+0000', '') + ' GMT');
28    
29    var date=""
30    // 如果是带Z的ISO格式
31    if (str.includes('Z')) {
32      date = new Date(str);
33    }else{
34      date = new Date(str.replace(/-/g, '/').replace('T', ' ').replace('.000+0000', '') + ' GMT');// 如果是带+0000的格式
35    }
36    let ret;
37    const opt = {
38      "Y+": date.getFullYear().toString(),        // å¹´
39      "m+": (date.getMonth() + 1).toString(),     // 月
40      "d+": date.getDate().toString(),            // 日
41      "H+": date.getHours().toString(),           // 时
42      "M+": date.getMinutes().toString(),         // 分
43      "S+": date.getSeconds().toString()          // 秒
44      // 有其他格式化字符需求可以继续添加,必须转化成字符串
45    };
46    for (let k in opt) {
47      ret = new RegExp("(" + k + ")").exec(fmt);
48      if (ret) {
49        //fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")));
50        fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (this.pad(opt[k],ret[1].length)));
51      };
52    };
53    return fmt;
54  }
55  /* 质朴长存法 by lifesinger */
56  static pad(num, n) {
57    var len = num.toString().length;
58    while(len < n) {
59      num = "0" + num;
60      len++;
61    }
62    return num;
63  }
64  //获取距现在时间差
65  static getEndTime({fmt='YYYY.mm.dd HH:MM:SS',publishTime}){
66    /*
67    *publishTime:发表时间
68    */
69    let endTime = ""
70    let nowTime = new Date().getTime()
71    let time = new Date(publishTime).getTime()
72    let timeD = nowTime - time
73    if(timeD>0){
74      if(timeD<60*60*24*1000){
75        timeD = String(timeD).slice(0,-3)
76        timeD = Number(timeD)
77        let dayD = Math.floor(timeD/3600/24)
78        let hoursD = Math.floor(timeD/3600)
79        let minutesD = Math.floor(timeD/60)
80        if(dayD>0){
81          endTime = `${dayD}天前`
82        }else if(hoursD>0){
83          endTime = `${hoursD}小時前`
84        }else if(minutesD>0){
85          endTime = `${minutesD}分鐘前`
86        }else{
87          endTime = `剛剛`
88        }
89      }else{
90        endTime = this.dateFormat(fmt,publishTime)
91      }
92    }
93    return endTime
94  }
95  //获取距现在时间差並分組
96  static getEndTimeArr({fmt='YYYY.mm.dd HH:MM:SS',publishTime}){
97    /*
98    *publishTime:发表时间
99    */
100    let endTime = ""
101    let nowTime = new Date().getTime()
102    let time = new Date(publishTime).getTime()
103    let timeD = nowTime - time
104    if(timeD>0){
105      if(timeD<60*60*24*1000){
106        timeD = String(timeD).slice(0,-3)
107        timeD = Number(timeD)
108        let dayD = Math.floor(timeD/3600/24)
109        let hoursD = Math.floor(timeD/3600)
110        let minutesD = Math.floor(timeD/60)
111        if(dayD>0){
112          endTime = `${dayD}天前`
113        }else if(hoursD>0){
114          endTime = `${hoursD}小時前`
115        }else if(minutesD>0){
116          endTime = `${minutesD}分鐘前`
117        }else{
118          endTime = `剛剛`
119        }
120      }else{
121        endTime = this.dateFormat(fmt,publishTime)
122      }
123    }
124    let endTimeArr = endTime.split(" ")
125    return endTimeArr
126  }
127  //深拷贝
128  static deepCopy({obj}){
129    return JSON.parse(JSON.stringify(obj))
130  }
131  //是否为电脑端
132  static isPc(){
133    let userAgent = navigator.userAgent.toLowerCase();
134    // 用 test 匹配浏览器信息
135    if (/ipad|iphone|midp|rv:1.2.3.4|ucweb|android|windows ce|windows mobile/.test(userAgent)||window.innerWidth<=1099) {
136        return false
137    } else {
138        return true
139    }
140  }
141  //是否为电脑端
142  static isPcV5(){
143    if (window.innerWidth<=992) {
144        return false
145    } else {
146        return true
147    }
148  }
149  static isPad(){
150    if (window.innerWidth<=992&&window.innerWidth>576) {
151        return true
152    } else {
153        return false
154    }
155  }
156  static isPhone(){
157    if (window.innerWidth<=576) {
158        return true
159    } else {
160        return false
161    }
162  }
163  /*去重*/
164  static uniqueFunc(arr, uniId){
165    const res = new Map();
166    return arr.filter((item) => !res.has(item[uniId]) && res.set(item[uniId], 1));
167  }
168  /*讀取圖片*/
169  static getImgSrc(files,i){
170    return new Promise((resolve,reject)=>{
171      var file = files.item(i);
172      if (!(/^image\/.*$/i.test(file.type))) {
173        resolve(false) //不是图片 就跳出这一次循环
174      }
175      //实例化FileReader API
176      var freader = new FileReader();
177      freader.readAsDataURL(file);
178      freader.onload = function(e) {
179        resolve(e.target.result)
180      }
181      freader.onerror = function(e){
182        resolve(false)
183      }
184    })
185  }
186  /*判定微信*/
187  static is_weixin(){
188    var ua = navigator.userAgent.toLowerCase();
189    if(ua.match(/MicroMessenger/i)=="micromessenger") {
190      return true;
191    } else {
192      return false;
193    }
194  }
195  /*深拷贝*/
196  static deepCopyObjectWithMethods(obj) {
197    if (typeof obj !== 'object' || obj === null) {
198      return obj; // 非对象直接返回原值
199    }
200    let copy = Array.isArray(obj) ? [] : {};
201    for (let key in obj) {
202      if (Object.prototype.hasOwnProperty.call(obj, key)) {
203        if (typeof obj[key] === 'object') {
204          copy[key] = this.deepCopyObjectWithMethods(obj[key]); // 递归调用深拷贝函数
205        } else {
206          copy[key] = obj[key]; // 基本类型直接赋值
207        }
208      }
209    }
210    return Object.assign({}, copy); // 将新创建的对象与原始对象合并
211  }
212  static formatTimestamp(timestamp, format) {
213    const date = new Date(Number(timestamp));
214
215    const year = date.getFullYear();
216    const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始,需加1
217    const day = String(date.getDate()).padStart(2, '0');
218    const hours = String(date.getHours()).padStart(2, '0');
219    const minutes = String(date.getMinutes()).padStart(2, '0');
220    const seconds = String(date.getSeconds()).padStart(2, '0');
221
222    switch (format) {
223      case 'YYYY-mm-dd HH:mm:ss':
224        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
225      case 'YYYY.mm.dd HH:mm:ss':
226        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
227      case 'YYYY-mm-dd':
228        return `${year}-${month}-${day}`;
229      case 'HH:mm:ss':
230        return `${hours}:${minutes}:${seconds}`;
231      case 'dd/mm/yyyy':
232        return `${day}/${month}/${year}`;
233      case 'YYYY年mm月dd日':
234        return `${year}年${month}月${day}日`;
235      case 'mm月dd日':
236        return `${month}月${day}日`;
237      case 'YYYYmmdd':
238        return `${year}${month}${day}`;
239      default:
240        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; // 默认格式
241    }
242  }
243  static getHotEwStoryUrl({jsonUrl}){
244    let url = "https://epaper.tkww.hk/wenweipo/storyContent?jsonLink=" + encodeURIComponent(jsonUrl.replace("www.wenweipo.com","www.tkww.hk/channels/wenweipo/todaywenwei"))
245    return url
246  }
247  static getEwStoryUrl({jsonUrl}){
248    let url = "https://epaper.tkww.hk/wenweipo/storyContent?jsonLink=" + encodeURIComponent(jsonUrl)
249    return url
250  }
251  static getEwColumnUrl({columnUuid}){
252    let url ="https://epaper.tkww.hk/wenweipo/public?columnUuid="+encodeURIComponent(columnUuid)
253    return url
254  }
255  static getDevicePixelRatio(){
256    // 兼容极少数不支持该属性的浏览器(如老旧 IE)
257    return window?.devicePixelRatio?Math.ceil(window?.devicePixelRatio):1;
258  }
259}
260export default Util

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.