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
|
}
|
}
|