LoginController.cs 5.21 KB
using Hh.Mes.Common;
using Hh.Mes.Common.config;
using Hh.Mes.Common.Json;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.Service;
using Hh.Mes.Service.Logs;
using Hh.Mes.Service.SystemAuth;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
using System;
using System.Linq;
using System.Reflection;
using System.Text;

namespace WebMvc
{
    public class LoginController : Controller
    {
        #region 属性
        private readonly string _appKey = "hhweb";
        private readonly IAuth _authUtil;
        private readonly FaceLoginService faceLoginService;
        private readonly LogService logService;
        #endregion

        public LoginController(IAuth authUtil, LogService logService, FaceLoginService faceLoginService)
        {
            _authUtil = authUtil;
            this.faceLoginService = faceLoginService;
            this.logService = logService;
        }

        public ActionResult Index()
        {
            ViewBag.copyright = "Copyright © " + DateTime.Now.ToString("yyyy ") + AppSettings.GetAppSeting("copyright");
            return View();
        }

        /// <summary>
        /// 登入
        /// </summary>
        [HttpPost]
        public string Login(string username, string password, string webcam, string idcard, string lineCode)
        {
            var resp = new Response();
            try
            {
                if (!string.IsNullOrEmpty(idcard))
                {
                    #region   工卡登录
                    Response faceResult = faceLoginService.IdCardSearchService(idcard);
                    if (faceResult.Code != 200)
                    {
                        resp.Code = 500;
                        resp.Message = faceResult.Message;
                        return JsonHelper.Instance.Serialize(resp);
                    }
                    username = faceResult.Result.account;
                    password = Encryption.Decrypt(faceResult.Result.password);
                    #endregion
                }
                password = JsEncrypt.DecodeBase64(Encoding.Default, password);
                var result = _authUtil.Login(_appKey, username, password);
                if (result.Code == 200)
                {
                    resp.Token = result.Token;
                    resp.Result = result.currentSession;

                    //写登入日志
                    var sysLogs = new sys_login_log
                    {
                        token = result.Token,
                        id = result.currentSession.Id,
                        account = result.currentSession.Account,
                        name = result.currentSession.Name,
                        ipaddr = HttpContext.Connection.RemoteIpAddress.ToString(),
                        browser = Request.Headers[HeaderNames.UserAgent].ToString(),
                        loginTime = DateTime.Now,
                    };
                    logService.loginAfter(sysLogs);

                    //写cookies
                    //https://www.cnblogs.com/land/archive/2009/04/10/1433074.html
                    Response.Cookies.Append(SSOAuthAttribute.token, result.Token);
                }
                else
                {
                    resp.Code = 500;
                    resp.Message = result.Message;
                }
            }
            catch (Exception e)
            {
                resp.Code = 500;
                resp.Message = e.Message;
            }

            return JsonHelper.Instance.Serialize(resp);
        }

        /// <summary>
        /// 第3方登入new
        /// 测试地址: https://localhost:5001/Login/OtherLogin?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpbk5hbWUiOiJTeXN0ZW0iLCJJU1NVUkUiOiJkaW1zIiwiZXhwIjoxNjUwNTI3MDQ2LjY2MTY2NjZ9.fJbsXxC0eirEZRW87BxfMULwwMiSMGDtOVZKdeLQHTM
        /// </summary>
        /// <returns></returns>
        public ActionResult OtherLogin(string token)
        {
            var userInfo = JwtEncryption.Decode(token);
            var userInfoJosn = DynamicJson.Parse(userInfo);
            Response loginResponseResult = faceLoginService.OtherLoginNew(userInfoJosn.loginName);
            if (loginResponseResult.Code != 200)
            {
                ViewBag.msgInfo = loginResponseResult.Message + "--" + userInfo;
                return View();
            }
            //var herf= sysCompanyService.GetDictionaryDictValue(MethodBase.GetCurrentMethod().Name);
            var cookieOptions = new CookieOptions();
            Response.Cookies.Append(SSOAuthAttribute.token, loginResponseResult.Token, cookieOptions);
            //javascript 输出到页面 写入缓存跳转页面
            //ViewBag.js = $@"localStorage.setItem('Account', '{loginResponseResult.Result.Account}');
            //                localStorage.setItem('Name', '{loginResponseResult.Result.Name}')
            //                window.location.href = '{herf}'";
            return View();
        }

        /// <summary>
        /// 退出
        /// </summary>
        public ActionResult Logout()
        {
            var token = Request.Cookies[SSOAuthAttribute.token];
            logService.Logout(token);
            return RedirectToAction("Index", "Login");
        }

    }
}