服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2024-08-29 3cab868262d5e84cd1000851c07cce43a459f3ea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using Tiger.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Rhea.Common;
using Tiger.IBusiness;
using Dm.filter.log;
 
namespace Tiger.Business
{
    public partial class Biz : ILogin
    {
        /// <summary>
        /// 获取CS登录信息
        /// </summary>
        /// <param name="token"></param>
        /// <param name="Async"></param>
        /// <returns></returns>
        public async Task<SYS_CS_LOGIN> GetCSLoginInfo(string token, bool Async = true)
        {
            SYS_CS_LOGIN query = null;
            try
            {
                query = Db.Queryable<SYS_CS_LOGIN>().Where(x => x.TOKEN == token).First();
                if (query != null)
                {
                    //超过8小时删除登录
                    if ((DateTime.Now - query.LAST_ACCESS).Hours > 4)
                    {
                        Db.Deleteable(query).ExecuteCommand();
                        return null;
                    }
                }
            }
            catch (System.Exception ex)
            {
 
            }
            return await Task.FromResult(query);
        }
 
        /// <summary>
        /// CS登录
        /// </summary>
        /// <param name="action"></param>
        /// <param name="Async"></param>
        /// <returns></returns>
        public async Task<ApiAction<SYS_CS_LOGIN>> CSLogin(ApiAction<UserInfo> action, bool Async = true)
        {
            var res = new ApiAction<SYS_CS_LOGIN>();
            try
            {
                UserInfo userInfo = action.Data;
                var user = Db.Queryable<SYS_USER>().Where(x => x.USER_ID == userInfo.USER_CODE && x.USER_PWD == userInfo.PASSWORD).First();
                if (user != null)
                {
                    if (user.STATUS_CODE == "ENABLE")
                    {
                        MES_FACTORY _factory = Db.Queryable<MES_FACTORY>()
                                                .WhereIF(!userInfo.FTY_CODE.IsNullOrEmpty(),x => x.FTY_CODE == userInfo.FTY_CODE)
                                                .First();
 
                        SYS_CS_LOGIN logininfo = new SYS_CS_LOGIN
                        {
                            USER_ID = user.USER_ID,
                            USER_NAME = user.USER_NAME,
                            LAST_ACCESS = DateTime.Now,
                            CREATE_TIME = DateTime.Now,
                            STATUS_CODE = user.STATUS_CODE,
                            CLIENT_IP = userInfo?.ip,
                            TOKEN = userInfo.Token ?? Guid.NewGuid().ToString("N"),
                            ID = Guid.NewGuid().ToString("N"),
                            ACCOUNT_TYPE = 0,
                            ORG_CODE = userInfo.ORG_CODE,
                            PROD_CODE = _factory?.FTY_CODE,
                        };
                        var org_code=Db.Queryable<V_USER_ORG>().Where(q=>q.USER_ID == userInfo.USER_CODE && q.ORG_CODE==userInfo.ORG_CODE).First();
                        if (org_code != null)
                        {
                            logininfo.ORG_CODE = org_code.ORG_CODE;
                        }
                        else
                        {
                            var prod_code = Db.Queryable<V_USER_PROD>().Where(q => q.USER_ID == userInfo.USER_CODE && q.PROD_CODE == userInfo.PROD_CODE).First();
                            if (prod_code != null)
                            {
                                logininfo.PROD_CODE = prod_code.PROD_CODE;
                            }
                            else
                            {
                                res.IsSuccessed = false;
                                res.Data = null;
                                res.LocaleMsg = L("SYS.Login.isNotOrgCode");//用户没有该据点的权限 }
                            }
                        }
                        LOG_CS_LOGIN loginlog = new()
                        {
                            USER_ID = user.USER_ID,
                            LOGIN_FROM = $"{userInfo?.ad_info?.nation}->{userInfo?.ad_info?.province}->{userInfo?.ad_info?.city};IP:{userInfo?.ip}",
                            LOGIN_TIME = DateTime.Now,
                            ORG_CODE = userInfo.ORG_CODE,
                            PROD_CODE = _factory?.FTY_CODE,
                        };
                        //保存登录日志
                        var db = Business.Biz.Db;
                        var dbTran = db.UseTran(() =>
                        {
                            db.Insertable(loginlog).ExecuteCommand();
 
                            if (logininfo != null)
                            {
                                var deres = db.Storageable(logininfo).ExecuteCommand();
                            }
                            res.Data = logininfo;
                        });
                        if (!dbTran.IsSuccess)
                        {
                            res.IsSuccessed = false;
                            res.Data = null;
                            res.LocaleMsg = L("SYS.Login.dbTranError");
                        }
                        res.Data = logininfo;
                    }
                    else
                    {
                        res.IsSuccessed = false;
                        res.Data = null;
                        res.LocaleMsg = L("SYS.Login.dbTranError");//用户已禁用
                    }
                }
                else
                {
                    res.IsSuccessed = false;
                    res.Data = null;
                    res.LocaleMsg = L("SYS.Login.errMsg401");//用户没有权限(令牌、用户名、密码错误)!
                }
            }
            catch (Exception ex)
            {
                res.CatchExceptionWithLog(ex, "登录异常");
            }
            return await Task.FromResult(res);
        }
 
        /// <summary>
        /// CS登出
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task<ApiAction> CSLogout(string token)
        {
            var res = new ApiAction();
            try
            {
                var query = Db.Queryable<SYS_CS_LOGIN>().Where(x => x.TOKEN == token).First();
                if (query != null)
                {
                    Db.Deleteable(query).ExecuteCommand();
                    return res;
                }
            }
            catch (Exception ex)
            {
                res.CatchExceptionWithLog(ex, "注销登录异常");
            }
            return await Task.FromResult(res);
        }
    }
}