定位获取城市功能,需要微信地图的key?

This commit is contained in:
2026-07-06 14:02:07 +08:00
parent 67af6d7d53
commit c87d79109a
18 changed files with 792 additions and 70 deletions
@@ -1,7 +1,12 @@
import { BadRequestException, Body, Controller, Get, Inject, Post, Query } from '@nestjs/common';
import { IsIn, IsNotEmpty, IsOptional, IsString } from 'class-validator';
import { BadRequestException, Body, Controller, Get, Inject, Post, Query, Req, UseGuards } from '@nestjs/common';
import { IsIn, IsNotEmpty, IsNumber, IsOptional, IsString } from 'class-validator';
import { ClientApp } from '@dukang/shared-types';
import type { Request } from 'express';
import { WECHAT_PROVIDER } from '../../integrations/integrations.constants';
import type { IWechatProvider } from '../../integrations/wechat/wechat.interface';
import { OptionalJwtAuthGuard } from '../../common/guards/optional-jwt-auth.guard';
import type { AuthUser } from '../../common/guards/jwt-auth.guard';
import { WechatLocationService } from './wechat-location.service';
class PhoneNumberDto {
@IsString()
@@ -14,15 +19,43 @@ class PhoneNumberDto {
platform?: 'mini' | 'h5';
}
class WechatLocationDto {
@IsNumber()
@IsOptional()
latitude?: number;
@IsNumber()
@IsOptional()
longitude?: number;
@IsString()
@IsIn(['jssdk', 'geolocation'])
sdk: 'jssdk' | 'geolocation';
@IsString()
@IsIn(['success', 'fail'])
status: 'success' | 'fail';
@IsString()
@IsOptional()
errMsg?: string;
}
@Controller('common/wechat')
export class WechatController {
constructor(@Inject(WECHAT_PROVIDER) private readonly wechat: IWechatProvider) {}
constructor(
@Inject(WECHAT_PROVIDER) private readonly wechat: IWechatProvider,
private readonly locationService: WechatLocationService,
) {}
@Get('jssdk-config')
async jssdkConfig(@Query('url') url: string) {
async jssdkConfig(@Query('url') url: string, @Req() req: Request) {
if (!url) throw new BadRequestException('url 参数必填');
const pageUrl = decodeURIComponent(url).split('#')[0];
return this.wechat.createJssdkConfig(pageUrl);
const user = (req as Request & { user?: AuthUser }).user;
const actorRef =
user?.actorType === 'USER' ? { refType: 'USER', refId: user.actorId } : undefined;
return this.wechat.createJssdkConfig(pageUrl, actorRef);
}
@Get('oauth-url')
@@ -41,4 +74,21 @@ export class WechatController {
.getPhoneNumberByCode(dto.code, dto.platform ?? 'mini')
.then((phone) => ({ phone }));
}
@Post('location')
@UseGuards(OptionalJwtAuthGuard)
reportLocation(@Req() req: Request, @Body() dto: WechatLocationDto) {
const user = (req as Request & { user?: AuthUser }).user;
const userId = user?.actorType === 'USER' ? user.actorId : undefined;
const clientApp = (req.headers['x-client-app'] as string) || ClientApp.USER_H5;
return this.locationService.reportLocation({
latitude: dto.latitude,
longitude: dto.longitude,
sdk: dto.sdk,
status: dto.status,
errMsg: dto.errMsg,
clientApp,
userId,
});
}
}