111
This commit is contained in:
@@ -5,6 +5,9 @@ public interface IUnitUserService
|
||||
#region 用户信息
|
||||
|
||||
Task<List<unit_user>> GetUserDataByAccId(string accId);
|
||||
Task<unit_user> GetUserInfoByUserId(string userId);
|
||||
Task<unit_user> GetUserInfoByUserNo(string userNo);
|
||||
Task<unit_user> GetUserInfoByToken(string token);
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -18,5 +21,7 @@ public interface IUnitUserService
|
||||
/// <returns></returns>
|
||||
Task<unit_user> Register(int areaId, string accId);
|
||||
|
||||
Task<bool> RegisterUserInfo(string userId, string nick, string sex);
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -19,6 +19,22 @@ public class UnitUserService : IUnitUserService, ITransient
|
||||
var db = _dbClient.AsTenant().GetConnectionWithAttr<unit_user>();
|
||||
return await db.Queryable<unit_user>().Where(it => it.accId == accId).ToListAsync();
|
||||
}
|
||||
public async Task<unit_user> GetUserInfoByUserId(string userId)
|
||||
{
|
||||
string key = string.Format(UserCache.BaseCacheKeys, "UserInfo", "UserId");
|
||||
var data = await _redisClient.GetHashAsync<unit_user>(key, userId);
|
||||
if (data == null)
|
||||
{
|
||||
var db = _dbClient.AsTenant().GetConnectionWithAttr<unit_user>();
|
||||
data = await db.Queryable<unit_user>().Where(it => it.userId == userId).SingleAsync();
|
||||
if (data != null)
|
||||
{
|
||||
await _redisClient.AddHashAsync(key, userId, data);
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
public async Task<unit_user> GetUserInfoByUserNo(string userNo)
|
||||
{
|
||||
string key = string.Format(UserCache.BaseCacheKeys, "UserInfo", "UserNo");
|
||||
@@ -36,9 +52,9 @@ public class UnitUserService : IUnitUserService, ITransient
|
||||
return data;
|
||||
}
|
||||
|
||||
public async Task<unit_user> GetUserInfoBySid(string token)
|
||||
public async Task<unit_user> GetUserInfoByToken(string token)
|
||||
{
|
||||
string key = string.Format(UserCache.BaseCacheKeys, "UserInfo", "Sid");
|
||||
string key = string.Format(UserCache.BaseCacheKeys, "UserInfo", "token");
|
||||
var data = await _redisClient.GetHashAsync<unit_user>(key, token);
|
||||
if (data == null)
|
||||
{
|
||||
@@ -52,6 +68,23 @@ public class UnitUserService : IUnitUserService, ITransient
|
||||
|
||||
return data;
|
||||
}
|
||||
private async Task ClearUserInfo(int type, string id)
|
||||
{
|
||||
unit_user result = new unit_user();
|
||||
switch (type)
|
||||
{
|
||||
case 0:
|
||||
result = await GetUserInfoByUserId(id);
|
||||
break;
|
||||
|
||||
case 1:
|
||||
result = await GetUserInfoByToken(id);
|
||||
break;
|
||||
}
|
||||
await _redisClient.DelHashAsync(string.Format(UserCache.BaseCacheKeys, "UserInfo","UserId"), result.userId);
|
||||
await _redisClient.DelHashAsync(string.Format(UserCache.BaseCacheKeys, "UserInfo","UserNo"), result.userNo);
|
||||
await _redisClient.DelHashAsync(string.Format(UserCache.BaseCacheKeys, "UserInfo","Sid"), result.token);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -98,8 +131,27 @@ public class UnitUserService : IUnitUserService, ITransient
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<int> GetUserNo()
|
||||
public async Task<bool> RegisterUserInfo(string userId, string nick, string sex)
|
||||
{
|
||||
var db = _dbClient.AsTenant().GetConnectionWithAttr<unit_user>();
|
||||
bool result = await db.Updateable<unit_user>().SetColumns(it => it.nick == nick)
|
||||
.SetColumns(it => it.sex == sex)
|
||||
.SetColumns(it => it.regOk == 1)
|
||||
.Where(it => it.userId == userId).ExecuteCommandAsync() > 0;
|
||||
if (result)
|
||||
{
|
||||
//注册配置信息
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await ClearUserInfo(0, userId);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
private async Task<int> GetUserNo()
|
||||
{
|
||||
int No = 0;
|
||||
bool ok = true;
|
||||
@@ -117,18 +169,18 @@ public class UnitUserService : IUnitUserService, ITransient
|
||||
|
||||
private async Task<string> GetToken()
|
||||
{
|
||||
string sid = string.Empty;
|
||||
string token = string.Empty;
|
||||
bool ok = true;
|
||||
while (ok)
|
||||
{
|
||||
sid = StringAssist.RandomString(32);
|
||||
if (await GetUserInfoBySid(sid) == null)
|
||||
token = StringAssist.RandomString(32);
|
||||
if (await GetUserInfoByToken(token) == null)
|
||||
{
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
|
||||
return sid;
|
||||
return token;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -138,6 +138,11 @@ namespace Application.Web.Controllers.Login
|
||||
return PoAction.Message("未登录账号!");
|
||||
}
|
||||
|
||||
if (accInfo.status != 1)
|
||||
{
|
||||
return PoAction.Message("账号已冻结!");
|
||||
}
|
||||
|
||||
var areaInfo = await _areaService.GetAreaInfo(area);
|
||||
if (areaInfo == null)
|
||||
{
|
||||
@@ -172,5 +177,94 @@ namespace Application.Web.Controllers.Login
|
||||
string token = JwtHelper.CreateToken(Key, Issuer, Audience, loadData, 300);
|
||||
return PoAction.Ok(new { token = token, refToken = userInfo.token, userId = userInfo.userId });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 登录到游戏
|
||||
/// </summary>
|
||||
/// <param name="sid"></param>
|
||||
/// <param name="user"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IPoAction> LoginGame(string sid, string user)
|
||||
{
|
||||
if (string.IsNullOrEmpty(sid))
|
||||
{
|
||||
return PoAction.Message("未登录账号!");
|
||||
}
|
||||
|
||||
var accInfo = await _accountService.GetAccInfoByToken(sid);
|
||||
if (accInfo == null)
|
||||
{
|
||||
return PoAction.Message("未登录账号!");
|
||||
}
|
||||
|
||||
if (accInfo.status != 1)
|
||||
{
|
||||
return PoAction.Message("账号已冻结!");
|
||||
}
|
||||
|
||||
var userInfo = await _userService.GetUserInfoByUserId(user);
|
||||
if (userInfo == null)
|
||||
{
|
||||
return PoAction.Message("角色不存在!");
|
||||
}
|
||||
|
||||
if (userInfo.accId != accInfo.accId)
|
||||
{
|
||||
return PoAction.Message("角色不存在!");
|
||||
}
|
||||
|
||||
if (userInfo.status == 0)
|
||||
{
|
||||
return PoAction.Message("角色已封禁!");
|
||||
}
|
||||
|
||||
Dictionary<string, object> loadData = new Dictionary<string, object>();
|
||||
loadData.Add("userId", userInfo.userId);
|
||||
loadData.Add("accId", userInfo.accId);
|
||||
|
||||
string Key = App.Configuration["JwtTokenOptions:SecurityKey"].ToString();
|
||||
string Issuer = App.Configuration["JwtTokenOptions:Issuer"].ToString();
|
||||
string Audience = App.Configuration["JwtTokenOptions:Audience"].ToString();
|
||||
string token = JwtHelper.CreateToken(Key, Issuer, Audience, loadData, 1);
|
||||
return PoAction.Ok(new
|
||||
{ regOk = userInfo.regOk, token = token, refToken = userInfo.token, userId = userInfo.userId });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注册角色信息
|
||||
/// </summary>
|
||||
/// <param name="parms"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
[Authorize]
|
||||
public async Task<IPoAction> RegisterInfo([FromBody] RegisterInfoParms parms)
|
||||
{
|
||||
if (string.IsNullOrEmpty(parms.nick))
|
||||
{
|
||||
return PoAction.Message("昵称不能为空!");
|
||||
}
|
||||
|
||||
if (!RegExpAssist.RegStringLeght(parms.nick, 1, 12))
|
||||
{
|
||||
return PoAction.Message("昵称需要1-12字符之间哦!");
|
||||
}
|
||||
string sex = parms.sex == 0 ? "女" : "男";
|
||||
string userId = StateHelper.userId;
|
||||
var userInfo = await _userService.GetUserInfoByUserId(userId);
|
||||
if (userInfo.regOk == 1)
|
||||
{
|
||||
return PoAction.Ok(true);
|
||||
}
|
||||
|
||||
if (await _userService.RegisterUserInfo(userId, parms.nick, sex))
|
||||
{
|
||||
return PoAction.Ok(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
return PoAction.Message("注册失败,请稍后尝试!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -86,5 +86,26 @@ namespace Application.Web.Controllers.Pub
|
||||
data.sign = data.sign.Replace("[Br]", "<br />");
|
||||
return PoAction.Ok(data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户游戏角色
|
||||
/// </summary>
|
||||
/// <param name="sid"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IPoAction> GetMyGame(string? sid)
|
||||
{
|
||||
List<unit_user> userData = new List<unit_user>();
|
||||
if (!string.IsNullOrEmpty(sid))
|
||||
{
|
||||
var account = await _accountService.GetAccInfoByToken(sid);
|
||||
if (account != null)
|
||||
{
|
||||
userData = await _userService.GetUserDataByAccId(account.accId);
|
||||
}
|
||||
}
|
||||
return PoAction.Ok(userData);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Application.Web;
|
||||
|
||||
public class RegisterInfoParms
|
||||
{
|
||||
public string nick { get; set; } = string.Empty;
|
||||
public int sex { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user