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