添加 cclog ccinfo ccwarn ccerr 方法;给 callBackend 的 request/response 上背景色
This commit is contained in:
parent
fd4396213a
commit
caaf23e007
44
index.js
44
index.js
@ -14,17 +14,34 @@ export default {
|
||||
|
||||
BACKEND: 'SERVER', // 通过变量来动态切换后台类型:服务器 SERVER,或云服务 CLOUD。应当根据实际需要,在前端所用的 unitool 里覆盖。
|
||||
|
||||
// 快速输出详尽提示,可用来取代 console.log
|
||||
colog(...message) {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
// 在首页时,getApp() 或 getCurrentPages() 有可能获取不到。
|
||||
const pageName = getCurrentPages().length > 0 ? getCurrentPages().pop().route : 'App'
|
||||
console.log(`【 ${pageName} 【 `, ...message, ' 】】')
|
||||
}
|
||||
thisPage() {
|
||||
return getCurrentPages()[getCurrentPages().length - 1] || {} // 准备挂在到 window 下使用,因此不必探测 this?.$store 了,肯定不存在。用 {} 做备份是为了在 App.vue 中使用时,getCurrentPages() 有可能获取不到。
|
||||
},
|
||||
|
||||
thisPage() {
|
||||
return getCurrentPages()[getCurrentPages().length - 1] // 准备挂在到 window 下使用,因此不必探测 this?.$store 了,肯定不存在。
|
||||
// 输出命令行提示,可用来取代 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:green', ...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:blue', ...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:yellow', ...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)
|
||||
}
|
||||
},
|
||||
|
||||
localizeText(i18nText) {
|
||||
@ -125,8 +142,8 @@ export default {
|
||||
* - CLIENT_BACKEND_TIMEOUT: 前端发现后台超时
|
||||
* - CLINET_BACKEND_EXCEPTION: 前端发现后台异常
|
||||
**/
|
||||
async callBackend({ backend = this.BACKEND, httpMethod = 'POST', apiVersion = 'api', apiWho, apiTodo, apiWhat = {} }) {
|
||||
process.env.NODE_ENV === 'production' || console.log('👇 BackendRequest 👇 ', { backend, apiWho, apiTodo, apiWhat }, ' 👇 👇')
|
||||
async callBackend({ backend = this.BACKEND, httpMethod = 'POST', apiVersion = 'api', apiWho, apiTodo, apiWhat = {} }) {
|
||||
process.env.NODE_ENV === 'production' || (this.cclog() & console.log('%c '+ JSON.stringify({ backend, apiWho, apiTodo, apiWhat }), 'background:SkyBlue'))
|
||||
let result = {}
|
||||
if (backend === 'UNICLOUD') {
|
||||
let { /* success, header, requestedId, */ result: resultCloud = {} } = await uniCloud
|
||||
@ -177,7 +194,9 @@ export default {
|
||||
result = resultServer
|
||||
}
|
||||
}
|
||||
process.env.NODE_ENV === 'production' || console.log('👆 BackendResult 👆 ', JSON.stringify(result), ' 👆 👆') // 不知为何,直接用 result 会输出一个奇怪的对象,要主动添加 JSON.stringify 才按照期望输出。
|
||||
// 注意1,resultServer 和 resultCloud 推荐遵循同样的格式 { _state, error | data },这样方便前端做统一判断。
|
||||
// 注意2,虽然预设了 resultServer 和 resultCloud = {},但如果后台返回了 null,那么 resultServer/resultCloud 也是 null。
|
||||
process.env.NODE_ENV === 'production' || (this.cclog() & console.log('%c '+ JSON.stringify(result), 'background:PapayaWhip')) // 不知为何,直接用 result 会输出一个奇怪的对象,要主动添加 JSON.stringify 才按照期望输出。
|
||||
return result
|
||||
},
|
||||
|
||||
@ -289,7 +308,6 @@ export default {
|
||||
cloudPath: cloudPath, // 关键是要具有文件格式后缀名,这样可以保持阿里云下载链接也用这个后缀名。
|
||||
fileType: mediaType, // = image, video, audio
|
||||
onUploadProgress: function (progressEvent) {
|
||||
// console.log(progressEvent)
|
||||
var percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total)
|
||||
},
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user