445 lines
17 KiB
C#
445 lines
17 KiB
C#
using Crm.Core.Common.Helpers;
|
|
using Crm.Core.External.Domain.Dto;
|
|
using System.Text;
|
|
using System.Threading.Channels;
|
|
using static Crm.Core.External.Web.Controllers.CustomerController;
|
|
|
|
namespace Crm.Core.External.Web.Controllers
|
|
{
|
|
[Route("Customer")]
|
|
public class CustomerController : Controller
|
|
{
|
|
private readonly ICustomerDomain _customerDomain;
|
|
private readonly IMapper _mapper;
|
|
private readonly IRedisManager _redisManager;
|
|
|
|
public CustomerController(
|
|
ICustomerDomain customerDomain,
|
|
IMapper mapper,
|
|
IRedisManager redisManager)
|
|
{
|
|
_customerDomain = customerDomain;
|
|
_mapper = mapper;
|
|
_redisManager = redisManager;
|
|
}
|
|
|
|
public class ChannelPayImgItem
|
|
{
|
|
public string WxpayImg { get; set; }
|
|
|
|
public string WxpayUrl { get; set; }
|
|
|
|
public string AlipayImg { get; set; }
|
|
|
|
public string AlipayUrl { get; set; }
|
|
|
|
public List<PayImgItem> PayImgItems { get; set; }
|
|
}
|
|
|
|
public class PayImgItem
|
|
{
|
|
public string GroupName { get; set; }
|
|
|
|
public int? Channel { get; set; }
|
|
|
|
public string Title { get; set; }
|
|
|
|
public string WxpayImg { get; set; }
|
|
|
|
public string WxpayUrl { get; set; }
|
|
|
|
public string AlipayImg { get; set; }
|
|
|
|
public string AlipayUrl { get; set; }
|
|
}
|
|
|
|
public class RedisCacheItem
|
|
{
|
|
public string Val { get; set; }
|
|
|
|
public string Remark { get; set; }
|
|
}
|
|
|
|
public async Task<IActionResult> Index(decimal eid, string? appuserid, string? appid, int? deptid, string tabs = "1")
|
|
{
|
|
try
|
|
{
|
|
if (!string.IsNullOrEmpty(appid))
|
|
{
|
|
appid = appid.Replace("_1", "");
|
|
}
|
|
var data = await _customerDomain.GetWxworkCustomer(eid, appuserid, appid);
|
|
var model = JsonSerializer.Deserialize<WxworkCustomerModel>(data.ToJson());
|
|
if (model == null) return View(new WxworkCustomerModel());
|
|
var capitalSelect = await _customerDomain.GetCapitalSelect(eid);
|
|
|
|
ViewBag.CapitalSelect = capitalSelect.Select(x => new { name = x.Value }).ToJson();
|
|
ViewBag.Capital = capitalSelect.Where(x => x.Key.ToString() == model.WxworkCustomerInfo.CustomerCapital)
|
|
.Select(x => x.Value).FirstOrDefault();
|
|
model.OrderTypes = await _customerDomain.GetOrderTypes();
|
|
|
|
var defaultKey = "DGKJ.DG_SOFTWARE";
|
|
var defaultKeyItem = await _redisManager.GetAsync<RedisCacheItem>($"CrmCore:PayChannel:DefaultKey");
|
|
if (defaultKeyItem != null && !string.IsNullOrEmpty(defaultKeyItem.Val))
|
|
{
|
|
defaultKey = defaultKeyItem.Val;
|
|
}
|
|
|
|
var otherKey = "DGKJ.NEW_MEDIA_3";
|
|
var otherKeyItem = await _redisManager.GetAsync<RedisCacheItem>($"CrmCore:PayChannel:OtherKey");
|
|
if (otherKeyItem != null && !string.IsNullOrEmpty(otherKeyItem.Val))
|
|
{
|
|
otherKey = otherKeyItem.Val;
|
|
}
|
|
|
|
ViewBag.Tabs = tabs;
|
|
ViewBag.PayImgItems = await CreatePayImg(defaultKey, Convert.ToInt64(eid), deptid);
|
|
ViewBag.PayImgItems2 = await CreatePayImg(otherKey, Convert.ToInt64(eid), deptid);
|
|
ViewBag.HidePayUrl = await _redisManager.GetAsync<string>("CrmCore:HidePayUrl");
|
|
return View(model);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "加载客户跟进记录报错!");
|
|
return View("../home/error", new ErrorViewModel { ErrorMessage = ex.Message });
|
|
}
|
|
}
|
|
|
|
[Route("GetCommonPayInfo")]
|
|
public async Task<IActionResult> GetCommonPayInfo(long? eid, int? deptid)
|
|
{
|
|
var defaultKey = "DGKJ.DG_SOFTWARE";
|
|
var defaultKeyItem = await _redisManager.GetAsync<RedisCacheItem>($"CrmCore:PayChannel:DefaultKey");
|
|
if (defaultKeyItem != null && !string.IsNullOrEmpty(defaultKeyItem.Val))
|
|
{
|
|
defaultKey = defaultKeyItem.Val;
|
|
}
|
|
var PayImgItems = await CreatePayImg(defaultKey, eid, deptid);
|
|
return Json(PayImgItems);
|
|
}
|
|
|
|
[Route("GetCommonPayInfo3")]
|
|
public async Task<IActionResult> GetCommonPayInfo3(long? eid, int? deptid)
|
|
{
|
|
var PayImgItems = new List<PayImgItem>();
|
|
var defaultKey = "DGKJ.DG_SOFTWARE";
|
|
var defaultKeyItem = await _redisManager.GetAsync<RedisCacheItem>($"CrmCore:PayChannel:DefaultKey");
|
|
if (defaultKeyItem != null && !string.IsNullOrEmpty(defaultKeyItem.Val))
|
|
{
|
|
defaultKey = defaultKeyItem.Val;
|
|
}
|
|
var items = await CreatePayImg(defaultKey, eid, deptid);
|
|
foreach (var item in items)
|
|
{
|
|
item.GroupName = "主要支付二维码";
|
|
}
|
|
PayImgItems.AddRange(items);
|
|
|
|
var otherKey = "DGKJ.NEW_MEDIA_3";
|
|
var otherKeyItem = await _redisManager.GetAsync<RedisCacheItem>($"CrmCore:PayChannel:OtherKey");
|
|
if (otherKeyItem != null && !string.IsNullOrEmpty(otherKeyItem.Val))
|
|
{
|
|
otherKey = otherKeyItem.Val;
|
|
}
|
|
var items2 = await CreatePayImg(otherKey, eid, deptid);
|
|
foreach (var item in items2)
|
|
{
|
|
item.GroupName = "备用支付二维码";
|
|
}
|
|
PayImgItems.AddRange(items2);
|
|
return Json(PayImgItems);
|
|
}
|
|
|
|
[Route("GetCommonPayUrl")]
|
|
public async Task<IActionResult> GetCommonPayUrl(long? eid, int? deptid)
|
|
{
|
|
var channel = default(int?);
|
|
var channels = await GetPayChannelAsync(deptid.GetValueOrDefault());
|
|
if (channels.Count > 0)
|
|
{
|
|
channel = channels[0].Channel;
|
|
}
|
|
var url = await _redisManager.GetAsync<string>($"CrmCore:PayUrl");
|
|
if (string.IsNullOrEmpty(url))
|
|
{
|
|
url = "https://uppay.soft.dn8188.com/onlinePaytest";
|
|
}
|
|
var defaultKey = "DGKJ.DG_SOFTWARE";
|
|
var defaultKeyItem = await _redisManager.GetAsync<RedisCacheItem>($"CrmCore:PayChannel:DefaultKey");
|
|
if (defaultKeyItem != null && !string.IsNullOrEmpty(defaultKeyItem.Val))
|
|
{
|
|
defaultKey = defaultKeyItem.Val;
|
|
}
|
|
url = $"{url}/alipay/wapPayPage?identityKey={defaultKey}&channel={channel}&eId={eid}";
|
|
return Json(new ApiResult<string> { Data = url });
|
|
}
|
|
|
|
private async Task<List<PayImgItem>> CreatePayImg(string defaultKey, long? eid, int? deptid)
|
|
{
|
|
var payImgItems = new List<PayImgItem>();
|
|
if (deptid.GetValueOrDefault() > 0 && eid > 0)
|
|
{
|
|
var cacheKey = $"CrmCore:PayImg:{defaultKey}:{deptid.GetValueOrDefault()}_{eid}";
|
|
var cache = await _redisManager.GetAsync<ChannelPayImgItem>(cacheKey);
|
|
if (cache == null)
|
|
{
|
|
cache = new ChannelPayImgItem
|
|
{
|
|
PayImgItems = new List<PayImgItem>()
|
|
};
|
|
|
|
//var defaultKey = "DGKJ.DG_SOFTWARE";
|
|
//var defaultKeyItem = await _redisManager.GetAsync<RedisCacheItem>($"CrmCore:PayChannel:DefaultKey");
|
|
//if (defaultKeyItem != null && !string.IsNullOrEmpty(defaultKeyItem.Val))
|
|
//{
|
|
// defaultKey = defaultKeyItem.Val;
|
|
//}
|
|
|
|
//var otherKey = "DGKJ.NEW_MEDIA_3";
|
|
//var otherKeyItem = await _redisManager.GetAsync<RedisCacheItem>($"CrmCore:PayChannel:OtherKey");
|
|
//if (otherKeyItem != null && !string.IsNullOrEmpty(otherKeyItem.Val))
|
|
//{
|
|
// otherKey = otherKeyItem.Val;
|
|
//}
|
|
|
|
//var deptIdsItem = await _redisManager.GetAsync<RedisCacheItem>($"CrmCore:PayChannel:OtherKeyDeptIds");
|
|
//if (deptIdsItem != null && !string.IsNullOrEmpty(deptIdsItem.Val))
|
|
//{
|
|
// if (deptIdsItem.Val.Split(',').Any(a => a == deptid.GetValueOrDefault().ToString()))
|
|
// {
|
|
// defaultKey = otherKey;
|
|
// }
|
|
//}
|
|
|
|
var channels = await GetPayChannelAsync(deptid.GetValueOrDefault());
|
|
if (channels.Count > 0)
|
|
{
|
|
foreach (var channel in channels)
|
|
{
|
|
var payImgItem = await CreatePayImgItem(defaultKey, channel, eid);
|
|
cache.PayImgItems.Add(payImgItem);
|
|
}
|
|
}
|
|
|
|
_redisManager.Set<ChannelPayImgItem>(cacheKey, cache, TimeSpan.FromDays(1));
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(cache.WxpayImg))
|
|
{
|
|
payImgItems.Add(new PayImgItem
|
|
{
|
|
WxpayImg = cache.WxpayImg,
|
|
WxpayUrl = cache.WxpayUrl,
|
|
AlipayImg = cache.AlipayImg,
|
|
AlipayUrl = cache.AlipayUrl
|
|
});
|
|
}
|
|
|
|
if (cache.PayImgItems != null && cache.PayImgItems.Count > 0)
|
|
{
|
|
payImgItems.Clear();
|
|
foreach (var payImgItem in cache.PayImgItems)
|
|
{
|
|
payImgItems.Add(payImgItem);
|
|
}
|
|
}
|
|
}
|
|
return payImgItems;
|
|
}
|
|
|
|
private async Task<PayImgItem> CreatePayImgItem(string defaultKey, ChannelItem channel, long? eid)
|
|
{
|
|
var item = new PayImgItem
|
|
{
|
|
Channel = channel.Channel,
|
|
Title = channel.Title
|
|
};
|
|
var title = await _redisManager.GetAsync<string>($"CrmCore:PayTitle");
|
|
if (string.IsNullOrEmpty(title))
|
|
{
|
|
title = "东高(广东)科技发展有限公司-{0}";
|
|
}
|
|
title = string.Format(title, channel.Channel);
|
|
var subtitle = $"{channel.Title}({channel.Channel})";
|
|
var url = await _redisManager.GetAsync<string>($"CrmCore:PayUrl");
|
|
if (string.IsNullOrEmpty(url))
|
|
{
|
|
url = "https://uppay.soft.dn8188.com/onlinePaytest";
|
|
}
|
|
var wxpayUrl = $"{url}/wechat/authorize?identityKey={defaultKey}&channel={channel.Channel}&eId={eid}";
|
|
var alipayUrl = $"{url}/alipay/wapPayPage?identityKey={defaultKey}&channel={channel.Channel}&eId={eid}";
|
|
item.WxpayUrl = wxpayUrl;
|
|
item.AlipayUrl = alipayUrl;
|
|
|
|
try
|
|
{
|
|
item.WxpayImg = CreatePayImg("wxpay", wxpayUrl, title, subtitle);
|
|
item.AlipayImg = CreatePayImg("alipay", alipayUrl, title, subtitle);
|
|
}
|
|
catch
|
|
{ }
|
|
|
|
return item;
|
|
}
|
|
|
|
private async Task<List<ChannelItem>> GetPayChannelAsync(int deptId)
|
|
{
|
|
var channels = new List<ChannelItem>();
|
|
var url = await _redisManager.GetAsync<string>($"CrmCore:GetPayChannelUrl");
|
|
if (string.IsNullOrEmpty(url))
|
|
{
|
|
url = "http://120.77.165.155:8089/Api/Deptment/Depts";
|
|
}
|
|
var client = new System.Net.Http.HttpClient();
|
|
var result = await client.GetAsync(url);
|
|
if (result.IsSuccessStatusCode)
|
|
{
|
|
var bytes = await result.Content.ReadAsByteArrayAsync();
|
|
var rspJson = Encoding.UTF8.GetString(bytes);
|
|
var rsp = System.Text.Json.JsonSerializer.Deserialize<GetDepartmentResponse>(rspJson);
|
|
if (rsp != null && rsp.data != null)
|
|
{
|
|
var dept = rsp.data.Where(w => w.id == deptId).FirstOrDefault();
|
|
if (dept != null)
|
|
{
|
|
if (dept.deptmentCampains != null && dept.deptmentCampains.Count > 0)
|
|
{
|
|
channels.Add(new ChannelItem
|
|
{
|
|
Channel = dept.deptmentCampains.FirstOrDefault().startCampainId,
|
|
Title = dept.title
|
|
});
|
|
}
|
|
else
|
|
{
|
|
if (!string.IsNullOrEmpty(dept.appid))
|
|
{
|
|
var depts = rsp.data.Where(w => w.appid == dept.appid && w.id > 0);
|
|
foreach (var dept_item in depts)
|
|
{
|
|
if (dept_item.deptmentCampains != null && dept_item.deptmentCampains.Count > 0)
|
|
{
|
|
channels.Add(new ChannelItem
|
|
{
|
|
Channel = dept_item.deptmentCampains.FirstOrDefault().startCampainId,
|
|
Title = dept_item.title
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return channels;
|
|
}
|
|
|
|
public class ChannelItem
|
|
{
|
|
public string Title { get; set; }
|
|
|
|
public int? Channel { get; set; }
|
|
}
|
|
|
|
public class GetDepartmentResponse
|
|
{
|
|
public List<DepartmentItem> data { get; set; }
|
|
|
|
public int code { get; set; }
|
|
|
|
public string message { get; set; }
|
|
}
|
|
|
|
public class DepartmentItem
|
|
{
|
|
public int? id { get; set; }
|
|
|
|
public string? title { get; set; }
|
|
|
|
public string? code { get; set; }
|
|
|
|
public string? appid { get; set; }
|
|
|
|
public int? departmentId { get; set; }
|
|
|
|
public bool? isDept { get; set; }
|
|
|
|
public string? companyCode { get; set; }
|
|
|
|
public List<Deptmentcampain> deptmentCampains { get; set; }
|
|
}
|
|
|
|
public class Deptmentcampain
|
|
{
|
|
public int? startCampainId { get; set; }
|
|
|
|
public int? endCampainId { get; set; }
|
|
}
|
|
|
|
private string CreatePayImg(string payType, string url, string title, string subtitle)
|
|
{
|
|
switch (payType)
|
|
{
|
|
default:
|
|
throw new ApiException("支付方式无效:" + payType, 1);
|
|
case "wxpay":
|
|
{
|
|
var bgImg = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", "images", "wxpay.png");
|
|
var bytes = new QRCodeHelper().Create(url, 180, bgImg, 65, 158, title, -1, 363);
|
|
var base64 = Convert.ToBase64String(bytes);
|
|
return $"data:image/png;base64,{base64}";
|
|
}
|
|
case "alipay":
|
|
{
|
|
var bgImg = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", "images", "alipay.png");
|
|
var bytes = new QRCodeHelper().Create(url, 180, bgImg, 65, 158, title, -1, 363);
|
|
var base64 = Convert.ToBase64String(bytes);
|
|
return $"data:image/png;base64,{base64}";
|
|
}
|
|
}
|
|
}
|
|
|
|
[HttpPost("Capital")]
|
|
public async Task<IApiResult> UpdateCapital([FromBody] UpdateCapitalModel model)
|
|
{
|
|
var data = JsonSerializer.Deserialize<UpdateCapitalDto>(model.ToJson());
|
|
if (data == null) return new ApiResult
|
|
{
|
|
Code = -1,
|
|
Message = "数据不能为空!"
|
|
};
|
|
if (await _customerDomain.UpdateCapital(data))
|
|
{
|
|
return new ApiResult
|
|
{
|
|
Code = 0,
|
|
Message = "更新成功!"
|
|
};
|
|
}
|
|
return new ApiResult
|
|
{
|
|
Code = -1,
|
|
Message = "更新失败!"
|
|
};
|
|
}
|
|
|
|
[HttpPost("CustomerOrder")]
|
|
public async Task<IApiResult> CreateCustomerOrder([FromBody] CreateCustomerOrderDto dto)
|
|
{
|
|
if (await _customerDomain.CreateCustomerOrder(dto))
|
|
{
|
|
return new ApiResult
|
|
{
|
|
Code = 0,
|
|
Message = "提交成功!"
|
|
};
|
|
}
|
|
return new ApiResult
|
|
{
|
|
Code = -1,
|
|
Message = "提交失败!"
|
|
};
|
|
}
|
|
}
|
|
} |