TG.WXCRM.V4/RiaService/WXFilePathService.svc.cs

230 lines
9.8 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 RiaServiceLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.IO;
using System.Threading;
using System.Web;
using WX.CRM.BLL.Util;
using WX.CRM.WebHelper;
using WX.CRM.Model.Enum;
using WX.CRM.Common;
using WX.CRM.BLL.Wx;
namespace RiaService
{
// 注意: 使用“重构”菜单上的“重命名”命令可以同时更改代码、svc 和配置文件中的类名“WXFilePathService”。
// 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 WXFilePathService.svc 或 WXFilePathService.svc.cs然后开始调试。
public class WXFilePathService : IWXFilePathService
{
private readonly WX_FILEPATH_BL _pathBl = new WX_FILEPATH_BL();
public Stream GetUrlByMD5SerialNum(string content, string clientid, string sign)
{
string retMsg = "";
bool isTrue = false;
string pm = "";
try
{
var sHelper = new SecurityHelper();
#region
if (!sHelper.CheckClientValid(clientid, content, sign))
{
retMsg = JsonHelper.ObjDivertToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retmsg = "非法请求" });
retMsg = sHelper.encyptData(clientid, retMsg);
return Utility.GetStream(retMsg);
}
#endregion
#region
content = sHelper.decyptData(clientid, content); //解密操作
try
{
pm = Utility.JSONToObject<string>(content);
if (string.IsNullOrEmpty(pm))
{
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retmsg = "参数MD5SerialNum为空" });
retMsg = sHelper.encyptData(clientid, retMsg);
return Utility.GetStream(retMsg);
}
isTrue = true;
}
catch (Exception ex)
{
isTrue = false;
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retmsg = ex.Message });
LogHelper.Error(string.Concat("【MD5SerialNumService.GetUrlByMD5SerialNum参数验证】", "", content, "|", ex.Message, ex.StackTrace));
}
#endregion
#region
if (isTrue)
{
try
{
string res = _pathBl.GetUrl(pm);
retMsg = Utility.ObjectToJson(new { result = true, retcode = Convert.ToInt32(EnumInterfaceErrcode.), retmsg = res });
}
catch (Exception ex)
{
retMsg = JsonHelper.ObjDivertToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retmsg = ex.Message });
LogHelper.Error(ex.Message + "|" + ex.StackTrace);
}
}
#endregion
retMsg = sHelper.encyptData(clientid, retMsg);
return Utility.GetStream(retMsg);
}
catch (Exception ex)
{
retMsg = JsonHelper.ObjDivertToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retmsg = ex.Message });
LogHelper.Error(string.Concat("【MD5SerialNumService.GetUrlByMD5SerialNum】:", ex.Message, ex.StackTrace));
}
return Utility.GetStream(retMsg);
}
public Stream AddMD5SerialNum(string content, string clientid, string sign)
{
string retMsg = "";
bool isTrue = false;
MD5SerialNum pm = new MD5SerialNum();
try
{
var sHelper = new SecurityHelper();
#region
if (!sHelper.CheckClientValid(clientid, content, sign))
{
retMsg = JsonHelper.ObjDivertToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retmsg = "非法请求" });
retMsg = sHelper.encyptData(clientid, retMsg);
return Utility.GetStream(retMsg);
}
#endregion
#region
content = sHelper.decyptData(clientid, content); //解密操作
try
{
pm = Utility.JSONToObject<MD5SerialNum>(content);
if (string.IsNullOrEmpty(pm.serialNum))
{
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retmsg = "参数MD5SerialNum为空" });
retMsg = sHelper.encyptData(clientid, retMsg);
return Utility.GetStream(retMsg);
}
if (string.IsNullOrEmpty(pm.url))
{
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retmsg = "参数url为空" });
retMsg = sHelper.encyptData(clientid, retMsg);
return Utility.GetStream(retMsg);
}
isTrue = true;
}
catch (Exception ex)
{
isTrue = false;
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retmsg = ex.Message });
LogHelper.Error(string.Concat("【MD5SerialNumService.AddMD5SerialNum参数验证】", "", content, "|", ex.Message, ex.StackTrace));
}
#endregion
#region
if (isTrue)
{
try
{
bool res = _pathBl.Add(pm.serialNum, pm.url);
retMsg = Utility.ObjectToJson(new { result = true, retcode = Convert.ToInt32(EnumInterfaceErrcode.), retmsg = res });
}
catch (Exception ex)
{
retMsg = JsonHelper.ObjDivertToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retmsg = ex.Message });
LogHelper.Error(ex.Message + "|" + ex.StackTrace);
}
}
#endregion
retMsg = sHelper.encyptData(clientid, retMsg);
return Utility.GetStream(retMsg);
}
catch (Exception ex)
{
retMsg = JsonHelper.ObjDivertToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retmsg = ex.Message });
LogHelper.Error(string.Concat("【MD5SerialNumService.AddMD5SerialNum】:", ex.Message, ex.StackTrace));
}
return Utility.GetStream(retMsg);
}
public void Upload(Stream input, string fileName)
{
try
{
#region
string uploadFolder = AppDomain.CurrentDomain.BaseDirectory;
string savaPath = "uploadfile";
//Stream sourceStream = request.FileData;
FileStream targetStream = null;
if (!input.CanRead)
{
throw new Exception("数据流不可读!");
}
uploadFolder = Path.Combine(uploadFolder, savaPath);
if (!Directory.Exists(uploadFolder))
{
Directory.CreateDirectory(uploadFolder);
}
string filePath = Path.Combine(uploadFolder, fileName);
using (targetStream = new FileStream(filePath, 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();
}
#endregion
#region
var redisKey = "wx:" + Path.GetFileNameWithoutExtension(fileName);
var fileWebUrl = Utility.GetSettingByKey("fileWebUrl") + fileName;
_pathBl.Add(redisKey, fileWebUrl);
#endregion
HttpContext.Current.Response.Write(Utility.ConvertToJSON(new { result = true, message = "接收成功", url = fileName }));
}
catch (Exception ex)
{
LogHelper.Error(ex);
HttpContext.Current.Response.Write(Utility.ConvertToJSON(new { result = false, message = "接收失败:" + ex.Message }));
}
}
public void test()
{
try
{
var pstr = "testparams";
var ret = Utility.PostData("http://localhost:18739/WXFilePathService.svc/Upload", pstr, Encoding.UTF8);
HttpContext.Current.Response.Write(ret);
}
catch (Exception ex)
{
}
}
private class MD5SerialNum
{
public string serialNum { get; set; }
public string url { get; set; }
}
}
}