u
This commit is contained in:
		
							parent
							
								
									a12226c4e2
								
							
						
					
					
						commit
						bb777ea128
					
				
							
								
								
									
										59
									
								
								usertool.js
									
									
									
									
									
								
							
							
						
						
									
										59
									
								
								usertool.js
									
									
									
									
									
								
							| @ -13,7 +13,7 @@ export default { | |||||||
|   WHITE_BUTTON: 'default', |   WHITE_BUTTON: 'default', | ||||||
| 
 | 
 | ||||||
|   // [todo] 能否把这些默认值放到 export 以外?
 |   // [todo] 能否把这些默认值放到 export 以外?
 | ||||||
|   BACKEND_DEFAULT: 'SERVER', // 服务器 SERVER 或云服务 UNICLOUD
 |   BASE_TYPE_DEFAULT: 'SERVER', // 服务器 SERVER 或云服务 UNICLOUD
 | ||||||
| 
 | 
 | ||||||
|   thisPage () { |   thisPage () { | ||||||
|     return this.__page__ |     return this.__page__ | ||||||
| @ -152,12 +152,12 @@ export default { | |||||||
| 
 | 
 | ||||||
|   /** 统一 uni.request 和 uniCloud.callFunction 的调用方法,提供统一、透明的后台调用 |   /** 统一 uni.request 和 uniCloud.callFunction 的调用方法,提供统一、透明的后台调用 | ||||||
|    * 返回值:{ _state, 成功结果或错误结果 },其中 _state 除了后台返回的,还可以是 |    * 返回值:{ _state, 成功结果或错误结果 },其中 _state 除了后台返回的,还可以是 | ||||||
|    * - CLIENT_BACKEND_BROKEN: 前端发现后台断线 |    * - CLIENT_BASEND_BROKEN: 前端发现后台断线 | ||||||
|    * - CLIENT_BACKEND_TIMEOUT: 前端发现后台超时 |    * - CLIENT_BASEND_TIMEOUT: 前端发现后台超时 | ||||||
|    * - CLINET_BACKEND_EXCEPTION: 前端发现后台异常 |    * - CLINET_BASEND_EXCEPTION: 前端发现后台异常 | ||||||
|    **/ |    **/ | ||||||
|   async callBackend ({ |   async callBase ({ | ||||||
|     backend = this.envar?.Backend_Default || wo?.envar?.Backend_Default || this.BACKEND_DEFAULT, |     basendType = this.envar?.Basend_Type_Default || wo?.envar?.Basend_Type_Default || this.BASE_TYPE_DEFAULT, | ||||||
|     httpMethod = 'POST', |     httpMethod = 'POST', | ||||||
|     apiVersion = 'api', |     apiVersion = 'api', | ||||||
|     apiWho, |     apiWho, | ||||||
| @ -168,7 +168,14 @@ export default { | |||||||
|     const startTime = new Date().toJSON() |     const startTime = new Date().toJSON() | ||||||
|     let url = undefined |     let url = undefined | ||||||
|     let result = {} |     let result = {} | ||||||
|     if (backend === 'UNICLOUD') { |     if (basendType === 'UNICLOUD_OBJECT') { | ||||||
|  |       const uniObj = uniCloud.importObject(apiWho) | ||||||
|  |       try { | ||||||
|  |         result = await uniObj[apiTodo](apiWhat) | ||||||
|  |       }catch(error){ | ||||||
|  |         result = { _state: 'CLINET_BASEND_EXCEPTION', error } | ||||||
|  |       } | ||||||
|  |     }else if (basendType === 'UNICLOUD') { | ||||||
|       let { /* success, header, requestedId, */ result: resultCloud = {} } = await uniCloud |       let { /* success, header, requestedId, */ result: resultCloud = {} } = await uniCloud | ||||||
|         .callFunction({ |         .callFunction({ | ||||||
|           name: apiWho, |           name: apiWho, | ||||||
| @ -183,14 +190,14 @@ export default { | |||||||
|           // {errMsg, stack} = error
 |           // {errMsg, stack} = error
 | ||||||
|           if (/request:fail/.test(error.errMsg)) { |           if (/request:fail/.test(error.errMsg)) { | ||||||
|             // 后台云服务无法连接
 |             // 后台云服务无法连接
 | ||||||
|             return { _state: 'CLIENT_BACKEND_BROKEN', error } |             return { _state: 'CLIENT_BASEND_BROKEN', error } | ||||||
|           } else { |           } else { | ||||||
|             // 后台云服务返回异常
 |             // 后台云服务返回异常
 | ||||||
|             return { _state: 'CLIENT_BACKEND_EXCEPTION', error } |             return { _state: 'CLIENT_BASEND_EXCEPTION', error } | ||||||
|           } |           } | ||||||
|         }) |         }) | ||||||
|       result = resultCloud |       result = resultCloud | ||||||
|     } else { |     } else if (basendType === 'SERVER') { | ||||||
|       if (httpMethod === 'GET') { |       if (httpMethod === 'GET') { | ||||||
|         // 如果不是 POST 方法,要额外把参数JSON化
 |         // 如果不是 POST 方法,要额外把参数JSON化
 | ||||||
|         for (let key in apiWhat) { |         for (let key in apiWhat) { | ||||||
| @ -207,17 +214,19 @@ export default { | |||||||
|       if (error) { |       if (error) { | ||||||
|         if (error.errMsg === 'request:fail') { |         if (error.errMsg === 'request:fail') { | ||||||
|           // 后台服务器无法连接
 |           // 后台服务器无法连接
 | ||||||
|           result = { _state: 'CLIENT_BACKEND_BROKEN', error } |           result = { _state: 'CLIENT_BASEND_BROKEN', error } | ||||||
|         } else if (error.errMsg === 'request:fail timeout') { |         } else if (error.errMsg === 'request:fail timeout') { | ||||||
|           // 后台服务器超时
 |           // 后台服务器超时
 | ||||||
|           result = { _state: 'CLIENT_BACKEND_TIMEOUT', error } |           result = { _state: 'CLIENT_BASEND_TIMEOUT', error } | ||||||
|         } else { |         } else { | ||||||
|           // 后台服务器返回异常
 |           // 后台服务器返回异常
 | ||||||
|           result = { _state: 'CLIENT_BACKEND_EXCEPTION', error } |           result = { _state: 'CLIENT_BASEND_EXCEPTION', error } | ||||||
|         } |         } | ||||||
|       } else { |       } else { | ||||||
|         result = resultServer |         result = resultServer | ||||||
|       } |       } | ||||||
|  |     }else { | ||||||
|  |       result = { _state: 'CLIENT_BASEND_TYPE_UNKNOWN' } | ||||||
|     } |     } | ||||||
|     // 注意1,resultServer 和 resultCloud 推荐遵循同样的格式 { _state, error | data },这样方便前端做统一判断。
 |     // 注意1,resultServer 和 resultCloud 推荐遵循同样的格式 { _state, error | data },这样方便前端做统一判断。
 | ||||||
|     // 注意2,虽然预设了 resultServer 和 resultCloud = {},但如果后台返回了 null,那么 resultServer/resultCloud 也是 null。
 |     // 注意2,虽然预设了 resultServer 和 resultCloud = {},但如果后台返回了 null,那么 resultServer/resultCloud 也是 null。
 | ||||||
| @ -226,7 +235,7 @@ export default { | |||||||
|         '%c ' + |         '%c ' + | ||||||
|           JSON.stringify({ startTime: startTime, page: thisRoute, endTime: new Date().toJSON() }) + |           JSON.stringify({ startTime: startTime, page: thisRoute, endTime: new Date().toJSON() }) + | ||||||
|           ' %c ' + |           ' %c ' + | ||||||
|           JSON.stringify({ backend, apiWho, apiTodo, apiWhat, url }) + |           JSON.stringify({ basendType, apiWho, apiTodo, apiWhat, url }) + | ||||||
|           ' %c ' + |           ' %c ' + | ||||||
|           JSON.stringify(result), |           JSON.stringify(result), | ||||||
|         'color:blue', |         'color:blue', | ||||||
| @ -359,7 +368,7 @@ export default { | |||||||
|   }, |   }, | ||||||
| 
 | 
 | ||||||
|   async pickupFile ({ |   async pickupFile ({ | ||||||
|     backend = this.envar?.Backend_Default || wo?.envar?.Backend_Default || this.BACKEND_DEFAULT, |     basendType = this.envar?.Basend_Type_Default || wo?.envar?.Basend_Type_Default || this.BASE_TYPE_DEFAULT, | ||||||
|     mediaType = 'image', |     mediaType = 'image', | ||||||
|     count = 1, |     count = 1, | ||||||
|     sizeType = ['original', 'compressed'], |     sizeType = ['original', 'compressed'], | ||||||
| @ -370,14 +379,14 @@ export default { | |||||||
|     formData = {}, |     formData = {}, | ||||||
|     name = 'file', |     name = 'file', | ||||||
|   } = {}) { |   } = {}) { | ||||||
|     if (backend === 'UNICLOUD') { |     if (/^UNICLOUD/.test(basendType)) { | ||||||
|       const resultCloud = await this.pickupFile2Cloud({ mediaType, count, sizeType, sourceType, maxDuration }) |       const resultCloud = await this.pickupFile2Cloud({ mediaType, count, sizeType, sourceType, maxDuration }) | ||||||
|       return resultCloud |       return resultCloud | ||||||
|     } else if (backend === 'SERVER') { |     } else if (basendType === 'SERVER') { | ||||||
|       const resultServer = await this.pickupFile2Server({ mediaType, count, sizeType, sourceType, maxDuration, url, header, formData, name }) |       const resultServer = await this.pickupFile2Server({ mediaType, count, sizeType, sourceType, maxDuration, url, header, formData, name }) | ||||||
|       return resultServer |       return resultServer | ||||||
|     } else { |     } else { | ||||||
|       return { _state: 'CLEINT_FAIL_UNKNOWN_BACKEND_TYPE', backend } |       return { _state: 'CLEINT_FAIL_UNKNOWN_BASEND_TYPE', basendType } | ||||||
|     } |     } | ||||||
|   }, |   }, | ||||||
| 
 | 
 | ||||||
| @ -437,7 +446,7 @@ export default { | |||||||
|      type=info (by default)/success/error/warning|loading,  |      type=info (by default)/success/error/warning|loading,  | ||||||
|      position:top/bottom  |      position:top/bottom  | ||||||
|     }) |     }) | ||||||
|    * u-toast.show({  |    * uToptips.show({  | ||||||
|      type=default (by default)/primary/success/error/warning/info,  |      type=default (by default)/primary/success/error/warning/info,  | ||||||
|      position:center/top/bottom,  |      position:center/top/bottom,  | ||||||
|      callback // 发生在 toast 之后
 |      callback // 发生在 toast 之后
 | ||||||
| @ -445,16 +454,20 @@ export default { | |||||||
|    */ |    */ | ||||||
|   showToast ({ tool, type, image, title, duration = 2000, ...rest }) { |   showToast ({ tool, type, image, title, duration = 2000, ...rest }) { | ||||||
|     const pageNow = this.thisPage() |     const pageNow = this.thisPage() | ||||||
|     if (tool === 'uni' || !pageNow?.$refs?.toast) { |     const toast = pageNow?.$refs?.toast || getApp().globalData.toast || wo.toast | ||||||
|  |     const popupMessage = pageNow?.$refs?.popupMessage || getApp().globalData.popupMessage || wo.popupMessage | ||||||
|  |     if (tool!=='uni' && toast) { // 来自 <ucToast> 或 <u-toast> 或 <u-top-tips>
 | ||||||
|  |       toast.show({ type, title, duration, ...rest }) | ||||||
|  |     } else if (tool!=='uni' && popupMessage) { // 来自 <uni-popup><uni-popup-message></uni-popup-message></uni-popup>
 | ||||||
|  |       popupMessage.popmsg = title | ||||||
|  |       popupMessage.open() | ||||||
|  |     } else { | ||||||
|       // #ifdef APP-PLUS
 |       // #ifdef APP-PLUS
 | ||||||
|       uni.showToast({ icon: 'none', title, duration, ...rest }) |       uni.showToast({ icon: 'none', title, duration, ...rest }) | ||||||
|       // #endif
 |       // #endif
 | ||||||
|       // #ifdef H5
 |       // #ifdef H5
 | ||||||
|       uni.showToast({ icon: 'none', image, title, duration, ...rest }) |       uni.showToast({ icon: 'none', image, title, duration, ...rest }) | ||||||
|       // #endif
 |       // #endif
 | ||||||
|     } else { |  | ||||||
|       // 根据 html 中不同的组件 <ucToast/> 或 <toast/> 而不同。
 |  | ||||||
|       pageNow.$refs.toast.show({ type, title, duration, ...rest }) |  | ||||||
|     } |     } | ||||||
|   }, |   }, | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user