Utils.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import Consts from "./Consts";
  2. class Utils {
  3. static twoDecimal_f(x) {
  4. var f_x = parseFloat(x);
  5. if (isNaN(f_x)) {
  6. console.error('Utils.twoDecimal_f -> parameter error', x);
  7. return '';
  8. }
  9. var f_x = Math.round(x * 100) / 100;
  10. var s_x = f_x.toString();
  11. var pos_decimal = s_x.indexOf('.');
  12. if (pos_decimal < 0) {
  13. pos_decimal = s_x.length;
  14. s_x += '.';
  15. }
  16. while (s_x.length <= pos_decimal + 2) {
  17. s_x += '0';
  18. }
  19. return s_x;
  20. }
  21. /**
  22. * 给数字字符串的整数部分打上千分位隔断标识
  23. */
  24. static thousandthFormat(n) {
  25. if (!n) {
  26. return n;
  27. }
  28. try {
  29. let n_str = n.toString();
  30. let result = '';
  31. let int_str = n_str.split('.')[0];
  32. let f_str = n_str.split('.')[1];
  33. while (int_str.length > 3) {
  34. result = ',' + int_str.slice(-3) + result;
  35. int_str = int_str.slice(0, int_str.length - 3);
  36. }
  37. result = int_str + result + '.' + f_str;
  38. return result;
  39. } catch(e) {
  40. return n;
  41. }
  42. }
  43. static getStrLength(str) {
  44. return str.replace(/[\u0391-\uFFE5]/g, "aa").length;
  45. }
  46. static isOverflow(str, keepLength) {
  47. return Utils.getStrLength(str) > keepLength;
  48. }
  49. static fixOverflowStr(str, keepLength, suffix) {
  50. function getSubString(str, keepLength) {
  51. var realLength = 0;
  52. var charCode = -1;
  53. var newStr = '';
  54. for (var i = 0; i < str.length; i++) {
  55. charCode = str.charCodeAt(i);
  56. realLength += charCode >= 0 && charCode <= 128 ? 1 : 2;
  57. if (realLength >= keepLength) {
  58. break;
  59. }
  60. newStr += str.charAt(i);
  61. }
  62. return newStr;
  63. }
  64. if (!Utils.isOverflow(str, keepLength)) {
  65. return str;
  66. }
  67. return getSubString(str, keepLength - suffix.length) + suffix;
  68. }
  69. static generateUrl(src, type){
  70. if('/' != src[0])
  71. {
  72. src = '/' + src;
  73. }
  74. if ('img' == type){
  75. return Consts.IMG_PATH + src;
  76. }
  77. else if ('video' == type) {
  78. return Consts.VIDEO_PATH + src;
  79. }
  80. return src;
  81. }
  82. static createMoyeEvent(eventType){
  83. let e = document.createEvent('Event');
  84. if(moye.isAtv)
  85. {
  86. //Atv event
  87. if(Consts.EVENT_TYPE_OK == eventType){
  88. e.initEvent('ok', true, true);
  89. e.keyCode = moye.keyCodes.ENTER;
  90. return e;
  91. }
  92. }
  93. else{
  94. if(Consts.EVENT_TYPE_OK == eventType){
  95. //非Atv的onOK, 监听的是ENTER的keyup事件
  96. e.initEvent('keyup', true, true);
  97. e.keyCode = moye.keyCodes.ENTER;
  98. return e;
  99. }
  100. }
  101. return null;
  102. }
  103. /*
  104. fire event for target dom node
  105. */
  106. static fireEventById(nodeID, eventType){
  107. //alert('fireEventById Begin!');
  108. let srcNode = document.getElementById(nodeID);
  109. if(!srcNode){
  110. console.log('Cannot Find Target Node, nodeID=%s', nodeID);
  111. //alert('Cannot Find Target Node, nodeID='+nodeID);
  112. return false;
  113. }
  114. let targetEvent = Utils.createMoyeEvent(eventType);
  115. if(!targetEvent){
  116. return false;
  117. }
  118. srcNode.dispatchEvent(targetEvent);
  119. return true;
  120. }
  121. //日期格式化输出
  122. static dateFormat(dateObj, format){
  123. let date = {
  124. "M+": dateObj.getMonth() + 1,
  125. "d+": dateObj.getDate(),
  126. "h+": dateObj.getHours(),
  127. "m+": dateObj.getMinutes(),
  128. "s+": dateObj.getSeconds(),
  129. "q+": Math.floor((dateObj.getMonth() + 3) / 3),
  130. "S+": dateObj.getMilliseconds()
  131. };
  132. if (/(y+)/i.test(format)) {
  133. format = format.replace(RegExp.$1, (dateObj.getFullYear() + '').substr(4 - RegExp.$1.length));
  134. }
  135. for (let k in date) {
  136. if (new RegExp("(" + k + ")").test(format)) {
  137. format = format.replace(RegExp.$1, RegExp.$1.length == 1
  138. ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
  139. }
  140. }
  141. return format;
  142. }
  143. static trimJsonStr(src){
  144. //所有"'" 替换为"\""
  145. if(typeof(src) == "string"){
  146. return src.replace(new RegExp("'","gm"), "\"");
  147. }
  148. return src;
  149. }
  150. /**
  151. * @desc uuid生成器
  152. */
  153. static uuid(len, radix) {
  154. let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  155. let uuid = [];
  156. radix = radix || chars.length;
  157. if (len) {
  158. for(let i = 0; i < len; i++) {
  159. uuid[i] = chars[0 | Math.random()*radix];
  160. }
  161. } else {
  162. //默认,rfc4122协议类型
  163. let r;
  164. //以下位置的字符串固定
  165. uuid[8] = uuid[13] = uuid[18] = uuid[13];
  166. uuid[14] = '4';
  167. for (let i = 0; i < 36; i++) {
  168. if (!uuid[i]) {
  169. r = 0 | Math.random()*16;
  170. uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
  171. }
  172. }
  173. }
  174. return uuid.join('');
  175. }
  176. static getUuidForWeb() {
  177. const uuid = Utils.uuid(32);
  178. const start = uuid.slice(0,8);
  179. const next = uuid.slice(8,12);
  180. const nextNext = uuid.slice(13,17);
  181. const last = uuid.slice(17,32);
  182. return `${start}-${next}-${nextNext}-${last}`;
  183. }
  184. static videoUrlFormat(url) {
  185. if (!url) { return; }
  186. if (url.startsWith('http')) {
  187. return url.replace('http://ljvideo.ai160.com', Consts.VIDEO_PATH);
  188. } else if (url.startsWith('https')) {
  189. return url.replace('https', 'http');
  190. } else {
  191. return url;
  192. }
  193. }
  194. }
  195. export default Utils;