优化客服统计功能

This commit is contained in:
wule committed 2026-02-05 19:32:45 +08:00
1 parent a2a6719a99
commit 4fd8d9cc17
17 files changed
+1218 -471

No files matched your search

+241 -11
View File
@@ -1,10 +1,12 @@
using AllBase;
using AllBase.Helper;
using AllBase.Model.Config;
using AllBase.Model.Result;
using CefSharp;
using CefSharp.WinForms;
using NaiBa.CefHelper;
using NaiBa.CefHelper.Handler;
using NaiBa.Util;
using NaiBa.Util.Extension;
using System;
using System.Collections.Generic;
@@ -20,6 +22,7 @@ using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using TemuKJD.BaseForms;
using TemuKJD.BLL;
using TemuKJD.Helper;
using TemuKJD.ToolForms;
using TemuKJD.WXForms.ShopForms;
@@ -29,7 +32,8 @@ namespace TemuKJD.WXForms.TalkForms
{
public partial class WxTalkForm : BaseTalkForm
{
public Dictionary<string, int> ShopChatCount = new Dictionary<string, int>();
public ConcurrentList<WxShopKfStatistics> lstShopKfStatistics = new ConcurrentList<WxShopKfStatistics>();
public ConcurrentList<WxShopAfterSaleInfo> lstShopAfterSale = new ConcurrentList<WxShopAfterSaleInfo>();
public delegate void SearchResultReceivedHandler(ShopLoginInfo shop);
/// <summary>
@@ -106,7 +110,7 @@ namespace TemuKJD.WXForms.TalkForms
lifeSpanHandler.parent = mainWebBrowser;
lifeSpanHandler.OnPop += MainLifeHandler_OnPop;
mainWebBrowser.LifeSpanHandler = lifeSpanHandler;
//mainWebBrowser.AddressChanged += mainWebBrowser_UrlChanged;
mainWebBrowser.AddressChanged += MainWebBrowser_AddressChanged;
mainWebBrowser.FrameLoadEnd += cefWebBrowser1_FrameLoadEnd;
mainPanel.Controls.Add(mainWebBrowser);
}
@@ -125,9 +129,38 @@ namespace TemuKJD.WXForms.TalkForms
{
this.cmbFiltNotes.Items.Add(shopNote);
}
lstShopAfterSale = NaiBa.Util.Helper.ConfigHelper.LoadConfig<NaiBa.Util.ConcurrentList<WxShopAfterSaleInfo>>("wx_shop_after_sale_list");
if (lstShopAfterSale == null)
lstShopAfterSale = new ConcurrentList<WxShopAfterSaleInfo>();
RefreshAfterSale();
}));
while (true)
{
Thread.Sleep(1000);
if (DateTime.Now > nextRefreshTime)
{
nextRefreshTime = DateTime.Now.AddMinutes(30);
Invoke(new Action(() => { RefreshCookie(true); }));
Invoke(new Action(() => { RefreshMsgCount(); }));
}
var deltaTime = nextRefreshTime - DateTime.Now;
Invoke(new Action(() =>
{
this.pnlRefreshShop.Text = "刷 新 店 铺 " + deltaTime.Minutes.ToString("00") + ":" + deltaTime.Seconds.ToString("00");
}));
}
});
}
private void MainWebBrowser_AddressChanged(object sender, AddressChangedEventArgs e)
{
if (sender == currentShop?.Chat)
this.txtAddress.Text = e.Address;
}
private void cefWebBrowser1_FrameLoadEnd(object sender, CefSharp.FrameLoadEndEventArgs e)
{
//ChromiumWebBrowser cefWebBrowser1 = (ChromiumWebBrowser)sender;
@@ -241,15 +274,18 @@ namespace TemuKJD.WXForms.TalkForms
iGrid = dgvShop.Rows.Add();
if (iGrid == 0)
dgvShop.Rows[iGrid].Cells["colSelect"].Value = true;
r = dgvShop.Rows[iGrid];
}
else
iGrid = r.Index;
dgvShop.Rows[iGrid].Cells["colShopId"].Value = shop.ShopId; // 店铺ID
dgvShop.Rows[iGrid].Cells["colShopName"].Value = shop.ShopAccount; // 店铺名称
dgvShop.Rows[iGrid].Cells["colShopNotes"].Value = shop.Notes; // 备注
dgvShop.Rows[iGrid].Cells["colLoginStatus"].Value = shop.LoginStatus == "登录成功" ? "在线" : "掉线"; // 登录状态
dicRowColor[dgvShop.Rows[iGrid]] = dgvShop.Rows[iGrid].DefaultCellStyle.BackColor;
r.Cells["colShopId"].Value = shop.ShopId; // 店铺ID
r.Cells["colShopName"].Value = shop.ShopAccount; // 店铺名称
r.Cells["colShopNotes"].Value = shop.Notes; // 备注
r.Cells["colLoginStatus"].Value = shop.LoginStatus == "登录成功" ? "在线" : "掉线"; // 登录状态
bool canUse = shop.LoginStatus == "登录成功";
(r.Cells["colLoginStatus"] as DataGridViewLinkCell).Style.ForeColor = canUse ? Color.Blue : Color.Red;
(r.Cells["colLoginStatus"] as DataGridViewLinkCell).LinkColor = canUse ? Color.Blue : Color.Red;
(r.Cells["colLoginStatus"] as DataGridViewLinkCell).ActiveLinkColor = canUse ? Color.LightBlue : Color.OrangeRed;
(r.Cells["colLoginStatus"] as DataGridViewLinkCell).VisitedLinkColor = canUse ? Color.DarkBlue : Color.DarkRed;
dicRowColor[r] = r.DefaultCellStyle.BackColor;
if (shop.Chat == null)
{
@@ -324,6 +360,17 @@ namespace TemuKJD.WXForms.TalkForms
}
}
private void LoadShopAfterSale(WxShopAfterSaleInfo shopAfterSale, string msg)
{
var r = dgvShop.Rows.Cast<DataGridViewRow>().FirstOrDefault(x => x.Cells["colShopId"].Value.ToString() == shopAfterSale.ShopId);
if (r != null)
{
r.Cells["colMessage"].Value = msg;
r.Cells["colAfterSale"].Value = shopAfterSale.AfterSaleCount;
r.Cells["colAfterSale"].Style.ForeColor = shopAfterSale.AfterSaleCount > 0 ? Color.Red : Color.Black;
}
}
private void RequestHandler_ResourceLoadComplete(object sender, string url, string postData, string responseData)
{
//if (responseData.Contains("你好"))
@@ -897,11 +944,25 @@ namespace TemuKJD.WXForms.TalkForms
if (shop?.Chat != null)
{
// 之前的当前店铺如果不在聊天界面强制转回聊天界面
if (currentShop?.Chat != null)
{
//if (currentShop.LoginStatus == "登录成功" && !currentShop.Chat.Address.StartsWith("https://store.weixin.qq.com/shop/kf"))
// currentShop.Chat.Load("https://store.weixin.qq.com/shop/kf");
var script = "document.documentElement.outerHTML;";
string htmlContent = currentShop.Chat.EvaluateScriptAsync(script).Result.Result.ParseToString();
if (currentShop.LoginStatus == "登录成功" && !htmlContent.Contains("按Enter发送"))
currentShop.Chat.Load("https://store.weixin.qq.com/shop/kf");
}
currentShop = shop;
shop.Chat.Dock = DockStyle.Fill;
shop.Chat.BringToFront();
btnPrev.BringToFront();
btnNext.BringToFront();
txtAddress.Text = currentShop.Chat.Address;
}
Task.Run(() =>
@@ -923,11 +984,23 @@ namespace TemuKJD.WXForms.TalkForms
}
private void pnlRefresh_Click(object sender, EventArgs e)
{
nextRefreshTime = DateTime.Now.AddMinutes(30);
RefreshCookie();
}
DateTime nextRefreshTime = DateTime.Now;
void RefreshCookie(bool isAuto = false)
{
pbLoading.Visible = true;
Task.Run(() =>
{
WXShopListForm.UpdateShop();
if (!isAuto)
WXShopListForm.UpdateShop();
foreach (var shop in GlobalConfig.ShopLoginList)
{
WXShopListForm.Instance.CheckShopLogin(new ShopLoginInfo[] { shop }, false);
}
LoadShop();
Invoke(new Action(() => { pbLoading.Visible = false; }));
});
@@ -969,5 +1042,162 @@ namespace TemuKJD.WXForms.TalkForms
{
currentShop.Chat.Forward();
}
private void btnRefreshAfterSale_Click(object sender, EventArgs e)
{
RefreshAfterSale(true);
}
void RefreshAfterSale(bool refreshAll = false)
{
this.pbLoading.Visible = true;
Task.Run(() =>
{
List<Task> tList = new List<Task>();
if (refreshAll)
lstShopAfterSale.Clear();
foreach (var shop in GlobalConfig.ShopLoginList)
{
var afterInfo = lstShopAfterSale.FirstOrDefault(sa => sa.ShopId == shop.ShopId);
if (afterInfo == null)
afterInfo = new WxShopAfterSaleInfo() { ShopId = shop.ShopId };
Invoke(new Action(() => { LoadShopAfterSale(afterInfo, ""); }));
var t = Task.Run(() =>
{
try
{
if (shop.LoginStatus != "登录成功")
{
Invoke(new Action(() => { LoadShopAfterSale(new WxShopAfterSaleInfo() { ShopId = shop.ShopId }, "店铺掉线"); }));
return;
}
var afterSaleInfo = lstShopAfterSale.FirstOrDefault(sa => sa.ShopId == shop.ShopId);
if (refreshAll || afterSaleInfo == null || afterSaleInfo.RefreshTime.Date != DateTime.Now.Date)
{
lstShopAfterSale.RemoveAll(sa => sa.ShopId == shop.ShopId);
var rst = GetApiServiceList(shop);
if (!rst.Contains("\"code\":0"))
{
WXShopListForm.Instance.CheckShopLogin(new ShopLoginInfo[] { shop }, false);
if (shop.LoginStatus != "登录成功")
{
Invoke(new Action(() => { LoadShopAfterSale(new WxShopAfterSaleInfo() { ShopId = shop.ShopId }, "店铺掉线"); }));
return;
}
afterInfo = new WxShopAfterSaleInfo() { ShopId = shop.ShopId, RefreshTime = DateTime.Now };
lstShopAfterSale.Add(afterInfo);
Invoke(new Action(() => { LoadShopAfterSale(afterInfo, "查询失败"); }));
return;
}
var afterCount = JsonHelper.JObjectParse(rst)["aftersaleAllRet"]["totalNum"].ParseToInt();
afterInfo = new WxShopAfterSaleInfo() { ShopId = shop.ShopId, RefreshTime = DateTime.Now, AfterSaleCount = afterCount };
lstShopAfterSale.Add(afterInfo);
Invoke(new Action(() => { LoadShopAfterSale(afterInfo, "已查询"); }));
return;
}
}
catch (Exception ex)
{
}
});
tList.Add(t);
while (tList.Where(ta => !ta.IsFaulted && !ta.IsCompleted).Count() >= 20) { Thread.Sleep(100); }
}
while (tList.Where(ta => !ta.IsFaulted && !ta.IsCompleted).Count() >= 1) { Thread.Sleep(100); }
NaiBa.Util.Helper.ConfigHelper.SaveConfig("wx_shop_after_sale_list", lstShopAfterSale);
Invoke(new Action(() => { pbLoading.Visible = false; }));
});
}
private void btnToKfMainPage_Click(object sender, EventArgs e)
{
if (currentShop?.Chat != null && currentShop.LoginStatus == "登录成功")
currentShop.Chat.Load("https://store.weixin.qq.com/shop/kf");
}
private void txtAddress_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (currentShop?.Chat != null)
{
currentShop?.Chat.Load(txtAddress.Text);
}
}
}
/// <summary>
/// 获取店铺后台主页售后数量等数据
/// </summary>
/// <param name="shop"></param>
/// <returns></returns>
public static string GetApiServiceList(ShopLoginInfo shop)
{
string url = "https://store.weixin.qq.com/shop-faas/mmchannelstradehome/home/main?token=&lang=zh_CN";
string referer = "https://store.weixin.qq.com/shop/home";
var result = WXHttpResponseBLL.WeiXinGetResponse(shop, url, referer);
return result;
//{"code":0,"orderNumRet":{"code":0,"pendingShipNum":"1"},"waybillErrorRet":{"code":0,"totalNum":0},"aftersaleAllRet":{"code":0,"totalNum":3},"guaranteeRet":{"code":0,"totalNum":0},"prewarnRet":{"code":0,"waitStatusCount":0},"punishRet":{"code":0,"pendingNum":1},"mchUndealRet":{"code":0,"totalNum":0}}
//pendingShipNum待发货 waybillErrorRet物流异常 aftersaleAllRet售后 guaranteeRet服务保障 prewarnRet店铺预警 punishRet店铺违规 mchUndealRet工单
}
public static string GetKfMsgStatistics(ShopLoginInfo shop, long beginDateTick, long endDateTick)
{
string url = "https://store.weixin.qq.com/shop/kf/cgi/data/getRealTimeShopV2?" + "beginDate=" + (beginDateTick / 1000) + "&endDate=" + (endDateTick / 1000m).ToString("0.000");
string referer = "https://store.weixin.qq.com/shop/kf/data/store";
var result = WXHttpResponseBLL.WeiXinGetResponse(shop, url, referer);
return result;
}
private void btnRefreshMsgCount_Click(object sender, EventArgs e)
{
RefreshMsgCount();
}
void RefreshMsgCount()
{
pbLoading.Visible = true;
Task.Run(() =>
{
lstShopKfStatistics.Clear();
foreach (var shop in GlobalConfig.ShopLoginList)
{
var kfMsg = new WxShopKfStatistics() { ShopId = shop.ShopId };
var kfMsgRst = GetKfMsgStatistics(shop, BaseClass.DateTimeToTimestampMilliseconds(DateTime.Now.Date), BaseClass.DateTimeToTimestampMilliseconds(DateTime.Now));
if (!kfMsgRst.Contains("\"ret\":0"))
continue;
try
{
var jObj = JsonHelper.JObjectParse(kfMsgRst);
kfMsg.user_cnt = jObj["cardResp"]["objects"]["tableValueLine"][0]["columnValue"][1].ParseToInt();
kfMsg.sesseion_cnt = jObj["cardResp"]["objects"]["tableValueLine"][0]["columnValue"][2].ParseToInt();
kfMsg.service_noreply_ratio = jObj["cardResp"]["objects"]["tableValueLine"][0]["columnValue"][5].ParseToDecimal();
lstShopKfStatistics.Add(kfMsg);
Invoke(new Action(() =>
{
int count = Math.Max(1, lstShopKfStatistics.Count());
lblUserCount.Text = lstShopKfStatistics.Sum(s => s.user_cnt).ParseToString();
lblTalkCount.Text = lstShopKfStatistics.Sum(s => s.sesseion_cnt).ParseToString();
lblReplyCount.Text = lstShopKfStatistics.Sum(s => s.service_reply_ratio * 100 / count).ParseToDecimal().ToString("0.0") + "%";
}));
}
catch
{
}
}
Invoke(new Action(() =>
{
int count = Math.Max(1, lstShopKfStatistics.Count());
lblUserCount.Text = lstShopKfStatistics.Sum(s => s.user_cnt).ParseToString();
lblTalkCount.Text = lstShopKfStatistics.Sum(s => s.sesseion_cnt).ParseToString();
lblReplyCount.Text = lstShopKfStatistics.Sum(s => s.service_reply_ratio * 100 / count).ParseToDecimal().ToString("0.0") + "%";
pbLoading.Visible = false;
}));
});
}
}
}