diff --git a/ZR.Admin.WebApi/Controllers/WebApiController.cs b/ZR.Admin.WebApi/Controllers/WebApiController.cs index dca21de..b1464f4 100644 --- a/ZR.Admin.WebApi/Controllers/WebApiController.cs +++ b/ZR.Admin.WebApi/Controllers/WebApiController.cs @@ -51,6 +51,7 @@ namespace ZR.Admin.WebApi.Controllers [AllowAnonymous] public async Task GetOpenId([FromQuery] string code) { + var response = await _client.ExecuteSnsJsCode2SessionAsync(new SnsJsCode2SessionRequest { JsCode = code, @@ -159,6 +160,22 @@ namespace ZR.Admin.WebApi.Controllers { finalPath = domainUrl + "/" + finalPath; } + var ip = HttpContextExtension.GetClientUserIp(HttpContext); + GeoCodeService geoCodeService = new GeoCodeService(new HttpClient()); + var resultTuple = await geoCodeService.GetReGeoCodeAsync(giftClaim.Longitude + "," + giftClaim.Latitude); + string regeo = ""; + string geocodedAddress = ""; + if (resultTuple.HasValue) + { + var (result, message) = resultTuple.Value; + if (result != null) + { + geocodedAddress = result.Regeocode.Formatted_Address; + } + regeo = message ?? ""; + } + //if(t.) + var ipAddress = HttpContextExtension.GetIpInfo(ip); GiftClaim giftClaim1 = new GiftClaim() { Address = giftClaim.Address, @@ -172,7 +189,14 @@ namespace ZR.Admin.WebApi.Controllers ProductSerialNumber = giftClaim.ProductSerialNumber, CreatedAt = DateTime.Now, ProductImage = finalPath, - UserWxOpenId = user.Openid + UserWxOpenId = user.Openid, + Longitude = giftClaim.Longitude, + Latitude = giftClaim.Latitude, + GeocodedAddress = geocodedAddress, + IP = ip, + Regeo= regeo, + IPAddress = ipAddress, + }; _GiftClaimService.AddGiftClaim(giftClaim1); return SUCCESS(new { giftClaim1.Id }); diff --git a/ZR.Admin.WebApi/gift/2025/0807/1754566097_2805.jpg b/ZR.Admin.WebApi/gift/2025/0807/1754566097_2805.jpg new file mode 100644 index 0000000..d023d6c Binary files /dev/null and b/ZR.Admin.WebApi/gift/2025/0807/1754566097_2805.jpg differ diff --git a/ZR.Admin.WebApi/gift/2025/0807/1754566453_3587.jpg b/ZR.Admin.WebApi/gift/2025/0807/1754566453_3587.jpg new file mode 100644 index 0000000..d023d6c Binary files /dev/null and b/ZR.Admin.WebApi/gift/2025/0807/1754566453_3587.jpg differ diff --git a/ZR.Admin.WebApi/gift/2025/0807/1754567126_4074.jpg b/ZR.Admin.WebApi/gift/2025/0807/1754567126_4074.jpg new file mode 100644 index 0000000..d023d6c Binary files /dev/null and b/ZR.Admin.WebApi/gift/2025/0807/1754567126_4074.jpg differ diff --git a/ZR.Common/GeoCodeService.cs b/ZR.Common/GeoCodeService.cs new file mode 100644 index 0000000..7e778bb --- /dev/null +++ b/ZR.Common/GeoCodeService.cs @@ -0,0 +1,133 @@ +using System; +using System.Net.Http; +using System.Threading.Tasks; +using System.Text.Json; +using Newtonsoft.Json; + +namespace ZR.Common; +/// +/// +/// +public class GeoCodeService +{ + private const string ApiKey = "675b1cc0e0d2e61fd475b668b9fd869d"; + private const string BaseUrl = "https://restapi.amap.com/v3/geocode/regeo"; + + private readonly HttpClient _httpClient; + + public GeoCodeService(HttpClient httpClient) + { + _httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient)); + } + + public async Task<(ReGeoCodeResult, string)?> GetReGeoCodeAsync(string location, string coordsys = "gcj02") + { + if (string.IsNullOrWhiteSpace(location)) + { + throw new ArgumentException("Location cannot be null or empty.", nameof(location)); + } + + var requestUrl = $"{BaseUrl}?key={ApiKey}&location={location}&coordsys={coordsys}"; + + try + { + var response = await _httpClient.GetAsync(requestUrl); + response.EnsureSuccessStatusCode(); + + var content = await response.Content.ReadAsStringAsync(); + + var result = JsonConvert.DeserializeObject(content); + + return new(result, content); + } + catch (HttpRequestException ex) + { + //throw new ApplicationException("Error calling AMap API", ex); + + } + return new(null, ""); + } +} +/// +/// 高德地图逆地理编码API返回结果 +/// +public class ReGeoCodeResult +{ + /// + /// 返回结果状态值 + /// 0:请求失败;1:请求成功 + /// + public int Status { get; set; } + + /// + /// 返回状态说明 + /// 当status为0时,info返回错误原因;否则返回"OK" + /// + public string Info { get; set; } + + /// + /// 返回状态码 + /// + public string Infocode { get; set; } + + /// + /// 逆地理编码结果 + /// + public Regeocode Regeocode { get; set; } +} + +/// +/// 逆地理编码详细信息 +/// +public class Regeocode +{ + /// + /// 结构化地址信息 + /// 例如:"广东省广州市天河区体育西路189号" + /// + public string Formatted_Address { get; set; } + + /// + /// 地址组成元素 + /// 包含国家、省份、城市等信息 + /// + public AddressComponent AddressComponent { get; set; } + // 可以根据需要添加其他字段 +} + +/// +/// 地址组成元素 +/// +public class AddressComponent +{ + /// + /// 国家名称 + /// 例如:"中国" + /// + public string Country { get; set; } + + /// + /// 省份名称 + /// 例如:"广东省" + /// + public string Province { get; set; } + + /// + /// 城市名称 + /// 例如:"广州市" + /// + public string City { get; set; } + + /// + /// 区县名称 + /// 例如:"天河区" + /// + public string District { get; set; } + + /// + /// 乡镇/街道名称 + /// 例如:"天园街道" + /// + public string Township { get; set; } + // 可以根据需要添加其他字段 +} \ No newline at end of file diff --git a/ZR.Model/Business/Dto/GiftClaimDto.cs b/ZR.Model/Business/Dto/GiftClaimDto.cs index bc29cdd..e6093ef 100644 --- a/ZR.Model/Business/Dto/GiftClaimDto.cs +++ b/ZR.Model/Business/Dto/GiftClaimDto.cs @@ -97,6 +97,42 @@ namespace ZR.Model.Business.Dto [ExcelColumn(Name = "微信用户id")] [ExcelColumnName("微信用户id")] public string UserWxOpenId { get; set; } + + + /// + /// 经度 + /// + [ExcelColumn(Name = "经度")] + [ExcelColumnName("经度")] + public string Latitude { get; set; } + + /// + /// 纬度 + /// + [ExcelColumn(Name = "纬度")] + [ExcelColumnName("纬度")] + public string Longitude { get; set; } + /// + /// 经纬度地址 + /// + [ExcelColumn(Name = "经纬度地址")] + [ExcelColumnName("经纬度地址")] + public string GeocodedAddress { get; set; } + + /// + /// ip地址 + /// + [ExcelColumn(Name = "ip")] + [ExcelColumnName("ip")] + public string IP { get; set; } + + /// + /// ip地址 + /// + [ExcelColumn(Name = "ip地址")] + [ExcelColumnName("ip地址")] + public string IPAddress { get; set; } + } /// /// 礼品申领表输入输出对象 @@ -147,6 +183,16 @@ namespace ZR.Model.Business.Dto [ExcelColumnName("出品图片")] public string ProductImage { get; set; } + /// + /// 纬度 + /// + public string Latitude { get; set; } + + /// + /// 经度 + /// + public string Longitude { get; set; } + } diff --git a/ZR.Model/Business/GiftClaim.cs b/ZR.Model/Business/GiftClaim.cs index 4cdf7e3..734c029 100644 --- a/ZR.Model/Business/GiftClaim.cs +++ b/ZR.Model/Business/GiftClaim.cs @@ -79,5 +79,33 @@ namespace ZR.Model.Business /// public string UserWxOpenId { get; set; } + /// + /// 经度 + /// + public string Latitude { get; set; } + + /// + /// 纬度 + /// + public string Longitude { get; set; } + + /// + /// ip地址 + /// + public string IP { get; set; } + /// + /// 经纬度地址 + /// + public string GeocodedAddress { get; set; } + /// + /// ip地址 + /// + public string IPAddress { get; set; } + + /// + /// + /// + public string Regeo { get; set; } + } } \ No newline at end of file