123 lines
4.6 KiB
C#
Executable File
123 lines
4.6 KiB
C#
Executable File
using GXWebApi.Common;
|
|
using log4net;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Web.Http;
|
|
|
|
namespace GXWebApi.Controllers
|
|
{
|
|
public class AuthController : ApiController
|
|
{
|
|
private static readonly ILog Logger = LogManager.GetLogger(typeof(AuthController));
|
|
|
|
/// <summary>
|
|
/// 登录验证
|
|
/// </summary>
|
|
[HttpPost]
|
|
public IHttpActionResult Login([FromBody] JObject obj)
|
|
{
|
|
try
|
|
{
|
|
string userId = obj["userId"]?.ToString() ?? "";
|
|
string password = obj["password"]?.ToString() ?? "";
|
|
|
|
if (string.IsNullOrEmpty(userId))
|
|
return Ok(new { success = false, message = "用户名不能为空" });
|
|
|
|
DBHelpe db = new DBHelpe();
|
|
DataTable dt = db.ExecuteTable($@"
|
|
SELECT S.USER_ID, S.USER_NAME, S.GX_PASSWORD
|
|
FROM SYS_LOGINUSER S
|
|
WHERE S.USER_ID = '{userId.Replace("'", "''")}'");
|
|
|
|
if (dt.Rows.Count == 0)
|
|
return Ok(new { success = false, message = "用户不存在" });
|
|
|
|
DataRow row = dt.Rows[0];
|
|
string dbPassword = row["GX_PASSWORD"].ToStringEx();
|
|
string userName = row["USER_NAME"].ToStringEx();
|
|
|
|
if (dbPassword != password)
|
|
return Ok(new { success = false, message = "密码错误" });
|
|
|
|
// 查询角色
|
|
DataTable dtRole = db.ExecuteTable($@"
|
|
SELECT S2.FROLENAME, S2.FROLENUMBER
|
|
FROM SYS_USERROLERELATION S1
|
|
LEFT JOIN SYS_USERROLE S2 ON S1.FROLEID = S2.FROLENUMBER
|
|
WHERE S1.USER_ID = '{userId.Replace("'", "''")}'");
|
|
|
|
List<string> roles = new List<string>();
|
|
List<Dictionary<string, object>> roleList = new List<Dictionary<string, object>>();
|
|
foreach (DataRow r in dtRole.Rows)
|
|
{
|
|
string roleNo = r["FROLENUMBER"].ToStringEx();
|
|
string roleName = r["FROLENAME"].ToStringEx();
|
|
roles.Add(roleNo);
|
|
roleList.Add(new Dictionary<string, object>
|
|
{
|
|
["roleNumber"] = roleNo,
|
|
["roleName"] = roleName
|
|
});
|
|
}
|
|
|
|
return Ok(new
|
|
{
|
|
success = true,
|
|
message = "登录成功",
|
|
data = new
|
|
{
|
|
userId = userId,
|
|
userName = userName,
|
|
roles = roleList,
|
|
roleNumbers = roles
|
|
}
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Error("登录异常:", ex);
|
|
return Ok(new { success = false, message = "登录异常:" + ex.Message });
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改密码
|
|
/// </summary>
|
|
[HttpPost]
|
|
public IHttpActionResult ChangePassword([FromBody] JObject obj)
|
|
{
|
|
try
|
|
{
|
|
string userId = obj["userId"]?.ToString() ?? "";
|
|
string oldPwd = obj["oldPassword"]?.ToString() ?? "";
|
|
string newPwd = obj["newPassword"]?.ToString() ?? "";
|
|
|
|
if (string.IsNullOrEmpty(userId))
|
|
return Ok(new { success = false, message = "用户名不能为空" });
|
|
|
|
DBHelpe db = new DBHelpe();
|
|
DataTable dt = db.ExecuteTable($"SELECT GX_PASSWORD FROM SYS_LOGINUSER WHERE USER_ID = '{userId.Replace("'", "''")}'");
|
|
|
|
if (dt.Rows.Count == 0)
|
|
return Ok(new { success = false, message = "用户不存在" });
|
|
|
|
string dbPwd = dt.Rows[0]["GX_PASSWORD"].ToStringEx();
|
|
if (dbPwd != oldPwd)
|
|
return Ok(new { success = false, message = "原密码错误" });
|
|
|
|
db.ExecuteNonQuery($"UPDATE SYS_LOGINUSER SET GX_PASSWORD = '{newPwd.Replace("'", "''")}' WHERE USER_ID = '{userId.Replace("'", "''")}'");
|
|
|
|
return Ok(new { success = true, message = "密码修改成功" });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Error("修改密码异常:", ex);
|
|
return Ok(new { success = false, message = "修改密码异常:" + ex.Message });
|
|
}
|
|
}
|
|
}
|
|
}
|