471 lines
19 KiB
C#
471 lines
19 KiB
C#
using CRM.Core.DTO;
|
||
using Ninject;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Text;
|
||
using System.Web.Mvc;
|
||
using WX.CRM.Common;
|
||
using WX.CRM.Common.Layui;
|
||
using WX.CRM.IBLL.Base;
|
||
using WX.CRM.IBLL.Cms;
|
||
using WX.CRM.IBLL.Csvr;
|
||
using WX.CRM.IBLL.Util;
|
||
using WX.CRM.Model.Entity;
|
||
using WX.CRM.Model.Enum;
|
||
using WX.CRM.Model.QueryMap;
|
||
using WX.CRM.WebHelper;
|
||
|
||
namespace WX.CRM.WEB.Controllers.Cms
|
||
{
|
||
public class CmsUpController : BaseController
|
||
{
|
||
[Inject]
|
||
public ICMS_NEWS cms_news_bl { get; set; }
|
||
|
||
[Inject]
|
||
public ICSVR_MESSAGE csvr_message_bl { get; set; }
|
||
[Inject]
|
||
public IBAS_SALESDEPARTMENT_Q bas_salesdepartment { get; set; }
|
||
|
||
[Inject]
|
||
public ICACHE_Q cache_BL { get; set; }
|
||
|
||
[HttpGet]
|
||
[AuthorizeRedirect(Roles = InitRights.CONST_广告策划)]
|
||
/// <summary>
|
||
/// /
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
|
||
public ActionResult Index()
|
||
{
|
||
ToolBar tool = new ToolBar();
|
||
string[] toolbtn = new ToolButtonView().ToolButtonRight(InitRights.广告策划, userRightId);
|
||
tool.AllowButton(toolbtn);
|
||
tool.AddOtherButton("Other1", "上传", "icon-lookup", "FenPei_Click", true);
|
||
ViewBag.ToolBar = tool;
|
||
|
||
|
||
//Pager pager = new Pager() { page = 1, rows = 10 };
|
||
//string tableId = "tablist";
|
||
//Table tab = new Table(tableId);
|
||
//tab.AddHiddenHeadCol("pkid", "ID");//影藏列
|
||
//tab.AddHeadCol("map_eID", "", "工号", true);
|
||
//tab.AddHeadCol("map_uName", "", "姓名", true);
|
||
//tab.AddHeadCol("map_trueName", "", "真实姓名");
|
||
//tab.AddHeadCol("gName", "", "销售组");
|
||
//tab.AddHeadCol("deptName", "", "部门");
|
||
//tab.AddHeadCol("MANAGERSTATUS", "", "客服经理");
|
||
//tab.AddHeadCol("GENERALCUSTOMER", "", "客服");
|
||
//tab.AddHeadCol("SENIORCUSTOMER", "", "高级客服");
|
||
//tab.AddHeadCol("map_gender", "", "性别");
|
||
//tab.AddHeadCol("map_birthday", "", "生日", true);
|
||
//tab.AddHeadCol("map_EntryDate", "", "入职时间", true);
|
||
//tab.AddHeadCol("map_POSITIVETIME", "", "转正时间", true);
|
||
//tab.AddHeadCol("map_isDismiss", "", "是否离职");
|
||
//tab.AddHeadCol("map_dismissTime", "", "离职时间", true);
|
||
//tab.AddHeadCol("map_dismissType", "", "离职类型");
|
||
//tab.AddHeadRow();
|
||
//ViewBag.gridTable = tab.GetHead() + Pagination.GetPage(pager, tableId, "5,8,10,15");
|
||
List<WX.CRM.Model.Entity.BAS_SALESDEPARTMENT> list = bas_salesdepartment.GetList();
|
||
ViewBag.deptList = list;
|
||
return View();
|
||
}
|
||
|
||
[AuthorizeRedirect(Roles = InitRights.CONST_广告策划)]
|
||
|
||
public JsonResult GetHtmlList(Laypage pager, string columns, Cms_News_Query param)
|
||
{
|
||
var layUidata = new LayuiData<CMS_NEWS_Map>();
|
||
try
|
||
{
|
||
List<CMS_NEWS> list = cms_news_bl.GetList(ref pager, param);
|
||
List<CMS_NEWS_Map> newlist = Newtonsoft.Json.JsonConvert.DeserializeObject<List<CMS_NEWS_Map>>(list.ToJson());
|
||
List<WX.CRM.Model.Entity.BAS_SALESDEPARTMENT> salelist = bas_salesdepartment.GetList();
|
||
foreach (var item in newlist)
|
||
{
|
||
if (item.UTIME.HasValue)
|
||
item.utimeStr = item.UTIME.Value.ToUnityString(6);
|
||
|
||
if (item.STATUSTIME.HasValue)
|
||
item.statustimeStr = item.STATUSTIME.Value.ToUnityString(6);
|
||
var info = salelist.FirstOrDefault(m => m.SALEDEPTCODE == item.DEPTCODE);
|
||
item.DEPTCODE = info == null ? "" : info.DEPTNAME;
|
||
}
|
||
layUidata.msg = "数据加载成功";
|
||
layUidata.code = 0;
|
||
layUidata.data = newlist;
|
||
layUidata.count = pager.count;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error(ex.ToString());
|
||
layUidata.SetFail(1, "出现错误!" + ex.Message);
|
||
}
|
||
return Json(layUidata);
|
||
}
|
||
|
||
[HttpGet]
|
||
[AuthorizeToolBar(InitRights.CONST_广告策划, InitToolBar.CONST_Add)]
|
||
public ActionResult Edit(decimal? ID)
|
||
{
|
||
CMS_NEWS model = new CMS_NEWS() { CREATEUSER = Eid, CREATEUSERNAME = UserName, ID = 0 };
|
||
if (ID.HasValue)
|
||
{
|
||
model = cms_news_bl.GetModel(ID.Value);
|
||
if (model == null)
|
||
{
|
||
model = new CMS_NEWS() { CREATEUSER = Eid, CREATEUSERNAME = UserName };
|
||
}
|
||
}
|
||
ViewBag.NewsJson = Newtonsoft.Json.JsonConvert.SerializeObject(model);
|
||
ViewBag.Files_Server = cache_BL.GetValue_Parameter(Parameter.Files_Server);
|
||
List<WX.CRM.Model.Entity.BAS_SALESDEPARTMENT> list = cache_BL.GetList_SalesDepartMent().Where(p => p.CHANNELDEF != null).ToList();
|
||
ViewBag.SaleDept = list;
|
||
|
||
return View(model);
|
||
}
|
||
|
||
ValidationErrors errors = new ValidationErrors();
|
||
|
||
[HttpPost]
|
||
[AuthorizeToolBar(InitRights.CONST_广告策划, InitToolBar.CONST_Add)]
|
||
public JsonResult EditSave(CMS_NEWS model, bool isUploadCore = false)
|
||
{
|
||
retMsg ret = new retMsg();
|
||
if (model.ID == 0)
|
||
{
|
||
model.CREATEUSER = Eid;
|
||
model.CREATEUSERNAME = UserName;
|
||
if (cms_news_bl.Create(ref errors, model))
|
||
{
|
||
ret = new retMsg() { result = true, retcode = 200, retmsg = "创建成功!" };
|
||
}
|
||
else
|
||
{
|
||
ret = new retMsg() { result = false, retcode = 200, retmsg = "创建失败!" };
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (cms_news_bl.Update(ref errors, model))
|
||
{
|
||
ret = new retMsg() { result = true, retcode = 200, retmsg = "修改成功!" };
|
||
}
|
||
else
|
||
{
|
||
ret = new retMsg() { result = false, retcode = 200, retmsg = "修改失败!" };
|
||
}
|
||
}
|
||
if (isUploadCore)
|
||
{
|
||
return UploadCore(model.ID);
|
||
}
|
||
return Json(ret);
|
||
}
|
||
|
||
|
||
public JsonResult LookMsg()
|
||
{
|
||
retMsg ret = new retMsg();
|
||
try
|
||
{
|
||
|
||
var list = csvr_message_bl.GetList(m => m.EID == Eid && m.ISLOOK == 0);
|
||
foreach (var item in list)
|
||
{
|
||
item.ISLOOK = 1;
|
||
item.LOOKTIME = DateTime.Now;
|
||
csvr_message_bl.Update(item);
|
||
}
|
||
ret = new retMsg() { result = true, retcode = 200, retmsg = "修改成功!" };
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
LogHelper.Error(e.ToString());
|
||
ret = new retMsg() { result = false, retcode = 200, retmsg = "修改失败!" };
|
||
}
|
||
return Json(ret);
|
||
}
|
||
|
||
[HttpPost]
|
||
public JsonResult UploadAtt()
|
||
{
|
||
var file = Request.Files[0];
|
||
var vPath = System.Configuration.ConfigurationManager.AppSettings["UploadTemFileDic"] + "/att";
|
||
var rootPath = Server.MapPath(vPath);
|
||
if (!System.IO.Directory.Exists(rootPath))
|
||
{
|
||
System.IO.Directory.CreateDirectory(rootPath);
|
||
}
|
||
var saveFileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
|
||
var urlPath = vPath + "/" + saveFileName;
|
||
var fileName = Path.Combine(rootPath, saveFileName);
|
||
file.SaveAs(fileName);
|
||
|
||
return Json(new { result = true, retcode = 1, retmsg = urlPath }, JsonRequestBehavior.AllowGet);
|
||
}
|
||
[HttpGet]
|
||
[AuthorizeToolBar(InitRights.CONST_广告策划, InitToolBar.CONST_Add)]
|
||
public ActionResult Detail(decimal ID)
|
||
{
|
||
CMS_NEWS model = cms_news_bl.GetModel(ID);
|
||
if (model == null)
|
||
{
|
||
model = new CMS_NEWS() { CREATEUSER = Eid, CREATEUSERNAME = UserName };
|
||
ViewBag.NewsJson = Newtonsoft.Json.JsonConvert.SerializeObject(model);
|
||
|
||
return View(model);
|
||
}
|
||
ViewBag.NewsJson = Newtonsoft.Json.JsonConvert.SerializeObject(model);
|
||
List<WX.CRM.Model.Entity.BAS_SALESDEPARTMENT> list = bas_salesdepartment.GetList();
|
||
var info = list.FirstOrDefault(m => m.SALEDEPTCODE == model.DEPTCODE);
|
||
model.DEPTCODE = (info == null ? "" : info.DEPTNAME);
|
||
ViewBag.Core_ZxdCoreUrl = cache_BL.GetValue_Parameter(Parameter.Core_ZxdCoreUrl);
|
||
return View(model);
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 上传至中心点
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public JsonResult UploadCore(decimal ID)
|
||
{
|
||
CMS_NEWS model = cms_news_bl.GetModel(ID);
|
||
var depment = cache_BL.GetList_SalesDepartMent();
|
||
if (model == null)
|
||
{
|
||
return Json(new { result = false, retcode = 1, retmsg = "找不到这条数据!" }, JsonRequestBehavior.AllowGet);
|
||
}
|
||
else
|
||
{
|
||
if (model.UPSTATUS == 1)
|
||
{
|
||
return Json(new { result = false, retcode = 1, retmsg = "请不要重复上传!" }, JsonRequestBehavior.AllowGet);
|
||
}
|
||
try
|
||
{
|
||
var url = cache_BL.GetValue_Parameter(Parameter.Core_CmsNewsUpload_Url);
|
||
if (string.IsNullOrEmpty(model.DEPTCODE))
|
||
{
|
||
model.DEPTCODE = companyCode;
|
||
}
|
||
model.Companycode = companyCode;//公司编码
|
||
model.UPLOADNAME = UserName;
|
||
var dep = depment.Find(p => p.SALEDEPTCODE == model.DEPTCODE);
|
||
if (dep != null)
|
||
{
|
||
if (dep.CHANNELDEF.HasValue)
|
||
model.CHANNEL = dep.CHANNELDEF;
|
||
}
|
||
var resault = string.Empty;
|
||
//if (!string.IsNullOrEmpty(model.ATT))
|
||
//{
|
||
// Dictionary<string, string> dic = new Dictionary<string, string>();
|
||
// dic.Add("param", model.ToJson());
|
||
// string path = Server.MapPath(model.ATT);
|
||
// resault = PostFileToServer(path, Path.GetFileName(path), url, dic);
|
||
//}
|
||
//else
|
||
//{
|
||
// Dictionary<string, string> dic = new Dictionary<string, string>();
|
||
// dic.Add("param", model.ToJson());
|
||
// string path = Server.MapPath(model.ATT);
|
||
// resault = PostFileToServer("", "", url, dic);
|
||
// //Utility.PostAjaxData(url, "param=" + model.ToJson(), Encoding.UTF8);
|
||
//}
|
||
|
||
Dictionary<string, string> dic = new Dictionary<string, string>();
|
||
dic.Add("param", model.ToJson());
|
||
resault = PostFileToServer("", "", url, dic);
|
||
|
||
var ret = Utility.ToObject<retMsg<string>>(resault);
|
||
if (ret.result)
|
||
{
|
||
if (!cms_news_bl.UpUploadStatus(ref errors, model.ID))
|
||
{
|
||
return Json(new { result = false, retcode = 1, retmsg = "系统错误:" + errors.Error }, JsonRequestBehavior.AllowGet);
|
||
}
|
||
}
|
||
|
||
return Json(new { result = true, retcode = 1, retmsg = "上传成功!等待审核!" }, JsonRequestBehavior.AllowGet);
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
return Json(new { result = false, retcode = 1, retmsg = "系统错误:" + e.Message }, JsonRequestBehavior.AllowGet);
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
|
||
public static string PostFileToServer(string filePath, string fileName, string postURL, Dictionary<string, string> stringDict)
|
||
{
|
||
//参考http://www.cnblogs.com/greenerycn/archive/2010/05/15/csharp_http_post.html
|
||
//string filePath = "d:\\apple4.jpg";
|
||
//string fileName = "apple4.jpg";
|
||
//string postURL = "http://192.168.1.11/testhandler/accfile.ashx";
|
||
|
||
// 边界符
|
||
var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");
|
||
var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");
|
||
|
||
// 最后的结束符
|
||
var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n");
|
||
// 开始拼数据
|
||
var memStream = new MemoryStream();
|
||
if (!string.IsNullOrEmpty(filePath))
|
||
{
|
||
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
|
||
|
||
// 文件参数头
|
||
const string filePartHeader =
|
||
"Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
|
||
"Content-Type: application/octet-stream\r\n\r\n";
|
||
var fileHeader = string.Format(filePartHeader, "file", fileName);
|
||
var fileHeaderBytes = Encoding.UTF8.GetBytes(fileHeader);
|
||
|
||
|
||
memStream.Write(beginBoundary, 0, beginBoundary.Length);
|
||
|
||
// 文件数据
|
||
memStream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length);
|
||
var buffer = new byte[1024];
|
||
int bytesRead; // =0
|
||
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
|
||
{
|
||
memStream.Write(buffer, 0, bytesRead);
|
||
}
|
||
fileStream.Close();
|
||
}
|
||
// Key-Value数据
|
||
var stringKeyHeader = "\r\n--" + boundary +
|
||
"\r\nContent-Disposition: form-data; name=\"{0}\"" +
|
||
"\r\n\r\n{1}\r\n";
|
||
|
||
//Dictionary<string, string> stringDict = new Dictionary<string, string>();
|
||
//stringDict.Add("len", "500");
|
||
//stringDict.Add("wid", "300");
|
||
foreach (byte[] formitembytes in from string key in stringDict.Keys
|
||
select string.Format(stringKeyHeader, key, stringDict[key])
|
||
into formitem
|
||
select Encoding.UTF8.GetBytes(formitem))
|
||
{
|
||
memStream.Write(formitembytes, 0, formitembytes.Length);
|
||
}
|
||
|
||
// 写入最后的结束边界符
|
||
memStream.Write(endBoundary, 0, endBoundary.Length);
|
||
|
||
//倒腾到tempBuffer?
|
||
memStream.Position = 0;
|
||
var tempBuffer = new byte[memStream.Length];
|
||
memStream.Read(tempBuffer, 0, tempBuffer.Length);
|
||
memStream.Close();
|
||
|
||
// 创建webRequest并设置属性
|
||
var webRequest = (HttpWebRequest)WebRequest.Create(postURL);
|
||
webRequest.Method = "POST";
|
||
webRequest.Timeout = 100000;
|
||
webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
|
||
webRequest.ContentLength = tempBuffer.Length;
|
||
|
||
var requestStream = webRequest.GetRequestStream();
|
||
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
|
||
requestStream.Close();
|
||
|
||
var httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
|
||
string responseContent;
|
||
using (var httpStreamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.GetEncoding("utf-8")))
|
||
{
|
||
responseContent = httpStreamReader.ReadToEnd();
|
||
}
|
||
|
||
httpWebResponse.Close();
|
||
webRequest.Abort();
|
||
|
||
return responseContent;
|
||
}
|
||
/// <summary>
|
||
|
||
/// Http上传文件
|
||
|
||
/// </summary>
|
||
|
||
public static string HttpUploadFile(string url, string path)
|
||
{
|
||
|
||
// 设置参数
|
||
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
|
||
CookieContainer cookieContainer = new CookieContainer();
|
||
request.CookieContainer = cookieContainer;
|
||
request.AllowAutoRedirect = true;
|
||
request.Method = "POST";
|
||
string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
|
||
request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
|
||
byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
|
||
byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
|
||
int pos = path.LastIndexOf("\\");
|
||
string fileName = path.Substring(pos + 1);
|
||
//请求头部信息
|
||
StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName));
|
||
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
|
||
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
|
||
byte[] bArr = new byte[fs.Length];
|
||
fs.Read(bArr, 0, bArr.Length);
|
||
fs.Close();
|
||
Stream postStream = request.GetRequestStream();
|
||
postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
|
||
postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
|
||
postStream.Write(bArr, 0, bArr.Length);
|
||
postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
|
||
postStream.Close();
|
||
//发送请求并获取相应回应数据
|
||
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
|
||
//直到request.GetResponse()程序才开始向目标网页发送Post请求
|
||
Stream instream = response.GetResponseStream();
|
||
StreamReader sr = new StreamReader(instream, Encoding.UTF8);
|
||
//返回结果网页(html)代码
|
||
string content = sr.ReadToEnd();
|
||
return content;
|
||
}
|
||
|
||
|
||
|
||
[AuthorizeRedirect(Roles = InitRights.CONST_广告策划)]
|
||
public ActionResult Log(int Id)
|
||
{
|
||
ViewBag.Core_ZxdCoreUrl = cache_BL.GetValue_Parameter(Parameter.Core_ZxdCoreUrl);
|
||
ViewBag.Id = Id;
|
||
return View();
|
||
}
|
||
|
||
[HttpPost]
|
||
[AuthorizeRedirect(Roles = InitRights.CONST_广告策划)]
|
||
public JsonResult NewsLog(decimal Id)
|
||
{
|
||
|
||
var list = cms_news_bl.GetLogList(Id).ToList();
|
||
List<WX.CRM.Model.Entity.BAS_SALESDEPARTMENT> deptlist = bas_salesdepartment.GetList();
|
||
foreach (var item in list)
|
||
{
|
||
var info = deptlist.FirstOrDefault(m => m.SALEDEPTCODE == item.DEPTCODE);
|
||
item.DEPTCODE = info == null ? "" : info.DEPTNAME;
|
||
}
|
||
var data = new LayuiData<CMS_NEWS_LOG>()
|
||
{
|
||
msg = "数据加载成功!",
|
||
count = list.Count(),
|
||
code = 0,
|
||
data = list
|
||
};
|
||
return Json(data, JsonRequestBehavior.AllowGet);
|
||
}
|
||
|
||
}
|
||
}
|