把 makeUrl 和 makeBgUrl 吸收进来。

This commit is contained in:
陆柯 2021-08-18 15:07:04 +08:00
parent 70efe57b8b
commit 6fa099771a

150
index.js
View File

@ -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(
@ -30,6 +34,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 = {} }) {
url = this.makeUrl(url)
@ -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标签
// <iframe id="iframe" sandbox="allow-same-origin" style="display:none;"></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)
},
}