From 6fa099771a75f151f7601c04db0b8ff2ed6172d2 Mon Sep 17 00:00:00 2001 From: "luk.lu" Date: Wed, 18 Aug 2021 15:07:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=8A=20makeUrl=20=E5=92=8C=20makeBgUrl=20?= =?UTF-8?q?=E5=90=B8=E6=94=B6=E8=BF=9B=E6=9D=A5=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 150 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 116 insertions(+), 34 deletions(-) diff --git a/index.js b/index.js index 52d0935..5de2627 100644 --- a/index.js +++ b/index.js @@ -20,6 +20,10 @@ module.exports = { BLACK_TOAST: 'default', WHITE_BUTTON: 'default', + BACKEND: 'SERVER', // 通过变量来动态切换后台类型:服务器 SERVER,或云服务 CLOUD。应当根据实际需要,在前端所用的 unitool 里覆盖。 + + sleep: (ms) => new Promise((resolve, reject) => setTimeout(resolve, ms)), + // 快速输出详尽提示,可用来取代 console.log clog(...message) { console.log( @@ -29,6 +33,74 @@ module.exports = { '】】】】】】】】】】】' ) }, + + thisPage(){ + return getCurrentPages()[getCurrentPages().length - 1] + }, + + localizeText(i18nText) { + // 如果直接挂载到 Vue.prototype 下,那么可以直接访问 this.i18nText。但如果通过 this.$T.localeText 访问,那么 this.i18nText 就报错了。因此安全起见,先获取当前 page + const thisPage = getCurrentPages()[getCurrentPages().length - 1] + if (thisPage.$store?.state?.i18n?.mylang) { + if (typeof(i18nText)==='object' && i18nText && i18nText[thisPage.$store.state.i18n.mylang]){ + return i18nText[thisPage.$store.state.i18n.mylang] + }else if (typeof(i18nText)==='undefined') { + return thisPage.i18nText[thisPage.$store.state.i18n.mylang] + } + } + return '' + }, + + localeText() { + let thisPage = this.$store ? this // 对于组件内定义的 i18nText,要使用 this 而不是 getCurrentPages[...] 去访问。 + : getCurrentPages()[getCurrentPages().length - 1] // 如果直接挂载到 Vue.prototype 下,那么可以直接访问 this.i18nText。但如果通过 this.$T.localeText 访问,那么 this.i18nText 就报错了。因此安全起见,先获取当前 page + return thisPage.i18nText[thisPage.$store.state.i18n.mylang] + }, + + // setBarTitles 迁移到 unip.i18n 库,通过 this.$store.commit('i18n/setBarTitles') 来调用 + // setBarTitles({ windowTitle, pageTitle } = {}) { + // let page = getCurrentPages()[getCurrentPages().length - 1] + // uni.setNavigationBarTitle({ title: pageTitle || page.i18nText[page.$store.state.i18n.mylang].tPageTitle }) + // // #ifdef H5 + // document.title = windowTitle || page.$store.getters['i18n/getAppName'] // 必须放在 setNavigationBarTitle 之后,否则会被其覆盖掉。 + // // #endif + // if (page.$store._mutations['i18n/setTabbar']) page.$store.commit('i18n/setTabbar') // 必须要在有 tab 的页面里重置才有效果 + // }, + + makeUrl(route = '') { + if (route && typeof route === 'object') { + route = `${route.api}/${route.class}/${route.method}` + } + + if (/^https?:\/\//.test(route)) { + return route + } + + let port = this.SERVER_PORT || 6789 + let hostname + let protocol + if (process.env.NODE_ENV === 'production') { + hostname = this.SERVER_HOSTNAME, + protocol = 'https:' + } else { + // #ifdef H5 + hostname = window.location.hostname + protocol = 'http:' + // #endif + // #ifndef H5 + hostname = this.SERVER_HOSTNAME4DEV // 在本机的手机模拟器里可以,在虚拟机的浏览器里也可以,但是运行到连接的iPhone里就无法连接,不知为何 + protocol = 'http:' + // #endif + } + return `${protocol}//${hostname}:${port}/${route}` + }, + + makeBgUrl(path) { + if (path) { + return `url(${this.makeUrl(path)})` + } + return '' + }, // 再次封装 uni.request,输入参数和 uni.request 保持基本一致。主要为了插入 _passtoken,简化 url 的组装,以及输出提示。 async request({ method = 'POST', url, header = {}, data = {} }) { @@ -47,7 +119,6 @@ module.exports = { return [error, response] }, - BACKEND: 'SERVER', // 通过变量来动态切换后台类型:服务器 SERVER,或云服务 CLOUD /** 统一 uni.request 和 uniCloud.callFunction 的调用方法,提供统一、透明的后台调用 * 返回值:{ _state, 成功结果或错误结果 },其中 _state 除了后台返回的,还可以是 * - CLIENT_BACKEND_BROKEN: 前端发现后台断线 @@ -297,39 +368,6 @@ module.exports = { } }, - // setBarTitles 迁移到 unip.i18n 库,通过 this.$store.commit('i18n/setBarTitles') 来调用 - // setBarTitles({ windowTitle, pageTitle } = {}) { - // let page = getCurrentPages()[getCurrentPages().length - 1] - // uni.setNavigationBarTitle({ title: pageTitle || page.i18nText[page.$store.state.i18n.mylang].tPageTitle }) - // // #ifdef H5 - // document.title = windowTitle || page.$store.getters['i18n/getAppName'] // 必须放在 setNavigationBarTitle 之后,否则会被其覆盖掉。 - // // #endif - // if (page.$store._mutations['i18n/setTabbar']) page.$store.commit('i18n/setTabbar') // 必须要在有 tab 的页面里重置才有效果 - // }, - - thisPage(){ - return getCurrentPages()[getCurrentPages().length - 1] - }, - - localizeText(i18nText) { - // 如果直接挂载到 Vue.prototype 下,那么可以直接访问 this.i18nText。但如果通过 this.$T.localeText 访问,那么 this.i18nText 就报错了。因此安全起见,先获取当前 page - const thisPage = getCurrentPages()[getCurrentPages().length - 1] - if (thisPage.$store?.state?.i18n?.mylang) { - if (typeof(i18nText)==='object' && i18nText && i18nText[thisPage.$store.state.i18n.mylang]){ - return i18nText[thisPage.$store.state.i18n.mylang] - }else if (typeof(i18nText)==='undefined') { - return thisPage.i18nText[thisPage.$store.state.i18n.mylang] - } - } - return '' - }, - - localeText() { - let thisPage = this.$store ? this // 对于组件内定义的 i18nText,要使用 this 而不是 getCurrentPages[...] 去访问。 - : getCurrentPages()[getCurrentPages().length - 1] // 如果直接挂载到 Vue.prototype 下,那么可以直接访问 this.i18nText。但如果通过 this.$T.localeText 访问,那么 this.i18nText 就报错了。因此安全起见,先获取当前 page - return thisPage.i18nText[thisPage.$store.state.i18n.mylang] - }, - formatMoney(value, decimal) { return Number(value || 0).toFixed(decimal || 2) // Number(undefined)===NaN }, @@ -393,4 +431,48 @@ module.exports = { } return null }, + + getUserEndLanIp(callback) { + let recode = {}; + let RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; + // 如果不存在则使用一个iframe绕过 + if (!RTCPeerConnection) { + // 因为这里用到了iframe,所以在调用这个方法的script上必须有一个iframe标签 + // + let win = iframe.contentWindow; + RTCPeerConnection = win.RTCPeerConnection || win.mozRTCPeerConnection || win.webkitRTCPeerConnection; + } + //创建实例,生成连接 + let pc = new RTCPeerConnection(); + // 匹配字符串中符合ip地址的字段 + function handleCandidate(candidate) { + let ip_regexp = /([0-9]{1,3}(\.[0-9]{1,3}){3}|([a-f0-9]{1,4}((:[a-f0-9]{1,4}){7}|:+[a-f0-9]{1,4}){6}))/; + let ip_isMatch = candidate.match(ip_regexp)[1]; + if (!recode[ip_isMatch]) { + callback(ip_isMatch); + recode[ip_isMatch] = true; + } + } + //监听icecandidate事件 + pc.onicecandidate = (ice) => { + if (ice.candidate) { + handleCandidate(ice.candidate.candidate); + } + }; + //建立一个伪数据的通道 + pc.createDataChannel(''); + pc.createOffer((res) => { + pc.setLocalDescription(res); + }, () => {}); + //延迟,让一切都能完成 + setTimeout(() => { + let lines = pc.localDescription.sdp.split('\n'); + lines.forEach(item => { + if (item.indexOf('a=candidate:') === 0) { + handleCandidate(item); + } + }) + }, 1000) + }, + }