500 lines
17 KiB
C#
500 lines
17 KiB
C#
using Core.Web.WebHelper;
|
||
using Core.Web.WebHelper.UserRight;
|
||
using Core.Web.WebHelper.UtilityModel;
|
||
using CRM.Core.BLL.Util;
|
||
using CRM.Core.Common.WebHelper;
|
||
using CRM.Core.DTO;
|
||
using CRM.Core.Model.QueryModels;
|
||
using LumenWorks.Framework.IO.Csv;
|
||
using NPOI.HSSF.UserModel;
|
||
using NPOI.SS.UserModel;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.Data.Entity.Validation;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Web;
|
||
using System.Web.Mvc;
|
||
using System.Web.Security;
|
||
using WX.CRM.Common;
|
||
|
||
namespace Core.Web.Controllers
|
||
{
|
||
public class BaseController : Controller
|
||
{
|
||
private UserInfo _userinfo;
|
||
private string[] _rights;
|
||
private bool needToRedirect;
|
||
private bool rightRedirct = false;
|
||
public static Dictionary<string, string> tokenDictionary = new Dictionary<string, string>();
|
||
|
||
public BaseController()
|
||
{
|
||
_rights = UserRightsHelper.getUserRights();
|
||
if (_rights.Contains("-1"))
|
||
{
|
||
rightRedirct = false;
|
||
}
|
||
if (System.Web.HttpContext.Current.Request.IsAuthenticated)
|
||
{
|
||
FormsIdentity id = (FormsIdentity)System.Web.HttpContext.Current.User.Identity;
|
||
string userinfo_str = id.Ticket.UserData;
|
||
//_userinfo = JsonHelper.JsonDivertToObj<UserInfo>(userinfo_str);
|
||
try
|
||
{
|
||
_userinfo = Newtonsoft.Json.JsonConvert.DeserializeObject<UserInfo>(userinfo_str);
|
||
needToRedirect = false;
|
||
}
|
||
catch (Exception)
|
||
{
|
||
needToRedirect = true;
|
||
}
|
||
|
||
try
|
||
{
|
||
var refreshTokenCookie = System.Web.HttpContext.Current.Request.Cookies["refreshToken"];
|
||
var expireTimeCookie = System.Web.HttpContext.Current.Request.Cookies["expireTime"];
|
||
if (refreshTokenCookie != null && expireTimeCookie != null)
|
||
{
|
||
var refreshToken = refreshTokenCookie.Value;
|
||
var expireTime = expireTimeCookie.Value;
|
||
if (!string.IsNullOrEmpty(refreshToken) && !string.IsNullOrEmpty(expireTime) && !tokenDictionary.ContainsKey(refreshToken))
|
||
{
|
||
DateTime guoqishijian = DateTime.Parse(expireTime);
|
||
if (guoqishijian.AddMinutes(-5) < DateTime.Now)//数据同步
|
||
{
|
||
tokenDictionary.Add(refreshToken, refreshToken);
|
||
|
||
var ssoUrl = System.Configuration.ConfigurationManager.AppSettings["SSOUrl"];
|
||
var appid = System.Configuration.ConfigurationManager.AppSettings["appid"];
|
||
if (!string.IsNullOrEmpty(ssoUrl) && !string.IsNullOrEmpty(appid))
|
||
{
|
||
var url = $"{ssoUrl}/v1/api/open/sso/token";
|
||
var param = new
|
||
{
|
||
appId = appid,
|
||
grantType = 2,
|
||
token = refreshToken
|
||
};
|
||
|
||
var result = Utility.PostAjaxData(url, JsonHelper.ObjDivertToJson(param), System.Text.Encoding.UTF8);
|
||
var response = JsonHelper.JsonDivertToObj<SSORequest.SsoResponse<SSORequest.SsoResponseData>>(result);
|
||
if (response.Ret == 0)
|
||
{
|
||
System.Web.HttpContext.Current.Response.Cookies.Add(new HttpCookie("AuthToken", response.Data.AccessToken));
|
||
//System.Web.HttpContext.Current.Response.Cookies.Add(new HttpCookie("refreshToken", response.Data.refreshToken));//刷新token
|
||
System.Web.HttpContext.Current.Response.Cookies.Add(new HttpCookie("expireTime", response.Data.ExpireTime));//过期时间
|
||
}
|
||
}
|
||
tokenDictionary.Remove(refreshToken);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception)
|
||
{
|
||
|
||
}
|
||
|
||
|
||
}
|
||
else
|
||
{
|
||
_userinfo = new UserInfo();
|
||
needToRedirect = true;
|
||
// Redirect("Base/Account/LogOn");
|
||
}
|
||
|
||
}
|
||
protected override void OnActionExecuting(ActionExecutingContext filterContext)
|
||
{
|
||
if (this.needToRedirect || rightRedirct)
|
||
{
|
||
FormsAuthentication.SignOut();
|
||
var ssoUrl = System.Configuration.ConfigurationManager.AppSettings["SSOUrl"];
|
||
|
||
string RedirectLocation = string.Format("~/AccountSSO/LogOn?ReturnUrl={0}", filterContext.HttpContext.Request.RawUrl);
|
||
if (string.IsNullOrEmpty(ssoUrl)) {
|
||
RedirectLocation = string.Format("~/Account/LogOn?ReturnUrl={0}", filterContext.HttpContext.Request.RawUrl);
|
||
}
|
||
filterContext.Result = new RedirectResult(RedirectLocation);
|
||
return;
|
||
}
|
||
}
|
||
protected override void OnException(ExceptionContext filterContext)
|
||
{
|
||
string controllerName = filterContext.RouteData.Values["controller"].ToString();
|
||
string actionName = filterContext.RouteData.Values["action"].ToString();
|
||
string msg = string.Concat(controllerName, "-", actionName, ";");
|
||
var dbEx = filterContext.Exception as DbEntityValidationException;
|
||
|
||
if (dbEx != null)
|
||
{
|
||
foreach (var validationErrors in dbEx.EntityValidationErrors)
|
||
{
|
||
msg += validationErrors.Entry.Entity.ToString();
|
||
foreach (var validationError in validationErrors.ValidationErrors)
|
||
{
|
||
msg += string.Format("。Property:{0} Error:{1}", validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine;
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
msg += filterContext.Exception.ToString() + ";" + filterContext.Exception.StackTrace;
|
||
}
|
||
LogHelper.Error(msg);
|
||
|
||
if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
|
||
{
|
||
filterContext.HttpContext.Response.StatusCode = 200;
|
||
filterContext.ExceptionHandled = true;
|
||
filterContext.Result = new JsonResult
|
||
{
|
||
ContentType = "text/html",
|
||
Data = new
|
||
{
|
||
type = 0,
|
||
message = "系统错误:" + filterContext.Exception.Message,
|
||
errorMessag = "系统错误:" + filterContext.Exception.Message
|
||
},
|
||
JsonRequestBehavior = JsonRequestBehavior.AllowGet
|
||
};
|
||
}
|
||
else
|
||
{
|
||
//filterContext.ExceptionHandled = true;
|
||
//string msg = string.Concat(controllerName, "_", actionName, ";", filterContext.Exception.Message);
|
||
//filterContext.Result = new RedirectResult(Url.Action("ErrorView", "Error", new { message = msg }));
|
||
// filterContext.Result = new PartialViewResult("/Bas/Error/ErrorView", new { message = msg});
|
||
base.OnException(filterContext);
|
||
}
|
||
// JsonHandler.ExceptionMessage(filterContext.Exception.Message);
|
||
|
||
|
||
}
|
||
protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
|
||
{
|
||
return new JsonResultConfig { Data = data, ContentType = contentType, ContentEncoding = contentEncoding, JsonRequestBehavior = behavior };
|
||
}
|
||
public bool IsLogin
|
||
{
|
||
get
|
||
{
|
||
if (System.Web.HttpContext.Current.Request.IsAuthenticated)
|
||
return true;
|
||
|
||
else
|
||
return false;
|
||
|
||
}
|
||
}
|
||
public string UserName
|
||
{
|
||
get
|
||
{
|
||
return _userinfo.userName;
|
||
}
|
||
}
|
||
public string Fjh
|
||
{
|
||
get
|
||
{
|
||
return _userinfo.FJH;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 员工工号
|
||
/// </summary>
|
||
public int Eid
|
||
{
|
||
get
|
||
{
|
||
return _userinfo.userEid;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 员工Id
|
||
/// </summary>
|
||
public int UserId
|
||
{
|
||
get
|
||
{
|
||
return _userinfo.userId;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 公司id
|
||
/// </summary>
|
||
public string FJH
|
||
{
|
||
get { return _userinfo.FJH; }
|
||
}
|
||
/// <summary>
|
||
/// 员工组别id
|
||
/// </summary>
|
||
public decimal userGroupId
|
||
{
|
||
get
|
||
{
|
||
return _userinfo.userGroupId;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 部门id
|
||
/// </summary>
|
||
public decimal deptId
|
||
{
|
||
get
|
||
{
|
||
return _userinfo.deptId;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 营业部id
|
||
/// </summary>
|
||
public decimal saleDeptId
|
||
{
|
||
get { return _userinfo.saleDeptId; }
|
||
}
|
||
/// <summary>
|
||
/// 营业部编码
|
||
/// </summary>
|
||
public string saleDeptCode
|
||
{
|
||
get { return _userinfo.saleDeptCode; }
|
||
}
|
||
/// <summary>
|
||
/// 公司id
|
||
/// </summary>
|
||
public decimal companyId
|
||
{
|
||
get { return _userinfo.companyId; }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 业务名称字符串用"[]"隔开
|
||
/// </summary>
|
||
public string companyBusiness
|
||
{
|
||
get { return _userinfo.companyBusiness; }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 员工所管理的组id
|
||
/// </summary>
|
||
public decimal[] userOnGroupId
|
||
{
|
||
get { return _userinfo.userOnGroupId; }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 员工角色id
|
||
/// </summary>
|
||
public int[] userRoleId
|
||
{
|
||
get { return _userinfo.userRoleId; }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 员工角色id
|
||
/// </summary>
|
||
public string userRoleNames
|
||
{
|
||
get { return _userinfo.userRoleName; }
|
||
}
|
||
|
||
public decimal LoginLogId
|
||
{
|
||
get { return _userinfo.logInLogID; }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 员工权限id
|
||
/// </summary>
|
||
public string[] userRightId
|
||
{
|
||
get
|
||
{
|
||
return _rights;
|
||
}
|
||
}
|
||
public bool containRight(string rightcode)
|
||
{
|
||
bool iscontain = false;
|
||
foreach (var item in userRightId)
|
||
{
|
||
if (item.StartsWith(rightcode + "|"))
|
||
return true;
|
||
}
|
||
return iscontain;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 客服类型,1客户经理、2高级客服、3客服
|
||
/// </summary>
|
||
/// <param name="userPurview"></param>
|
||
/// <returns></returns>
|
||
protected string GetKFType(int userPurview)
|
||
{
|
||
string kftype = "";
|
||
int purviewValue = (int)Math.Pow(2, 1);
|
||
if ((userPurview & purviewValue) == purviewValue)
|
||
kftype += "、客服经理";
|
||
purviewValue = (int)Math.Pow(2, 2);
|
||
if ((userPurview & purviewValue) == purviewValue)
|
||
kftype += "、高级客服";
|
||
purviewValue = (int)Math.Pow(2, 3);
|
||
if ((userPurview & purviewValue) == purviewValue)
|
||
kftype += "、客服";
|
||
if (kftype != "")
|
||
{
|
||
kftype = kftype.Substring(1);
|
||
}
|
||
return kftype;
|
||
}
|
||
|
||
public string userRoleCodes
|
||
{
|
||
get { return new CACHE_BL().Get_RoleCodes(userRoleId); }
|
||
}
|
||
|
||
public DataTable ExcelToDataTable(HttpPostedFileBase file)
|
||
{
|
||
return ExcelToDataTable(file.InputStream, file.FileName);
|
||
}
|
||
|
||
public DataTable ExcelToDataTable(Stream stream, string fileName)
|
||
{
|
||
//1、缓存中读取此datatype对应的数据字典,创建datatable
|
||
//2、根据数据字典的关键字,找出每一列对应的excel中字段的序号
|
||
//3、循环生成每一条datarow记录
|
||
|
||
var tab = new DataTable();
|
||
IWorkbook workbook = null;
|
||
|
||
if (Path.GetExtension(fileName).ToLower().Equals(".xls"))
|
||
{
|
||
//LogHelper.Info("2003");
|
||
workbook = new HSSFWorkbook(stream);
|
||
}
|
||
else
|
||
{
|
||
//LogHelper.Info("2007");
|
||
//workbook = WorkbookFactory.Create(FileUpload1.PostedFile.InputStream);
|
||
workbook = WorkbookFactory.Create(stream);
|
||
}
|
||
|
||
//var excelImportType = DataCacheHelper.GetCache().GetModel_ExcelImportType(ExcelDataType);//导入excel类型
|
||
//var headerStartRow = int.Parse(excelImportType.HEADERSTARTROW.ToString());
|
||
//var dataStartRow = int.Parse(excelImportType.DATASTARTROW.ToString());
|
||
//var tableNameRow = int.Parse(excelImportType.TABLENAMEROW.ToString());
|
||
var sheet = workbook.GetSheetAt(0);
|
||
////====================获取表名比较=============
|
||
//var tableName = sheet.GetRow(tableNameRow).GetCell(0).ToString().Trim();
|
||
//if (tableName != excelImportType.TYPENAME)
|
||
//{
|
||
// errMsg.Add("excel类型错误,请确认是否导错excel!");
|
||
// return tab;
|
||
//}
|
||
//====================表头=====================
|
||
var headerRow = sheet.GetRow(0);
|
||
for (int i = headerRow.FirstCellNum; i < headerRow.LastCellNum; i++)
|
||
{
|
||
var cellValue = headerRow.GetCell(i).StringCellValue;
|
||
var column = new DataColumn
|
||
{
|
||
Caption = cellValue,
|
||
ColumnName = cellValue,
|
||
DataType = Type.GetType("System.String")
|
||
};
|
||
tab.Columns.Add(column);
|
||
}
|
||
|
||
//=========================数据========================
|
||
var rows = sheet.GetRowEnumerator();
|
||
var x = sheet.FirstRowNum;
|
||
IRow row = null;
|
||
while (rows.MoveNext())
|
||
{
|
||
if (x < 1)
|
||
{
|
||
x++;
|
||
continue;//头部不加入数据
|
||
}
|
||
row = (IRow)rows.Current;
|
||
if (string.IsNullOrWhiteSpace(row.GetCell(0).ToString()))
|
||
continue;
|
||
var dataRow = tab.NewRow();
|
||
for (var i = row.FirstCellNum; i < row.LastCellNum; i++)
|
||
{
|
||
var cell = row.GetCell(i);
|
||
if (cell == null)
|
||
{
|
||
dataRow[i] = DBNull.Value;
|
||
}
|
||
else
|
||
{
|
||
dataRow[i] = cell.ToString();
|
||
}
|
||
}
|
||
tab.Rows.Add(dataRow);
|
||
|
||
}
|
||
|
||
return tab;
|
||
|
||
}
|
||
|
||
public DataTable CsvToDataTable(HttpPostedFileBase file)
|
||
{
|
||
var tab = new DataTable();
|
||
|
||
using (var reader = new StreamReader(file.InputStream, Encoding.GetEncoding("GB2312")))
|
||
{
|
||
using (var csv = new CsvReader(reader, true))
|
||
{
|
||
var headers = csv.GetFieldHeaders();
|
||
foreach (var item in headers)
|
||
{
|
||
var column = new DataColumn
|
||
{
|
||
Caption = item,
|
||
ColumnName = item,
|
||
DataType = Type.GetType("System.String")
|
||
};
|
||
tab.Columns.Add(column);
|
||
}
|
||
|
||
while (csv.ReadNextRecord())
|
||
{
|
||
var dataRow = tab.NewRow();
|
||
for (int i = 0; i < csv.FieldCount; i++)
|
||
{
|
||
dataRow[i] = csv[i];
|
||
}
|
||
tab.Rows.Add(dataRow);
|
||
}
|
||
}
|
||
}
|
||
|
||
return tab;
|
||
}
|
||
|
||
public JsonResult JsonResult(ValidationErrors errors)
|
||
{
|
||
if (errors.Count > 0)
|
||
{
|
||
return Json(new retMsg { result = false, retcode = 500, retmsg = errors.Error }, JsonRequestBehavior.AllowGet);
|
||
}
|
||
else
|
||
{
|
||
return Json(new retMsg { result = true }, JsonRequestBehavior.AllowGet);
|
||
}
|
||
}
|
||
}
|
||
} |