winform+dev的前后台分离标准项目
lg
2024-08-29 f9f3b38bf70e71f8181df4a064d1e210e2b53545
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
using Gs.Toolbox;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using Gs.User.Modes;
using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Security.Principal;
using System.Net;
 
namespace Gs.User.Service
{
 
    [ApiGroup(ApiGroupNames.Auth)]
    public class UserController : IRomteService
    {
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [RequestMethod(RequestMethods.POST)]
        public ReturnDto<System.Dynamic.ExpandoObject> UserLogin([FromBody] UserLogin model)
        {
            string accountPwd = model.accountPwd;
            string accountNo = model.accountNo;
            Guid orgGuid = model.orgGuid;
            string strPass = "";
            DataSet dset = new DataSet();
            dynamic m = new System.Dynamic.ExpandoObject();
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.strConn))
            {
                using (SqlCommand cmd = new SqlCommand("[prc_user_login]", conn))
                {
                    try
                    {
                        conn.Open();
                        cmd.CommandType = CommandType.StoredProcedure;
                        SqlParameter[] parameters = new SqlParameter[] {
                                new SqlParameter("@accountNo",accountNo),
                                new SqlParameter("@accountPwd",strPass),
                                new SqlParameter("@orgGuid",orgGuid),
                            };
                        foreach (SqlParameter parameter in parameters)
                        {
                            cmd.Parameters.Add(parameter);
                        }
                        using (SqlDataAdapter dt = new SqlDataAdapter(cmd))
                        {
                            dt.Fill(dset, "0");
                        }
                        if (dset != null && dset.Tables.Count > 0 && dset.Tables[0].Rows.Count > 0)
                        {
                            System.Data.DataRow row = dset.Tables[0].Rows[0];
                            m.loginGuid = Guid.Parse(row["loginGuid"].ToString());
                            m.loginOrgGuid = row["loginOrgGuid"].ToString();
                            return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Success, "登录成功!");
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Debug(this.ToString(), "UserLogin error:" + ex.Message);
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
            return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Exception, "登录失败!");
        }
 
        /// <summary>
        /// 读取用户登录信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [RequestMethod(RequestMethods.POST)]
        public ReturnDto<System.Dynamic.ExpandoObject> GetUserLoginInfo([FromBody] UserLogin model)
        {
            Guid userGuid = model.userGuid;
            DataSet dset = new DataSet();
            dynamic m = new System.Dynamic.ExpandoObject();
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.strConn))
            {
                using (SqlCommand cmd = new SqlCommand("[prc_user_info]", conn))
                {
                    try
                    {
                        conn.Open();
                        cmd.CommandType = CommandType.StoredProcedure;
                        SqlParameter[] parameters = new SqlParameter[] {
                                new SqlParameter("@userGuid",userGuid),
                            };
                        foreach (SqlParameter parameter in parameters)
                        {
                            cmd.Parameters.Add(parameter);
                        }
                        using (SqlDataAdapter dt = new SqlDataAdapter(cmd))
                        {
                            dt.Fill(dset, "0");
                        }
                        if (dset != null && dset.Tables.Count > 0 && dset.Tables[0].Rows.Count > 0)
                        {
                            System.Data.DataRow row = dset.Tables[0].Rows[0];
                            m.loginGuid = Guid.Parse(row["loginGuid"].ToString());
                            m.loginOrgGuid = row["loginOrgGuid"].ToString();
                            m.list = new List<MenuAction>();
                            if (dset.Tables.Count > 1 && dset.Tables[1].Rows.Count > 1)
                            {
                                foreach (DataRow dr in dset.Tables[1].Rows)
                                {
                                    m.list.Add(
                                        new MenuAction()
                                        {
                                            guid = Guid.Parse(dr["rightGuid"].ToString()),
                                            upGuid = dr["upGuid"].ToString().Length > 0 ? Guid.Parse(dr["upGuid"].ToString()) : null,
                                            name = dr["name"].ToString(),
                                            icon = dr["icon"].ToString(),
                                            formPath = dr["formPath"].ToString(),
                                            category = int.Parse(dr["category"].ToString()),
                                        }
                                    );
                                }
                                return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Success, "登录成功!");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Debug(this.ToString(), "GetUserInfo error:" + ex.Message);
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
            return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Exception, "登录失败!");
        }
 
 
        #region
        /// <summary>
        /// 读取列表,支持分页
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [RequestMethod(RequestMethods.POST)]
        public ReturnDto<PageList<dynamic>> GetListPage([FromBody] PageQuery model)
        {
            int currentPage = model.currentPage;
            int everyPageSize = model.everyPageSize;
            string sortName = string.IsNullOrEmpty(model.sortName) ? "a.name" : model.sortName;
            System.Text.StringBuilder sbSql = new StringBuilder();
            sbSql.Append("select * from ");
            sbSql.Append("( ");
            sbSql.Append("select top 100000 ROW_NUMBER() over(order by " + sortName + " " + model.sortOrder + ") as rowIndex,* from sys_User a where 1=1" + model.keyWhere);
            sbSql.Append(") as T ");
            sbSql.Append(" where T.rowindex>(" + currentPage + "-1)*" + everyPageSize + " and  T.rowindex<=" + currentPage + "*" + everyPageSize + "");
            sbSql.Append(" select count(1) as intTotal  from sys_User a where 1=1 " + model.keyWhere).ToString();
            DataSet dset = new DataSet();
            try
            {
                dset = Gs.Toolbox.DbHelperSQL.Query(sbSql.ToString());
            }
            catch (Exception ex)
            {
                Gs.Toolbox.LogHelper.Debug(this.ToString(), "GetListPage error:" + ex.Message);
                return ReturnDto<PageList<dynamic>>.QuickReturn(default(PageList<dynamic>), ReturnCode.Exception, "读取失败");
            }
            PageList<dynamic> _pglist = new PageList<dynamic>
            {
                total = 0,
                everyPageSize = 0,
                pages = 0,
                list = new List<dynamic>()
            };
            if (dset != null && dset.Tables.Count > 0 && dset.Tables[0].Rows.Count > 0)//有数据
            {
                int intTotal = int.Parse(dset.Tables[1].Rows[0]["intTotal"].ToString());
                int pages = (intTotal % everyPageSize != 0) ? (intTotal / everyPageSize + 1) : (intTotal / everyPageSize);
                _pglist.total = intTotal;
                _pglist.everyPageSize = everyPageSize;
                _pglist.pages = pages;
                foreach (DataRow row in dset.Tables[0].Rows)
                {
                    Sys_User _model = new Sys_User();
                    if (row["guid"] != null && row["guid"].ToString() != "")
                    {
                        _model.guid = new Guid(row["guid"].ToString());
                    }
                    if (row["account"] != null)
                    {
                        _model.account = row["account"].ToString();
                    }
                    if (row["password"] != null && row["password"].ToString() != "")
                    {
                        _model.password = new Guid(row["password"].ToString());
                    }
                    if (row["userName"] != null)
                    {
                        _model.userName = row["userName"].ToString();
                    }
                    if (row["address"] != null)
                    {
                        _model.address = row["address"].ToString();
                    }
                    if (row["tel"] != null)
                    {
                        _model.tel = row["tel"].ToString();
                    }
                    if (row["email"] != null)
                    {
                        _model.email = row["email"].ToString();
                    }
                    if (row["lastLoginTime"] != null && row["lastLoginTime"].ToString() != "")
                    {
                        _model.lastLoginTime = DateTime.Parse(row["lastLoginTime"].ToString());
                    }
                    if (row["lastLogoutTime"] != null && row["lastLogoutTime"].ToString() != "")
                    {
                        _model.lastLogoutTime = DateTime.Parse(row["lastLogoutTime"].ToString());
                    }
                    if (row["isLocked"] != null && row["isLocked"].ToString() != "")
                    {
                        _model.isLocked = int.Parse(row["isLocked"].ToString());
                    }
                    if (row["createTime"] != null && row["createTime"].ToString() != "")
                    {
                        _model.createTime = DateTime.Parse(row["createTime"].ToString());
                    }
                    if (row["flagAdmin"] != null && row["flagAdmin"].ToString() != "")
                    {
                        _model.flagAdmin = int.Parse(row["flagAdmin"].ToString());
                    }
                    if (row["flagOnline"] != null && row["flagOnline"].ToString() != "")
                    {
                        _model.flagOnline = int.Parse(row["flagOnline"].ToString());
                    }
                    if (row["loginCounter"] != null && row["loginCounter"].ToString() != "")
                    {
                        _model.loginCounter = int.Parse(row["loginCounter"].ToString());
                    }
                    if (row["workerID"] != null)
                    {
                        _model.workerID = row["workerID"].ToString();
                    }
                    if (row["remark"] != null)
                    {
                        _model.remark = row["remark"].ToString();
                    }
                    if (row["departGuid"] != null && row["departGuid"].ToString() != "")
                    {
                        _model.departGuid = new Guid(row["departGuid"].ToString());
                    }
                    if (row["isSys"] != null && row["isSys"].ToString() != "")
                    {
                        _model.isSys = int.Parse(row["isSys"].ToString());
                    }
                    _pglist.list.Add(_model);
                }
            }
            return ReturnDto<PageList<dynamic>>.QuickReturn(_pglist, ReturnCode.Success, "读取成功");
        }
 
 
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [RequestMethod(RequestMethods.POST)]
        public ReturnDto<int?> DeleteModel([FromBody] string guidList)
        {
            int rtnInt = (int)ReturnCode.Default;
            int cont = 0;
            try
            {
                //是否内置
                cont = int.Parse(DbHelperSQL.GetSingle("select count(1) from sys_User where   [isSys]=1 and guid in split('" + guidList + "',',')").ToString());
                if (cont > 0)
                {
                    return ReturnDto<int>.QuickReturn(default(int?), ReturnCode.Exception, "删除失败,该条目为系统内置,不可删除!");
                }
                StringBuilder strSql = new StringBuilder();
                strSql.Append("delete from sys_User ");
                strSql.Append(" where guid in (" + guidList + ")  ");
                int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
                rtnInt = rows;
            }
            catch (Exception ex)
            {
                LogHelper.Debug(this.ToString(), "DeleteModel error:" + ex.Message);
                rtnInt = (int)ReturnCode.Exception;
            }
            if (rtnInt > 0)
                return ReturnDto<int>.QuickReturn(default(int?), ReturnCode.Success, "操作成功,共删除" + rtnInt.ToString() + "条数据!");
            else
                return ReturnDto<int>.QuickReturn(default(int?), ReturnCode.Exception, "删除失败,请重试!");
        }
 
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [RequestMethod(RequestMethods.POST)]
        public ReturnDto<int?> EditModel([FromBody] Sys_User model)
        {
            Guid? guid = model.guid;
            string strPass = Guid.NewGuid().ToString();
            int? rtnInt = (int)ReturnCode.Default;
            if (guid == null)
            {
                int cont = 0;
                cont = int.Parse(DbHelperSQL.GetSingle("select count(1) from sys_User where account='" + guid.ToString() + "'").ToString());
                if (cont > 0)
                {
                    return ReturnDto<int>.QuickReturn(default(int?), ReturnCode.Exception, "增加失败,该账号已存在!");
                }
            }
            StringBuilder strSql = new StringBuilder();
            if (guid != null)
            {
                strSql.Append(" UPDATE [dbo].[sys_User]");
                strSql.Append(" SET [userName] = @userName ,[address] =@address ,[tel] = @tel,[email] = @email ,[isLocked] =@isLocked,[createTime] = getdate() ,[flagAdmin] =@flagAdmin,[workerID] =@workerID ,[remark] = @remark,[departGuid] = @departGuid ");
                strSql.Append(" where guid='" + guid + "'");
            }
            else
            {
                guid = Guid.NewGuid();
                strSql.Append("insert into sys_User");
                strSql.Append(" ([guid],[account],[password],[userName],[address],[tel],[email],[isLocked],[createTime],[flagAdmin],[flagOnline],[loginCounter],[workerID],[remark],[departGuid])");
                strSql.Append(" values (");
                strSql.Append("'" + guid + "',@account,'" + strPass + "',@userName,@address,@tel,@email,@isLocked,getdate(),@flagAdmin,0,0 ,@workerID,@remark,@departGuid)");
            }
            SqlParameter[] parameters = {
             new SqlParameter("@account", model.account),
             new SqlParameter("@userName",model.userName),
             new SqlParameter("@address",model.address),
             new SqlParameter("@tel",model.tel),
             new SqlParameter("@email",model.email),
             new SqlParameter("@isLocked",model.isLocked),
             new SqlParameter("@flagAdmin",model.flagAdmin),
             new SqlParameter("@workerID",model.workerID),
             new SqlParameter("@remark",model.remark),
             new SqlParameter("@departGuid",model.departGuid),
         };
            try
            {
                rtnInt = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            }
            catch (Exception ex)
            {
                LogHelper.Debug(this.ToString(), "EditModel error:" + ex.Message);
                rtnInt = (int)ReturnCode.Exception;
            }
            if (rtnInt > 0)
                return ReturnDto<int>.QuickReturn(rtnInt, ReturnCode.Success, "操作成功!");
            else
                return ReturnDto<int>.QuickReturn(rtnInt, ReturnCode.Exception, "增加失败,请重试!");
        }
 
        /// <summary>
        /// 读取
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        [RequestMethod(RequestMethods.POST)]
        [AllowAnonymous]
        public ReturnDto<Sys_User> GetModel([FromBody] Sys_User model)
        {
            Sys_User _model = new Sys_User();
            System.Text.StringBuilder sbSql = new StringBuilder();
            sbSql.Append("select top 1 * from sys_Organization where 1=1 and guid='" + model.guid.ToString() + "' ");
            try
            {
                DataSet dset = new DataSet();
                dset = DbHelperSQL.Query(sbSql.ToString());
                if (dset != null && dset.Tables.Count > 0 && dset.Tables[0].Rows.Count > 0)
                {
                    System.Data.DataRow row = dset.Tables[0].Rows[0];
                    if (row["guid"] != null && row["guid"].ToString() != "")
                    {
                        _model.guid = new Guid(row["guid"].ToString());
                    }
                    if (row["account"] != null)
                    {
                        _model.account = row["account"].ToString();
                    }
                    if (row["password"] != null && row["password"].ToString() != "")
                    {
                        _model.password = new Guid(row["password"].ToString());
                    }
                    if (row["userName"] != null)
                    {
                        _model.userName = row["userName"].ToString();
                    }
                    if (row["address"] != null)
                    {
                        _model.address = row["address"].ToString();
                    }
                    if (row["tel"] != null)
                    {
                        _model.tel = row["tel"].ToString();
                    }
                    if (row["email"] != null)
                    {
                        _model.email = row["email"].ToString();
                    }
                    if (row["lastLoginTime"] != null && row["lastLoginTime"].ToString() != "")
                    {
                        _model.lastLoginTime = DateTime.Parse(row["lastLoginTime"].ToString());
                    }
                    if (row["lastLogoutTime"] != null && row["lastLogoutTime"].ToString() != "")
                    {
                        _model.lastLogoutTime = DateTime.Parse(row["lastLogoutTime"].ToString());
                    }
                    if (row["isLocked"] != null && row["isLocked"].ToString() != "")
                    {
                        _model.isLocked = int.Parse(row["isLocked"].ToString());
                    }
                    if (row["createTime"] != null && row["createTime"].ToString() != "")
                    {
                        _model.createTime = DateTime.Parse(row["createTime"].ToString());
                    }
                    if (row["flagAdmin"] != null && row["flagAdmin"].ToString() != "")
                    {
                        _model.flagAdmin = int.Parse(row["flagAdmin"].ToString());
                    }
                    if (row["flagOnline"] != null && row["flagOnline"].ToString() != "")
                    {
                        _model.flagOnline = int.Parse(row["flagOnline"].ToString());
                    }
                    if (row["loginCounter"] != null && row["loginCounter"].ToString() != "")
                    {
                        _model.loginCounter = int.Parse(row["loginCounter"].ToString());
                    }
                    if (row["workerID"] != null)
                    {
                        _model.workerID = row["workerID"].ToString();
                    }
                    if (row["remark"] != null)
                    {
                        _model.remark = row["remark"].ToString();
                    }
                    if (row["departGuid"] != null && row["departGuid"].ToString() != "")
                    {
                        _model.departGuid = new Guid(row["departGuid"].ToString());
                    }
                    if (row["isSys"] != null && row["isSys"].ToString() != "")
                    {
                        _model.isSys = int.Parse(row["isSys"].ToString());
                    }
                    return ReturnDto<Sys_User>.QuickReturn(_model, ReturnCode.Success, "读取成功!");
                }
                else
                    return ReturnDto<Sys_User>.QuickReturn(_model, ReturnCode.Default, "读取失败!");
            }
            catch (Exception ex)
            {
                LogHelper.Debug(this.ToString(), "GetModel error:" + ex.Message);
                return ReturnDto<Sys_User>.QuickReturn(_model, ReturnCode.Default, "读取失败!");
            }
        }
        #endregion
    }
}