using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;
using PMS.BusinessModels.Account;
using PMS.Interface;
using PMS.Interface.SysManager;
using QWPlatform.SystemLibrary.Security;
using QWPlatform.SystemLibrary.Web;
using PMS.BusinessModels.SysManager;
using QWPlatform.SystemLibrary.Utils;
using PMS.BusinessModels;
using System.Drawing;
using System.Collections;
using System.IO;
namespace PMS.WebUI.Controllers
{
///
/// 创 建 人:王海洋
/// 创建日期:2018-12-10
/// 功能描述:基类控制器
///
public class BaseController : Controller
{
public IAccount account_obj = InterfaceFactory.CreateBusinessInstance();
///
/// 返回一个标准请求的JSON结构对象
///
/// 状态码,成功OK
/// 说明消息
/// 需要进行序列化的json对象
///
protected ActionResult ResponseJson(HttpStatusCode code, string msg = "", object obj = null)
{
//var jobj = new JsonResult();
//jobj.Data = new PmsJsonResoult(code, msg, json).ToString();
//jobj.ContentEncoding = Encoding.UTF8;
//jobj.ContentType = "application/json";
//jobj.JsonRequestBehavior = JsonRequestBehavior.AllowGet;//允许外部访问
//return jobj;
return Content(new PmsJsonResoult(code, msg, obj).ToString(), "application/json");
}
///
/// 获取当前的登录用户
///
///
public UserInfo GetCurrentUser()
{//获取登录信息
return SysCom.Instance.GetCurrentAccount();
}
///
/// 获取数据权限(渠道,项目)
///
///
public AuthDatsInfo GetAuthDats()
{
var UserInfo = GetCurrentUser();
var model = UserInfo.AuthDats;
var Channel = new List();
var Item = new List();
///判断是否存在数据权限
if (model.Count > 0)
{
//便利渠道
foreach (account_authdata_model m in model)
{
if (!String.IsNullOrEmpty(m.orgid))
{
Channel.Add(m.orgid);
}
//便利项目
if (m.items != null && m.items.Count > 0)
{
Item.AddRange(m.items);
}
}
}
var AuthInfo = new AuthDatsInfo();
AuthInfo.Channel = string.Join(",", Channel);
AuthInfo.Project = string.Join(",", Item);
return AuthInfo;
}
//获取当前页面的工具栏
[HttpGet]
public string GetToolbar(string groupName)
{
var user = GetCurrentUser();
if (user != null)
{//用户不为空,生成工作栏(获取当前请求action及controller),
/* 1.确定当前访问模块
* 2.确定当前模块的按钮列表
* 3.确定当前模块的按钮分组(分组名称由开发人员在外填写div class="分组名")
*/
var obj = this.ControllerContext.RouteData.Values;
var controller = "";
var view = "";
if (obj.ContainsKey("controller"))
{//获取控制器
controller = obj["controller"].ToStringEx();
}
if (obj.ContainsKey("action"))
{//获取方法
view = obj["action"].ToStringEx();
}
var path = string.Format("{0}/{1}", controller, view);
//查询出该模块的按钮列表(路径,人员角色,分组名称)
var json = account_obj.GetButtionsForUserRole(user.Roles, path, groupName, user.IsSuperAdmin);
return json;
}
return string.Empty;
}
#region 附件处理
///
/// 上传文件附件
///
/// 文件内容
/// 文件名称
/// 文件类型
/// 返回
public FtpUploadResult UploadFile(byte[] file, string fileName, string filetype)
{
//调用公共方法的上传
return SysCom.Instance.UploadFileToFtp(file, fileName, filetype);
}
///
/// 获取FTP文件的url
///
/// 多个文件逗号隔开
///
///
public FtpUrlResult GetFtpUrl(string fileId, string filetype )
{
return SysCom.Instance.GetFtpUrl(fileId, filetype);
}
///
/// 下载文件
///
/// 文件ID
/// 图像类型:o原图,s缩略图,m大图
///
public byte[] DownloadFileBytes(string fileId, string imgType)
{
return SysCom.Instance.DownloadFileBytesFromFtp(fileId, imgType);
}
///
/// 下载文件
///
/// 文件ID
/// 图像类型:o原图,s缩略图,m大图
/// 返回下载结构,包括文件名,文件类型
public FtpDownloadResult DownloadFileBase64(string fileId, string imgType)
{
return SysCom.Instance.DownloadFileBase64FromFtp(fileId, imgType);
}
#endregion
#region 消息推送处理
//向客户端推送消息
public void PushMessage(string persionId, string message)
{
App_Start.ClientManager.SendMessage(persionId, message);
}
//推送问题处理信息
public void SendDealMessage(string ProblemId,string persionId, string message, int Type)
{
App_Start.ClientManager.SendDealMessage(ProblemId,persionId, message, Type);
}
///刷新列表并推送消息
public void RefreshProblemAndSendMessage(string persionId, string Message,string ProblemId)
{
App_Start.ClientManager.RefreshProblemAndSendMessage(persionId, Message, ProblemId);
}
///
/// 返回调用短信接口的webService授权码
///
/// 请的的webservice地址
///
public string GetSMSAuthkey(string url)
{
return SysCom.Instance.ZlsoftAppServiceAuthkey(url);
}
///
/// 短信发送接口,返回是否成功
///
/// 电话号码
/// 验证码
///
public bool SMSCodeSend(string telephone, string code)
{
return SysCom.Instance.ZLsoftSMSCodeSend(telephone, code);
}
///
/// 短信验证码
///
///
public string ZlsoftAppSms(string phone)
{
return SysCom.Instance.ZlsoftAppSms(phone);
}
#endregion
///
/// 获取邮件服务器的配置信息(服务器参数)
///
///
public emailConfig GetEmailConfig()
{
return SysCom.Instance.GetSysEmailConfig();
}
///
/// 获取当前登录用户的配置信息
///
///
public NotefiyConfigInfo GetMyConfigInfo()
{
var user = this.GetCurrentUser();
return account_obj.GetConfigInfo(user.ID);
}
//图像上传
public ActionResult ImageUpload(string upfile)
{
string pathbase = "/upload/"; //保存路径
int size = 10; //文件大小限制,单位mb
string[] filetype = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" }; //文件允许格式
string callback = this.Request["callback"];
string editorId = this.Request["editorid"];
//上传图片
Hashtable info;
Uploader up = new Uploader();
info = up.upFileFTP(this.HttpContext, size); //获取上传状态
return Json(info);
}
}
///
/// UEditor编辑器通用上传类
///
public class Uploader
{
string state = "SUCCESS";
string URL = null;
string currentType = null;
string uploadpath = null;
string filename = null;
string originalName = null;
HttpPostedFileBase uploadFile = null;
int fileLen = 0;
/**
* 上传文件的主处理方法
* @param HttpContext
* @param string
* @param string[]
*@param int
* @return Hashtable
*/
public Hashtable upFile(HttpContextBase context, string pathbase, string[] filetype, int size)
{
pathbase = pathbase + DateTime.Now.ToString("yyyy-MM-dd") + "/";
uploadpath = context.Server.MapPath(pathbase);//获取文件上传路径
try
{
uploadFile = context.Request.Files[0];
originalName = uploadFile.FileName;
//目录创建
createFolder();
//格式验证
if (checkType(filetype))
{
state = "不允许的文件类型";
}
//大小验证
if (checkSize(size))
{
state = "文件大小超出网站限制";
}
//保存图片
if (state == "SUCCESS")
{
filename = reName();
uploadFile.SaveAs(uploadpath + filename);
URL = pathbase + filename;
}
}
catch (Exception e)
{
state = "未知错误";
URL = "";
}
return getUploadInfo();
}
///
/// 上传图片至FTP
///
///
///
///
///
///
public Hashtable upFileFTP(HttpContextBase context, int size)
{
try
{
uploadFile = context.Request.Files[0];
originalName = uploadFile.FileName;
fileLen = uploadFile.ContentLength;
currentType = System.IO.Path.GetExtension(originalName);
//格式验证
if (currentType != ".jpg" && currentType != ".png" && currentType != ".jpeg" && currentType != ".bmp")
{
state = "不允许的文件类型";
}
//大小验证
if (fileLen >= (size * 1024 * 1024))
{
state = "文件大小超出网站限制";
}
//保存图片
if (state == "SUCCESS")
{
using (BinaryReader br = new BinaryReader(uploadFile.InputStream))
{
byte[] byteData = br.ReadBytes((int)uploadFile.InputStream.Length);
var UplodResult = SysCom.Instance.UploadFileToFtp(byteData, originalName, currentType);
if (UplodResult.code == 100)
{
URL = "/MobileProblem/ViewProblemImage/" + UplodResult.data + "?type=o";
}
else
{
state = "上传FTP失败";
}
}
}
}
catch (Exception ex)
{
state = "未知错误";
URL = "";
QWPlatform.SystemLibrary.LogManager.Logger.Instance.Error("调用方法upFileFTP上传截图至FTP失败!", ex);
}
return getUploadInfoFTP();
}
private Hashtable getUploadInfoFTP()
{
Hashtable infoList = new Hashtable();
infoList.Add("state", state);
infoList.Add("url", URL);
infoList.Add("originalName", originalName);
infoList.Add("name", originalName);
infoList.Add("size", fileLen);
infoList.Add("type", currentType);
return infoList;
}
/**
* 上传涂鸦的主处理方法
* @param HttpContext
* @param string
* @param string[]
*@param string
* @return Hashtable
*/
public Hashtable upScrawl(HttpContext cxt, string pathbase, string tmppath, string base64Data)
{
pathbase = pathbase + DateTime.Now.ToString("yyyy-MM-dd") + "/";
uploadpath = cxt.Server.MapPath(pathbase);//获取文件上传路径
FileStream fs = null;
try
{
//创建目录
createFolder();
//生成图片
filename = System.Guid.NewGuid() + ".png";
fs = File.Create(uploadpath + filename);
byte[] bytes = Convert.FromBase64String(base64Data);
fs.Write(bytes, 0, bytes.Length);
URL = pathbase + filename;
}
catch (Exception e)
{
state = "未知错误";
URL = "";
}
finally
{
fs.Close();
deleteFolder(cxt.Server.MapPath(tmppath));
}
return getUploadInfo();
}
/**
* 获取文件信息
* @param context
* @param string
* @return string
*/
public string getOtherInfo(HttpContext cxt, string field)
{
string info = null;
if (cxt.Request.Form[field] != null && !String.IsNullOrEmpty(cxt.Request.Form[field]))
{
info = field == "fileName" ? cxt.Request.Form[field].Split(',')[1] : cxt.Request.Form[field];
}
return info;
}
/**
* 获取上传信息
* @return Hashtable
*/
private Hashtable getUploadInfo()
{
Hashtable infoList = new Hashtable();
infoList.Add("state", state);
infoList.Add("url", URL);
infoList.Add("originalName", originalName);
infoList.Add("name", Path.GetFileName(URL));
infoList.Add("size", uploadFile.ContentLength);
infoList.Add("type", Path.GetExtension(originalName));
return infoList;
}
/**
* 重命名文件
* @return string
*/
private string reName()
{
return System.Guid.NewGuid() + getFileExt();
}
/**
* 文件类型检测
* @return bool
*/
private bool checkType(string[] filetype)
{
currentType = getFileExt();
return Array.IndexOf(filetype, currentType) == -1;
}
/**
* 文件大小检测
* @param int
* @return bool
*/
private bool checkSize(int size)
{
return uploadFile.ContentLength >= (size * 1024 * 1024);
}
/**
* 获取文件扩展名
* @return string
*/
private string getFileExt()
{
string[] temp = uploadFile.FileName.Split('.');
return "." + temp[temp.Length - 1].ToLower();
}
/**
* 按照日期自动创建存储文件夹
*/
private void createFolder()
{
if (!Directory.Exists(uploadpath))
{
Directory.CreateDirectory(uploadpath);
}
}
/**
* 删除存储文件夹
* @param string
*/
public void deleteFolder(string path)
{
//if (Directory.Exists(path))
//{
// Directory.Delete(path, true);
//}
}
}
}