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

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