123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- import Consts from "./Consts";
- class Utils {
- static twoDecimal_f(x) {
- var f_x = parseFloat(x);
- if (isNaN(f_x)) {
- console.error('Utils.twoDecimal_f -> parameter error', x);
- return '';
- }
- var f_x = Math.round(x * 100) / 100;
- var s_x = f_x.toString();
- var pos_decimal = s_x.indexOf('.');
- if (pos_decimal < 0) {
- pos_decimal = s_x.length;
- s_x += '.';
- }
- while (s_x.length <= pos_decimal + 2) {
- s_x += '0';
- }
- return s_x;
- }
- /**
- * 给数字字符串的整数部分打上千分位隔断标识
- */
- static thousandthFormat(n) {
- if (!n) {
- return n;
- }
- try {
- let n_str = n.toString();
- let result = '';
- let int_str = n_str.split('.')[0];
- let f_str = n_str.split('.')[1];
- while (int_str.length > 3) {
- result = ',' + int_str.slice(-3) + result;
- int_str = int_str.slice(0, int_str.length - 3);
- }
- result = int_str + result + '.' + f_str;
- return result;
- } catch(e) {
- return n;
- }
- }
- static getStrLength(str) {
- return str.replace(/[\u0391-\uFFE5]/g, "aa").length;
- }
- static isOverflow(str, keepLength) {
- return Utils.getStrLength(str) > keepLength;
- }
- static fixOverflowStr(str, keepLength, suffix) {
- function getSubString(str, keepLength) {
- var realLength = 0;
- var charCode = -1;
- var newStr = '';
- for (var i = 0; i < str.length; i++) {
- charCode = str.charCodeAt(i);
- realLength += charCode >= 0 && charCode <= 128 ? 1 : 2;
- if (realLength >= keepLength) {
- break;
- }
- newStr += str.charAt(i);
- }
- return newStr;
- }
- if (!Utils.isOverflow(str, keepLength)) {
- return str;
- }
- return getSubString(str, keepLength - suffix.length) + suffix;
- }
- static generateUrl(src, type){
- if('/' != src[0])
- {
- src = '/' + src;
- }
- if ('img' == type){
- return Consts.IMG_PATH + src;
- }
- else if ('video' == type) {
- return Consts.VIDEO_PATH + src;
- }
- return src;
- }
- static createMoyeEvent(eventType){
- let e = document.createEvent('Event');
- if(moye.isAtv)
- {
- //Atv event
- if(Consts.EVENT_TYPE_OK == eventType){
- e.initEvent('ok', true, true);
- e.keyCode = moye.keyCodes.ENTER;
- return e;
- }
- }
- else{
- if(Consts.EVENT_TYPE_OK == eventType){
- //非Atv的onOK, 监听的是ENTER的keyup事件
- e.initEvent('keyup', true, true);
- e.keyCode = moye.keyCodes.ENTER;
- return e;
- }
- }
- return null;
- }
- /*
- fire event for target dom node
- */
- static fireEventById(nodeID, eventType){
- //alert('fireEventById Begin!');
- let srcNode = document.getElementById(nodeID);
- if(!srcNode){
- console.log('Cannot Find Target Node, nodeID=%s', nodeID);
- //alert('Cannot Find Target Node, nodeID='+nodeID);
- return false;
- }
- let targetEvent = Utils.createMoyeEvent(eventType);
- if(!targetEvent){
- return false;
- }
- srcNode.dispatchEvent(targetEvent);
- return true;
- }
- //日期格式化输出
- static dateFormat(dateObj, format){
- let date = {
- "M+": dateObj.getMonth() + 1,
- "d+": dateObj.getDate(),
- "h+": dateObj.getHours(),
- "m+": dateObj.getMinutes(),
- "s+": dateObj.getSeconds(),
- "q+": Math.floor((dateObj.getMonth() + 3) / 3),
- "S+": dateObj.getMilliseconds()
- };
- if (/(y+)/i.test(format)) {
- format = format.replace(RegExp.$1, (dateObj.getFullYear() + '').substr(4 - RegExp.$1.length));
- }
- for (let k in date) {
- if (new RegExp("(" + k + ")").test(format)) {
- format = format.replace(RegExp.$1, RegExp.$1.length == 1
- ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
- }
- }
- return format;
- }
- static trimJsonStr(src){
- //所有"'" 替换为"\""
- if(typeof(src) == "string"){
- return src.replace(new RegExp("'","gm"), "\"");
- }
- return src;
- }
- /**
- * @desc uuid生成器
- */
- static uuid(len, radix) {
- let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
- let uuid = [];
- radix = radix || chars.length;
- if (len) {
- for(let i = 0; i < len; i++) {
- uuid[i] = chars[0 | Math.random()*radix];
- }
- } else {
- //默认,rfc4122协议类型
- let r;
- //以下位置的字符串固定
- uuid[8] = uuid[13] = uuid[18] = uuid[13];
- uuid[14] = '4';
- for (let i = 0; i < 36; i++) {
- if (!uuid[i]) {
- r = 0 | Math.random()*16;
- uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
- }
- }
- }
- return uuid.join('');
- }
- static getUuidForWeb() {
- const uuid = Utils.uuid(32);
- const start = uuid.slice(0,8);
- const next = uuid.slice(8,12);
- const nextNext = uuid.slice(13,17);
- const last = uuid.slice(17,32);
- return `${start}-${next}-${nextNext}-${last}`;
- }
- static videoUrlFormat(url) {
- if (!url) { return; }
- if (url.startsWith('http')) {
- return url.replace('http://ljvideo.ai160.com', Consts.VIDEO_PATH);
- } else if (url.startsWith('https')) {
- return url.replace('https', 'http');
- } else {
- return url;
- }
- }
- }
- export default Utils;
|