增加
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
|
||||
namespace Application.Domain.Entity
|
||||
{
|
||||
[Tenant("Kg.SeaTime.Game")]
|
||||
public class game_account
|
||||
{
|
||||
/// <summary>
|
||||
/// accId
|
||||
/// </summary>
|
||||
[SugarColumn(IsPrimaryKey = true, Length = 50)]
|
||||
public string accId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// userName
|
||||
/// </summary>
|
||||
[SugarColumn(Length = 20, IsNullable = true)]
|
||||
public string? userName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// nick
|
||||
/// </summary>
|
||||
[SugarColumn(Length = 30, IsNullable = true)]
|
||||
public string? nick { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// pwd
|
||||
/// </summary>
|
||||
[SugarColumn(Length = 255, IsNullable = true)]
|
||||
public string? pwd { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// npwd
|
||||
/// </summary>
|
||||
[SugarColumn(Length = 255, IsNullable = true)]
|
||||
public string? npwd { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// status
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public int? status { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// remCode
|
||||
/// </summary>
|
||||
[SugarColumn(Length = 255, IsNullable = true)]
|
||||
public string? remCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// remAccId
|
||||
/// </summary>
|
||||
[SugarColumn(Length = 50, IsNullable = true)]
|
||||
public string? remAccId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// token
|
||||
/// </summary>
|
||||
[SugarColumn(Length = 50, IsNullable = true)]
|
||||
public string? token { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// addTime
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public DateTime? addTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// openId
|
||||
/// </summary>
|
||||
[SugarColumn(Length = 50, IsNullable = true)]
|
||||
public string? openId { get; set; }
|
||||
}
|
||||
}
|
||||
7
Service/Application.Domain/Cache/UserCache.cs
Normal file
7
Service/Application.Domain/Cache/UserCache.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Application.Domain;
|
||||
|
||||
public class UserCache
|
||||
{
|
||||
public static string BaseCacheKey = "BaseUserCache:{0}";
|
||||
public static string BaseCacheKeys = "BaseUserCache:{0}:{1}";
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
namespace Application.Domain;
|
||||
|
||||
public interface IGameAccountService
|
||||
{
|
||||
/// <summary>
|
||||
/// 根据OpenId获取账号信息
|
||||
/// </summary>
|
||||
/// <param name="openId"></param>
|
||||
/// <returns></returns>
|
||||
Task<game_account> GetAccInfoByOpenId(string openId);
|
||||
/// <summary>
|
||||
/// 根据AccId获取账号信息
|
||||
/// </summary>
|
||||
/// <param name="accId"></param>
|
||||
/// <returns></returns>
|
||||
Task<game_account> GetAccInfoByAccId(string accId);
|
||||
|
||||
Task<game_account> GetAccInfoByToken(string token);
|
||||
/// <summary>
|
||||
/// 注册账号
|
||||
/// </summary>
|
||||
/// <param name="userName"></param>
|
||||
/// <param name="nick"></param>
|
||||
/// <param name="pwd"></param>
|
||||
/// <param name="remAccId"></param>
|
||||
/// <param name="openId"></param>
|
||||
/// <returns></returns>
|
||||
Task<game_account> Regist(string userName, string nick, string pwd, string remAccId = "",
|
||||
string openId = "");
|
||||
|
||||
Task<bool> UpdateAccountToken(string accId);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
using Photon.Core.Assist;
|
||||
|
||||
namespace Application.Domain;
|
||||
|
||||
public class GameAccountService : IGameAccountService, ITransient
|
||||
{
|
||||
private readonly ISqlSugarClient _dbClient;
|
||||
private readonly IRedisCache _redisClient;
|
||||
|
||||
public GameAccountService(ISqlSugarClient dbClient, IRedisCache redisClient)
|
||||
{
|
||||
_dbClient = dbClient;
|
||||
_redisClient = redisClient;
|
||||
}
|
||||
|
||||
|
||||
public async Task<game_account> GetAccInfoByOpenId(string openId)
|
||||
{
|
||||
string key = string.Format(UserCache.BaseCacheKeys, "UserAccountData", "OpenId");
|
||||
if (await _redisClient.HExistsHashAsync(key, openId))
|
||||
{
|
||||
return await _redisClient.GetHashAsync<game_account>(key, openId);
|
||||
}
|
||||
else
|
||||
{
|
||||
var db = _dbClient.AsTenant().GetConnectionWithAttr<game_account>();
|
||||
var data = await db.Queryable<game_account>().Where(it => it.openId == openId).FirstAsync();
|
||||
await _redisClient.AddHashAsync(key, openId, data);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<game_account> GetAccInfoByAccId(string accId)
|
||||
{
|
||||
string key = string.Format(UserCache.BaseCacheKeys, "UserAccountData", "AccId");
|
||||
if (await _redisClient.HExistsHashAsync(key, accId))
|
||||
{
|
||||
return await _redisClient.GetHashAsync<game_account>(key, accId);
|
||||
}
|
||||
else
|
||||
{
|
||||
var db = _dbClient.AsTenant().GetConnectionWithAttr<game_account>();
|
||||
var data = await db.Queryable<game_account>().Where(it => it.accId == accId).FirstAsync();
|
||||
await _redisClient.AddHashAsync(key, accId, data);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<game_account> GetAccInfoByToken(string token)
|
||||
{
|
||||
var db = _dbClient.AsTenant().GetConnectionWithAttr<game_account>();
|
||||
var data = await db.Queryable<game_account>().Where(it => it.token == token).FirstAsync();
|
||||
return data;
|
||||
}
|
||||
|
||||
private async Task ClearCache(string clearId)
|
||||
{
|
||||
string openId = string.Empty;
|
||||
string accId = string.Empty;
|
||||
var accInfo = await GetAccInfoByAccId(clearId);
|
||||
if (accInfo != null)
|
||||
{
|
||||
accId = accInfo.accId;
|
||||
openId = accInfo.openId;
|
||||
}
|
||||
else
|
||||
{
|
||||
accInfo = await GetAccInfoByOpenId(clearId);
|
||||
if (accInfo != null)
|
||||
{
|
||||
accId = accInfo.accId;
|
||||
openId = accInfo.openId;
|
||||
}
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(openId))
|
||||
{
|
||||
string key = string.Format(UserCache.BaseCacheKeys, "UserAccountData", "OpenId");
|
||||
await _redisClient.DelHashAsync(key, openId);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(accId))
|
||||
{
|
||||
string key = string.Format(UserCache.BaseCacheKeys, "UserAccountData", "AccId");
|
||||
await _redisClient.DelHashAsync(key, accId);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<game_account> Regist(string userName, string nick, string pwd, string remAccId = "",
|
||||
string openId = "")
|
||||
{
|
||||
game_account result = new game_account();
|
||||
try
|
||||
{
|
||||
var db = _dbClient.AsTenant().GetConnectionWithAttr<game_account>();
|
||||
await _dbClient.AsTenant().BeginTranAsync();
|
||||
string accId = StringAssist.NewGuid;
|
||||
result.accId = accId;
|
||||
result.userName = userName;
|
||||
result.nick = nick;
|
||||
string sid = await CreateToken();
|
||||
result.token = sid;
|
||||
result.npwd = pwd;
|
||||
result.remAccId = remAccId;
|
||||
result.remCode = StringAssist.RandomString(6);
|
||||
result.pwd = EncryptAssist.Md5Encryption(pwd);
|
||||
result.status = 1;
|
||||
result.addTime = DateTime.Now;
|
||||
result.openId = openId;
|
||||
bool isok = await db.Insertable(result).ExecuteCommandAsync() > 0;
|
||||
if (isok)
|
||||
{
|
||||
await ClearCache(openId);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
await _dbClient.AsTenant().CommitTranAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
result = null;
|
||||
await _dbClient.AsTenant().RollbackTranAsync();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private async Task<string> CreateToken()
|
||||
{
|
||||
var db = _dbClient.AsTenant().GetConnectionWithAttr<game_account>();
|
||||
string token = string.Empty;
|
||||
bool ok = true;
|
||||
while (ok)
|
||||
{
|
||||
token = string.Format("{0}", StringAssist.RandomString(32));
|
||||
if (await db.Queryable<game_account>().Where(it => it.token == token).AnyAsync() == false)
|
||||
{
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateAccountToken(string accId)
|
||||
{
|
||||
var db = _dbClient.AsTenant().GetConnectionWithAttr<game_account>();
|
||||
string token = await CreateToken();
|
||||
bool result = await db.Updateable<game_account>().SetColumns(it => it.token == token)
|
||||
.Where(it => it.accId == accId).ExecuteCommandAsync() > 0;
|
||||
if (result)
|
||||
{
|
||||
await ClearCache(accId);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
22
Service/Application.Web/Common/AutoLogin.cs
Normal file
22
Service/Application.Web/Common/AutoLogin.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Application.Web;
|
||||
|
||||
public class AutoLogin
|
||||
{
|
||||
public async Task<TwResult> TwLogin(string code)
|
||||
{
|
||||
using HttpClient client = new HttpClient();
|
||||
// 发送 GET 请求
|
||||
string url = $"https://m.twbar.cn/api/AppAuto/GetOpenInfo?code={code}";
|
||||
string responseText = await client.GetStringAsync(url);
|
||||
return JsonConvert.DeserializeObject<TwResult>(responseText);
|
||||
}
|
||||
}
|
||||
|
||||
public class TwResult
|
||||
{
|
||||
public string msg { get; set; } = "";
|
||||
public int code { get; set; }
|
||||
public object data { get; set; }
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Application.Web.Controllers.Login
|
||||
{
|
||||
@@ -9,39 +10,107 @@ namespace Application.Web.Controllers.Login
|
||||
/// </summary>
|
||||
[ApiExplorerSettings(GroupName = "Login")]
|
||||
[Route("[controller]/[action]")]
|
||||
[ApiController]
|
||||
[ApiController]
|
||||
public class LoginController : ControllerBase
|
||||
{
|
||||
private readonly IHubContext<ChatHub> _hubContext;
|
||||
public LoginController(IHubContext<ChatHub> hubContext)
|
||||
private readonly IGameAccountService _accountService;
|
||||
public LoginController(IGameAccountService accountService)
|
||||
{
|
||||
_hubContext = hubContext;
|
||||
_accountService = accountService;
|
||||
}
|
||||
/// <summary>
|
||||
/// 登录接口
|
||||
/// </summary>
|
||||
/// <param name="parms"></param>
|
||||
/// <returns></returns>
|
||||
|
||||
/// <summary>
|
||||
/// 登录接口
|
||||
/// </summary>
|
||||
/// <param name="parms"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<IPoAction> Login([FromBody] LoginParms parms)
|
||||
{
|
||||
|
||||
|
||||
return PoAction.Ok(parms.code);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试接口
|
||||
/// 探玩自动登录
|
||||
/// </summary>
|
||||
/// <param name="name">测试名</param>
|
||||
/// <param name="ttt">测试2</param>
|
||||
/// <param name="code"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IPoAction> Test(string name,string ttt)
|
||||
public async Task<IPoAction> TwLogin( string code)
|
||||
{
|
||||
await _hubContext.Clients.All.SendAsync("ReceiveMessage", "系统");
|
||||
|
||||
return PoAction.Ok(name);
|
||||
|
||||
if (string.IsNullOrEmpty(code))
|
||||
{
|
||||
return PoAction.Message("code值错误!");
|
||||
}
|
||||
AutoLogin login = new AutoLogin();
|
||||
var loginInfo = await login.TwLogin(code);
|
||||
if (loginInfo.code == 0)
|
||||
{
|
||||
dynamic twInfo = JsonConvert.DeserializeObject<dynamic>(loginInfo.data.ToString());
|
||||
string openId = twInfo.openid;
|
||||
string nick = "探玩玩家";
|
||||
var accInfo = await _accountService.GetAccInfoByOpenId(openId);
|
||||
if (accInfo == null)
|
||||
{
|
||||
var userData = await _accountService.Regist("", nick, "", "", openId);
|
||||
if (userData == null)
|
||||
{
|
||||
return PoAction.Message("登录失败,请联系客服!");
|
||||
}
|
||||
else
|
||||
{
|
||||
object ret =new { token = userData.token };
|
||||
return PoAction.Ok(ret);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (accInfo.status != 1)
|
||||
{
|
||||
return PoAction.Message("账号被冻结!");
|
||||
}
|
||||
else
|
||||
{
|
||||
object ret =new { token = accInfo.token };
|
||||
return PoAction.Ok(ret);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return PoAction.Message(loginInfo.msg);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 退出登录
|
||||
/// </summary>
|
||||
/// <param name="sid"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IPoAction> LoginOut(string? sid)
|
||||
{
|
||||
if (string.IsNullOrEmpty(sid))
|
||||
{
|
||||
return PoAction.Message("身份信息不存在!");
|
||||
}
|
||||
|
||||
var accInfo = await _accountService.GetAccInfoByToken(sid);
|
||||
if (accInfo == null)
|
||||
{
|
||||
return PoAction.Message("身份信息不存在!");
|
||||
}
|
||||
|
||||
if (await _accountService.UpdateAccountToken(accInfo.accId))
|
||||
{
|
||||
return PoAction.Ok(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
return PoAction.Message("退出失败,请稍后尝试!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user