Merge branch 'main' of http://192.168.1.42/developOne/harmonyPool
Showing
4 changed files
with
147 additions
and
2 deletions
| @@ -42,4 +42,6 @@ export { ErrorToastUtils } from './src/main/ets/utils/ErrorToastUtils' | @@ -42,4 +42,6 @@ export { ErrorToastUtils } from './src/main/ets/utils/ErrorToastUtils' | ||
| 42 | 42 | ||
| 43 | export { EmitterUtils } from './src/main/ets/utils/EmitterUtils' | 43 | export { EmitterUtils } from './src/main/ets/utils/EmitterUtils' |
| 44 | 44 | ||
| 45 | -export { EmitterEventId } from './src/main/ets/utils/EmitterEventId' | ||
| 45 | +export { EmitterEventId } from './src/main/ets/utils/EmitterEventId' | ||
| 46 | + | ||
| 47 | +export { HWLocationUtils } from './src/main/ets/utils/HWLocationUtils' |
| 1 | +import { Logger } from './Logger'; | ||
| 2 | +import { PermissionUtils } from './PermissionUtils'; | ||
| 3 | +import { abilityAccessCtrl, bundleManager, Permissions } from '@kit.AbilityKit'; | ||
| 4 | +import { BusinessError } from '@kit.BasicServicesKit'; | ||
| 5 | +import { geoLocationManager } from '@kit.LocationKit'; | ||
| 6 | +import { SPHelper } from './SPHelper'; | ||
| 7 | + | ||
| 8 | +/** | ||
| 9 | + * 系统定位服务实现 | ||
| 10 | + * */ | ||
| 11 | +export class HWLocationUtils { | ||
| 12 | + //d定位相关 | ||
| 13 | + static LOCATION_CITY_NAME = "location_city_name" //定位 | ||
| 14 | + static LOCATION_CITY_CODE = "location_city_code" //定位 | ||
| 15 | + | ||
| 16 | + | ||
| 17 | + static LOCATION: Permissions = 'ohos.permission.LOCATION' | ||
| 18 | + static APPROXIMATELY_LOCATION: Permissions = 'ohos.permission.APPROXIMATELY_LOCATION' | ||
| 19 | + | ||
| 20 | + private static getLocation() { | ||
| 21 | + let requestInfo: geoLocationManager.LocationRequest = { | ||
| 22 | + 'priority': geoLocationManager.LocationRequestPriority.FIRST_FIX, | ||
| 23 | + 'scenario': geoLocationManager.LocationRequestScenario.UNSET, | ||
| 24 | + distanceInterval: 0, | ||
| 25 | + timeInterval: 60, | ||
| 26 | + maxAccuracy: 0 | ||
| 27 | + }; | ||
| 28 | + geoLocationManager.on('locationChange', requestInfo, (data) => { | ||
| 29 | + Logger.debug('location :' + JSON.stringify(data)) | ||
| 30 | + }) | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + private static getLocationData() { | ||
| 34 | + return new Promise<Record<string, string | number>>((success, fail) => { | ||
| 35 | + let requestInfo: geoLocationManager.LocationRequest = { | ||
| 36 | + 'priority': geoLocationManager.LocationRequestPriority.FIRST_FIX, | ||
| 37 | + 'scenario': geoLocationManager.LocationRequestScenario.DAILY_LIFE_SERVICE, | ||
| 38 | + distanceInterval: 0, | ||
| 39 | + timeInterval: 60, | ||
| 40 | + maxAccuracy: 0 | ||
| 41 | + }; | ||
| 42 | + geoLocationManager.on('locationChange', requestInfo, (data) => { | ||
| 43 | + Logger.debug('location :' + JSON.stringify(data)) //{"latitude":31.86870096,"longitude":117.3015341,"altitude":0,"accuracy":5000,"speed":0,"timeStamp":1713332445643,"direction":0,"timeSinceBoot":589519570869240,"additions":"","additionSize":0} | ||
| 44 | + let record: Record<string, string | number> = {}; | ||
| 45 | + record['latitude'] = data.latitude | ||
| 46 | + record['longitude'] = data.longitude | ||
| 47 | + success(record) | ||
| 48 | + }) | ||
| 49 | + }) | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + //开启定位服务 | ||
| 53 | + static async startLocationService() { | ||
| 54 | + let grant = await PermissionUtils.checkPermissions(HWLocationUtils.APPROXIMATELY_LOCATION) | ||
| 55 | + if (grant) { | ||
| 56 | + HWLocationUtils.getCurrentLocation() | ||
| 57 | + return | ||
| 58 | + } | ||
| 59 | + let context = getContext(); | ||
| 60 | + let requestGrant = await PermissionUtils.reqPermissionsFromUser([HWLocationUtils.APPROXIMATELY_LOCATION], context); | ||
| 61 | + Logger.debug('location2 :' + requestGrant) | ||
| 62 | + if (requestGrant) { | ||
| 63 | + HWLocationUtils.getCurrentLocation() | ||
| 64 | + } else { | ||
| 65 | + PermissionUtils.openPermissionsInSystemSettings(context) | ||
| 66 | + } | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + private static getCurrentLocation() { | ||
| 70 | + let requestInfo: geoLocationManager.CurrentLocationRequest = { | ||
| 71 | + 'priority': geoLocationManager.LocationRequestPriority.FIRST_FIX, | ||
| 72 | + 'scenario': geoLocationManager.LocationRequestScenario.DAILY_LIFE_SERVICE, | ||
| 73 | + 'maxAccuracy': 0 | ||
| 74 | + }; | ||
| 75 | + try { | ||
| 76 | + geoLocationManager.getCurrentLocation(requestInfo).then((result) => { | ||
| 77 | + //{"latitude":31.8687047,"longitude":117.30152005,"altitude":0,"accuracy":5000,"speed":0,"timeStamp":1713331875766,"direction":0,"timeSinceBoot":588949694096931,"additions":"","additionSize":0} | ||
| 78 | + Logger.debug('location' + JSON.stringify(result)) | ||
| 79 | + HWLocationUtils.getReverseGeoCodeRequest(result.latitude, result.longitude) | ||
| 80 | + }) | ||
| 81 | + .catch((error: number) => { | ||
| 82 | + Logger.debug('location' + JSON.stringify(error)) | ||
| 83 | + }); | ||
| 84 | + } catch (err) { | ||
| 85 | + } | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + private static getGeoCodeRequest(description: string) { | ||
| 89 | + let requestInfo: geoLocationManager.GeoCodeRequest = { 'description': description }; | ||
| 90 | + geoLocationManager.getAddressesFromLocationName(requestInfo, (error, data) => { | ||
| 91 | + if (data) { | ||
| 92 | + Logger.debug('location :' + JSON.stringify(data)) | ||
| 93 | + } | ||
| 94 | + //[{"latitude":31.898204927828598,"longitude":117.29702564819466,"locale":"zh","placeName":"安徽省合肥市瑶海区白龙路与北二环路辅路交叉口南20米","countryCode":"CN","countryName":"中国","administrativeArea":"安徽省","subAdministrativeArea":"合肥市","locality":"合肥市","subLocality":"瑶海区","roadName":"白龙路与北二环路辅路","subRoadName":"20","premises":"20","postalCode":"","phoneNumber":"18756071597","addressUrl":"","descriptionsSize":0,"isFromMock":false}] | ||
| 95 | + }) | ||
| 96 | + } | ||
| 97 | + | ||
| 98 | + private static getReverseGeoCodeRequest(latitude: number, longitude: number) { | ||
| 99 | + let requestInfo: geoLocationManager.ReverseGeoCodeRequest = { | ||
| 100 | + "latitude": latitude, | ||
| 101 | + "longitude": longitude, | ||
| 102 | + "maxItems": 2 | ||
| 103 | + }; | ||
| 104 | + geoLocationManager.getAddressesFromLocation(requestInfo, (error, data) => { | ||
| 105 | + if (error) { | ||
| 106 | + Logger.debug('location :' + JSON.stringify(error)) | ||
| 107 | + } | ||
| 108 | + if (data) { | ||
| 109 | + Logger.debug('location :' + JSON.stringify(data)) | ||
| 110 | + if (data[0] && data[0].countryName) { | ||
| 111 | + if (data[0].descriptionsSize && data[0].descriptions) { | ||
| 112 | + let code = data[0].descriptions[1] | ||
| 113 | + let cityCode = code.substring(0, 6) | ||
| 114 | + let cityName = data[0].subAdministrativeArea; | ||
| 115 | + if (cityName) { | ||
| 116 | + SPHelper.default.save(HWLocationUtils.LOCATION_CITY_NAME, cityName) | ||
| 117 | + } | ||
| 118 | + if (cityCode) { | ||
| 119 | + SPHelper.default.save(HWLocationUtils.LOCATION_CITY_NAME, cityCode) | ||
| 120 | + } | ||
| 121 | + } | ||
| 122 | + } | ||
| 123 | + } | ||
| 124 | + }) | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + | ||
| 128 | + //取消定位 | ||
| 129 | + static cancelLocation() { | ||
| 130 | + // geoLocationManager.off('locationChange') | ||
| 131 | + return new Promise<boolean>((success, fail) => { | ||
| 132 | + geoLocationManager.off("locationChange", (data) => { | ||
| 133 | + Logger.debug('location :' + JSON.stringify(data)) | ||
| 134 | + success(true) | ||
| 135 | + }) | ||
| 136 | + }) | ||
| 137 | + } | ||
| 138 | +} |
| @@ -43,6 +43,10 @@ struct LoginProtocolWebview { | @@ -43,6 +43,10 @@ struct LoginProtocolWebview { | ||
| 43 | .width(24) | 43 | .width(24) |
| 44 | .aspectRatio(1) | 44 | .aspectRatio(1) |
| 45 | .onClick(() => { | 45 | .onClick(() => { |
| 46 | + if(this.webviewController.accessBackward()){ | ||
| 47 | + this.webviewController.backward() | ||
| 48 | + return | ||
| 49 | + } | ||
| 46 | router.back(); | 50 | router.back(); |
| 47 | }).margin({ left: 16 }) | 51 | }).margin({ left: 16 }) |
| 48 | Text() | 52 | Text() |
| 1 | import { BottomNavigationComponent, LogoutViewModel} from 'wdComponent'; | 1 | import { BottomNavigationComponent, LogoutViewModel} from 'wdComponent'; |
| 2 | import { BreakpointConstants } from 'wdConstant'; | 2 | import { BreakpointConstants } from 'wdConstant'; |
| 3 | 3 | ||
| 4 | -import { BreakpointSystem, EmitterEventId, EmitterUtils, Logger } from 'wdKit'; | 4 | +import { BreakpointSystem, EmitterEventId, EmitterUtils, HWLocationUtils, Logger } from 'wdKit'; |
| 5 | import router from '@ohos.router'; | 5 | import router from '@ohos.router'; |
| 6 | import { promptAction } from '@kit.ArkUI'; | 6 | import { promptAction } from '@kit.ArkUI'; |
| 7 | 7 | ||
| @@ -21,6 +21,7 @@ struct MainPage { | @@ -21,6 +21,7 @@ struct MainPage { | ||
| 21 | } | 21 | } |
| 22 | 22 | ||
| 23 | aboutToAppear() { | 23 | aboutToAppear() { |
| 24 | + HWLocationUtils.startLocationService() | ||
| 24 | this.breakpointSystem.register() | 25 | this.breakpointSystem.register() |
| 25 | Logger.info(TAG, `aboutToAppear `); | 26 | Logger.info(TAG, `aboutToAppear `); |
| 26 | EmitterUtils.receiveEvent(EmitterEventId.FORCE_USER_LOGIN_OUT, () => { | 27 | EmitterUtils.receiveEvent(EmitterEventId.FORCE_USER_LOGIN_OUT, () => { |
-
Please register or login to post a comment