From 15db17d5ab6b933f599293bbe6097665bfa6e9ed Mon Sep 17 00:00:00 2001 From: duqingquan Date: Wed, 6 Sep 2023 20:25:43 +0800 Subject: [PATCH] uniError add --- pages/index/index.vue | 13 +- .../utssdk/app-android/index.uts | 25 +-- .../utssdk/interface.uts | 10 +- .../uni-wifi/utssdk/app-android/index.uts | 165 +++++++++--------- uni_modules/uni-wifi/utssdk/app-ios/index.uts | 80 +++------ uni_modules/uni-wifi/utssdk/interface.uts | 12 +- 6 files changed, 127 insertions(+), 178 deletions(-) diff --git a/pages/index/index.vue b/pages/index/index.vue index 8a23d5c..744af7f 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -19,7 +19,7 @@ - + @@ -46,6 +46,17 @@ fn:function(res){ console.log(res) }, + getLocationTest() { + console.log(" ------- getLocationTest: "); + uni.getLocation({ + type: 'gcj02 ', + success (res) { + console.log(" success ",res); + },fail (res) { + console.log(" fail ",res); + } + }) + }, onGetWifiList2_assert0() { console.log(" ------- onGetWifiList2_assert0: ",this.id); const fn = res => console.log('onGetWifiList res', res) diff --git a/uni_modules/uni-getLocation-tencent/utssdk/app-android/index.uts b/uni_modules/uni-getLocation-tencent/utssdk/app-android/index.uts index 7e8be43..4ad38af 100644 --- a/uni_modules/uni-getLocation-tencent/utssdk/app-android/index.uts +++ b/uni_modules/uni-getLocation-tencent/utssdk/app-android/index.uts @@ -21,21 +21,14 @@ export const getLocation : GetLocation = function (options : GetLocationOptions) if(!checkHasIntegration()){ // 当前没有集成腾讯sdk,提示需要打包 - let ret : GetLocationFail = { - errCode: -10, - errMsg: "需要打自定义基座", - errSubject: "uni-getLocation-tencent" - } + let ret = new UniError("uni-getLocation-tencent",-10,"需要打自定义基座"); + options.fail?.(ret) options.complete?.(ret) return; } if (!checkLocationConfig()) { - let ret : GetLocationFail = { - errCode: -20, - errMsg: "未通过配置预校验,通常是app key 配置错误", - errSubject: "uni-getLocation-tencent" - } + let ret = new UniError("uni-getLocation-tencent",-20,"未通过配置预校验,通常是app key 配置错误"); options.fail?.(ret) options.complete?.(ret) return; @@ -52,11 +45,7 @@ export const getLocation : GetLocation = function (options : GetLocationOptions) } }, function (_doNotAskAgain:boolean,_grantedList:string[]) { console.log("用户拒绝了部分权限:") - let ret : GetLocationFail = { - errCode: -30, - errMsg: "permission missed.", - errSubject: "uni-getLocation-tencent" - } + let ret = new UniError("uni-getLocation-tencent",-30,"permission missed."); options.fail?.(ret) options.complete?.(ret) }) @@ -181,11 +170,7 @@ function getLocationImpl(locationOptions : GetLocationOptions) { if(locationOptions.type != null && locationOptions.type!.toUpperCase() != 'GCJ-02' && locationOptions.type!.toUpperCase() != 'GCJ02'){ // 腾讯定位只支持GCJ-02,如果不是则报错 - let ret : GetLocationFail = { - errCode: -1, - errMsg: "GCJ-02 support only.", - errSubject: "uni-getLocation-tencent" - } + let ret = new UniError("uni-getLocation-tencent",-1,"GCJ-02 support only."); locationOptions.fail?.(ret) locationOptions.complete?.(ret) return diff --git a/uni_modules/uni-getLocation-tencent/utssdk/interface.uts b/uni_modules/uni-getLocation-tencent/utssdk/interface.uts index 96bc627..3ed7b50 100644 --- a/uni_modules/uni-getLocation-tencent/utssdk/interface.uts +++ b/uni_modules/uni-getLocation-tencent/utssdk/interface.uts @@ -43,15 +43,7 @@ export type GetLocationSuccess = { address: any | null }; type GetLocationSuccessCallback = (result: GetLocationSuccess) => void; -export type GetLocationFail = UniError; -type UniError = { - errSubject: string, - errCode: number, - errMsg: string, - data: object | null, - cause: any | null -}; -type GetLocationFailCallback = (result: GetLocationFail) => void; +type GetLocationFailCallback = (result: UniError) => void; type GetLocationComplete = any; type GetLocationCompleteCallback = (result: GetLocationComplete) => void; export type GetLocationOptions = { diff --git a/uni_modules/uni-wifi/utssdk/app-android/index.uts b/uni_modules/uni-wifi/utssdk/app-android/index.uts index 5669130..40312b4 100644 --- a/uni_modules/uni-wifi/utssdk/app-android/index.uts +++ b/uni_modules/uni-wifi/utssdk/app-android/index.uts @@ -210,8 +210,8 @@ function wrapWifiConfiguration(SSID : string, password ?: string, passwordType : /** - * 判断当前wifi的加密类型 - */ + * 判断当前wifi的加密类型 + */ function getSecurityType(result : ScanResult) : string { if (result.capabilities.contains("WEP")) { return "WEP"; @@ -370,18 +370,21 @@ export function startWifi(option : WifiOption) { // 需要先开启wifi,才能使用后续的功能 let requestCode = 1001; let permissionWifi = arrayOf("android.permission.ACCESS_FINE_LOCATION"); - var result : UniWifiResult = { + + let result : UniWifiResult = { errCode: 12001, errMsg: "startWifi:premission loss", errSubject: "uni-startWifi" } + // 检查权限 if (ActivityCompat.checkSelfPermission(UTSAndroid.getUniActivity()!, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(UTSAndroid.getUniActivity()!, permissionWifi, requestCode.toInt()) // 尚不具备权限,返回错误 - option.fail?.(result) - option.complete?.(result) + let err = new UniError("uni-startWifi",12001,"startWifi:premission loss"); + option.fail?.(err) + option.complete?.(err) return; } @@ -392,11 +395,9 @@ export function startWifi(option : WifiOption) { // 用户没有开启wifi 总开关 if (!wifiManager.isWifiEnabled()) { // wifi 没开启 - result.errCode = 12005; - result.errMsg = "wifi not turned on"; - - option.fail?.(result); - option.complete?.(result); + let err = new UniError("uni-startWifi",12005,"wifi not turned on"); + option.fail?.(err); + option.complete?.(err); return; } // 初始化wifi 状态广播监听,后续所有的api,均基于此 @@ -456,16 +457,13 @@ export function startWifi(option : WifiOption) { @Suppress("DEPRECATION") export function getWifiList(option : WifiOption) { - var result : UniWifiResult = { - errCode: 12000, - errMsg: "getWifiList:fail:not invoke startWifi", - errSubject: "uni-getWifiList" - } if (Global.mReceiver == null) { // 还没调用startWifi 提示报错 - option.fail?.(result) - option.complete?.(result) + let err = new UniError("uni-getWifiList",12000,"getWifiList:fail:not invoke startWifi"); + + option.fail?.(err) + option.complete?.(err) return } @@ -550,11 +548,13 @@ export function offGetWifiList(callback? : UTSCallback) { /** * 真正执行wifi链接逻辑 */ -function realWifiConnect(option : WifiConnectOption,result : UniWifiResult){ +function realWifiConnect(option : WifiConnectOption){ if (Global.mReceiver == null || Global.scanList.length < 1) { - option.fail?.(result) - option.complete?.(result) + + let err = new UniError("uni-connectWifi",12000,"connectWifi:fail:not invoke startWifi"); + option.fail?.(err) + option.complete?.(err) return } @@ -569,8 +569,9 @@ function realWifiConnect(option : WifiConnectOption,result : UniWifiResult){ if (scanWifiInfo == null) { // 不在扫描列表中返回错误 - option.fail?.(result) - option.complete?.(result) + let err = new UniError("uni-connectWifi",12000,"connectWifi:fail:not invoke startWifi"); + option.fail?.(err) + option.complete?.(err) return } @@ -596,16 +597,16 @@ function realWifiConnect(option : WifiConnectOption,result : UniWifiResult){ // add since 2023-03-28,如果当前系统大于等于android10, 则明确当前系统不支持 if(Build.VERSION.SDK_INT > 28){ // 系统大于android 9 - result.errCode = 12001 - result.errMsg = "connectWifi:system not support" + let err = new UniError("uni-connectWifi",12001,"connectWifi:system not support"); + option.fail?.(err) + option.complete?.(err) }else{ // 移除之前的配置失败了,返回错误,需要用户手动取消保存一下 - result.errCode = 12013 - result.errMsg = "connectWifi:wifi config may be expired" + let err = new UniError("uni-connectWifi",12013,"connectWifi:wifi config may be expired"); + option.fail?.(err) + option.complete?.(err) } - option.fail?.(result) - option.complete?.(result) return } @@ -626,11 +627,11 @@ function realWifiConnect(option : WifiConnectOption,result : UniWifiResult){ let netID = wifiManager.addNetwork(wifiConfigration); // 如果-1 说明没添加上,报错即可 if (netID < 0) { - result.errCode = 12002 - result.errMsg = "connectWifi:password error Wi-Fi" + + let err = new UniError("uni-connectWifi",12002,"connectWifi:password error Wi-Fi"); - option.fail?.(result) - option.complete?.(result) + option.fail?.(err) + option.complete?.(err) return } @@ -639,11 +640,10 @@ function realWifiConnect(option : WifiConnectOption,result : UniWifiResult){ let enabled = wifiManager.enableNetwork(netID, true); if (!enabled) { - result.errCode = 12007 - result.errMsg = "connectWifi:user denied" - - option.fail?.(result) - option.complete?.(result) + let err = new UniError("uni-connectWifi",12007,"connectWifi:user denied"); + + option.fail?.(err) + option.complete?.(err) return } @@ -657,14 +657,18 @@ function realWifiConnect(option : WifiConnectOption,result : UniWifiResult){ if (!connected) { // 出错了,返回错误 // 兜底的报错 - result.errCode = 12010 - result.errMsg = "connectWifi:fail:unknown error" - - option.fail?.(result) - option.complete?.(result) + let err = new UniError("uni-connectWifi",12010,"connectWifi:fail:unknown error"); + option.fail?.(err) + option.complete?.(err) return } + let result : UniWifiResult = { + errCode: 0, + errMsg: "connectWifi:ok", + errSubject: "uni-connectWifi", + } + wifiManager.saveConfiguration() //scanWifiInfo 根据 partialInfo 填充给返回字段 if (option.partialInfo != null && option.partialInfo == true) { @@ -676,9 +680,6 @@ function realWifiConnect(option : WifiConnectOption,result : UniWifiResult){ result.wifi = wrapUniWifiInfoFromAndroid(scanWifiInfo) } - // result.wifi = scanWifiInfo.toUTSJSON(option.partialInfo) - result.errCode = 0 - result.errMsg = "connectWifi:ok" option.success?.(result) option.complete?.(result) @@ -690,20 +691,16 @@ function realWifiConnect(option : WifiConnectOption,result : UniWifiResult){ @Suppress("UNUSED_PARAMETER", "DEPRECATION") export function connectWifi(option : WifiConnectOption) { - - var result : UniWifiResult = { - errCode: 12000, - errMsg: "connectWifi:fail:not invoke startWifi", - errSubject: "uni-connectWifi", - } - if (option.maunal == true) { // 指定了手动模式 let manunalIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS); UTSAndroid.getUniActivity()!.startActivity(manunalIntent); - result.errCode = 0 - result.errMsg = "connectWifi:ok" + let result : UniWifiResult = { + errCode: 0, + errMsg: "connectWifi:ok", + errSubject: "uni-connectWifi", + } option.success?.(result) option.complete?.(result) @@ -722,7 +719,7 @@ export function connectWifi(option : WifiConnectOption) { if(taskCount >= 5 || startWifiScaning == false){ // 超过10s了。或者扫描过程结束了 clearInterval(taskId) - realWifiConnect(option,result) + realWifiConnect(option) } },2000) @@ -730,7 +727,7 @@ export function connectWifi(option : WifiConnectOption) { clearInterval(taskId) }); }else{ - realWifiConnect(option,result) + realWifiConnect(option) } @@ -744,13 +741,9 @@ export function connectWifi(option : WifiConnectOption) { export function stopWifi(option : WifiOption) { // 需要先开启wifi,才能使用后续的功能 if (Global.mReceiver == null) { - var result : UniWifiResult = { - errCode: 12000, - errSubject: "uni-stopWifi", - errMsg: "stopWifi:not init" - } - option.fail?.(result) - option.complete?.(result) + let err = new UniError("uni-stopWifi",12000,"stopWifi:not init"); + option.fail?.(err) + option.complete?.(err) return } @@ -771,6 +764,7 @@ export function stopWifi(option : WifiOption) { errSubject: "uni-stopWifi", errMsg: "stopWifi:ok" } + option.success?.(result) option.complete?.(result) @@ -786,16 +780,12 @@ export function getConnectedWifi(option : GetConnectedWifiOptions) { SSID: "" } - var res : UniWifiResult = { - errCode: 12000, - errMsg: "getConnectedWifi:fail:not invoke startWifi", - errSubject: "uni-getConnectedWifi", - } - + if (Global.mReceiver == null) { // 还没调用startWifi 提示报错 - option.fail?.(res) - option.complete?.(res) + let err = new UniError("uni-getConnectedWifi",12000,"getConnectedWifi:fail:not invoke startWifi"); + option.fail?.(err) + option.complete?.(err) return } @@ -803,14 +793,14 @@ export function getConnectedWifi(option : GetConnectedWifiOptions) { // 需要先校验权限,没有位置权限无法获取wifi if (ActivityCompat.checkSelfPermission(UTSAndroid.getUniActivity()!, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // 尚不具备权限,返回错误 - res.errCode = 12001 - res.errMsg = "getConnectedWifi:permission loss" - - option.fail?.(res) - option.complete?.(res) + let err = new UniError("uni-getConnectedWifi",12001,"getConnectedWifi:permission loss"); + option.fail?.(err) + option.complete?.(err) return; } + + const context = UTSAndroid.getAppContext(); if (context != null) { const wm = context.getSystemService( @@ -821,6 +811,13 @@ export function getConnectedWifi(option : GetConnectedWifiOptions) { const winfo = wm.getConnectionInfo(); wifiInfo = wrapUniWifiInfoFromConnectInfo(winfo); + + let res : UniWifiResult = { + errCode: 0, + errMsg: "getConnectedWifi:ok", + errSubject: "uni-getConnectedWifi", + } + // 判断一下是否wifi 关闭了 if (option.partialInfo!= null) { let ret : UniWifiInfo = { @@ -829,24 +826,22 @@ export function getConnectedWifi(option : GetConnectedWifiOptions) { res.wifi = ret; } else { if (wifiInfo.BSSID == null || zeroCountNum(wifiInfo.BSSID) > 3) { - res.errCode = 12005 - res.errMsg = "getConnectedWifi:fail:wifi is disable" - - option.fail?.(res) - option.complete?.(res) + let err = new UniError("uni-getConnectedWifi",12005,"getConnectedWifi:fail:wifi is disable"); + option.fail?.(err) + option.complete?.(err) return } res.wifi = wifiInfo; } - res.errCode = 0 - res.errMsg = "getConnectedWifi:ok" option.success?.(res) option.complete?.(res) return } - option.fail?.(res) - option.complete?.(res) + + let err = new UniError("uni-getConnectedWifi",12000,"getConnectedWifi:fail:not invoke startWifi"); + option.fail?.(err) + option.complete?.(err) } diff --git a/uni_modules/uni-wifi/utssdk/app-ios/index.uts b/uni_modules/uni-wifi/utssdk/app-ios/index.uts index 27f23ba..a3516a9 100644 --- a/uni_modules/uni-wifi/utssdk/app-ios/index.uts +++ b/uni_modules/uni-wifi/utssdk/app-ios/index.uts @@ -114,24 +114,14 @@ function fetchConnectedWifiWithLocationPromise(option: GetConnectedWifiOptions) option.success?.(res) option.complete?.(res) }else { - const res: UniWifiResult = { - errSubject: "uni-getConnectedWifi", - errCode: 12010, - errMsg: "getConnectedWifi:system internal error", - wifi: null - } - option.fail?.(res) - option.complete?.(res) + let err = new UniError("uni-getConnectedWifi",12010,"getConnectedWifi:system internal error"); + option.fail?.(err) + option.complete?.(err) } }else { - const res: UniWifiResult = { - errSubject: "uni-getConnectedWifi", - errCode: 12010, - errMsg: "getConnectedWifi:system internal error", - wifi: null - } - option.fail?.(res) - option.complete?.(res) + let err = new UniError("uni-getConnectedWifi",12010,"getConnectedWifi:system internal error"); + option.fail?.(err) + option.complete?.(err) } } @@ -174,7 +164,7 @@ export const stopWifi: StopWifi = function (option: WifiOption) { errCode: 0, errMsg: "stopWifi:ok", wifi: null - } + } option.success?.(res) option.complete?.(res) } @@ -183,14 +173,9 @@ export const stopWifi: StopWifi = function (option: WifiOption) { * 获取wifi列表, 在调用之前需要引导用户跳转到系统设置-WIFI设置页面,系统搜索周边wifi后app才能接收到回调 */ export const getWifiList: GetWifiList = function (option: WifiOption) { - let res: UniWifiResult = { - errSubject: "uni-getWifiList", - errCode: 12001, - errMsg: "getWifiList:system not support", - wifi: null - } - option.fail?.(res) - option.complete?.(res) + let err = new UniError("uni-getWifiList",12001,"getWifiList:system not support"); + option.fail?.(err) + option.complete?.(err) } /* 获取wifi列表的回调 @@ -213,28 +198,18 @@ export const offGetWifiList: OffGetWifiList = function (callback: UniWifiCallbac */ export const getConnectedWifi: GetConnectedWifi = function (option: GetConnectedWifiOptions) { if (UniWiFiModuleGloabInfo.alreadyStartWifi == false) { - let res: UniWifiResult = { - errSubject: "uni-getConnectedWifi", - errCode: 12000, - errMsg: "getConnectedWifi:not init", - wifi: null - } - option.fail?.(res) - option.complete?.(res) + let err = new UniError("uni-getConnectedWifi",12000,"getConnectedWifi:not init"); + option.fail?.(err) + option.complete?.(err) } else{ if (UIDevice.current.systemVersion >= "13.0") { requestLocationPromise((success) => { if (success == true) { fetchConnectedWifiWithLocationPromise(option) }else { - let res: UniWifiResult = { - errSubject: "uni-getConnectedWifi", - errCode: 12007, - errMsg: "getConnectedWifi:user denied", - wifi: null - } - option.fail?.(res) - option.complete?.(res) + let err = new UniError("uni-getConnectedWifi",12007,"getConnectedWifi:user denied"); + option.fail?.(err) + option.complete?.(err) } }) } else{ @@ -247,14 +222,10 @@ export const getConnectedWifi: GetConnectedWifi = function (option: GetConnected * 连接wifi */ export const connectWifi: ConnectWifi = function (option: WifiConnectOption) { - let res: UniWifiResult = { - errSubject: "uni-connectWifi", - errCode: 12001, - errMsg: "connectWifi:system not support", - wifi: null - } - option.fail?.(res) - option.complete?.(res) + + let err = new UniError("uni-connectWifi",12001,"connectWifi:system not support"); + option.fail?.(err) + option.complete?.(err) } @@ -290,12 +261,7 @@ export const onOffWifiConnectedWithPartialInfo: OnOffWifiConnectedWithPartialInf * 设置 wifiList 中 AP 的相关信息。在 onGetWifiList 回调后调用,iOS特有接口。 */ export const setWifiList: SetWifiList = function (option: WifiOption) { - let res: UniWifiResult = { - errSubject: "uni-setWifiList", - errCode: 12001, - errMsg: "setWifiList:system not support", - wifi: null - } - option.fail?.(res) - option.complete?.(res) + let err = new UniError("uni-setWifiList",12001,"setWifiList:system not support"); + option.fail?.(err) + option.complete?.(err) } \ No newline at end of file diff --git a/uni_modules/uni-wifi/utssdk/interface.uts b/uni_modules/uni-wifi/utssdk/interface.uts index 89cf346..393ab76 100644 --- a/uni_modules/uni-wifi/utssdk/interface.uts +++ b/uni_modules/uni-wifi/utssdk/interface.uts @@ -3,8 +3,8 @@ */ export type WifiOption = { success ?: (res : UniWifiResult) => void; - fail ?: (res : UniWifiResult) => void; - complete ?: (res : UniWifiResult) => void; + fail ?: (res : UniError) => void; + complete ?: (res : any) => void; }; /** @@ -17,8 +17,8 @@ export type WifiConnectOption = { maunal ?: boolean; partialInfo ?: boolean; //ios不生效 success ?: (res : UniWifiResult) => void; - fail ?: (res : UniWifiResult) => void; - complete ?: (res : UniWifiResult) => void; + fail ?: (res : UniError) => void; + complete ?: (res : any) => void; } /** @@ -27,8 +27,8 @@ export type WifiConnectOption = { export type GetConnectedWifiOptions = { partialInfo ?: boolean success ?: (res : UniWifiResult) => void - fail ?: (res : UniWifiResult) => void - complete ?: (res : UniWifiResult) => void + fail ?: (res : UniError) => void + complete ?: (res : any) => void } /* -- GitLab