把用户可调整的配置参数迁移出去

This commit is contained in:
陆柯 2022-04-02 19:48:21 +08:00
parent 98b3ef9d96
commit 8d2962ebce

View File

@ -12,11 +12,9 @@ export default {
BLACK_TOAST: 'default',
WHITE_BUTTON: 'default',
// internal consts used by unitool
RESPONSIVE_TABBAR_AUTOHIDE: false,
RESPONSIVE_TABBAR_AUTOHIDE_WIDTH_THRESHOLD: 768.768,
RESPONSIVE_TABBAR_ALWAYSHIDE: false,
BACKEND: 'SERVER', // 通过变量来动态切换后台类型:服务器 SERVER 或云服务 UNICLOUD. 应当根据实际需要,在前端所用的 unitool 里覆盖。
// [todo] 能否把这些默认值放到 export 以外?
RESPONSIVE_TABBAR_AUTOHIDE_WIDTH_THRESHOLD_DEFAULT: 0,
BACKEND_DEFAULT: 'SERVER', // 服务器 SERVER 或云服务 UNICLOUD
thisPage() {
return this.constructor.name==='VueComponent' ? this // 对于组件内定义的 i18nText要使用 this 来获得组建内的 i18nText而不是 getCurrentPages[...] 去访问全局页面的 i18nText。
@ -26,32 +24,24 @@ export default {
// 输出命令行提示,可用来取代 console.log/info/warn/error
cclog(...args) {
if (process.env.NODE_ENV === 'development') {
const pageName = this.thisPage()?.route || 'VueApp'
console.log('%c '+JSON.stringify({time:new Date().toJSON(), page:pageName}), 'color:blue', ...args)
}
const pageName = this.thisPage()?.route || 'VueApp'
console.log('%c '+JSON.stringify({time:new Date().toJSON(), page:pageName}), 'color:blue', ...args)
},
ccinfo(...args) {
if (process.env.NODE_ENV === 'development') {
const pageName = this.thisPage()?.route || 'VueApp'
console.info('%c '+JSON.stringify({time:new Date().toJSON(), page:pageName}), 'color:green', ...args)
}
const pageName = this.thisPage()?.route || 'VueApp'
console.info('%c '+JSON.stringify({time:new Date().toJSON(), page:pageName}), 'color:green', ...args)
},
ccwarn(...args) {
if (process.env.NODE_ENV === 'development') {
const pageName = this.thisPage().route || 'VueApp'
console.warn('%c '+JSON.stringify({time:new Date().toJSON(), page:pageName}), 'color:orange', ...args)
}
const pageName = this.thisPage().route || 'VueApp'
console.warn('%c '+JSON.stringify({time:new Date().toJSON(), page:pageName}), 'color:orange', ...args)
},
ccerr(...args) {
if (process.env.NODE_ENV === 'development') {
const pageName = this.thisPage()?.route || 'App'
console.error('%c '+JSON.stringify({time:new Date().toJSON(), page:pageName}), 'color:red', ...args)
}
const pageName = this.thisPage()?.route || 'VueApp'
console.error('%c '+JSON.stringify({time:new Date().toJSON(), page:pageName}), 'color:red', ...args)
},
ccdebug(...args) {
if (process.env.NODE_ENV === 'development') {
const pageName = this.thisPage()?.route || 'App'
const pageName = this.thisPage()?.route || 'VueApp'
console.debug('%c '+JSON.stringify({time:new Date().toJSON(), page:pageName}), 'color:cyan', ...args)
}
},
@ -68,7 +58,9 @@ export default {
return this.i18nText?.[getApp().$store.state.i18n.mylang] || {}
},
setBarTitles ({ windowTitle, pageTitle, pagesJson=wo?.pagesJson } = {}) {
setBarTitles ({ windowTitle, pageTitle,
pagesJson=this.pagesJson || wo?.pagesJson } = {}
) {
const mylang = getApp().$store.state.i18n.mylang
const pageNow = getCurrentPages()[getCurrentPages().length - 1]
@ -103,18 +95,19 @@ export default {
// #ifdef H5
if (uni.getSystemInfoSync().model==='PC') {
if (this.RESPONSIVE_TABBAR_AUTOHIDE) {
if (window.screen.width > this.RESPONSIVE_TABBAR_AUTOHIDE_WIDTH_THRESHOLD) {
const envi = this.envi || wo?.envi || {}
if (envi.ResponsiveTabbar==='AUTOHIDE') {
if (window.screen.width > (envi.ResponsiveTabbarAutohideWidthThreshold || this.RESPONSIVE_TABBAR_AUTOHIDE_WIDTH_THRESHOLD_DEFAULT)) {
uni.hideTabBar()
}
uni.onWindowResize(({size})=>{
if (size.windowWidth > this.RESPONSIVE_TABBAR_AUTOHIDE_WIDTH_THRESHOLD) {
if (size.windowWidth > (envi.ResponsiveTabbarAutohideWidthThreshold || this.RESPONSIVE_TABBAR_AUTOHIDE_WIDTH_THRESHOLD_DEFAULT)) {
uni.hideTabBar()
}else{
uni.showTabBar()
}
})
}else if (this.RESPONSIVE_TABBAR_ALWAYSHIDE) {
}else if (envi.ResponsiveTabbar==='ALWAYSHIDE') {
uni.hideTabBar()
}
}
@ -122,6 +115,7 @@ export default {
},
makeServerUrl(route = '') {
const envi = this.envi || wo?.envi || {}
if (typeof route !== 'string') route = '' // 防止 route 为 null, undefined 等由于后台数据库默认值而造成的异常。
route = route.replace('\\', '/')
@ -129,25 +123,25 @@ export default {
return route
}
let port = this.SERVER_PORT
let port = envi.ServerPort
// #ifdef H5
|| window.location.port
// #endif
let hostname
let protocol
if (process.env.NODE_ENV === 'production') {
hostname = this.SERVER_HOSTNAME
hostname = envi.ServerHostname
// #ifdef H5
|| window.location.hostname
// #endif
protocol = this.SERVER_PROTOCOL || 'https'
protocol = envi.ServerProtocol || 'https'
} else {
hostname =
this.SERVER_HOSTNAME_DEV // 在本机的手机模拟器里可以在虚拟机的浏览器里也可以但是运行到连接的iPhone里就无法连接不知为何
envi.ServerHostnameDev // 在本机的手机模拟器里可以在虚拟机的浏览器里也可以但是运行到连接的iPhone里就无法连接不知为何
// #ifdef H5
|| window.location.hostname
// #endif
protocol = this.SERVER_PROTOCOL_DEV || 'http'
protocol = envi.ServerProtocolDev || 'http'
}
return `${protocol}://${hostname}:${port}/${route.replace(/^\//, '')}`
},
@ -159,13 +153,27 @@ export default {
return ''
},
StartPageForAll ({envi=this.envi || wo?.envi || {}} = {}) {
uni.switchTab({ url: envi?.StartPageForAll })
},
relaunchForOnline ({envi=this.envi || wo?.envi || {}} = {}) {
process.env.NODE_ENV === 'production' && wo.ss.User.onlineUser.uuid && uni.reLaunch({ url: envi?.StartPageForOnline })
},
relaunchForOffline ({envi=this.envi || wo?.envi || {}} ={}) {
process.env.NODE_ENV === 'production' && ! wo.ss.User.onlineUser.uuid && uni.reLaunch({ url: envi?.StartPageForOffline })
},
/** uni.request uniCloud.callFunction
* 返回值{ _state, 成功结果或错误结果 }其中 _state 除了后台返回的还可以是
* - CLIENT_BACKEND_BROKEN: 前端发现后台断线
* - CLIENT_BACKEND_TIMEOUT: 前端发现后台超时
* - CLINET_BACKEND_EXCEPTION: 前端发现后台异常
**/
async callBackend({ backend = this.BACKEND, httpMethod = 'POST', apiVersion = 'api', apiWho, apiTodo, apiWhat = {} }) {
async callBackend({
backend = this.envi?.Backend || wo?.envi?.Backend || this.BACKEND_DEFAULT,
httpMethod = 'POST',
apiVersion = 'api', apiWho, apiTodo, apiWhat = {}
}) {
const thisRoute = this.thisPage()?.route || 'VueApp' // 立刻保存 this.thisPage().route因为在调用后台后可能已切换到了其他页面。
const startTime = new Date().toJSON()
let result = {}
@ -352,7 +360,7 @@ export default {
},
async pickupFile({
backend = this.BACKEND,
backend = this.envi?.Backend || wo?.envi?.Backend || this.BACKEND_DEFAULT,
mediaType = 'image', count = 1, sizeType = ['original', 'compressed'], sourceType = ['album', 'camera'], maxDuration,
url, header = {}, formData = {}, name = 'file',
}) {
@ -482,43 +490,43 @@ export default {
},
getUserEndLanIp(callback) {
let recode = {};
let RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
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 win = iframe.contentWindow
RTCPeerConnection = win.RTCPeerConnection || win.mozRTCPeerConnection || win.webkitRTCPeerConnection
}
//创建实例,生成连接
let pc = new RTCPeerConnection();
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];
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;
callback(ip_isMatch)
recode[ip_isMatch] = true
}
}
//监听icecandidate事件
pc.onicecandidate = (ice) => {
if (ice.candidate) {
handleCandidate(ice.candidate.candidate);
handleCandidate(ice.candidate.candidate)
}
};
//建立一个伪数据的通道
pc.createDataChannel('');
pc.createDataChannel('')
pc.createOffer((res) => {
pc.setLocalDescription(res);
}, () => {});
pc.setLocalDescription(res)
}, () => {})
//延迟,让一切都能完成
setTimeout(() => {
let lines = pc.localDescription.sdp.split('\n');
let lines = pc.localDescription.sdp.split('\n')
lines.forEach(item => {
if (item.indexOf('a=candidate:') === 0) {
handleCandidate(item);
handleCandidate(item)
}
})
}, 1000)