提交代码

This commit is contained in:
zpc 2025-08-07 20:12:50 +08:00
parent 1c1f5b8d98
commit 5afd8e06c2
7 changed files with 232 additions and 1 deletions

View File

@ -51,6 +51,7 @@ namespace ZR.Admin.WebApi.Controllers
[AllowAnonymous]
public async Task<IActionResult> 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 });

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

133
ZR.Common/GeoCodeService.cs Normal file
View File

@ -0,0 +1,133 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Text.Json;
using Newtonsoft.Json;
namespace ZR.Common;
/// <summary>
///
/// </summary>
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<ReGeoCodeResult>(content);
return new(result, content);
}
catch (HttpRequestException ex)
{
//throw new ApplicationException("Error calling AMap API", ex);
}
return new(null, "");
}
}
/// <summary>
/// 高德地图逆地理编码API返回结果
/// </summary>
public class ReGeoCodeResult
{
/// <summary>
/// 返回结果状态值
/// 0请求失败1请求成功
/// </summary>
public int Status { get; set; }
/// <summary>
/// 返回状态说明
/// 当status为0时info返回错误原因否则返回"OK"
/// </summary>
public string Info { get; set; }
/// <summary>
/// 返回状态码
/// </summary>
public string Infocode { get; set; }
/// <summary>
/// 逆地理编码结果
/// </summary>
public Regeocode Regeocode { get; set; }
}
/// <summary>
/// 逆地理编码详细信息
/// </summary>
public class Regeocode
{
/// <summary>
/// 结构化地址信息
/// 例如:"广东省广州市天河区体育西路189号"
/// </summary>
public string Formatted_Address { get; set; }
/// <summary>
/// 地址组成元素
/// 包含国家、省份、城市等信息
/// </summary>
public AddressComponent AddressComponent { get; set; }
// 可以根据需要添加其他字段
}
/// <summary>
/// 地址组成元素
/// </summary>
public class AddressComponent
{
/// <summary>
/// 国家名称
/// 例如:"中国"
/// </summary>
public string Country { get; set; }
/// <summary>
/// 省份名称
/// 例如:"广东省"
/// </summary>
public string Province { get; set; }
/// <summary>
/// 城市名称
/// 例如:"广州市"
/// </summary>
public string City { get; set; }
/// <summary>
/// 区县名称
/// 例如:"天河区"
/// </summary>
public string District { get; set; }
/// <summary>
/// 乡镇/街道名称
/// 例如:"天园街道"
/// </summary>
public string Township { get; set; }
// 可以根据需要添加其他字段
}

View File

@ -97,6 +97,42 @@ namespace ZR.Model.Business.Dto
[ExcelColumn(Name = "微信用户id")]
[ExcelColumnName("微信用户id")]
public string UserWxOpenId { get; set; }
/// <summary>
/// 经度
/// </summary>
[ExcelColumn(Name = "经度")]
[ExcelColumnName("经度")]
public string Latitude { get; set; }
/// <summary>
/// 纬度
/// </summary>
[ExcelColumn(Name = "纬度")]
[ExcelColumnName("纬度")]
public string Longitude { get; set; }
/// <summary>
/// 经纬度地址
/// </summary>
[ExcelColumn(Name = "经纬度地址")]
[ExcelColumnName("经纬度地址")]
public string GeocodedAddress { get; set; }
/// <summary>
/// ip地址
/// </summary>
[ExcelColumn(Name = "ip")]
[ExcelColumnName("ip")]
public string IP { get; set; }
/// <summary>
/// ip地址
/// </summary>
[ExcelColumn(Name = "ip地址")]
[ExcelColumnName("ip地址")]
public string IPAddress { get; set; }
}
/// <summary>
/// 礼品申领表输入输出对象
@ -147,6 +183,16 @@ namespace ZR.Model.Business.Dto
[ExcelColumnName("出品图片")]
public string ProductImage { get; set; }
/// <summary>
/// 纬度
/// </summary>
public string Latitude { get; set; }
/// <summary>
/// 经度
/// </summary>
public string Longitude { get; set; }
}

View File

@ -79,5 +79,33 @@ namespace ZR.Model.Business
/// </summary>
public string UserWxOpenId { get; set; }
/// <summary>
/// 经度
/// </summary>
public string Latitude { get; set; }
/// <summary>
/// 纬度
/// </summary>
public string Longitude { get; set; }
/// <summary>
/// ip地址
/// </summary>
public string IP { get; set; }
/// <summary>
/// 经纬度地址
/// </summary>
public string GeocodedAddress { get; set; }
/// <summary>
/// ip地址
/// </summary>
public string IPAddress { get; set; }
/// <summary>
///
/// </summary>
public string Regeo { get; set; }
}
}