util.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. export function numberToK(v) {
  2. if (!v) {
  3. return ''
  4. }
  5. try{
  6. // if (v >= 10000) {
  7. // return (v / 10000).toFixed(1) + 'w'
  8. // }
  9. if (v >= 1000) {
  10. return (v / 1000).toFixed(1) + 'K'
  11. }
  12. return v
  13. }catch(err) {
  14. return v
  15. }
  16. }
  17. export function formatTime(time, that) {
  18. if (('' + time).length === 10) {
  19. time = parseInt(time) * 1000
  20. } else {
  21. time = +time
  22. }
  23. const d = new Date(time)
  24. const now = Date.now()
  25. const diff = (now - d) / 1000
  26. if (diff < 30) {
  27. return '1' + that.$t('game.lab2')
  28. } else if (diff < 3600) {
  29. // less 1 hour
  30. return Math.ceil(diff / 60) + that.$t('game.lab2')
  31. } else if (diff < 3600 * 24) {
  32. return Math.ceil(diff / 3600) + that.$t('game.lab3')
  33. } else if (diff < 3600 * 24 * 30) {
  34. return Math.ceil(diff / (3600 * 24)) + that.$t('game.lab4')
  35. } else {
  36. return Math.ceil(diff / (3600 * 24 * 30)) + that.$t('game.lab5')
  37. }
  38. }
  39. export function parseTime(time, cFormat) {
  40. if (arguments.length === 0) {
  41. return null
  42. }
  43. const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  44. let date
  45. if (typeof time === 'object') {
  46. date = time
  47. } else {
  48. if (('' + time).length === 10) time = parseInt(time) * 1000
  49. date = new Date(time)
  50. }
  51. const formatObj = {
  52. y: date.getFullYear(),
  53. m: date.getMonth() + 1,
  54. d: date.getDate(),
  55. h: date.getHours(),
  56. i: date.getMinutes(),
  57. s: date.getSeconds(),
  58. a: date.getDay()
  59. }
  60. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  61. let value = formatObj[key]
  62. // Note: getDay() returns 0 on Sunday
  63. if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
  64. if (result.length > 0 && value < 10) {
  65. value = '0' + value
  66. }
  67. return value || 0
  68. })
  69. return time_str
  70. }
  71. export function getIPs(callback) {
  72. var ip_dups = {};
  73. var RTCPeerConnection = window.RTCPeerConnection
  74. || window.mozRTCPeerConnection
  75. || window.webkitRTCPeerConnection;
  76. var useWebKit = !!window.webkitRTCPeerConnection;
  77. var mediaConstraints = {
  78. optional: [{ RtpDataChannels: true }]
  79. };
  80. // 这里就是需要的ICEServer了
  81. var servers = {
  82. iceServers: [
  83. { urls: "stun:stun.services.mozilla.com" },
  84. { urls: "stun:stun.l.google.com:19302" },
  85. ]
  86. };
  87. var pc = new RTCPeerConnection(servers, mediaConstraints);
  88. function handleCandidate(candidate) {
  89. var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/
  90. var hasIp = ip_regex.exec(candidate)
  91. if (hasIp) {
  92. var ip_addr = ip_regex.exec(candidate)[1];
  93. if (ip_dups[ip_addr] === undefined)
  94. callback(ip_addr);
  95. ip_dups[ip_addr] = true;
  96. }
  97. }
  98. // 网络协商的过程
  99. pc.onicecandidate = function (ice) {
  100. if (ice.candidate) {
  101. handleCandidate(ice.candidate.candidate);
  102. }
  103. };
  104. pc.createDataChannel("");
  105. //创建一个SDP(session description protocol)会话描述协议 是一个纯文本信息 包含了媒体和网络协商的信息
  106. pc.createOffer(function (result) {
  107. pc.setLocalDescription(result, function () { }, function () { });
  108. }, function () { });
  109. setTimeout(function () {
  110. var lines = pc.localDescription.sdp.split('\n');
  111. lines.forEach(function (line) {
  112. if (line.indexOf('a=candidate:') === 0)
  113. handleCandidate(line);
  114. });
  115. }, 1000);
  116. }
  117. export function getLang() {
  118. let language = 'en'
  119. try{
  120. let lan
  121. if (localStorage.getItem('language')) {
  122. lan = JSON.parse(localStorage.getItem('language'))
  123. } else {
  124. lan = navigator.language
  125. }
  126. if (['en', 'hi'].indexOf(lan) !== -1) {
  127. language = lan
  128. }
  129. } catch(err) {
  130. }
  131. return language
  132. }
  133. export function setTabBar(that) {
  134. uni.setTabBarItem({
  135. index: 0,
  136. text: that.$t('navbar.txt10'),
  137. })
  138. // uni.setTabBarItem({
  139. // index: 1,
  140. // text: that.$t('navbar.txt15'),
  141. // })
  142. uni.setTabBarItem({
  143. index: 1,
  144. text: that.$t('navbar.txt12'),
  145. })
  146. uni.setTabBarItem({
  147. index: 2,
  148. text: that.$t('navbar.txt11'),
  149. })
  150. uni.setTabBarItem({
  151. index: 3,
  152. text: that.$t('navbar.txt14'),
  153. })
  154. uni.setTabBarItem({
  155. index: 4,
  156. text: that.$t('navbar.txt13'),
  157. })
  158. }