123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- export function numberToK(v) {
- if (!v) {
- return ''
- }
- try{
- // if (v >= 10000) {
- // return (v / 10000).toFixed(1) + 'w'
- // }
- if (v >= 1000) {
- return (v / 1000).toFixed(1) + 'K'
- }
- return v
- }catch(err) {
- return v
- }
-
- }
- export function formatTime(time, that) {
- if (('' + time).length === 10) {
- time = parseInt(time) * 1000
- } else {
- time = +time
- }
- const d = new Date(time)
- const now = Date.now()
- const diff = (now - d) / 1000
- if (diff < 30) {
- return '1' + that.$t('game.lab2')
- } else if (diff < 3600) {
- // less 1 hour
- return Math.ceil(diff / 60) + that.$t('game.lab2')
- } else if (diff < 3600 * 24) {
- return Math.ceil(diff / 3600) + that.$t('game.lab3')
- } else if (diff < 3600 * 24 * 30) {
- return Math.ceil(diff / (3600 * 24)) + that.$t('game.lab4')
- } else {
- return Math.ceil(diff / (3600 * 24 * 30)) + that.$t('game.lab5')
- }
- }
- export function parseTime(time, cFormat) {
- if (arguments.length === 0) {
- return null
- }
- const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
- let date
- if (typeof time === 'object') {
- date = time
- } else {
- if (('' + time).length === 10) time = parseInt(time) * 1000
- date = new Date(time)
- }
- const formatObj = {
- y: date.getFullYear(),
- m: date.getMonth() + 1,
- d: date.getDate(),
- h: date.getHours(),
- i: date.getMinutes(),
- s: date.getSeconds(),
- a: date.getDay()
- }
- const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
- let value = formatObj[key]
- // Note: getDay() returns 0 on Sunday
- if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
- if (result.length > 0 && value < 10) {
- value = '0' + value
- }
- return value || 0
- })
- return time_str
- }
- export function getIPs(callback) {
- var ip_dups = {};
- var RTCPeerConnection = window.RTCPeerConnection
- || window.mozRTCPeerConnection
- || window.webkitRTCPeerConnection;
- var useWebKit = !!window.webkitRTCPeerConnection;
- var mediaConstraints = {
- optional: [{ RtpDataChannels: true }]
- };
- // 这里就是需要的ICEServer了
- var servers = {
- iceServers: [
- { urls: "stun:stun.services.mozilla.com" },
- { urls: "stun:stun.l.google.com:19302" },
- ]
- };
- var pc = new RTCPeerConnection(servers, mediaConstraints);
- function handleCandidate(candidate) {
- var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/
- var hasIp = ip_regex.exec(candidate)
- if (hasIp) {
- var ip_addr = ip_regex.exec(candidate)[1];
- if (ip_dups[ip_addr] === undefined)
- callback(ip_addr);
- ip_dups[ip_addr] = true;
- }
- }
- // 网络协商的过程
- pc.onicecandidate = function (ice) {
- if (ice.candidate) {
- handleCandidate(ice.candidate.candidate);
- }
- };
- pc.createDataChannel("");
- //创建一个SDP(session description protocol)会话描述协议 是一个纯文本信息 包含了媒体和网络协商的信息
- pc.createOffer(function (result) {
- pc.setLocalDescription(result, function () { }, function () { });
- }, function () { });
- setTimeout(function () {
- var lines = pc.localDescription.sdp.split('\n');
- lines.forEach(function (line) {
- if (line.indexOf('a=candidate:') === 0)
- handleCandidate(line);
- });
- }, 1000);
- }
- export function getLang() {
- let language = 'en'
- try{
- let lan
- if (localStorage.getItem('language')) {
- lan = JSON.parse(localStorage.getItem('language'))
- } else {
- lan = navigator.language
- }
- if (['en', 'hi'].indexOf(lan) !== -1) {
- language = lan
- }
- } catch(err) {
- }
- return language
- }
- export function setTabBar(that) {
- uni.setTabBarItem({
- index: 0,
- text: that.$t('navbar.txt10'),
- })
- // uni.setTabBarItem({
- // index: 1,
- // text: that.$t('navbar.txt15'),
- // })
- uni.setTabBarItem({
- index: 1,
- text: that.$t('navbar.txt12'),
- })
- uni.setTabBarItem({
- index: 2,
- text: that.$t('navbar.txt11'),
- })
- uni.setTabBarItem({
- index: 3,
- text: that.$t('navbar.txt14'),
- })
- uni.setTabBarItem({
- index: 4,
- text: that.$t('navbar.txt13'),
- })
- }
|