SystemController.cs 4.21 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Enyim.Caching.Configuration;
using Hh.Mes.POJO.ViewModel;
using Hh.Mes.Service;
using Hh.Mes.Service.SystemAuth;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MySqlX.XDevAPI.Common;
using NetTaste;
using SqlSugar;

namespace Hh.Mes.Api.Controllers
{
    /// <summary>
    /// 系统公共的接口
    /// ps:新增方法需要在SystemVariable 注册 APIList、EnumLog 新增枚举 ,方便接口日志定位查询
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SystemController : BaseController
    {
        private readonly SystemService service;

        public SystemController(SystemService systemService, IHttpContextAccessor accessor)
        {

            service = systemService;
            context = accessor.HttpContext;

            var sysUserApi = base.GetUser(context);
            this.service.sysUserApi = sysUserApi;
        }

        #region 登入 ,退出,检查版本,获取枚举
        /// <summary>
        /// app检查
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ActionName("Mes/V1/base/AppCheckVerByAppNameAndVer")]
        public string AppCheckVerByAppNameAndVer(string appId, double ver)
        {
            var result = service.AppCheckVerByAppNameAndVer(appId, ver);
            return Serialize(result);
        }

        /// <summary>
        /// 登入退出 【删除redis,删除  sys_user_online】
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionName("Mes/V1/Login/Logout")]
        public string Logout()
        {
            var token = GetToken(HttpContext);
            return  Serialize(service.Logout(token));
        }

        /// <summary>
        /// 登入 登入成功后自动在输出到缓存Cookies 头部
        /// 其他接口调用前 要先调用登入接口,或者使用配置文件忽略登入验证
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionName("Mes/V1/Login")]
        public string Login()
        {
            string userName = Request.Form["username"].ToString().Trim();
            var password = Request.Form["password"].ToString().Trim();
            //登录
            var result = service.Login(userName, password, Program.AppKey, Program.AppSecret);
            bool isOk = result is string;
            if (!isOk)
            {
                //写入cookies
                //https://www.cnblogs.com/land/archive/2009/04/10/1433074.html
                Response.Cookies.Append(Program.tokens, result.Token);
            }
            else {
                Response.Cookies.Append(Program.tokens, "");
            }
            return  Serialize(result);
        }

        /// <summary>
        /// 判断token是否存在,过期
        /// </summary>
        [HttpPost]
        [ActionName("Mes/V1/AppCheckToken")]
        public string AppCheckToken()
        {
            var token = GetToken(context);
            return Serialize(service.AppCheckToken(token));
        }

        /// <summary>
        /// 枚举对象
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ActionName("Mes/V1/GetState")]
        public string GetState()
        {
            return service.GetState();
        }

        /// <summary>
        /// 获取PDA用户可访问的模块列表
        /// </summary>
        [HttpPost]
        [ActionName("Mes/V1/GetPDAModules")]
        public string GetPDAModules()
        {
            var token = GetToken(context);
            return Serialize(service.GetPDAModules(token));
        }
        #endregion

        /// <summary>
        ///  第3方登入
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ActionName("Mes/V1/Login/OtherLogin")]
        public string OtherLogin(string token)
        {
            //登录
            var result = service.OtherLogin(token);
            //写入cookies
            //https://www.cnblogs.com/land/archive/2009/04/10/1433074.html
            //Response.Cookies.Append(Program.tokens, result.Token);
            return  Serialize(result);
        }
    }
}