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

148 lines
5.7 KiB
C#

using WX.CRM.BLL.Wx;
using WX.CRM.Common;
using WX.CRM.Model.Entity;
using RiaService;
using RiaServiceLibrary;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.Threading;
using System.Web;
using WX.CRM.Model.Enum;
namespace RiaService
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[JavascriptCallbackBehavior(UrlParameterName = "jsoncallback")]
public class WeChatDbReceive : IWeChatDbReceive
{
public void Recive(Stream input)
{
try
{
string datatype = HttpContext.Current.Request["datatype"];
string body = new StreamReader(input).ReadToEnd();
//NameValueCollection nvc = HttpUtility.ParseQueryString(body);
//var filename = nvc["filename"];
string newFileName = datatype + "-" + DateTime.Now.Ticks.ToString() + ".txt";
var dir = HttpContext.Current.Server.MapPath("/WeChatDbRecive/");
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
while (File.Exists(dir + newFileName))
{
LogHelper.Info(dir + newFileName + "存在!");
Thread.Sleep(1);
newFileName = datatype + "-" + DateTime.Now.Ticks.ToString() + ".txt";
}
File.WriteAllText(dir + newFileName, body, Encoding.UTF8);
WX_DBRECIVE db = new WX_DBRECIVE();
db.STATUS = 0;
db.FILEPATH = dir + newFileName;
db.DATATYPE = datatype;
db.CTIME = DateTime.Now;
IWX_DBRECIVE wxbll = new WX_DBRECIVE_BL();
var rst = wxbll.Create(db);
if (rst)
{
HttpContext.Current.Response.Write(Utility.ConvertToJSON(new { result = 1, message = "接收成功" }));
}
else
{
HttpContext.Current.Response.Write(Utility.ConvertToJSON(new { result = 0, message = "接收失败,详情查看日志" }));
}
}
catch (Exception ex)
{
LogHelper.Error(ex);
HttpContext.Current.Response.Write(Utility.ConvertToJSON(new { result = 0, message = "接收失败:" + ex.Message }));
}
}
public JsonResult<string> Upload(Stream input)
{
try
{
string uploadFolder = System.AppDomain.CurrentDomain.BaseDirectory;
string savaPath = "WeChatDbRecive";
//string dateString = DateTime.Now.ToShortDateString() + @"\";
string fileName = "ddd.mp4";
//Stream sourceStream = request.FileData;
FileStream targetStream = null;
if (!input.CanRead)
{
throw new Exception("数据流不可读!");
}
uploadFolder = Path.Combine(uploadFolder, savaPath); //+ dateString;
if (!Directory.Exists(uploadFolder))
{
Directory.CreateDirectory(uploadFolder);
}
string filePath = Path.Combine(uploadFolder, fileName);
using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
//read from the input stream in 4K chunks
//and save to output stream
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 new JsonResult<string> {result = true, retcode = Convert.ToInt32(EnumInterfaceErrcode.), retmsg = "调用成功"};
}
catch (Exception ex)
{
LogHelper.Error(ex);
return new JsonResult<string> {result = false, retcode = (int) EnumInterfaceErrcode., retmsg = ex.ToString()};
}
}
public void test()
{
try
{
var pstr = "testparams";
var ret = Utility.PostData("http://localhost:18739/WeChatDbReceive.svc/Upload", pstr, Encoding.UTF8);
HttpContext.Current.Response.Write(ret);
}
catch (Exception ex)
{
}
//string path = @"C:\Users\Administrator\Desktop\22.jpg";
//System.Net.WebClient uploadWc = new System.Net.WebClient();
//uploadWc.Headers.Add("Content-Type", "image/jpeg");
//System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open);
//byte[] buffer = new byte[(int)fs.Length];
//fs.Read(buffer, 0, (int)fs.Length);
//fs.Close();
//uploadWc.UploadDataCompleted += UploadWc_UploadDataCompleted;
//uploadWc.UploadData(new Uri("http://localhost:18739/WeChatDbReceive.svc/Upload"), buffer);
}
private void UploadWc_UploadDataCompleted(object sender, System.Net.UploadDataCompletedEventArgs e)
{
Console.WriteLine(e.Result);
}
}
}