步步为盈
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
3.0 KiB

using BBWY.Client.Helpers;
using GalaSoft.MvvmLight.Messaging;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using WebSocketSharp;
namespace BBWY.Client.ViewModels
{
public partial class OrderListViewModel
{
private ConcurrentDictionary<string, string> dongdongBuyerDictionary;
private WebSocket webSocketClient;
public void InitDongDong()
{
dongdongBuyerDictionary = new ConcurrentDictionary<string, string>();
}
private void CloseWebSocket()
{
if (webSocketClient != null)
{
if (webSocketClient.IsAlive)
webSocketClient.Close();
webSocketClient.OnMessage -= WebSocketClient_OnMessage;
webSocketClient = null;
}
}
private void WebSocketClient_OnMessage(object sender, WebSocketSharp.MessageEventArgs e)
{
if (!e.IsText)
return;
var msg = e.Data;
CloseWebSocket();
Console.WriteLine($"OnDongDongSendMessage 接收到消息 {msg}");
if (string.IsNullOrEmpty(msg))
return;
try
{
var j = JObject.Parse(msg);
var buyerAccount = j.Value<string>("user");
var orderId = j.Value<string>("orderId");
if (string.IsNullOrEmpty(buyerAccount) && string.IsNullOrEmpty(orderId))
return;
var phone = j.Value<string>("phone");
if (!string.IsNullOrEmpty(buyerAccount) && !string.IsNullOrEmpty(phone))
{
if (!dongdongBuyerDictionary.TryAdd(msg, phone))
return;
}
var order = OrderList.FirstOrDefault(o => o.Id == orderId);
if (order == null)
order = OrderList.FirstOrDefault(o => !string.IsNullOrEmpty(o.BuyerAccount) && o.BuyerAccount == buyerAccount);
if (order == null || order.Consignee == null || order.Consignee.IsDecode)
return; //已经解密过
var response = orderService.DecodeConsignee(order.Id, phone);
if (!response.Success)
{
App.Current.Dispatcher.Invoke(() => MessageBox.Show(response.Msg, "解密失败"));
return;
}
order.Consignee.ContactName = response.Data.ContactName;
order.Consignee.Address = response.Data.Address;
order.Consignee.Mobile = response.Data.Mobile;
order.Consignee.IsDecode = true;
App.Current.Dispatcher.BeginInvoke((Action)delegate
{
Messenger.Default.Send(string.Empty, "ActiveMainWindow");
});
}
catch (Exception ex)
{
}
}
}
}