411 lines
20 KiB
C#
411 lines
20 KiB
C#
using Aliyun.OTS;
|
||
using Aliyun.OTS.DataModel;
|
||
using Aliyun.OTS.Request;
|
||
using Aliyun.OTS.Response;
|
||
using Ninject;
|
||
using RiaServiceLibrary;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using WX.CRM.Common;
|
||
using WX.CRM.IBLL.Ww;
|
||
using WX.CRM.IBLL.Wx;
|
||
using WX.CRM.Model.DTO;
|
||
using WX.CRM.Model.Entity;
|
||
using WX.CRM.Model.Enum;
|
||
using WX.CRM.WebHelper.Infrastructure;
|
||
|
||
namespace RiaService
|
||
{
|
||
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“WeiXinService”。
|
||
// 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 WeiXinService.svc 或 WeiXinService.svc.cs,然后开始调试。
|
||
public class WeiXinService : IWeiXinService
|
||
{
|
||
ValidationErrors errors = new ValidationErrors();
|
||
IWX_UINCONFIG iwx_Uinconfig = NinjectControllerFactory.ninjectKernel.Get<IWX_UINCONFIG>();
|
||
IWX_USERIMEI wx_UserIMEI_BL = NinjectControllerFactory.ninjectKernel.Get<IWX_USERIMEI>();
|
||
IWw_huser _ww_huser_bl = NinjectControllerFactory.ninjectKernel.Get<IWw_huser>();
|
||
public JsonResult Put(string content)
|
||
{
|
||
if (string.IsNullOrEmpty(content))
|
||
{
|
||
//return Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode.非法请求 });
|
||
return new JsonResult() { result = false, retcode = (int)EnumInterfaceErrcode.非法请求 };
|
||
}
|
||
try
|
||
{
|
||
PutRow(content);
|
||
//return Utility.ObjectToJson(new { result = true, retcode = (int)EnumInterfaceErrcode.调用成功 });
|
||
return new JsonResult() { result = true, retcode = (int)EnumInterfaceErrcode.调用成功 };
|
||
}
|
||
catch (System.Exception e)
|
||
{
|
||
LogHelper.Error(e.Message);
|
||
//return Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode.系统错误 });
|
||
return new JsonResult() { result = false, retcode = (int)EnumInterfaceErrcode.系统错误 };
|
||
}
|
||
}
|
||
|
||
public JsonResult Get(string content)
|
||
{
|
||
try
|
||
{
|
||
var query = Utility.JSONToObject<WeiXinMessageQuery>(content);
|
||
if (string.IsNullOrEmpty(query.UserName))
|
||
{
|
||
//return Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode.参数错误, retmsg = "参数必须大于0" });
|
||
return new JsonResult() { result = false, retcode = (int)EnumInterfaceErrcode.参数错误 };
|
||
}
|
||
if (!query.PageIndex.HasValue)
|
||
query.PageIndex = 1;
|
||
if (!query.PageSize.HasValue)
|
||
query.PageSize = 20;
|
||
long? sendStartTime = query.SendStartTime.HasValue ? Utility.ConvertDateTimeToInt(query.SendStartTime.Value) : (long?)null;
|
||
long? sendEndTime = query.SendEndTime.HasValue ? Utility.ConvertDateTimeToInt(query.SendEndTime.Value) : (long?)null;
|
||
var sort = GetRangeDirection.Backward;
|
||
if (query.Sort.HasValue)
|
||
{
|
||
if (query.Sort.Value == 0)
|
||
{
|
||
sort = GetRangeDirection.Backward;
|
||
}
|
||
else if (query.Sort.Value == 1)
|
||
{
|
||
sort = GetRangeDirection.Forward;
|
||
}
|
||
}
|
||
var list = GetList(query.UserName, query.Talker, sendStartTime, sendEndTime, query.MsgSvrId, Convert.ToInt32(query.PageIndex), Convert.ToInt32(query.PageSize), sort);
|
||
//return Utility.ObjectToJson(new { result = true, retcode = Convert.ToInt32(EnumInterfaceErrcode.调用成功), retmsg = list });
|
||
return new JsonResult() { result = true, retcode = Convert.ToInt32(EnumInterfaceErrcode.调用成功), retmsg = list };
|
||
}
|
||
catch (System.Exception e)
|
||
{
|
||
LogHelper.Error(e.Message);
|
||
//return Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode.系统错误 });
|
||
return new JsonResult() { result = false, retcode = (int)EnumInterfaceErrcode.系统错误 };
|
||
}
|
||
}
|
||
|
||
public JsonResult<List<WX_UINCONFIG>> GetWxUinConfig()
|
||
{
|
||
try
|
||
{
|
||
List<WX_UINCONFIG> list = null;
|
||
string cacheKey = "catch_uincConfigAllList";
|
||
if (CacheHelper.Exists(cacheKey))
|
||
{
|
||
list = CacheHelper.Get<List<WX_UINCONFIG>>(cacheKey);
|
||
}
|
||
else
|
||
{
|
||
list = iwx_Uinconfig.GetAllUinConfigList();
|
||
}
|
||
return new JsonResult<List<WX_UINCONFIG>> { result = true, retcode = Convert.ToInt32(EnumInterfaceErrcode.调用成功), retmsg = list };
|
||
}
|
||
catch (System.Exception e)
|
||
{
|
||
LogHelper.Error(e.Message);
|
||
return new JsonResult<List<WX_UINCONFIG>> { result = true, retcode = Convert.ToInt32(EnumInterfaceErrcode.系统错误), retmsg = null };
|
||
}
|
||
}
|
||
|
||
public JsonResult<string> Insert(string uin, string userName, string wxPath)
|
||
{
|
||
try
|
||
{
|
||
LogHelper.Info(String.Format("uin:{0},userNmae:{1},wxPath:{2}", uin, userName, wxPath));
|
||
if (uin != null && !iwx_Uinconfig.IsHaveUin(uin))
|
||
{
|
||
bool flag = iwx_Uinconfig.Update(ref errors, new WX_UINCONFIG() { UIN = uin, USERNAME = userName, WX_PATH = wxPath });
|
||
if (flag)
|
||
{
|
||
string cacheKey = "cache_uincConfigAllList";
|
||
CacheHelper.Set<List<WX_UINCONFIG>>(cacheKey, null);
|
||
}
|
||
return new JsonResult<string> { result = true, retcode = (int)EnumInterfaceErrcode.调用成功, retmsg = "调用成功" };
|
||
}
|
||
return new JsonResult<string> { result = true, retcode = (int)EnumInterfaceErrcode.调用成功, retmsg = "插入失败!" };
|
||
}
|
||
catch (System.Exception e)
|
||
{
|
||
LogHelper.Error(e.Message);
|
||
return new JsonResult<string> { result = false, retcode = (int)EnumInterfaceErrcode.系统错误, retmsg = null };
|
||
}
|
||
|
||
}
|
||
|
||
public bool UploadQrCode(Stream input, string filePath)
|
||
{
|
||
if (!input.CanRead || string.IsNullOrWhiteSpace(filePath))
|
||
{
|
||
//return new JsonResult<string> { result = false, retcode = (int)EnumInterfaceErrcode.参数错误, retmsg = "参数错误" };
|
||
//throw new Exception("数据流不可读!");
|
||
return false;
|
||
}
|
||
|
||
try
|
||
{
|
||
|
||
string uploadFolder = AppDomain.CurrentDomain.BaseDirectory;
|
||
string fileDir = filePath.Substring(0, filePath.LastIndexOf('/'));
|
||
|
||
FileStream targetStream = null;
|
||
uploadFolder = uploadFolder + fileDir;
|
||
if (!Directory.Exists(uploadFolder))
|
||
{
|
||
Directory.CreateDirectory(uploadFolder);
|
||
}
|
||
string fileFullPath = AppDomain.CurrentDomain.BaseDirectory + filePath;
|
||
using (targetStream = new FileStream(fileFullPath, FileMode.Create, FileAccess.Write, FileShare.None))
|
||
{
|
||
const int bufferLen = 4096;
|
||
byte[] buffer = new byte[bufferLen];
|
||
int count = 0;
|
||
while ((count = input.Read(buffer, 0, bufferLen)) > 0)
|
||
{
|
||
targetStream.Write(buffer, 0, count);
|
||
}
|
||
targetStream.Close();
|
||
input.Close();
|
||
}
|
||
return true;
|
||
|
||
//return new JsonResult<string> { result = true, retcode = (int)EnumInterfaceErrcode.调用成功, retmsg = filePath };
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error("上传二维码到接口站点错误:" + ex.Message);
|
||
//throw new Exception("上传二维码到接口站点错误");
|
||
return false;
|
||
//return new JsonResult<string> { result = false, retcode = (int)EnumInterfaceErrcode.系统错误, retmsg = null };
|
||
}
|
||
}
|
||
|
||
public JsonResult<string> RContactPut(rcontact content)
|
||
{
|
||
try
|
||
{
|
||
LogHelper.Info("ok");
|
||
//LogHelper.Info(content.jobwxusername);
|
||
return new JsonResult<string> { result = true, retcode = (int)EnumInterfaceErrcode.调用成功, retmsg = "调用成功" };
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error(ex.ToString());
|
||
return new JsonResult<string> { result = false, retcode = (int)EnumInterfaceErrcode.系统错误, retmsg = null };
|
||
}
|
||
|
||
}
|
||
|
||
private void PutRow(string data)
|
||
{
|
||
OTSClient otsClient = Config.GetClient();
|
||
|
||
var msg = Utility.JSONToObject<WX_MESSAGE>(data);
|
||
LogHelper.Info(data);
|
||
#region 写入message
|
||
// 定义行的主键,必须与创建表时的TableMeta中定义的一致
|
||
PrimaryKey primaryKey = new PrimaryKey
|
||
{
|
||
{ "UserName", new ColumnValue(msg.USERNAME) },
|
||
{ "Talker", new ColumnValue(msg.TALKER) },
|
||
{ "CreateTime", new ColumnValue(Convert.ToInt64(msg.CREATETIME)) },
|
||
{ "MsgSvrId", new ColumnValue(msg.MSGSVRID.ToString()) }
|
||
};
|
||
|
||
// 定义要写入改行的属性列
|
||
AttributeColumns attribute = new AttributeColumns
|
||
{
|
||
{"NickName", string.IsNullOrEmpty(msg.NICKNAME) ? new ColumnValue(string.Empty) : new ColumnValue(msg.NICKNAME)},
|
||
{"Content", string.IsNullOrEmpty(msg.MSG_CONTENT) ? new ColumnValue(string.Empty) : new ColumnValue(msg.MSG_CONTENT)},
|
||
{"ImgPath", string.IsNullOrEmpty(msg.IMGPATH) ? new ColumnValue(string.Empty) : new ColumnValue(msg.IMGPATH)},
|
||
{"Url", string.IsNullOrEmpty(msg.MSG_URL) ? new ColumnValue(string.Empty) : new ColumnValue(msg.MSG_URL)},
|
||
{"Type", msg.MSG_TYPE.HasValue ? new ColumnValue(string.Empty) : new ColumnValue(Convert.ToInt32(msg.MSG_TYPE))},
|
||
{"Status", msg.STATUS.HasValue ? new ColumnValue(string.Empty) : new ColumnValue(Convert.ToInt32(msg.STATUS))},
|
||
{"RESERVED", string.IsNullOrEmpty(msg.RESERVED) ? new ColumnValue(string.Empty) : new ColumnValue(msg.RESERVED)},
|
||
{"LVBUFFER", string.IsNullOrEmpty(msg.LVBUFFER) ? new ColumnValue(string.Empty) : new ColumnValue(msg.LVBUFFER)},
|
||
{"TRANSCONTENT", string.IsNullOrEmpty(msg.TRANSCONTENT) ? new ColumnValue(string.Empty) : new ColumnValue(msg.TRANSCONTENT)},
|
||
{"TRANSBRANDWORDING", string.IsNullOrEmpty(msg.TRANSBRANDWORDING) ? new ColumnValue(string.Empty) : new ColumnValue(msg.TRANSBRANDWORDING)},
|
||
{"BIZCLIENTMSGID", string.IsNullOrEmpty(msg.BIZCLIENTMSGID) ? new ColumnValue(string.Empty) : new ColumnValue(msg.BIZCLIENTMSGID)},
|
||
{"BIZCHATUSERID", string.IsNullOrEmpty(msg.BIZCHATUSERID) ? new ColumnValue(string.Empty) : new ColumnValue(msg.BIZCHATUSERID)},
|
||
{"ISSEND", msg.ISSEND.HasValue ? new ColumnValue(Convert.ToInt32(msg.ISSEND.Value)) : new ColumnValue(string.Empty)},
|
||
{"BIZCHATID", msg.BIZCHATID.HasValue ? new ColumnValue(Convert.ToInt32(msg.BIZCHATID.Value)) : new ColumnValue(string.Empty)},
|
||
{"MSGSEQ", msg.MSGSEQ.HasValue ? new ColumnValue(Convert.ToInt32(msg.MSGSEQ.Value)) : new ColumnValue(string.Empty)},
|
||
{"FLAG", msg.FLAG.HasValue ? new ColumnValue(Convert.ToInt32(msg.FLAG.Value)) : new ColumnValue(string.Empty)}
|
||
};
|
||
PutRowRequest request = new PutRowRequest("message", new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute);
|
||
|
||
otsClient.PutRow(request);
|
||
LogHelper.Info("ostx调用成功");
|
||
|
||
#endregion
|
||
}
|
||
|
||
private List<WX_MESSAGE> GetList(string userName, string talker = null, long? sendStartTime = null, long? sendEndTime = null, string msgSvrId = null, int page = 1, int pageSize = 20, GetRangeDirection direction = GetRangeDirection.Backward)
|
||
{
|
||
OTSClient otsClient = Config.GetClient();
|
||
PrimaryKey startKey = new PrimaryKey();
|
||
PrimaryKey endKey = new PrimaryKey();
|
||
startKey.Add("UserName", !string.IsNullOrEmpty(userName) ? new ColumnValue(userName) : ColumnValue.INF_MIN);
|
||
startKey.Add("Talker", !string.IsNullOrEmpty(talker) ? new ColumnValue(talker) : ColumnValue.INF_MIN);
|
||
startKey.Add("CreateTime", sendStartTime.HasValue ? new ColumnValue(sendStartTime.Value) : ColumnValue.INF_MIN);
|
||
startKey.Add("MsgSvrId", !string.IsNullOrEmpty(msgSvrId) ? new ColumnValue(msgSvrId) : ColumnValue.INF_MIN);
|
||
endKey.Add("UserName", !string.IsNullOrEmpty(userName) ? new ColumnValue(userName) : ColumnValue.INF_MAX);
|
||
endKey.Add("Talker", !string.IsNullOrEmpty(talker) ? new ColumnValue(talker) : ColumnValue.INF_MAX);
|
||
endKey.Add("CreateTime", sendEndTime.HasValue ? new ColumnValue(sendEndTime.Value) : ColumnValue.INF_MAX);
|
||
endKey.Add("MsgSvrId", !string.IsNullOrEmpty(msgSvrId) ? new ColumnValue(msgSvrId) : ColumnValue.INF_MAX);
|
||
var rows = direction == GetRangeDirection.Backward
|
||
? ReadByPage(otsClient, "message", endKey, startKey, (page - 1) * pageSize, pageSize, direction)
|
||
: ReadByPage(otsClient, "message", startKey, endKey, (page - 1) * pageSize, pageSize, direction);
|
||
|
||
var result = new List<WX_MESSAGE>();
|
||
WX_MESSAGE info = null;
|
||
foreach (RowDataFromGetRange row in rows)
|
||
{
|
||
info = new WX_MESSAGE();
|
||
foreach (KeyValuePair<string, ColumnValue> entry in row.PrimaryKey)
|
||
{
|
||
switch (entry.Key)
|
||
{
|
||
case "UserName": info.USERNAME = entry.Value.StringValue; break;
|
||
case "Talker": info.TALKER = entry.Value.StringValue; break;
|
||
case "CreateTime": info.CREATETIME = entry.Value.IntegerValue; break;
|
||
case "MsgSvrId":
|
||
//info.MSGSVRID = !string.IsNullOrEmpty(entry.Value.StringValue) ? Convert.ToDecimal(entry.Value.StringValue) : (decimal?) null;
|
||
info.MSGSVRID = entry.Value.StringValue;
|
||
break;
|
||
}
|
||
}
|
||
foreach (KeyValuePair<string, ColumnValue> entry in row.Attribute)
|
||
{
|
||
switch (entry.Key)
|
||
{
|
||
case "NickName": info.NICKNAME = entry.Value.StringValue; break;
|
||
case "Type": info.MSG_TYPE = string.IsNullOrEmpty(entry.Value.StringValue) ? (short?)null : Convert.ToInt16(entry.Value.StringValue); break;
|
||
case "ISSEND": info.ISSEND = string.IsNullOrEmpty(entry.Value.StringValue) ? (short?)null : Convert.ToInt16(entry.Value.StringValue); break;
|
||
//case "Status": info.STATUS = Convert.ToInt32(entry.Value.StringValue); break;
|
||
case "Content": info.MSG_CONTENT = entry.Value.StringValue; break;
|
||
//case "ImgPath": info.IMGPATH = entry.Value.StringValue; break;
|
||
case "Url": info.MSG_URL = entry.Value.StringValue; break;
|
||
}
|
||
}
|
||
result.Add(info);
|
||
}
|
||
return result;
|
||
}
|
||
|
||
private List<RowDataFromGetRange> ReadByPage(OTSClient client, String tableName, PrimaryKey startKey, PrimaryKey endKey, int offset, int pageSize, GetRangeDirection direction)
|
||
{
|
||
int limit = pageSize;
|
||
int skip = offset;
|
||
PrimaryKey nextStart = startKey;
|
||
IList<RowDataFromGetRange> rows = new List<RowDataFromGetRange>();
|
||
|
||
while (limit > 0 && nextStart != null)
|
||
{
|
||
var rowQueryCriteria = new RangeRowQueryCriteria(tableName)
|
||
{
|
||
InclusiveStartPrimaryKey = startKey,
|
||
ExclusiveEndPrimaryKey = endKey,
|
||
Limit = skip + limit,
|
||
Direction = direction
|
||
};
|
||
|
||
GetRangeRequest request = new GetRangeRequest(rowQueryCriteria);
|
||
|
||
GetRangeResponse response = client.GetRange(request);
|
||
|
||
foreach (RowDataFromGetRange row in response.RowDataList)
|
||
{
|
||
if (skip > 0)
|
||
{
|
||
skip--;
|
||
}
|
||
else
|
||
{
|
||
rows.Add(row);
|
||
limit--;
|
||
}
|
||
//rows.Add(row);
|
||
}
|
||
nextStart = response.NextPrimaryKey;
|
||
}
|
||
return rows.ToList();
|
||
}
|
||
|
||
public bool BatchInsertUserIMEI(string imeis)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(imeis))
|
||
{
|
||
return false;
|
||
}
|
||
return wx_UserIMEI_BL.BatchInsert(imeis);
|
||
}
|
||
|
||
|
||
public JsonResult<string> WeWorkAssignStatus(AssignStatusDto dto)
|
||
{
|
||
try
|
||
{
|
||
LogHelper.Info("AssignStatusDto:" + dto.ToJson());
|
||
|
||
var res = _ww_huser_bl.SetAssignStatus(dto);
|
||
|
||
if (res)
|
||
return new JsonResult<string> { result = true, retcode = (int)EnumInterfaceErrcode.调用成功, retmsg = "调用成功" };
|
||
else
|
||
return new JsonResult<string> { result = false, retcode = (int)EnumInterfaceErrcode.调用成功, retmsg = "调用成功,执行失败!" };
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error(ex.ToString());
|
||
return new JsonResult<string> { result = false, retcode = (int)EnumInterfaceErrcode.系统错误, retmsg = null };
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
public class WeiXinMessageQuery
|
||
{
|
||
public string UserName { get; set; }
|
||
public string Talker { get; set; }
|
||
public DateTime? SendStartTime { get; set; }
|
||
public DateTime? SendEndTime { get; set; }
|
||
public string MsgSvrId { get; set; }
|
||
public int? PageIndex { get; set; }
|
||
public int? PageSize { get; set; }
|
||
public int? Sort { get; set; }
|
||
}
|
||
public class Config
|
||
{
|
||
private static string AccessKeyId = "iLCVNfsiH4F1JLNU";
|
||
|
||
private static string AccessKeySecret = "24DOemjGGeQeG2WJNzHBqi1wNtySgB";
|
||
|
||
private static string Endpoint = "http://WX-Message.cn-shenzhen.ots.aliyuncs.com";
|
||
|
||
private static string InstanceName = "WX-Message";
|
||
|
||
private static OTSClient OtsClient = null;
|
||
|
||
public static OTSClient GetClient()
|
||
{
|
||
if (OtsClient != null)
|
||
{
|
||
return OtsClient;
|
||
}
|
||
|
||
OTSClientConfig config = new OTSClientConfig(Endpoint, AccessKeyId, AccessKeySecret, InstanceName);
|
||
config.OTSDebugLogHandler = null;
|
||
config.OTSErrorLogHandler = null;
|
||
OtsClient = new OTSClient(config);
|
||
return OtsClient;
|
||
}
|
||
}
|
||
|
||
|
||
}
|
||
|