From 8905745828e5530a459d664a17f2129aa6c586c4 Mon Sep 17 00:00:00 2001 From: Taric Xin Date: Fri, 24 Dec 2021 15:26:03 +0800 Subject: [PATCH 1/3] edit --- src/app/core/core.service.ts | 4 +- src/app/core/net/business.interceptor.ts | 13 +++++- src/app/core/startup/startup.service.ts | 11 ++++- .../shared/components/amap/amap.service.ts | 46 ++++++++++++++++++- 4 files changed, 68 insertions(+), 6 deletions(-) diff --git a/src/app/core/core.service.ts b/src/app/core/core.service.ts index ab9b87dc..3324f528 100644 --- a/src/app/core/core.service.ts +++ b/src/app/core/core.service.ts @@ -15,7 +15,7 @@ import { EnvironmentService } from '@env/environment.service'; import { NzMessageService } from 'ng-zorro-antd/message'; @Injectable({ - providedIn: 'root', + providedIn: 'root' }) export class CoreService { // 获取当前登录用户信息 @@ -24,6 +24,8 @@ export class CoreService { // 获取当前用户所拥有的菜单 public $api_get_current_user_menus = `/scm/cuc/cuc/functionInfo/getUserHaveFunctionsList`; + position = { lat: '', lng: '' }; + constructor(private injector: Injector) {} // 注入路由 public get router(): Router { diff --git a/src/app/core/net/business.interceptor.ts b/src/app/core/net/business.interceptor.ts index 5a47a69a..8e9db9f3 100644 --- a/src/app/core/net/business.interceptor.ts +++ b/src/app/core/net/business.interceptor.ts @@ -13,6 +13,7 @@ import { environment } from '@env/environment'; import { EAEnvironmentService, EAUserService } from '@shared'; import { Observable, of } from 'rxjs'; import { catchError, mergeMap } from 'rxjs/operators'; +import { CoreService } from '../core.service'; @Injectable() export class BusinessInterceptor implements HttpInterceptor { @@ -21,7 +22,8 @@ export class BusinessInterceptor implements HttpInterceptor { private eaUserSrv: EAUserService, @Optional() @Inject(DA_SERVICE_TOKEN) - private tokenSrv: ITokenService + private tokenSrv: ITokenService, + private coreSrv: CoreService ) {} intercept(req: HttpRequest, next: HttpHandler): Observable> { // 构造新的请求URL @@ -52,12 +54,19 @@ export class BusinessInterceptor implements HttpInterceptor { * 附加额外的请求头 */ private attachAdditionalHeaders(req: HttpRequest): HttpRequest { + let position = {}; + if (this.coreSrv.position.lat && this.coreSrv.position.lng) { + position = { lat: this.coreSrv.position.lat.toString(), lng: this.coreSrv.position.lng.toString() }; + } // 附加环境变量 const header: any = { appId: this.envSrv.env.appId, tenantId: this.envSrv.env.tenantId, - enterpriseId: '0' + enterpriseId: '0', + ...position }; + console.log(header); + // 附加授权声明 const token = this.tokenSrv.get()?.token; diff --git a/src/app/core/startup/startup.service.ts b/src/app/core/startup/startup.service.ts index f3f5e62c..78763f48 100644 --- a/src/app/core/startup/startup.service.ts +++ b/src/app/core/startup/startup.service.ts @@ -2,7 +2,7 @@ import { HttpClient } from '@angular/common/http'; import { Inject, Injectable } from '@angular/core'; import { ACLService } from '@delon/acl'; import { MenuService, SettingsService, TitleService, _HttpClient } from '@delon/theme'; -import { EAUserService } from '@shared'; +import { AmapService, EAUserService } from '@shared'; import { NzSafeAny } from 'ng-zorro-antd/core/types'; import { NzIconService } from 'ng-zorro-antd/icon'; import { Observable, zip } from 'rxjs'; @@ -26,6 +26,7 @@ export class StartupService { private titleService: TitleService, private httpClient: _HttpClient, private userSrv: EAUserService, + private amapService: AmapService, private coreSrv: CoreService ) { iconSrv.addIcon(...ICONS_AUTO, ...ICONS); @@ -35,6 +36,12 @@ export class StartupService { load(): Promise { return new Promise(resolve => { + this.amapService.getCurrentPosition().subscribe(res => { + if (res.position) { + this.coreSrv.position = { lat: res.position.lat, lng: res.position.lng }; + } + }); + let data; if (this.coreSrv.loginStatus) { // 本地菜单 @@ -96,7 +103,7 @@ export class StartupService { const appData = this.httpClient.get(`assets/mocks/app-data.json`).pipe(map((res: any) => res.app)); // 用户数据 - const userData = this.httpClient.post(this.userSrv.$api_get_user_by_token,{}).pipe(map((res: any) => res.data)); + const userData = this.httpClient.post(this.userSrv.$api_get_user_by_token, {}).pipe(map((res: any) => res.data)); // 菜单数据 const menuData = this.httpClient.get('assets/mocks/menu-data.json').pipe(map((res: any) => res.data.menu)); diff --git a/src/app/shared/components/amap/amap.service.ts b/src/app/shared/components/amap/amap.service.ts index 0cc02256..23c3231d 100644 --- a/src/app/shared/components/amap/amap.service.ts +++ b/src/app/shared/components/amap/amap.service.ts @@ -1,6 +1,10 @@ import { Injectable } from '@angular/core'; -import { Observable, Subject } from 'rxjs'; +import { Observable, Subject, throwError } from 'rxjs'; +import AMapLoader from '@amap/amap-jsapi-loader'; +import { amapConf } from '@conf/amap.config'; declare var AMap: any; + +const CONFIG = amapConf; @Injectable({ providedIn: 'root' }) @@ -10,6 +14,7 @@ export class AmapService { public time = 0; sub = new Subject(); + currentSub = new Subject(); //计算路径驾车最优路线的长度与所需时间 drivingCompute(starts: any[], ends: any[]): Observable { @@ -28,4 +33,43 @@ export class AmapService { }); return this.sub; } + + getCurrentPosition(): Observable { + AMapLoader.load({ + key: CONFIG.key, + version: CONFIG.version, + plugins: [ + // 需要使用的的插件列表,如比例尺'AMap.Scale'等 + 'AMap.PathSimplifier' + ], + AMapUI: { + version: CONFIG.AMapUIVersion, + plugins: ['misc/PathSimplifier'] // 需要加载的 AMapUI ui插件 + } + }) + .then(AMap => { + AMap.plugin('AMap.Geolocation', () => { + let driving = new AMap.Geolocation({ + enableHighAccuracy: true, //是否使用高精度定位,默认:true + timeout: 10000, //超过10秒后停止定位,默认:5s + buttonPosition: 'RB', //定位按钮的停靠位置 + buttonOffset: new AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20) + zoomToAccuracy: true //定位成功后是否自动调整地图视野到定位点 + }); + driving.getCurrentPosition((status: string, result: any) => { + console.log(status,result); + + if (status == 'complete') { + this.currentSub.next(result); + } else { + } + }); + }); + }) + .catch(e => { + throwError(e); + }); + + return this.currentSub; + } } From 3c618492a9836d846bde321d3f1f3daa32b6c720 Mon Sep 17 00:00:00 2001 From: Taric Xin Date: Fri, 24 Dec 2021 15:26:15 +0800 Subject: [PATCH 2/3] edit --- src/app/core/net/business.interceptor.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/app/core/net/business.interceptor.ts b/src/app/core/net/business.interceptor.ts index 8e9db9f3..8179cedc 100644 --- a/src/app/core/net/business.interceptor.ts +++ b/src/app/core/net/business.interceptor.ts @@ -65,8 +65,6 @@ export class BusinessInterceptor implements HttpInterceptor { enterpriseId: '0', ...position }; - console.log(header); - // 附加授权声明 const token = this.tokenSrv.get()?.token; From c421bcd829b304530faee7a9dd0454b7bad7c3e2 Mon Sep 17 00:00:00 2001 From: Taric Xin Date: Fri, 24 Dec 2021 15:28:17 +0800 Subject: [PATCH 3/3] edit --- src/app/shared/components/amap/amap.service.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/app/shared/components/amap/amap.service.ts b/src/app/shared/components/amap/amap.service.ts index 23c3231d..d05e58fe 100644 --- a/src/app/shared/components/amap/amap.service.ts +++ b/src/app/shared/components/amap/amap.service.ts @@ -57,11 +57,10 @@ export class AmapService { zoomToAccuracy: true //定位成功后是否自动调整地图视野到定位点 }); driving.getCurrentPosition((status: string, result: any) => { - console.log(status,result); - if (status == 'complete') { this.currentSub.next(result); } else { + console.log('定位获取失败'); } }); });