220 lines
7.9 KiB
C#
220 lines
7.9 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using SKIT.FlurlHttpClient.Wechat.Api.Models;
|
|
using SKIT.FlurlHttpClient.Wechat.Api;
|
|
using System.Web;
|
|
using ZR.Model.Business;
|
|
using ZR.Service.Business.IBusinessService;
|
|
using ZR.Model.Business.Dto;
|
|
using Org.BouncyCastle.Utilities;
|
|
using System.Net;
|
|
|
|
namespace ZR.Admin.WebApi.Controllers
|
|
{
|
|
/// <summary>
|
|
/// webApi
|
|
/// </summary>
|
|
[Route("[controller]/[action]")]
|
|
[AllowAnonymous]
|
|
[ApiExplorerSettings(GroupName = "webapi")]
|
|
public class WebApiController : BaseController
|
|
{
|
|
private readonly WechatApiClient _client;
|
|
/// <summary>
|
|
/// 礼品申领表接口
|
|
/// </summary>
|
|
private readonly IGiftClaimService _GiftClaimService;
|
|
/// <summary>
|
|
/// 微信用户表接口
|
|
/// </summary>
|
|
private readonly IGiftUserService _GiftUserService;
|
|
private ISysFileService SysFileService;
|
|
public WebApiController(WechatApiClient client, IGiftUserService GiftUserService, IGiftClaimService giftClaimService, ISysFileService sysFileService)
|
|
{
|
|
_client = client;
|
|
_GiftUserService = GiftUserService;
|
|
_GiftClaimService = giftClaimService;
|
|
SysFileService = sysFileService;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="code"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="Exception"></exception>
|
|
[HttpPost()]
|
|
[Route("/userLogin")]
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> GetOpenId([FromQuery] string code)
|
|
{
|
|
|
|
var response = await _client.ExecuteSnsJsCode2SessionAsync(new SnsJsCode2SessionRequest
|
|
{
|
|
JsCode = code,
|
|
GrantType = "authorization_code"
|
|
});
|
|
if (!response.IsSuccessful())
|
|
{
|
|
throw new Exception($"获取OpenId失败: {response.ErrorMessage}"); // 可以根据需要处理异常
|
|
}
|
|
var openId = response.OpenId;
|
|
var user = await _GiftUserService.AsQueryable().Where(it => it.Openid == openId).FirstAsync();
|
|
if (user == null)
|
|
{
|
|
user = new GiftUser()
|
|
{
|
|
AvatarUrl = "",
|
|
CreateTime = DateTime.Now,
|
|
Nickname = "微信用户",
|
|
Openid = openId,
|
|
UpdateTime = DateTime.Now,
|
|
LastLoginTime = DateTime.Now,
|
|
Phone = "",
|
|
Status = "0",
|
|
Unionid = ""
|
|
};
|
|
_GiftUserService.AddGiftUser(user);
|
|
}
|
|
return SUCCESS(new { user_id = user.Id });
|
|
|
|
}
|
|
[HttpGet("/getRecord")]
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> GetRecord([FromQuery] int userId)
|
|
{
|
|
if (userId == 0)
|
|
{
|
|
return ToResponse(ResultCode.CUSTOM_ERROR, "用户不存在");
|
|
}
|
|
var user = await _GiftUserService.AsQueryable().Where(it => it.Id == userId).FirstAsync();
|
|
if (user == null)
|
|
{
|
|
return ToResponse(ResultCode.CUSTOM_ERROR, "用户不存在");
|
|
}
|
|
var list = await _GiftClaimService.AsQueryable().Where(it => it.UserId == userId).OrderByDescending(it => it.Id).ToListAsync();
|
|
List<object> list1 = new List<object>();
|
|
list?.ForEach(it =>
|
|
{
|
|
list1.Add(new
|
|
{
|
|
it.Status,
|
|
it.Name,
|
|
it.Address,
|
|
it.Phone,
|
|
time = it.CreatedAt?.ToString("yyyy-MM-dd")
|
|
});
|
|
});
|
|
return SUCCESS(list1);
|
|
}
|
|
public static string domainUrl = AppSettings.GetConfig("ALIYUN_OSS:domainUrl");
|
|
|
|
[HttpPost()]
|
|
[Route("/addRecord")]
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> AddRecord([FromBody] AddGiftClaimDto giftClaim)
|
|
{
|
|
if (giftClaim.UserId == 0)
|
|
{
|
|
return ToResponse(ResultCode.CUSTOM_ERROR, "用户不存在");
|
|
}
|
|
var user = await _GiftUserService.AsQueryable().Where(it => it.Id == giftClaim.UserId).FirstAsync();
|
|
if (user == null)
|
|
{
|
|
return ToResponse(ResultCode.CUSTOM_ERROR, "用户不存在");
|
|
}
|
|
var images = ImageConverter.Base64ToImageBytes(giftClaim.ProductImage);
|
|
if (images.Length == 0)
|
|
{
|
|
return ToResponse(ResultCode.CUSTOM_ERROR, "图片上传失败");
|
|
}
|
|
var imageprx = ImageConverter.GetFileExtensionFromBase64(giftClaim.ProductImage);
|
|
var imageName = ImageConverter.GenerateImageFileName(imageprx);
|
|
var filePath = "gift/" + DateTime.Now.ToString("yyyy/MMdd/");
|
|
//备份一下本地
|
|
var finalPath = filePath + imageName;
|
|
if (!string.IsNullOrEmpty(domainUrl))
|
|
{
|
|
var statusCode = AliyunOssHelper.PutObjectFromFile(new MemoryStream(images), finalPath, "");
|
|
}
|
|
|
|
try
|
|
{
|
|
var path = Path.GetFullPath(filePath);
|
|
if (!Directory.Exists(path))
|
|
{
|
|
Directory.CreateDirectory(path);
|
|
}
|
|
using (var stream = new FileStream(finalPath, FileMode.Create))
|
|
{
|
|
await stream.WriteAsync(images, 0, images.Length);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
}
|
|
if (!string.IsNullOrEmpty(domainUrl))
|
|
{
|
|
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,
|
|
Name = giftClaim.Name,
|
|
Phone = giftClaim.Phone,
|
|
UserId = giftClaim.UserId,
|
|
Status = 0,
|
|
Company = giftClaim.Company,
|
|
ProductModel = giftClaim.ProductModel,
|
|
ProductDate = giftClaim.ProductDate,
|
|
ProductSerialNumber = giftClaim.ProductSerialNumber,
|
|
CreatedAt = DateTime.Now,
|
|
ProductImage = finalPath,
|
|
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 });
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet()]
|
|
[Route("/getBannerList")]
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> GetBannerList()
|
|
{
|
|
var list = await SysFileService.AsQueryable().Where(it => it.ClassifyType == "banner").Select(it =>
|
|
new { it.AccessUrl, it.RealName }).ToListAsync();
|
|
return SUCCESS(list);
|
|
}
|
|
}
|
|
}
|