You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.6 KiB

3 years ago
using ce.autofac.extension;
using learun.iapplication;
using learun.util;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
namespace learun.webapi
{
/// <summary>
/// 版 本 Learun-ADMS-Core 力软管理后台
/// Copyright (c) 2019-present 力软信息技术(苏州)有限公司
/// 创建人:tobin
/// 日 期:2019.09.11
/// 描 述:基础控制器
/// </summary>
[TypeFilter(typeof(ExceptionFilter))]
[ApiController]
[Route("[controller]/[action]")]
public class MvcControllerBase : Controller
{
#region 获取基础信息
private UserEntity userInfo;
/// <summary>
/// 获取当前用户信息
/// </summary>
/// <param name="userId">用户id</param>
/// <returns></returns>
protected async Task<UserEntity> CurrentUser(string userId = null)
{
var userBLL = IocManager.Instance.GetService<UserIBLL>();
if (string.IsNullOrEmpty(userId))
{
if (userInfo == null)
{
userInfo = await userBLL.GetEntity();
}
return userInfo;
}
else
{
return await userBLL.GetEntity(userId);
}
}
/// <summary>
/// 获取编码
/// </summary>
/// <param name="code">编码规则编码</param>
/// <returns></returns>
protected Task<string> GetRuleCode(string code)
{
var codeRuleIBLL = IocManager.Instance.GetService<CodeRuleIBLL>();
return codeRuleIBLL.GetBillCode(code);
}
/// <summary>
/// 占用编码
/// </summary>
/// <param name="code">编码规则编码</param>
/// <returns></returns>
protected async Task UseRuleSeed(string code)
{
var codeRuleIBLL = IocManager.Instance.GetService<CodeRuleIBLL>();
await codeRuleIBLL.UseRuleSeed(code);
}
/// <summary>
/// 获取登录者用户名称
/// </summary>
/// <returns></returns>
protected string GetUserName()
{
return ContextHelper.GetItem("userName") as string;
}
/// <summary>
/// 获取登录者用户Id
/// </summary>
/// <returns></returns>
protected string GetUserId()
{
return ContextHelper.GetItem("userId") as string;
}
/// <summary>
/// 获取登录者用户账号
/// </summary>
/// <returns></returns>
protected string GetUserAccount()
{
return ContextHelper.GetItem("account") as string;
}
#endregion
#region 请求响应
/// <summary>
/// 返回成功数据
/// </summary>
/// <param name="data">数据</param>
/// <returns></returns>
protected virtual IActionResult Success(object data)
{
return Content(new ResParameter { code = ResponseCode.success, info = "响应成功", data = data }.ToJson());
}
/// <summary>
/// 返回成功消息
/// </summary>
/// <param name="info">信息</param>
/// <param name="data">数据</param>
/// <returns></returns>
protected virtual IActionResult Success(string info, object data)
{
return Content(new ResParameter { code = ResponseCode.success, info = info, data = data }.ToJson());
}
/// <summary>
/// 返回成功数据
/// </summary>
/// <param name="info">信息</param>
/// <returns></returns>
protected virtual IActionResult SuccessInfo(string info)
{
return Content(new ResParameter { code = ResponseCode.success, info = info }.ToJson());
}
/// <summary>
/// 返回失败消息
/// </summary>
/// <param name="info">消息</param>
/// <returns></returns>
protected virtual IActionResult Fail(string info)
{
return Content(new ResParameter { code = ResponseCode.fail, info = info }.ToJson());
}
#endregion
/// <summary>
/// 获取cookies
/// </summary>
/// <param name="key">键</param>
/// <returns>返回对应的值</returns>
protected string GetCookies(string key)
{
HttpContext.Request.Cookies.TryGetValue(key, out string value);
if (string.IsNullOrEmpty(value))
value = string.Empty;
return value;
}
}
}