11
This commit is contained in:
@@ -10,4 +10,8 @@
|
||||
<ProjectReference Include="..\Application.Service.Pub\Application.Service.Pub.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="game\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
93
Service/Application.Domain.Entity/game/user/unit_user.cs
Normal file
93
Service/Application.Domain.Entity/game/user/unit_user.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
|
||||
namespace Application.Domain.Entity
|
||||
{
|
||||
[Tenant("Kg.SeaTime.Game")]
|
||||
public class unit_user
|
||||
{
|
||||
/// <summary>
|
||||
/// userId
|
||||
/// </summary>
|
||||
[SugarColumn(IsPrimaryKey = true, Length = 50)]
|
||||
public string userId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 账号ID
|
||||
/// </summary>
|
||||
[SugarColumn(Length = 50, IsNullable = true)]
|
||||
public string? accId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 区服
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public int? areaId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// userNo
|
||||
/// </summary>
|
||||
[SugarColumn(Length = 12, IsNullable = false)]
|
||||
public string userNo { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 名称
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public string? nick { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 头像
|
||||
/// </summary>
|
||||
[SugarColumn(Length = 255, IsNullable = true)]
|
||||
public string? headImg { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 性别
|
||||
/// </summary>
|
||||
[SugarColumn(Length = 50, IsNullable = true)]
|
||||
public string? sex { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 简介
|
||||
/// </summary>
|
||||
[SugarColumn(Length = 50, IsNullable = true)]
|
||||
public string? sign { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 状态
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public int? status { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 注册状态
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public int? regOk { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// token
|
||||
/// </summary>
|
||||
[SugarColumn(Length = 50, IsNullable = true)]
|
||||
public string? token { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否系统账号
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public int? isSystem { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 创建时间
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public DateTime? addTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 最后更新时间
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public DateTime? upTime { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -4,4 +4,5 @@ public interface INoticeService
|
||||
{
|
||||
Task<List<game_notice>> GetNoticeDataByTake(int take);
|
||||
Task<List<game_notice>> GetNoticeData(int page, int limit, RefAsync<int> total);
|
||||
Task<game_notice> GetNoticeInfo(string id);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace Application.Domain;
|
||||
|
||||
public interface IUnitUserService
|
||||
{
|
||||
#region 用户信息
|
||||
|
||||
Task<List<unit_user>> GetUserDataByAccId(string accId);
|
||||
|
||||
#endregion
|
||||
|
||||
#region 用户注册
|
||||
|
||||
/// <summary>
|
||||
/// 注册游戏角色
|
||||
/// </summary>
|
||||
/// <param name="areaId"></param>
|
||||
/// <param name="accId"></param>
|
||||
/// <returns></returns>
|
||||
Task<unit_user> Register(int areaId, string accId);
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -22,4 +22,10 @@ public class NoticeService:INoticeService,ITransient
|
||||
var db = _dbClient.AsTenant().GetConnectionWithAttr<game_notice>();
|
||||
return await db.Queryable<game_notice>().OrderByDescending(it=>it.addTime).ToPageListAsync(page, limit, total);
|
||||
}
|
||||
|
||||
public async Task<game_notice> GetNoticeInfo(string id)
|
||||
{
|
||||
var db = _dbClient.AsTenant().GetConnectionWithAttr<game_notice>();
|
||||
return await db.Queryable<game_notice>().Where(it => it.noticeId == id).SingleAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
using Photon.Core.Assist;
|
||||
|
||||
namespace Application.Domain;
|
||||
|
||||
public class UnitUserService : IUnitUserService, ITransient
|
||||
{
|
||||
private readonly ISqlSugarClient _dbClient;
|
||||
private readonly IRedisCache _redisClient;
|
||||
|
||||
public UnitUserService(ISqlSugarClient dbClient, IRedisCache redisClient)
|
||||
{
|
||||
_dbClient = dbClient;
|
||||
_redisClient = redisClient;
|
||||
}
|
||||
|
||||
#region 用户信息
|
||||
public async Task<List<unit_user>> GetUserDataByAccId(string accId)
|
||||
{
|
||||
var db = _dbClient.AsTenant().GetConnectionWithAttr<unit_user>();
|
||||
return await db.Queryable<unit_user>().Where(it => it.accId == accId).ToListAsync();
|
||||
}
|
||||
public async Task<unit_user> GetUserInfoByUserNo(string userNo)
|
||||
{
|
||||
string key = string.Format(UserCache.BaseCacheKeys, "UserInfo", "UserNo");
|
||||
var data = await _redisClient.GetHashAsync<unit_user>(key, userNo);
|
||||
if (data == null)
|
||||
{
|
||||
var db = _dbClient.AsTenant().GetConnectionWithAttr<unit_user>();
|
||||
data = await db.Queryable<unit_user>().Where(it => it.userNo == userNo).SingleAsync();
|
||||
if (data != null)
|
||||
{
|
||||
await _redisClient.AddHashAsync(key, userNo, data);
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
public async Task<unit_user> GetUserInfoBySid(string token)
|
||||
{
|
||||
string key = string.Format(UserCache.BaseCacheKeys, "UserInfo", "Sid");
|
||||
var data = await _redisClient.GetHashAsync<unit_user>(key, token);
|
||||
if (data == null)
|
||||
{
|
||||
var db = _dbClient.AsTenant().GetConnectionWithAttr<unit_user>();
|
||||
data = await db.Queryable<unit_user>().Where(it => it.token == token).SingleAsync();
|
||||
if (data != null)
|
||||
{
|
||||
await _redisClient.AddHashAsync(key, token, data);
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 用户注册
|
||||
|
||||
|
||||
|
||||
public async Task<unit_user> Register(int areaId, string accId)
|
||||
{
|
||||
unit_user result = new unit_user();
|
||||
try
|
||||
{
|
||||
var db = _dbClient.AsTenant().GetConnectionWithAttr<unit_user>();
|
||||
await _dbClient.AsTenant().BeginTranAsync();
|
||||
string userId = StringAssist.NewGuid;
|
||||
result.userId = userId;
|
||||
result.areaId = areaId;
|
||||
result.accId = accId;
|
||||
result.nick = "四海虾米";
|
||||
result.headImg = "";
|
||||
int no = await GetUserNo();
|
||||
result.userNo = no.ToString();
|
||||
string token = await GetToken();
|
||||
result.token = token;
|
||||
result.sign = "这个小家伙儿很懒,什么也没留下.";
|
||||
result.status = 1;
|
||||
result.regOk = 0;
|
||||
result.isSystem = 0;
|
||||
result.addTime = DateTime.Now;
|
||||
result.upTime = DateTime.Now;
|
||||
bool isok = db.Insertable(result).ExecuteCommand() > 0;
|
||||
if (!isok)
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
|
||||
await _dbClient.AsTenant().CommitTranAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
result = null;
|
||||
await _dbClient.AsTenant().RollbackTranAsync();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<int> GetUserNo()
|
||||
{
|
||||
int No = 0;
|
||||
bool ok = true;
|
||||
while (ok)
|
||||
{
|
||||
No = RandomAssist.GetFormatedNumeric(11012585, 97521695);
|
||||
if (await GetUserInfoByUserNo(No.ToString()) == null)
|
||||
{
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
|
||||
return No;
|
||||
}
|
||||
|
||||
private async Task<string> GetToken()
|
||||
{
|
||||
string sid = string.Empty;
|
||||
bool ok = true;
|
||||
while (ok)
|
||||
{
|
||||
sid = StringAssist.RandomString(32);
|
||||
if (await GetUserInfoBySid(sid) == null)
|
||||
{
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
|
||||
return sid;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
21
Service/Application.Web/Common/DateTimeJsonConverter.cs
Normal file
21
Service/Application.Web/Common/DateTimeJsonConverter.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Application.Web;
|
||||
|
||||
public class DateTimeJsonConverter: JsonConverter<DateTime>
|
||||
{
|
||||
public override DateTime Read(
|
||||
ref Utf8JsonReader reader,
|
||||
Type typeToConvert,
|
||||
JsonSerializerOptions options)
|
||||
{
|
||||
return DateTime.ParseExact(reader.GetString(), "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStringValue(value.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture));
|
||||
}
|
||||
}
|
||||
@@ -14,9 +14,15 @@ namespace Application.Web.Controllers.Login
|
||||
public class LoginController : ControllerBase
|
||||
{
|
||||
private readonly IGameAccountService _accountService;
|
||||
public LoginController(IGameAccountService accountService)
|
||||
private readonly IAreaService _areaService;
|
||||
private readonly IUnitUserService _userService;
|
||||
|
||||
public LoginController(IGameAccountService accountService, IAreaService areaService,
|
||||
IUnitUserService userService)
|
||||
{
|
||||
_accountService = accountService;
|
||||
_areaService = areaService;
|
||||
_userService = userService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -27,8 +33,6 @@ namespace Application.Web.Controllers.Login
|
||||
[HttpPost]
|
||||
public async Task<IPoAction> Login([FromBody] LoginParms parms)
|
||||
{
|
||||
|
||||
|
||||
return PoAction.Ok(parms.code);
|
||||
}
|
||||
|
||||
@@ -38,14 +42,15 @@ namespace Application.Web.Controllers.Login
|
||||
/// <param name="code"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IPoAction> TwLogin( string code)
|
||||
public async Task<IPoAction> TwLogin(string code)
|
||||
{
|
||||
if (string.IsNullOrEmpty(code))
|
||||
{
|
||||
return PoAction.Message("code值错误!");
|
||||
}
|
||||
|
||||
AutoLogin login = new AutoLogin();
|
||||
var loginInfo = await login.TwLogin(code);
|
||||
var loginInfo = await login.TwLogin(code);
|
||||
if (loginInfo.code == 0)
|
||||
{
|
||||
dynamic twInfo = JsonConvert.DeserializeObject<dynamic>(loginInfo.data.ToString());
|
||||
@@ -54,14 +59,14 @@ namespace Application.Web.Controllers.Login
|
||||
var accInfo = await _accountService.GetAccInfoByOpenId(openId);
|
||||
if (accInfo == null)
|
||||
{
|
||||
var userData = await _accountService.Regist("", nick, "", "", openId);
|
||||
var userData = await _accountService.Regist("", nick, "", "", openId);
|
||||
if (userData == null)
|
||||
{
|
||||
return PoAction.Message("登录失败,请联系客服!");
|
||||
}
|
||||
else
|
||||
{
|
||||
object ret =new { token = userData.token };
|
||||
object ret = new { token = userData.token };
|
||||
return PoAction.Ok(ret);
|
||||
}
|
||||
}
|
||||
@@ -73,7 +78,7 @@ namespace Application.Web.Controllers.Login
|
||||
}
|
||||
else
|
||||
{
|
||||
object ret =new { token = accInfo.token };
|
||||
object ret = new { token = accInfo.token };
|
||||
return PoAction.Ok(ret);
|
||||
}
|
||||
}
|
||||
@@ -112,5 +117,60 @@ namespace Application.Web.Controllers.Login
|
||||
return PoAction.Message("退出失败,请稍后尝试!");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注册基础账号信息
|
||||
/// </summary>
|
||||
/// <param name="sid"></param>
|
||||
/// <param name="area"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IPoAction> Register(string sid, int area)
|
||||
{
|
||||
if (string.IsNullOrEmpty(sid))
|
||||
{
|
||||
return PoAction.Message("未登录账号!");
|
||||
}
|
||||
|
||||
var accInfo = await _accountService.GetAccInfoByToken(sid);
|
||||
if (accInfo == null)
|
||||
{
|
||||
return PoAction.Message("未登录账号!");
|
||||
}
|
||||
|
||||
var areaInfo = await _areaService.GetAreaInfo(area);
|
||||
if (areaInfo == null)
|
||||
{
|
||||
return PoAction.Message("区服不存在!");
|
||||
}
|
||||
|
||||
if (areaInfo.status != 1)
|
||||
{
|
||||
return PoAction.Message("当前区繁忙,无法进入!");
|
||||
}
|
||||
|
||||
//判断是否已经注册
|
||||
var userData = await _userService.GetUserDataByAccId(accInfo.accId);
|
||||
if (userData.Any(it => it.areaId == area))
|
||||
{
|
||||
return PoAction.Message("该区已存在角色!");
|
||||
}
|
||||
|
||||
var userInfo = await _userService.Register(areaInfo.areaId, accInfo.accId);
|
||||
if (userInfo == null)
|
||||
{
|
||||
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, 300);
|
||||
return PoAction.Ok(new { token = token, refToken = userInfo.token, userId = userInfo.userId });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,12 +13,14 @@ namespace Application.Web.Controllers.Pub
|
||||
private readonly IAreaService _areaService;
|
||||
private readonly INoticeService _noticeService;
|
||||
private readonly IGameAccountService _accountService;
|
||||
private readonly IUnitUserService _userService;
|
||||
|
||||
public PubController(IAreaService areaService, INoticeService noticeService,IGameAccountService accountService)
|
||||
public PubController(IAreaService areaService, INoticeService noticeService, IGameAccountService accountService,IUnitUserService userService)
|
||||
{
|
||||
_areaService = areaService;
|
||||
_noticeService = noticeService;
|
||||
_accountService = accountService;
|
||||
_userService = userService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -31,20 +33,27 @@ namespace Application.Web.Controllers.Pub
|
||||
{
|
||||
bool isOnline = false;
|
||||
game_account account = new game_account();
|
||||
List<unit_user> userData = new List<unit_user>();
|
||||
if (!string.IsNullOrEmpty(sid))
|
||||
{
|
||||
account = await _accountService.GetAccInfoByToken(sid);
|
||||
if (account != null)
|
||||
{
|
||||
isOnline = true;
|
||||
userData = await _userService.GetUserDataByAccId(account.accId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var areaData = await _areaService.GetAreaData();
|
||||
var notice = await _noticeService.GetNoticeDataByTake(5);
|
||||
foreach (var _user in userData)
|
||||
{
|
||||
areaData.RemoveAll(it => it.areaId == _user.areaId);
|
||||
}
|
||||
|
||||
var notice = await _noticeService.GetNoticeDataByTake(5);
|
||||
|
||||
int OnCount = 100;
|
||||
return PoAction.Ok(new { area = areaData, notice, isOnline, onCount = OnCount,account });
|
||||
return PoAction.Ok(new { area = areaData, notice, isOnline, onCount = OnCount, account,userData });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -54,11 +63,28 @@ namespace Application.Web.Controllers.Pub
|
||||
/// <param name="limit"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IPoAction> GetNoticeData(int page,int limit)
|
||||
public async Task<IPoAction> GetNoticeData(int page, int limit)
|
||||
{
|
||||
RefAsync<int> total = 0;
|
||||
var data = await _noticeService.GetNoticeData(page, limit, total);
|
||||
return PoAction.Ok(new { data, total=total.Value });
|
||||
return PoAction.Ok(new { data, total = total.Value });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取公告详情
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IPoAction> GetNoticeInfo(string id)
|
||||
{
|
||||
var data = await _noticeService.GetNoticeInfo(id);
|
||||
if (data == null)
|
||||
{
|
||||
return PoAction.Message("公告不存在!");
|
||||
}
|
||||
data.sign = data.sign.Replace("[Br]", "<br />");
|
||||
return PoAction.Ok(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -53,8 +53,13 @@ builder.Services.InjectTimer(services =>
|
||||
builder.Logging.InjectLog();
|
||||
// Add services to the container.
|
||||
|
||||
builder.Services.AddControllers().AddJsonOptions(option =>
|
||||
{
|
||||
option.JsonSerializerOptions.Converters.Add(new DateTimeJsonConverter());
|
||||
option.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles;
|
||||
option.JsonSerializerOptions.MaxDepth =int.MaxValue;
|
||||
});
|
||||
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
|
||||
#region 配置跨域处理,允许所有来源
|
||||
|
||||
Reference in New Issue
Block a user