From caaf23e007109508d957b6228a69cf703bfbc2fe Mon Sep 17 00:00:00 2001 From: Luk Lu Date: Fri, 11 Mar 2022 14:43:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20cclog=20ccinfo=20ccwarn=20?= =?UTF-8?q?ccerr=20=E6=96=B9=E6=B3=95=EF=BC=9B=E7=BB=99=20callBackend=20?= =?UTF-8?q?=E7=9A=84=20request/response=20=E4=B8=8A=E8=83=8C=E6=99=AF?= =?UTF-8?q?=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 823b316..a5f2efb 100644 --- a/index.js +++ b/index.js @@ -14,17 +14,34 @@ export default { BACKEND: 'SERVER', // 通过变量来动态切换后台类型:服务器 SERVER,或云服务 CLOUD。应当根据实际需要,在前端所用的 unitool 里覆盖。 - // 快速输出详尽提示,可用来取代 console.log - colog(...message) { + thisPage() { + return getCurrentPages()[getCurrentPages().length - 1] || {} // 准备挂在到 window 下使用,因此不必探测 this?.$store 了,肯定不存在。用 {} 做备份是为了在 App.vue 中使用时,getCurrentPages() 有可能获取不到。 + }, + + // 输出命令行提示,可用来取代 console.log/info/warn/error + cclog(...args) { if (process.env.NODE_ENV === 'development') { - // 在首页时,getApp() 或 getCurrentPages() 有可能获取不到。 - const pageName = getCurrentPages().length > 0 ? getCurrentPages().pop().route : 'App' - console.log(`【 ${pageName} 【 `, ...message, ' 】】') + const pageName = this.thisPage()?.route || 'VueApp' + console.log('%c '+JSON.stringify({time:new Date().toJSON(), page:pageName}), 'color:green', ...args) } }, - - thisPage() { - return getCurrentPages()[getCurrentPages().length - 1] // 准备挂在到 window 下使用,因此不必探测 this?.$store 了,肯定不存在。 + 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) }, })