SessionHelper

using Microsoft.AspNetCore.Http;
using PT.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PT.Common
{
    public class SessionHelper
    {

        private ISession _session;

        public SessionHelper(ISession session)
        {
            _session = session;
        }

        public void SetString(string key, string val)
        {
            _session.SetString(key, val);
        }

        public string GetString(string key)
        {
            return _session.GetString(key);
        }


        /// <summary>
        /// 设置登录用户信息
        /// </summary>
        /// <param name="UserInfo"></param>
        public void SetUserInfoString(UserList UserInfo)
        {
            string data = UserInfo.ToJson();
            //data = "{\"Id\":24,\"UserID\":\"lrh\",\"UserName\":\"李儒鸿\",\"Dept\":\"isd\",\"Position\":\"ids\",\"LoginID\":\"lrh\",\"LoginPwd\":\"195BCD7153C4FC6BD053C3B71DC73B05\",\"GroupId\":4,\"Active\":true,\"IsDelete\":false,\"CreatBy\":null,\"CreatDate\":\"2023-04-27T13:19:32.633\",\"ModifyBy\":null,\"ModifyDate\":null}";
            _session.SetString("UserInfo", data);
        }

        /// <summary>
        /// 获取登录用户
        /// </summary>
        /// <returns></returns>
        public UserList GetUserInfoString()
        {
            return _session.GetString("UserInfo").ToObject<UserList>();
        }



        /// <summary>
        /// 设置实体
        /// </summary>
        /// <param name="UserInfo"></param>
        public void SetEntity<T>(string key, T t)
        {
            string data = t.ToJson();
            //data = "{\"Id\":24,\"UserID\":\"lrh\",\"UserName\":\"李儒鸿\",\"Dept\":\"isd\",\"Position\":\"ids\",\"LoginID\":\"lrh\",\"LoginPwd\":\"195BCD7153C4FC6BD053C3B71DC73B05\",\"GroupId\":4,\"Active\":true,\"IsDelete\":false,\"CreatBy\":null,\"CreatDate\":\"2023-04-27T13:19:32.633\",\"ModifyBy\":null,\"ModifyDate\":null}";
            _session.SetString(key, data);
        }

        /// <summary>
        /// 获取实体
        /// </summary>
        /// <returns></returns>
        public T GetEntity<T>(string key)
        {
            return _session.GetString(key).ToObject<T>();
        }

    }
}

 

posted @ 2023-06-26 11:07  妖狐鬼魅  阅读(30)  评论(0编辑  收藏  举报