TG.WXCRM.V4/WxService/WeWorkService.svc.cs

164 lines
7.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DAL.Redis;
using Ninject;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using WX.CRM.Common;
using WX.CRM.DAL.Redis;
using WX.CRM.IBLL.Res;
using WX.CRM.IBLL.WeWork;
using WX.CRM.Model.DTO.wework;
using WX.CRM.Model.Enum;
using WX.CRM.WebHelper;
using WX.CRM.WebHelper.Infrastructure;
namespace WxService
{
// 注意: 使用“重构”菜单上的“重命名”命令可以同时更改代码、svc 和配置文件中的类名“WeWorkService”。
// 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 WeWorkService.svc 或 WeWorkService.svc.cs然后开始调试。
public class WeWorkService : IWeWorkService
{
private ICorpInfo wework_corpinfo = NinjectControllerFactory.ninjectKernel.Get<ICorpInfo>();
private IRES_CUSTOMER _RES_CUSTOMER = NinjectControllerFactory.ninjectKernel.Get<IRES_CUSTOMER>();
private IRcontact wework_rcontact = NinjectControllerFactory.ninjectKernel.Get<IRcontact>();
private IWeWork_MsgKey wework_msgkey = NinjectControllerFactory.ninjectKernel.Get<IWeWork_MsgKey>();
private RedisString<string> _redisMsg = new RedisString<string>();
private readonly PubSub _sub = new PubSub();
//private const string WxFix = "weworkid:";
private const string WxMsgFix = "wework:";
public async Task<JsonResult<string>> XposedMessage(weworkmsg msg)
{
try
{
msg.JsonText = HttpUtility.UrlDecode(msg.JsonText, Encoding.UTF8);//url解码
if (msg.tabtype == "message")
{
bool isExists = true;
StorageMessage info = JsonHelper.JsonDivertToObj<StorageMessage>(msg.JsonText);// JsonConvert.DeserializeObject();
string keyname = WxMsgFix + info.id + "|" + info.vid;
isExists = await _redisMsg.ExistsAsync(keyname);
if (!isExists)
{
isExists = wework_msgkey.HasMsgKey(keyname);//redis中找不到就再从数据库中找一次
}
if (isExists)
{
return new JsonResult<string> { result = true, retcode = (int)EnumInterfaceErrcode., retmsg = "success" };
}
else
{
bool ret = await PushRedisAndPublish<StorageMessage>(info, "weworkmessage");
if (ret)
{
TimeSpan span = new TimeSpan(60, 0, 0, 0, 0);//保留60天
await _redisMsg.SetAsync(keyname, string.Empty, span);
wework_msgkey.CheckAndSetKey(keyname);
}
else
{
return new JsonResult<string> { result = false, retcode = (int)EnumInterfaceErrcode., retmsg = "调用成功但有错误" };
}
}
//return new JsonResult<string> { result = false, retcode = (int)EnumInterfaceErrcode.调用成功但有错误, retmsg = "暂时不处理Message数据" };
}
else if (msg.tabtype == "corpinfo")
{
CorpInfo info = JsonHelper.JsonDivertToObj<CorpInfo>(msg.JsonText);// JsonConvert.DeserializeObject();
if (info.corpName == "微信")
return new JsonResult<string> { result = true, retcode = (int)EnumInterfaceErrcode., retmsg = "上传成功" };
wework_corpinfo.CorpInfo_Set(info);//企业信息 入库
if (info.staffInfo != null)
wework_corpinfo.StaffInfo_Set(info);//工作人员入库
}
else if (msg.tabtype == "rcontact")
{
Rcontact info = JsonHelper.JsonDivertToObj<Rcontact>(msg.JsonText);// JsonConvert.DeserializeObject();
if (info.extras != null)
{
wework_rcontact.Rcontact_Set(info, msg.JsonText);//好友 入库
//LogHelper.Info("好友上传了:" + msg.JsonText);
var db = info.extras.remarkPhone;
if (db != null && db.Length > 0)
{
List<string> resids = new List<string>();
foreach (var item in db)
{
string phone = Utility.JavaByteToString(item.phone);
if (!string.IsNullOrEmpty(phone) && Utility.ChekMobile(phone))
{
string resid = WX.CRM.Common.ResUtil.CreateResId(phone);
resids.Add(resid);
_RES_CUSTOMER.ResgisterCustomer(phone, resid, "CRM_WeWork");
wework_rcontact.Rcontact_Relate(info.remoteId.ToString(), info.vid.ToString(), resid);
}
}
if (resids.Count > 1)
{
wework_rcontact.Res_Customer_Relate(string.Join(",", resids));
}
}
}
}
return new JsonResult<string> { result = true, retcode = (int)EnumInterfaceErrcode., retmsg = "上传成功" };
}
catch (Exception ex)
{
LogHelper.Error("企业微信上传数据错误" + msg.tabtype + "" + ex.ToString());
return new JsonResult<string> { result = false, retcode = (int)EnumInterfaceErrcode., retmsg = "调用成功但有错误" };
}
}
public JsonResult<string> AlivePut(weworkalive info)
{
ValidationErrors errors = new ValidationErrors();
try
{
wework_corpinfo.Alive_Put(info);//改成过程逻辑方式
}
catch (Exception ex)
{
LogHelper.Error("企业微信CRM心跳异常:" + ex.ToString());
errors.Add(ex.ToString());
}
if (errors.Count == 0)
return new JsonResult<string> { result = true, retcode = (int)EnumInterfaceErrcode., retmsg = "调用成功" };
return new JsonResult<string> { result = false, retcode = (int)EnumInterfaceErrcode., retmsg = "调用成功但有错误" };
}
private async Task<bool> PushRedisAndPublish<T>(T model, string key)
{
try
{
//写入队列
var result = await new RedisList<T>().LeftPushAsync(key, model);
if (result > 0)
{
//发送订阅通知
await _sub.PublishAsync("sub:" + key, "");
return true;
}
return false;
}
catch (Exception ex)
{
LogHelper.Error(ex.ToString());
return false;
}
}
}
}