忽略dll文件git
This commit is contained in:
@@ -1,342 +1,344 @@
|
||||
using Hncore.Infrastructure.Common;
|
||||
using Hncore.Infrastructure.Data;
|
||||
using Hncore.Infrastructure.Extension;
|
||||
using Hncore.Infrastructure.Serializer;
|
||||
using Hncore.Infrastructure.WebApi;
|
||||
using Hncore.Pass.BaseInfo.Models;
|
||||
using Hncore.Pass.BaseInfo.Request;
|
||||
using Hncore.Pass.BaseInfo.Request.Manager;
|
||||
using Hncore.Pass.BaseInfo.Response;
|
||||
using JWT;
|
||||
using JWT.Algorithms;
|
||||
using JWT.Serializers;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Pass.BaseInfo.Service
|
||||
{
|
||||
public class ManageService
|
||||
{
|
||||
private static string _secret = "etor_yh_lzh_20f_2020_YES";
|
||||
|
||||
private UserDbContext _dbContext;
|
||||
|
||||
public ManageService(UserDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public async Task<LoginResponse> Login(LoginRequest request)
|
||||
{
|
||||
if (request == null
|
||||
// || !request.Code.Has()
|
||||
|| !request.CodeKey.Has()
|
||||
|| !request.Logincode.Has()
|
||||
|| !request.Password.Has()
|
||||
)
|
||||
{
|
||||
BusinessException.Throw("登陆信息异常");
|
||||
}
|
||||
|
||||
//string valCode = await RedisHelper.GetAsync(request.CodeKey);
|
||||
//if (!valCode.Has() || valCode.ToLower() != request.Code.ToLower())
|
||||
//{
|
||||
// BusinessException.Throw("验证码错误");
|
||||
//}
|
||||
|
||||
var manage = await _dbContext.Set<Manager>()
|
||||
.FirstOrDefaultAsync(m => (m.LoginCode == request.Logincode || m.Phone == request.Logincode)
|
||||
&& m.State == (int)ManagerState.Enabled);
|
||||
|
||||
if (manage == null)
|
||||
{
|
||||
BusinessException.Throw("登录名不存在");
|
||||
}
|
||||
|
||||
if (HashPassword(request.Password).ToUpper() != manage.Password.ToUpper())
|
||||
{
|
||||
BusinessException.Throw("密码错误");
|
||||
}
|
||||
|
||||
return await LoginInternal(manage);
|
||||
}
|
||||
|
||||
public async Task<LoginResponse> LoginTemp(LoginRequest request)
|
||||
{
|
||||
if (request == null
|
||||
|| !request.Logincode.Has()
|
||||
|| !request.Password.Has()
|
||||
)
|
||||
{
|
||||
BusinessException.Throw("登陆信息异常");
|
||||
}
|
||||
|
||||
var manage = await _dbContext.Set<Manager>()
|
||||
.FirstOrDefaultAsync(m => (m.LoginCode == request.Logincode || m.Phone == request.Logincode)
|
||||
&& m.State == (int)ManagerState.Enabled);
|
||||
|
||||
if (manage == null)
|
||||
{
|
||||
BusinessException.Throw("登录名不存在");
|
||||
}
|
||||
|
||||
if (HashPassword(request.Password).ToUpper() != manage.Password.ToUpper())
|
||||
{
|
||||
BusinessException.Throw("密码错误");
|
||||
}
|
||||
|
||||
return await LoginInternal(manage);
|
||||
}
|
||||
|
||||
public async Task<LoginAndroidManagerResponse> LoginAndroidTemp(LoginRequest request)
|
||||
{
|
||||
if (request == null
|
||||
|| !request.Logincode.Has()
|
||||
|| !request.Password.Has()
|
||||
)
|
||||
{
|
||||
BusinessException.Throw("登陆信息异常");
|
||||
}
|
||||
|
||||
var manage = await _dbContext.Set<Manager>()
|
||||
.FirstOrDefaultAsync(m => (m.LoginCode == request.Logincode || m.Phone == request.Logincode)
|
||||
&& m.DeleteTag == 0
|
||||
&& m.State == (int)ManagerState.Enabled);
|
||||
|
||||
if (manage == null)
|
||||
{
|
||||
BusinessException.Throw("登录名不存在");
|
||||
}
|
||||
|
||||
if (HashPassword(request.Password).ToUpper() != manage.Password.ToUpper())
|
||||
{
|
||||
BusinessException.Throw("密码错误");
|
||||
}
|
||||
|
||||
return await LoginAndroidInternal(manage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过微信的opid登录
|
||||
/// </summary>
|
||||
/// <param name="openId"></param>
|
||||
/// <returns></returns>
|
||||
|
||||
public async Task<LoginResponse> LoginOpenid(string openId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(openId))
|
||||
{
|
||||
BusinessException.Throw("登陆信息异常");
|
||||
}
|
||||
|
||||
openId = openId.Trim();
|
||||
var manage = await _dbContext.Set<Manager>()
|
||||
.FirstOrDefaultAsync(m => (m.WxOpenid == openId
|
||||
&& m.DeleteTag == 0
|
||||
&& m.State == (int)ManagerState.Enabled));
|
||||
if (manage == null)
|
||||
{
|
||||
BusinessException.Throw("没有绑定微信登录");
|
||||
}
|
||||
|
||||
return await LoginInternal(manage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过微信的opid登录
|
||||
/// </summary>
|
||||
/// <param name="openId"></param>
|
||||
/// <returns></returns>
|
||||
|
||||
public async Task<string> GenerateTokenByOpenid(string openId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(openId))
|
||||
{
|
||||
BusinessException.Throw("登陆信息异常");
|
||||
}
|
||||
|
||||
openId = openId.Trim();
|
||||
var manage = await _dbContext.Set<Manager>()
|
||||
.FirstOrDefaultAsync(m => (m.WxOpenid == openId
|
||||
&& m.DeleteTag == 0
|
||||
&& m.State == (int)ManagerState.Enabled));
|
||||
if (manage == null)
|
||||
{
|
||||
BusinessException.Throw("没有绑定微信登录");
|
||||
}
|
||||
|
||||
var tokenDic = new Dictionary<string, object>()
|
||||
{
|
||||
{"OpenId",openId},
|
||||
{"LoginName", manage.LoginCode},
|
||||
{"RoleName", ""},
|
||||
{"OperaterID", manage.Id},
|
||||
{"TenantId", manage.TenantId}
|
||||
};
|
||||
|
||||
var token = GenerateToken(tokenDic);
|
||||
return token;
|
||||
}
|
||||
|
||||
private async Task<LoginResponse> LoginInternal(Manager manage)
|
||||
{
|
||||
var tokenDic = new Dictionary<string, object>()
|
||||
{
|
||||
{"LoginName", manage.LoginCode},
|
||||
{"RoleName", ""},
|
||||
{"OperaterID", manage.Id},
|
||||
{"TenantId", manage.TenantId}
|
||||
};
|
||||
|
||||
if (manage.IsRoot != 1)
|
||||
{
|
||||
//int[] domains = await _dbContext.Set<etor_authority_managerdatadomain>()
|
||||
// .Where(p => p.owner_id == manage.TenantId && p.DeleteTag == 0 && p.managerid == manage.Id)
|
||||
// .Select(t => t.projectcode)
|
||||
// .ToArrayAsync();
|
||||
|
||||
//tokenDic.Add("DataDomain", domains);
|
||||
}
|
||||
|
||||
var token = GenerateToken(tokenDic);
|
||||
|
||||
var response = new LoginManagerResponse().FromEntity(manage);
|
||||
return new LoginResponse()
|
||||
{
|
||||
Token = token,
|
||||
Manager = response
|
||||
};
|
||||
}
|
||||
/// <summary>
|
||||
/// 重新获取token
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
internal async Task<string> AgainGetToken(RequestBase<AgainTokenDTO> request)
|
||||
{
|
||||
if (null == request|| request.Data==null) BusinessException.Throw("参数错误");
|
||||
var token = request.Data.Token;
|
||||
var ManageUserInfo = new ManageUserInfo();
|
||||
if (CheckToken(request.Data.Token, out ManageUserInfo))
|
||||
{
|
||||
var tokenDic = new Dictionary<string, object>()
|
||||
{
|
||||
{"LoginName", ManageUserInfo.LoginName},
|
||||
{"RoleName", ManageUserInfo.RoleName},
|
||||
{"OperaterID", ManageUserInfo.OperaterId},
|
||||
{"OwnerID", ManageUserInfo.TenantId}
|
||||
};
|
||||
switch (request.Data.Type)
|
||||
{
|
||||
case AgainTokenType.Expire:
|
||||
break;
|
||||
//case AgainTokenType.Project:
|
||||
// var manager = await _dbContext.Set<Manager>()
|
||||
// .FirstOrDefaultAsync(s=>s.Id == request.OperaterId);
|
||||
// if (manager.IsRoot != 1)
|
||||
// {
|
||||
// int[] domains = await _dbContext.Set<etor_authority_managerdatadomain>()
|
||||
// .Where(p => p.owner_id == manager.TenantId && p.DeleteTag == 0 && p.managerid == manager.Id)
|
||||
// .Select(t => t.projectcode)
|
||||
// .ToArrayAsync();
|
||||
|
||||
// tokenDic.Add("DataDomain", domains);
|
||||
// }
|
||||
// break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
token = GenerateToken(tokenDic);
|
||||
}
|
||||
else
|
||||
{
|
||||
BusinessException.Throw("参数错误");
|
||||
}
|
||||
return token;
|
||||
}
|
||||
|
||||
private async Task<LoginAndroidManagerResponse> LoginAndroidInternal(Manager manage)
|
||||
{
|
||||
var response = new LoginAndroidManagerResponse().FromEntity(manage);
|
||||
return response;
|
||||
}
|
||||
|
||||
private static string GenerateToken(Dictionary<string, object> param, int timeoutMinutes = 180)
|
||||
{
|
||||
IJwtAlgorithm algorithm = new HMACSHA256Algorithm();
|
||||
IJsonSerializer serializer = new JsonNetSerializer();
|
||||
IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
|
||||
IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder);
|
||||
|
||||
long now = DateTimeHelper.ToUnixTimestamp(DateTime.Now);
|
||||
|
||||
param["iat"] = now; //签发时间
|
||||
param["exp"] = now + Math.Max(0, timeoutMinutes) * 60; //过期时间
|
||||
|
||||
var token = encoder.Encode(param, _secret);
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
public bool CheckToken(string token, out ManageUserInfo userInfo)
|
||||
{
|
||||
userInfo = null;
|
||||
try
|
||||
{
|
||||
IJsonSerializer serializer = new JsonNetSerializer();
|
||||
IDateTimeProvider provider = new UtcDateTimeProvider();
|
||||
IJwtValidator validator = new JwtValidator(serializer, provider);
|
||||
IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
|
||||
IJwtDecoder decoder = new JwtDecoder(serializer, validator, urlEncoder);
|
||||
var payload = decoder.Decode(token, _secret, verify: true);
|
||||
|
||||
userInfo = payload.FromJsonTo<ManageUserInfo>();
|
||||
|
||||
if (userInfo == null || userInfo.TenantId == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (userInfo.IssueTimestamp == 0
|
||||
|| DateTimeHelper.UnixTimeStampToDateTime(userInfo.IssueTimestamp) <
|
||||
DateTime.Now.AddHours(-4))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (TokenExpiredException ex)
|
||||
{
|
||||
LogHelper.Error("oken has expired", ex.Message);
|
||||
return false;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Error("CheckToken失败", ex.Message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static string HashPassword(string password)
|
||||
{
|
||||
using (MD5 md5 = MD5.Create())
|
||||
{
|
||||
byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(password));
|
||||
return Convert.ToBase64String(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Manager> GetById(int id)
|
||||
{
|
||||
var manage = await _dbContext.Set<Manager>().FindAsync(id);
|
||||
|
||||
return manage;
|
||||
|
||||
}
|
||||
}
|
||||
using Hncore.Infrastructure.Common;
|
||||
using Hncore.Infrastructure.Data;
|
||||
using Hncore.Infrastructure.Extension;
|
||||
using Hncore.Infrastructure.Serializer;
|
||||
using Hncore.Infrastructure.WebApi;
|
||||
using Hncore.Pass.BaseInfo.Models;
|
||||
using Hncore.Pass.BaseInfo.Request;
|
||||
using Hncore.Pass.BaseInfo.Request.Manager;
|
||||
using Hncore.Pass.BaseInfo.Response;
|
||||
using JWT;
|
||||
using JWT.Algorithms;
|
||||
using JWT.Serializers;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Pass.BaseInfo.Service
|
||||
{
|
||||
public class ManageService
|
||||
{
|
||||
private static string _secret = "etor_yh_lzh_20f_2020_YES";
|
||||
|
||||
private UserDbContext _dbContext;
|
||||
|
||||
public ManageService(UserDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public async Task<LoginResponse> Login(LoginRequest request)
|
||||
{
|
||||
if (request == null
|
||||
// || !request.Code.Has()
|
||||
|| !request.CodeKey.Has()
|
||||
|| !request.Logincode.Has()
|
||||
|| !request.Password.Has()
|
||||
)
|
||||
{
|
||||
BusinessException.Throw("登陆信息异常");
|
||||
}
|
||||
|
||||
//string valCode = await RedisHelper.GetAsync(request.CodeKey);
|
||||
//if (!valCode.Has() || valCode.ToLower() != request.Code.ToLower())
|
||||
//{
|
||||
// BusinessException.Throw("验证码错误");
|
||||
//}
|
||||
|
||||
var manage = await _dbContext.Set<Manager>()
|
||||
.FirstOrDefaultAsync(m => (m.LoginCode == request.Logincode || m.Phone == request.Logincode)
|
||||
&& m.State == (int)ManagerState.Enabled);
|
||||
|
||||
if (manage == null)
|
||||
{
|
||||
BusinessException.Throw("登录名不存在");
|
||||
}
|
||||
|
||||
if (HashPassword(request.Password).ToUpper() != manage.Password.ToUpper())
|
||||
{
|
||||
BusinessException.Throw("密码错误");
|
||||
}
|
||||
manage.TenantId = 1;
|
||||
_dbContext.Set<Manager>().Update(manage);
|
||||
|
||||
return await LoginInternal(manage);
|
||||
}
|
||||
|
||||
public async Task<LoginResponse> LoginTemp(LoginRequest request)
|
||||
{
|
||||
if (request == null
|
||||
|| !request.Logincode.Has()
|
||||
|| !request.Password.Has()
|
||||
)
|
||||
{
|
||||
BusinessException.Throw("登陆信息异常");
|
||||
}
|
||||
|
||||
var manage = await _dbContext.Set<Manager>()
|
||||
.FirstOrDefaultAsync(m => (m.LoginCode == request.Logincode || m.Phone == request.Logincode)
|
||||
&& m.State == (int)ManagerState.Enabled);
|
||||
|
||||
if (manage == null)
|
||||
{
|
||||
BusinessException.Throw("登录名不存在");
|
||||
}
|
||||
|
||||
if (HashPassword(request.Password).ToUpper() != manage.Password.ToUpper())
|
||||
{
|
||||
BusinessException.Throw("密码错误");
|
||||
}
|
||||
|
||||
return await LoginInternal(manage);
|
||||
}
|
||||
|
||||
public async Task<LoginAndroidManagerResponse> LoginAndroidTemp(LoginRequest request)
|
||||
{
|
||||
if (request == null
|
||||
|| !request.Logincode.Has()
|
||||
|| !request.Password.Has()
|
||||
)
|
||||
{
|
||||
BusinessException.Throw("登陆信息异常");
|
||||
}
|
||||
|
||||
var manage = await _dbContext.Set<Manager>()
|
||||
.FirstOrDefaultAsync(m => (m.LoginCode == request.Logincode || m.Phone == request.Logincode)
|
||||
&& m.DeleteTag == 0
|
||||
&& m.State == (int)ManagerState.Enabled);
|
||||
|
||||
if (manage == null)
|
||||
{
|
||||
BusinessException.Throw("登录名不存在");
|
||||
}
|
||||
|
||||
if (HashPassword(request.Password).ToUpper() != manage.Password.ToUpper())
|
||||
{
|
||||
BusinessException.Throw("密码错误");
|
||||
}
|
||||
|
||||
return await LoginAndroidInternal(manage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过微信的opid登录
|
||||
/// </summary>
|
||||
/// <param name="openId"></param>
|
||||
/// <returns></returns>
|
||||
|
||||
public async Task<LoginResponse> LoginOpenid(string openId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(openId))
|
||||
{
|
||||
BusinessException.Throw("登陆信息异常");
|
||||
}
|
||||
|
||||
openId = openId.Trim();
|
||||
var manage = await _dbContext.Set<Manager>()
|
||||
.FirstOrDefaultAsync(m => (m.WxOpenid == openId
|
||||
&& m.DeleteTag == 0
|
||||
&& m.State == (int)ManagerState.Enabled));
|
||||
if (manage == null)
|
||||
{
|
||||
BusinessException.Throw("没有绑定微信登录");
|
||||
}
|
||||
|
||||
return await LoginInternal(manage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过微信的opid登录
|
||||
/// </summary>
|
||||
/// <param name="openId"></param>
|
||||
/// <returns></returns>
|
||||
|
||||
public async Task<string> GenerateTokenByOpenid(string openId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(openId))
|
||||
{
|
||||
BusinessException.Throw("登陆信息异常");
|
||||
}
|
||||
|
||||
openId = openId.Trim();
|
||||
var manage = await _dbContext.Set<Manager>()
|
||||
.FirstOrDefaultAsync(m => (m.WxOpenid == openId
|
||||
&& m.DeleteTag == 0
|
||||
&& m.State == (int)ManagerState.Enabled));
|
||||
if (manage == null)
|
||||
{
|
||||
BusinessException.Throw("没有绑定微信登录");
|
||||
}
|
||||
|
||||
var tokenDic = new Dictionary<string, object>()
|
||||
{
|
||||
{"OpenId",openId},
|
||||
{"LoginName", manage.LoginCode},
|
||||
{"RoleName", ""},
|
||||
{"OperaterID", manage.Id},
|
||||
{"TenantId", manage.TenantId}
|
||||
};
|
||||
|
||||
var token = GenerateToken(tokenDic);
|
||||
return token;
|
||||
}
|
||||
|
||||
private async Task<LoginResponse> LoginInternal(Manager manage)
|
||||
{
|
||||
var tokenDic = new Dictionary<string, object>()
|
||||
{
|
||||
{"LoginName", manage.LoginCode},
|
||||
{"RoleName", ""},
|
||||
{"OperaterID", manage.Id},
|
||||
{"TenantId", manage.TenantId}
|
||||
};
|
||||
|
||||
if (manage.IsRoot != 1)
|
||||
{
|
||||
//int[] domains = await _dbContext.Set<etor_authority_managerdatadomain>()
|
||||
// .Where(p => p.owner_id == manage.TenantId && p.DeleteTag == 0 && p.managerid == manage.Id)
|
||||
// .Select(t => t.projectcode)
|
||||
// .ToArrayAsync();
|
||||
|
||||
//tokenDic.Add("DataDomain", domains);
|
||||
}
|
||||
|
||||
var token = GenerateToken(tokenDic);
|
||||
|
||||
var response = new LoginManagerResponse().FromEntity(manage);
|
||||
return new LoginResponse()
|
||||
{
|
||||
Token = token,
|
||||
Manager = response
|
||||
};
|
||||
}
|
||||
/// <summary>
|
||||
/// 重新获取token
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
internal async Task<string> AgainGetToken(RequestBase<AgainTokenDTO> request)
|
||||
{
|
||||
if (null == request|| request.Data==null) BusinessException.Throw("参数错误");
|
||||
var token = request.Data.Token;
|
||||
var ManageUserInfo = new ManageUserInfo();
|
||||
if (CheckToken(request.Data.Token, out ManageUserInfo))
|
||||
{
|
||||
var tokenDic = new Dictionary<string, object>()
|
||||
{
|
||||
{"LoginName", ManageUserInfo.LoginName},
|
||||
{"RoleName", ManageUserInfo.RoleName},
|
||||
{"OperaterID", ManageUserInfo.OperaterId},
|
||||
{"OwnerID", ManageUserInfo.TenantId}
|
||||
};
|
||||
switch (request.Data.Type)
|
||||
{
|
||||
case AgainTokenType.Expire:
|
||||
break;
|
||||
//case AgainTokenType.Project:
|
||||
// var manager = await _dbContext.Set<Manager>()
|
||||
// .FirstOrDefaultAsync(s=>s.Id == request.OperaterId);
|
||||
// if (manager.IsRoot != 1)
|
||||
// {
|
||||
// int[] domains = await _dbContext.Set<etor_authority_managerdatadomain>()
|
||||
// .Where(p => p.owner_id == manager.TenantId && p.DeleteTag == 0 && p.managerid == manager.Id)
|
||||
// .Select(t => t.projectcode)
|
||||
// .ToArrayAsync();
|
||||
|
||||
// tokenDic.Add("DataDomain", domains);
|
||||
// }
|
||||
// break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
token = GenerateToken(tokenDic);
|
||||
}
|
||||
else
|
||||
{
|
||||
BusinessException.Throw("参数错误");
|
||||
}
|
||||
return token;
|
||||
}
|
||||
|
||||
private async Task<LoginAndroidManagerResponse> LoginAndroidInternal(Manager manage)
|
||||
{
|
||||
var response = new LoginAndroidManagerResponse().FromEntity(manage);
|
||||
return response;
|
||||
}
|
||||
|
||||
private static string GenerateToken(Dictionary<string, object> param, int timeoutMinutes = 180)
|
||||
{
|
||||
IJwtAlgorithm algorithm = new HMACSHA256Algorithm();
|
||||
IJsonSerializer serializer = new JsonNetSerializer();
|
||||
IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
|
||||
IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder);
|
||||
|
||||
long now = DateTimeHelper.ToUnixTimestamp(DateTime.Now);
|
||||
|
||||
param["iat"] = now; //签发时间
|
||||
param["exp"] = now + Math.Max(0, timeoutMinutes) * 60; //过期时间
|
||||
|
||||
var token = encoder.Encode(param, _secret);
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
public bool CheckToken(string token, out ManageUserInfo userInfo)
|
||||
{
|
||||
userInfo = null;
|
||||
try
|
||||
{
|
||||
IJsonSerializer serializer = new JsonNetSerializer();
|
||||
IDateTimeProvider provider = new UtcDateTimeProvider();
|
||||
IJwtValidator validator = new JwtValidator(serializer, provider);
|
||||
IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
|
||||
IJwtDecoder decoder = new JwtDecoder(serializer, validator, urlEncoder);
|
||||
var payload = decoder.Decode(token, _secret, verify: true);
|
||||
|
||||
userInfo = payload.FromJsonTo<ManageUserInfo>();
|
||||
|
||||
if (userInfo == null || userInfo.TenantId == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (userInfo.IssueTimestamp == 0
|
||||
|| DateTimeHelper.UnixTimeStampToDateTime(userInfo.IssueTimestamp) <
|
||||
DateTime.Now.AddHours(-4))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (TokenExpiredException ex)
|
||||
{
|
||||
LogHelper.Error("oken has expired", ex.Message);
|
||||
return false;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Error("CheckToken失败", ex.Message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static string HashPassword(string password)
|
||||
{
|
||||
using (MD5 md5 = MD5.Create())
|
||||
{
|
||||
byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(password));
|
||||
return Convert.ToBase64String(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Manager> GetById(int id)
|
||||
{
|
||||
var manage = await _dbContext.Set<Manager>().FindAsync(id);
|
||||
|
||||
return manage;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,115 +1,115 @@
|
||||
using Hncore.Infrastructure.Common;
|
||||
using Hncore.Infrastructure.Data;
|
||||
using Hncore.Infrastructure.Service;
|
||||
using Hncore.Infrastructure.WebApi;
|
||||
using Hncore.Pass.BaseInfo.Models;
|
||||
using Hncore.Pass.BaseInfo.Request;
|
||||
using Hncore.Pass.BaseInfo.Request.User;
|
||||
using Hncore.Pass.BaseInfo.Service;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Pass.Vpn.Service
|
||||
{
|
||||
public partial class UserChargeOrderService : ServiceBase<UserChargeOrderEntity>, IFindService
|
||||
{
|
||||
UserDbContext m_DbContext;
|
||||
UserService m_UserService;
|
||||
public UserChargeOrderService(UserDbContext dbContext
|
||||
, UserService _UserService
|
||||
, IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
|
||||
{
|
||||
m_DbContext = dbContext;
|
||||
m_UserService = _UserService;
|
||||
}
|
||||
|
||||
public async Task<ApiResult<UserChargeOrderEntity>> CreateOrder(CreateOrderRequest request, int userId)
|
||||
{
|
||||
if (request.ChargeAmount <= 0)
|
||||
{
|
||||
BusinessException.Throw("充值金额不正确");
|
||||
}
|
||||
var userEntity = await m_UserService.GetById(userId);
|
||||
|
||||
if (userEntity == null)
|
||||
{
|
||||
BusinessException.Throw("用户不存在");
|
||||
}
|
||||
var order = new UserChargeOrderEntity()
|
||||
{
|
||||
OrderName = $"充值{request.ChargeAmount}",
|
||||
OrderNo = GeneratOrderNO(userId),
|
||||
OrderState = UOrderStatus.NoPay,
|
||||
UserId = userId,
|
||||
UserName = userEntity.LoginCode,
|
||||
PayChannel = request.PayChannel,
|
||||
Channel = request.Channel,
|
||||
OrderAmount = request.ChargeAmount,
|
||||
PaymentAmount = request.ChargeAmount,
|
||||
};
|
||||
await this.Add(order);
|
||||
return new ApiResult<UserChargeOrderEntity>(order);
|
||||
}
|
||||
|
||||
private string GeneratOrderNO(int userId)
|
||||
{
|
||||
var code = ValidateCodeHelper.MakeNumCode(6);
|
||||
var userStr = userId.ToString().PadRight(6, '0');
|
||||
var str = $"{DateTime.Now.ToString("yyyyMMdd")}{userStr}{code}";
|
||||
var pre = "cz";
|
||||
return pre + str;
|
||||
}
|
||||
|
||||
public async Task<UserChargeOrderEntity> GetOrderByNo(string no, UOrderStatus status = 0)
|
||||
{
|
||||
var order = await this.Query(m => m.OrderNo == no && (status == 0 || m.OrderState == status)).FirstOrDefaultAsync();
|
||||
return order;
|
||||
}
|
||||
|
||||
public async Task<List<UserChargeOrderEntity>> GetOrders(UOrderStatus status = 0, int top = 100)
|
||||
{
|
||||
var orders = await this.Query(m => m.OrderState == status).OrderByDescending(m => m.Id).Take(top).ToListAsync();
|
||||
return orders;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 处理成功的订单
|
||||
/// </summary>
|
||||
/// <param name="order"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> ProcessOrderAccount(UserChargeOrderEntity order)
|
||||
{
|
||||
if (order.OrderState != UOrderStatus.PayOk) return false;
|
||||
|
||||
var request = new UpdateAmountRequest()
|
||||
{
|
||||
Amount = order.OrderAmount,
|
||||
AttchInfo = order.OrderNo,
|
||||
OpAmountType = ScoreType.UserCharge,
|
||||
OperateUserName = order.UserName,
|
||||
UserId = order.UserId
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var ret = await m_UserService.UpdateAmount(request);
|
||||
if (ret.Code == ResultCode.C_SUCCESS)
|
||||
{
|
||||
order.OrderState = UOrderStatus.Complete;
|
||||
await this.Update(order);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Error("处理充值订单", ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
using Hncore.Infrastructure.Common;
|
||||
using Hncore.Infrastructure.Data;
|
||||
using Hncore.Infrastructure.Service;
|
||||
using Hncore.Infrastructure.WebApi;
|
||||
using Hncore.Pass.BaseInfo.Models;
|
||||
using Hncore.Pass.BaseInfo.Request;
|
||||
using Hncore.Pass.BaseInfo.Request.User;
|
||||
using Hncore.Pass.BaseInfo.Service;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Pass.Vpn.Service
|
||||
{
|
||||
public partial class UserChargeOrderService : ServiceBase<UserChargeOrderEntity>, IFindService
|
||||
{
|
||||
UserDbContext m_DbContext;
|
||||
UserService m_UserService;
|
||||
public UserChargeOrderService(UserDbContext dbContext
|
||||
, UserService _UserService
|
||||
, IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
|
||||
{
|
||||
m_DbContext = dbContext;
|
||||
m_UserService = _UserService;
|
||||
}
|
||||
|
||||
public async Task<ApiResult<UserChargeOrderEntity>> CreateOrder(CreateOrderRequest request, int userId)
|
||||
{
|
||||
if (request.ChargeAmount <= 0)
|
||||
{
|
||||
BusinessException.Throw("充值金额不正确");
|
||||
}
|
||||
var userEntity = await m_UserService.GetById(userId);
|
||||
|
||||
if (userEntity == null)
|
||||
{
|
||||
BusinessException.Throw("用户不存在");
|
||||
}
|
||||
var order = new UserChargeOrderEntity()
|
||||
{
|
||||
OrderName = $"充值{request.ChargeAmount}",
|
||||
OrderNo = GeneratOrderNO(userId),
|
||||
OrderState = UOrderStatus.NoPay,
|
||||
UserId = userId,
|
||||
UserName = userEntity.LoginCode,
|
||||
PayChannel = request.PayChannel,
|
||||
Channel = request.Channel,
|
||||
OrderAmount = request.ChargeAmount,
|
||||
PaymentAmount = request.ChargeAmount,
|
||||
};
|
||||
await this.Add(order);
|
||||
return new ApiResult<UserChargeOrderEntity>(order);
|
||||
}
|
||||
|
||||
private string GeneratOrderNO(int userId)
|
||||
{
|
||||
var code = ValidateCodeHelper.MakeNumCode(6);
|
||||
var userStr = userId.ToString().PadRight(6, '0');
|
||||
var str = $"{DateTime.Now.ToString("yyyyMMdd")}{userStr}{code}";
|
||||
var pre = "cz";
|
||||
return pre + str;
|
||||
}
|
||||
|
||||
public async Task<UserChargeOrderEntity> GetOrderByNo(string no, UOrderStatus status = 0)
|
||||
{
|
||||
var order = await this.Query(m => m.OrderNo == no && (status == 0 || m.OrderState == status)).FirstOrDefaultAsync();
|
||||
return order;
|
||||
}
|
||||
|
||||
public async Task<List<UserChargeOrderEntity>> GetOrders(UOrderStatus status = 0, int top = 100)
|
||||
{
|
||||
var orders = await this.Query(m => m.OrderState == status).OrderByDescending(m => m.Id).Take(top).ToListAsync();
|
||||
return orders;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 处理成功的订单
|
||||
/// </summary>
|
||||
/// <param name="order"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> ProcessOrderAccount(UserChargeOrderEntity order)
|
||||
{
|
||||
if (order.OrderState != UOrderStatus.PayOk) return false;
|
||||
|
||||
var request = new UpdateAmountRequest()
|
||||
{
|
||||
Amount = order.OrderAmount,
|
||||
AttchInfo = order.OrderNo,
|
||||
OpAmountType = ScoreType.UserCharge,
|
||||
OperateUserName = order.UserName,
|
||||
UserId = order.UserId
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var ret = await m_UserService.UpdateAmount(request);
|
||||
if (ret.Code == ResultCode.C_SUCCESS)
|
||||
{
|
||||
order.OrderState = UOrderStatus.Complete;
|
||||
await this.Update(order);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Error("处理充值订单", ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,70 +1,70 @@
|
||||
using Hncore.Infrastructure.Service;
|
||||
using Hncore.Pass.BaseInfo.Models;
|
||||
using Hncore.Pass.BaseInfo.Request.User;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using Hncore.Infrastructure.Extension;
|
||||
using Hncore.Infrastructure.EntitiesExtension;
|
||||
using Hncore.Infrastructure.Data;
|
||||
using System.Linq;
|
||||
|
||||
namespace Hncore.Pass.BaseInfo.Service
|
||||
{
|
||||
public class UserScoreService : ServiceBase<UserScore>, IFindService
|
||||
{
|
||||
private UserDbContext m_DbContext;
|
||||
public UserScoreService(UserDbContext dbContext, IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
|
||||
{
|
||||
m_DbContext = dbContext;
|
||||
}
|
||||
|
||||
public bool ExistTaoBaoScore(string orderNo)
|
||||
{
|
||||
return this.Exist(m => m.ScoreType == ScoreType.TaoBaoAdd && m.Remark == orderNo);
|
||||
}
|
||||
|
||||
public async Task<List<UserScore>> Details(int userId)
|
||||
{
|
||||
return await this.Query(m => m.UserId == userId).OrderByDescending(m => m.Id).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<PageData<UserScore>> Details(QueryAmountRequest request)
|
||||
{
|
||||
|
||||
Expression<Func<UserScore, bool>> exp = m => true;
|
||||
|
||||
if (request.KeyWord.Has())
|
||||
{
|
||||
exp = exp.And(m => m.UserName.Contains(request.KeyWord) || m.OperateUserName.Contains(request.KeyWord) || m.ScoreTypeName.Contains(request.KeyWord) || m.Remark.Contains(request.KeyWord));
|
||||
}
|
||||
if (request.Type.HasValue&&request.Type>0)
|
||||
{
|
||||
exp = exp.And(m => m.ScoreType == (ScoreType)request.Type);
|
||||
}
|
||||
|
||||
return await this.Query(exp).OrderByDescending(m=>m.Id).ListPagerAsync(request.PageSize, request.PageIndex,true);
|
||||
}
|
||||
|
||||
|
||||
public async Task<List<UserScore>> DetailsAll(QueryAmountRequest request)
|
||||
{
|
||||
|
||||
Expression<Func<UserScore, bool>> exp = m => true;
|
||||
|
||||
if (request.KeyWord.Has())
|
||||
{
|
||||
exp = exp.And(m => m.UserName.Contains(request.KeyWord) || m.OperateUserName.Contains(request.KeyWord));
|
||||
}
|
||||
if (request.Type.HasValue && request.Type > 0)
|
||||
{
|
||||
exp = exp.And(m => m.ScoreType == (ScoreType)request.Type);
|
||||
}
|
||||
|
||||
return await this.Query(exp).OrderByDescending(m => m.Id).ToListAsync();
|
||||
}
|
||||
}
|
||||
using Hncore.Infrastructure.Service;
|
||||
using Hncore.Pass.BaseInfo.Models;
|
||||
using Hncore.Pass.BaseInfo.Request.User;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using Hncore.Infrastructure.Extension;
|
||||
using Hncore.Infrastructure.EntitiesExtension;
|
||||
using Hncore.Infrastructure.Data;
|
||||
using System.Linq;
|
||||
|
||||
namespace Hncore.Pass.BaseInfo.Service
|
||||
{
|
||||
public class UserScoreService : ServiceBase<UserScore>, IFindService
|
||||
{
|
||||
private UserDbContext m_DbContext;
|
||||
public UserScoreService(UserDbContext dbContext, IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
|
||||
{
|
||||
m_DbContext = dbContext;
|
||||
}
|
||||
|
||||
public bool ExistTaoBaoScore(string orderNo)
|
||||
{
|
||||
return this.Exist(m => m.ScoreType == ScoreType.TaoBaoAdd && m.Remark == orderNo);
|
||||
}
|
||||
|
||||
public async Task<List<UserScore>> Details(int userId)
|
||||
{
|
||||
return await this.Query(m => m.UserId == userId).OrderByDescending(m => m.Id).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<PageData<UserScore>> Details(QueryAmountRequest request)
|
||||
{
|
||||
|
||||
Expression<Func<UserScore, bool>> exp = m => true;
|
||||
|
||||
if (request.KeyWord.Has())
|
||||
{
|
||||
exp = exp.And(m => m.UserName.Contains(request.KeyWord) || m.OperateUserName.Contains(request.KeyWord) || m.ScoreTypeName.Contains(request.KeyWord) || m.Remark.Contains(request.KeyWord));
|
||||
}
|
||||
if (request.Type.HasValue&&request.Type>0)
|
||||
{
|
||||
exp = exp.And(m => m.ScoreType == (ScoreType)request.Type);
|
||||
}
|
||||
|
||||
return await this.Query(exp).OrderByDescending(m=>m.Id).ListPagerAsync(request.PageSize, request.PageIndex,true);
|
||||
}
|
||||
|
||||
|
||||
public async Task<List<UserScore>> DetailsAll(QueryAmountRequest request)
|
||||
{
|
||||
|
||||
Expression<Func<UserScore, bool>> exp = m => true;
|
||||
|
||||
if (request.KeyWord.Has())
|
||||
{
|
||||
exp = exp.And(m => m.UserName.Contains(request.KeyWord) || m.OperateUserName.Contains(request.KeyWord));
|
||||
}
|
||||
if (request.Type.HasValue && request.Type > 0)
|
||||
{
|
||||
exp = exp.And(m => m.ScoreType == (ScoreType)request.Type);
|
||||
}
|
||||
|
||||
return await this.Query(exp).OrderByDescending(m => m.Id).ToListAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,361 +1,361 @@
|
||||
using Hncore.Infrastructure.Common;
|
||||
using Hncore.Infrastructure.Data;
|
||||
using Hncore.Infrastructure.Extension;
|
||||
using Hncore.Infrastructure.Service;
|
||||
using Hncore.Infrastructure.WebApi;
|
||||
using Hncore.Pass.BaseInfo.Models;
|
||||
using Hncore.Pass.BaseInfo.Request;
|
||||
using Hncore.Pass.BaseInfo.Request.User;
|
||||
using Hncore.Pass.BaseInfo.Response;
|
||||
using JWT;
|
||||
using JWT.Algorithms;
|
||||
using JWT.Serializers;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore.Internal;
|
||||
using Hncore.Infrastructure.SMS;
|
||||
|
||||
namespace Hncore.Pass.BaseInfo.Service
|
||||
{
|
||||
public class UserService : ServiceBase<User>, IFindService
|
||||
{
|
||||
private static readonly AsyncLock _mutex1 = new AsyncLock();
|
||||
private static string _secret = "hncore_yh_lzh_20f_2020_READY";
|
||||
|
||||
private UserDbContext _dbContext;
|
||||
|
||||
private WxAppService m_WxAppService;
|
||||
private WxAppUserService m_WxAppUserService;
|
||||
private UserScoreService m_UserScoreService;
|
||||
private ManageService m_ManageService;
|
||||
|
||||
private static ConcurrentDictionary<int, int> manangeDic = new ConcurrentDictionary<int, int>();
|
||||
public UserService(UserDbContext dbContext
|
||||
, WxAppService _WxAppService
|
||||
, WxAppUserService _WxAppUserService
|
||||
,UserScoreService _UserScoreService
|
||||
, ManageService _ManageService
|
||||
, IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
m_WxAppUserService = _WxAppUserService;
|
||||
m_WxAppService = _WxAppService;
|
||||
m_UserScoreService = _UserScoreService;
|
||||
m_ManageService = _ManageService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过微信的opid登录
|
||||
/// </summary>
|
||||
/// <param name="openId"></param>
|
||||
/// <returns></returns>
|
||||
|
||||
public async Task<UserLoginResponse> LoginAndBindWx(WxLoginRequest request)
|
||||
{
|
||||
|
||||
if (request == null || request.Openid.NotHas() || request.AppId.NotHas())
|
||||
{
|
||||
BusinessException.Throw("登陆信息异常");
|
||||
}
|
||||
|
||||
var existWxUserInfo = await m_WxAppUserService.GetWxAppUserInfo(request.AppId, request.Openid);
|
||||
if (existWxUserInfo == null || existWxUserInfo.UserId == 0)
|
||||
{
|
||||
var wxApp = await m_WxAppService.GetApp(request.AppId);
|
||||
if (wxApp == null)
|
||||
BusinessException.Throw("没有关联公众号");
|
||||
var userEntity = new User()
|
||||
{
|
||||
Name = request.UserName,
|
||||
PhotoUrl = request.HeadImgUrl,
|
||||
Sex = request.Sex,
|
||||
TenantId = wxApp.TenantId,
|
||||
Password = RandomHelper.GetRandomString(6)
|
||||
};
|
||||
await this.Add(userEntity);
|
||||
var wxUserInfo = request.MapTo<WxAppUserEntity>();
|
||||
wxUserInfo.UserId = userEntity.Id;
|
||||
wxUserInfo.TenantId = wxApp.TenantId;
|
||||
wxUserInfo.StoreId = wxApp.StoreId;
|
||||
existWxUserInfo = await m_WxAppUserService.Bind(wxUserInfo);
|
||||
}
|
||||
var user = new User()
|
||||
{
|
||||
TenantId = existWxUserInfo.TenantId,
|
||||
Id = existWxUserInfo.UserId
|
||||
};
|
||||
var ret = LoginInternal(user, existWxUserInfo);
|
||||
ret.MpUser = new WxMpUserModel()
|
||||
{
|
||||
AppId = request.AppId,
|
||||
OpenId = existWxUserInfo.Openid,
|
||||
};
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过微信的opid登录
|
||||
/// </summary>
|
||||
/// <param name="openId"></param>
|
||||
/// <returns></returns>
|
||||
|
||||
public async Task<UserLoginResponse> Login(LoginRequest request)
|
||||
{
|
||||
|
||||
if (request == null || request.Logincode.NotHas() || request.Password.NotHas())
|
||||
{
|
||||
BusinessException.Throw("用户名或者密码为空");
|
||||
}
|
||||
var userInfo = await this.Query(m => (m.Phone == request.Logincode && m.Password == HashPassword(request.Password))||(m.LoginCode == request.Logincode && m.Password == HashPassword(request.Password))||(m.TaoBao == request.Logincode && m.Password == HashPassword(request.Password))).FirstOrDefaultAsync();
|
||||
if (userInfo == null)
|
||||
{
|
||||
BusinessException.Throw("用户名或者密码不正确");
|
||||
}
|
||||
var ret = LoginInternal(userInfo);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
public UserLoginResponse LoginInternal(User manage, WxAppUserEntity wxUser)
|
||||
{
|
||||
var tokenDic = new Dictionary<string, object>()
|
||||
{
|
||||
{"LoginName", manage.LoginCode},
|
||||
{ "Name", wxUser.NickName},
|
||||
{"UserId", manage.Id},
|
||||
{"TenantId", manage.TenantId},
|
||||
{"OpenId", wxUser.Openid},
|
||||
{"AppType", wxUser.AppType},
|
||||
{"AppId", wxUser.Appid},
|
||||
{"StoreId", wxUser.StoreId},
|
||||
};
|
||||
var token = GenerateToken(tokenDic);
|
||||
var response = new UserLoginModel().FromEntity(manage);
|
||||
return new UserLoginResponse()
|
||||
{
|
||||
Token = token,
|
||||
User = response
|
||||
};
|
||||
}
|
||||
private UserLoginResponse LoginInternal(User manage)
|
||||
{
|
||||
var tokenDic = new Dictionary<string, object>()
|
||||
{
|
||||
{"LoginName", manage.LoginCode},
|
||||
{ "Name", manage.Name},
|
||||
{"UserId", manage.Id},
|
||||
{"TenantId", manage.TenantId},
|
||||
{"OpenId", ""},
|
||||
{"AppType", ""},
|
||||
{"AppId", ""},
|
||||
{"StoreId", "0"},
|
||||
};
|
||||
var token = GenerateToken(tokenDic);
|
||||
var response = new UserLoginModel().FromEntity(manage);
|
||||
return new UserLoginResponse()
|
||||
{
|
||||
Token = token,
|
||||
User = response
|
||||
};
|
||||
}
|
||||
|
||||
private static string GenerateToken(Dictionary<string, object> param, int timeoutMinutes = 180)
|
||||
{
|
||||
IJwtAlgorithm algorithm = new HMACSHA256Algorithm();
|
||||
IJsonSerializer serializer = new JsonNetSerializer();
|
||||
IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
|
||||
IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder);
|
||||
|
||||
long now = DateTimeHelper.ToUnixTimestamp(DateTime.Now);
|
||||
|
||||
param["iat"] = now; //签发时间
|
||||
param["exp"] = DateTimeHelper.ToUnixTimestamp(DateTime.Now.AddDays(10)); //now + Math.Max(0, timeoutMinutes) * 60; //过期时间
|
||||
|
||||
var token = encoder.Encode(param, _secret);
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
|
||||
public static string HashPassword(string password)
|
||||
{
|
||||
using (MD5 md5 = MD5.Create())
|
||||
{
|
||||
byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(password));
|
||||
return Convert.ToBase64String(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ApiResult> Regist(User entity)
|
||||
{
|
||||
if (entity.LoginCode.NotHas() || entity.Phone.NotHas())
|
||||
{
|
||||
return new ApiResult(ResultCode.C_LONGIN_NAME_ERROR, "账号或者手机号为空");
|
||||
}
|
||||
|
||||
if (this.Exist(m => m.LoginCode == entity.LoginCode || m.Phone == entity.Phone || m.TaoBao == entity.Phone))
|
||||
{
|
||||
return new ApiResult(ResultCode.C_ALREADY_EXISTS_ERROR, "该账号或者手机号被注册了");
|
||||
}
|
||||
entity.Password = HashPassword(entity.Password);
|
||||
entity.id_code="";
|
||||
|
||||
entity = await this.Add(entity);
|
||||
|
||||
await RandomAssignManager(entity.Id);
|
||||
|
||||
return new ApiResult(entity);
|
||||
}
|
||||
|
||||
public async Task<User> GetByPhone(string phone)
|
||||
{
|
||||
var entity = await this.Query(m => m.Phone == phone).FirstOrDefaultAsync();
|
||||
return entity;
|
||||
}
|
||||
|
||||
public async Task<ApiResult> UpdatePwd(int userId, string oldPwd, string newPwd)
|
||||
{
|
||||
var entity = await this.GetById(userId);
|
||||
if (newPwd.NotHas())
|
||||
{
|
||||
return new ApiResult(ResultCode.C_INVALID_ERROR, "新密码不能为空");
|
||||
}
|
||||
if (entity.Password != HashPassword(oldPwd))
|
||||
{
|
||||
return new ApiResult(ResultCode.C_INVALID_ERROR, "密码不正确");
|
||||
}
|
||||
entity.Password = HashPassword(newPwd);
|
||||
await this.Update(entity);
|
||||
return new ApiResult(entity);
|
||||
}
|
||||
|
||||
public async Task<ApiResult> UpdatePwd(User entity, string newPwd)
|
||||
{
|
||||
if (newPwd.NotHas())
|
||||
{
|
||||
return new ApiResult(ResultCode.C_INVALID_ERROR, "新密码不能为空");
|
||||
}
|
||||
entity.Password = HashPassword(newPwd);
|
||||
await this.Update(entity);
|
||||
return new ApiResult(ResultCode.C_SUCCESS,"重置成功");
|
||||
}
|
||||
|
||||
public async Task<ApiResult> UpdateAmount(UpdateAmountRequest request,String product="",String package="",String account="")
|
||||
{
|
||||
using (await _mutex1.LockAsync())
|
||||
{
|
||||
var entity = await this.GetById(request.UserId);
|
||||
if (entity == null)
|
||||
return new ApiResult(ResultCode.C_NOT_EXISTS_ERROR, "用户不存在");
|
||||
if (request.Amount <= 0)
|
||||
return new ApiResult(ResultCode.C_INVALID_ERROR, "金额必须大于0");
|
||||
|
||||
var remark = request.AttchInfo;
|
||||
if (product != "") {
|
||||
remark = product+"-"+package+"-"+account;
|
||||
}
|
||||
|
||||
var userScore = new UserScore()
|
||||
{
|
||||
UserId = request.UserId,
|
||||
UserName = entity.LoginCode,
|
||||
ScoreType = request.OpAmountType,
|
||||
ScoreValue = request.Amount,
|
||||
ScoreTypeName = request.OpAmountType.GetEnumDisplayName(),
|
||||
Remark = remark,
|
||||
OperateUserName = request.OperateUserName
|
||||
};
|
||||
|
||||
if (request.OpAmountType == ScoreType.ManagerDeduct || request.OpAmountType == ScoreType.Pay)
|
||||
{
|
||||
if (entity.RestAmount < request.Amount)
|
||||
{
|
||||
return new ApiResult(ResultCode.C_INVALID_ERROR, "余额不足");
|
||||
}
|
||||
userScore.RestAmount1 = entity.RestAmount;
|
||||
entity.RestAmount -= request.Amount;
|
||||
userScore.RestAmount2 = entity.RestAmount;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
userScore.RestAmount1 = entity.RestAmount;
|
||||
entity.RestAmount += request.Amount;
|
||||
userScore.RestAmount2 = entity.RestAmount;
|
||||
}
|
||||
|
||||
using (var tran = await m_DbContextBase.Database.BeginTransactionAsync())
|
||||
{
|
||||
try
|
||||
{
|
||||
await m_UserScoreService.Add(userScore);
|
||||
await this.Update(entity);
|
||||
tran.Commit();
|
||||
return new ApiResult(ResultCode.C_SUCCESS);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
tran.Rollback();
|
||||
LogHelper.Error("UpdateAmount", ex.Message);
|
||||
return new ApiResult(ResultCode.C_INVALID_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> RandomAssignManager(int userId)
|
||||
{
|
||||
var userEntity = await this.GetById(userId);
|
||||
var manage = await _dbContext.Set<Manager>()
|
||||
.Where(m => m.RoleId == 100).ToListAsync();
|
||||
|
||||
if (manage == null || manage.Count == 0)
|
||||
return false;
|
||||
|
||||
manage.ForEach(m =>
|
||||
{
|
||||
|
||||
if (!manangeDic.ContainsKey(m.Id))
|
||||
manangeDic[m.Id] = 1;
|
||||
|
||||
|
||||
});
|
||||
|
||||
var removeIds = manangeDic.Where(m => !manage.Select(p => p.Id).Contains(m.Key));
|
||||
|
||||
|
||||
foreach (var kv in removeIds)
|
||||
{
|
||||
manangeDic.TryRemove(kv.Key, out int data);
|
||||
}
|
||||
|
||||
var minKv = manangeDic.OrderBy(m => m.Value).FirstOrDefault();
|
||||
|
||||
//获取管理员信息
|
||||
var manger_info = manage.FirstOrDefault(m => m.Id == minKv.Key);
|
||||
|
||||
userEntity.ManagerId = minKv.Key;
|
||||
userEntity.ManagerName = manger_info.RealName;
|
||||
|
||||
await this.Update(userEntity);
|
||||
|
||||
manangeDic[minKv.Key] = manangeDic[minKv.Key] + 1;
|
||||
|
||||
if (manger_info.RoleId == 100) {
|
||||
AliSmsService.Send("SMS_462001365", new { name = manger_info.RealName,phone=userEntity.Phone }, "聚IP", manger_info.Phone);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
using Hncore.Infrastructure.Common;
|
||||
using Hncore.Infrastructure.Data;
|
||||
using Hncore.Infrastructure.Extension;
|
||||
using Hncore.Infrastructure.Service;
|
||||
using Hncore.Infrastructure.WebApi;
|
||||
using Hncore.Pass.BaseInfo.Models;
|
||||
using Hncore.Pass.BaseInfo.Request;
|
||||
using Hncore.Pass.BaseInfo.Request.User;
|
||||
using Hncore.Pass.BaseInfo.Response;
|
||||
using JWT;
|
||||
using JWT.Algorithms;
|
||||
using JWT.Serializers;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore.Internal;
|
||||
using Hncore.Infrastructure.SMS;
|
||||
|
||||
namespace Hncore.Pass.BaseInfo.Service
|
||||
{
|
||||
public class UserService : ServiceBase<User>, IFindService
|
||||
{
|
||||
private static readonly AsyncLock _mutex1 = new AsyncLock();
|
||||
private static string _secret = "hncore_yh_lzh_20f_2020_READY";
|
||||
|
||||
private UserDbContext _dbContext;
|
||||
|
||||
private WxAppService m_WxAppService;
|
||||
private WxAppUserService m_WxAppUserService;
|
||||
private UserScoreService m_UserScoreService;
|
||||
private ManageService m_ManageService;
|
||||
|
||||
private static ConcurrentDictionary<int, int> manangeDic = new ConcurrentDictionary<int, int>();
|
||||
public UserService(UserDbContext dbContext
|
||||
, WxAppService _WxAppService
|
||||
, WxAppUserService _WxAppUserService
|
||||
,UserScoreService _UserScoreService
|
||||
, ManageService _ManageService
|
||||
, IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
m_WxAppUserService = _WxAppUserService;
|
||||
m_WxAppService = _WxAppService;
|
||||
m_UserScoreService = _UserScoreService;
|
||||
m_ManageService = _ManageService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过微信的opid登录
|
||||
/// </summary>
|
||||
/// <param name="openId"></param>
|
||||
/// <returns></returns>
|
||||
|
||||
public async Task<UserLoginResponse> LoginAndBindWx(WxLoginRequest request)
|
||||
{
|
||||
|
||||
if (request == null || request.Openid.NotHas() || request.AppId.NotHas())
|
||||
{
|
||||
BusinessException.Throw("登陆信息异常");
|
||||
}
|
||||
|
||||
var existWxUserInfo = await m_WxAppUserService.GetWxAppUserInfo(request.AppId, request.Openid);
|
||||
if (existWxUserInfo == null || existWxUserInfo.UserId == 0)
|
||||
{
|
||||
var wxApp = await m_WxAppService.GetApp(request.AppId);
|
||||
if (wxApp == null)
|
||||
BusinessException.Throw("没有关联公众号");
|
||||
var userEntity = new User()
|
||||
{
|
||||
Name = request.UserName,
|
||||
PhotoUrl = request.HeadImgUrl,
|
||||
Sex = request.Sex,
|
||||
TenantId = wxApp.TenantId,
|
||||
Password = RandomHelper.GetRandomString(6)
|
||||
};
|
||||
await this.Add(userEntity);
|
||||
var wxUserInfo = request.MapTo<WxAppUserEntity>();
|
||||
wxUserInfo.UserId = userEntity.Id;
|
||||
wxUserInfo.TenantId = wxApp.TenantId;
|
||||
wxUserInfo.StoreId = wxApp.StoreId;
|
||||
existWxUserInfo = await m_WxAppUserService.Bind(wxUserInfo);
|
||||
}
|
||||
var user = new User()
|
||||
{
|
||||
TenantId = existWxUserInfo.TenantId,
|
||||
Id = existWxUserInfo.UserId
|
||||
};
|
||||
var ret = LoginInternal(user, existWxUserInfo);
|
||||
ret.MpUser = new WxMpUserModel()
|
||||
{
|
||||
AppId = request.AppId,
|
||||
OpenId = existWxUserInfo.Openid,
|
||||
};
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过微信的opid登录
|
||||
/// </summary>
|
||||
/// <param name="openId"></param>
|
||||
/// <returns></returns>
|
||||
|
||||
public async Task<UserLoginResponse> Login(LoginRequest request)
|
||||
{
|
||||
|
||||
if (request == null || request.Logincode.NotHas() || request.Password.NotHas())
|
||||
{
|
||||
BusinessException.Throw("用户名或者密码为空");
|
||||
}
|
||||
var userInfo = await this.Query(m => (m.Phone == request.Logincode && m.Password == HashPassword(request.Password))||(m.LoginCode == request.Logincode && m.Password == HashPassword(request.Password))||(m.TaoBao == request.Logincode && m.Password == HashPassword(request.Password))).FirstOrDefaultAsync();
|
||||
if (userInfo == null)
|
||||
{
|
||||
BusinessException.Throw("用户名或者密码不正确");
|
||||
}
|
||||
var ret = LoginInternal(userInfo);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
public UserLoginResponse LoginInternal(User manage, WxAppUserEntity wxUser)
|
||||
{
|
||||
var tokenDic = new Dictionary<string, object>()
|
||||
{
|
||||
{"LoginName", manage.LoginCode},
|
||||
{ "Name", wxUser.NickName},
|
||||
{"UserId", manage.Id},
|
||||
{"TenantId", manage.TenantId},
|
||||
{"OpenId", wxUser.Openid},
|
||||
{"AppType", wxUser.AppType},
|
||||
{"AppId", wxUser.Appid},
|
||||
{"StoreId", wxUser.StoreId},
|
||||
};
|
||||
var token = GenerateToken(tokenDic);
|
||||
var response = new UserLoginModel().FromEntity(manage);
|
||||
return new UserLoginResponse()
|
||||
{
|
||||
Token = token,
|
||||
User = response
|
||||
};
|
||||
}
|
||||
private UserLoginResponse LoginInternal(User manage)
|
||||
{
|
||||
var tokenDic = new Dictionary<string, object>()
|
||||
{
|
||||
{"LoginName", manage.LoginCode},
|
||||
{ "Name", manage.Name},
|
||||
{"UserId", manage.Id},
|
||||
{"TenantId", manage.TenantId},
|
||||
{"OpenId", ""},
|
||||
{"AppType", ""},
|
||||
{"AppId", ""},
|
||||
{"StoreId", "0"},
|
||||
};
|
||||
var token = GenerateToken(tokenDic);
|
||||
var response = new UserLoginModel().FromEntity(manage);
|
||||
return new UserLoginResponse()
|
||||
{
|
||||
Token = token,
|
||||
User = response
|
||||
};
|
||||
}
|
||||
|
||||
private static string GenerateToken(Dictionary<string, object> param, int timeoutMinutes = 180)
|
||||
{
|
||||
IJwtAlgorithm algorithm = new HMACSHA256Algorithm();
|
||||
IJsonSerializer serializer = new JsonNetSerializer();
|
||||
IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
|
||||
IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder);
|
||||
|
||||
long now = DateTimeHelper.ToUnixTimestamp(DateTime.Now);
|
||||
|
||||
param["iat"] = now; //签发时间
|
||||
param["exp"] = DateTimeHelper.ToUnixTimestamp(DateTime.Now.AddDays(10)); //now + Math.Max(0, timeoutMinutes) * 60; //过期时间
|
||||
|
||||
var token = encoder.Encode(param, _secret);
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
|
||||
public static string HashPassword(string password)
|
||||
{
|
||||
using (MD5 md5 = MD5.Create())
|
||||
{
|
||||
byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(password));
|
||||
return Convert.ToBase64String(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ApiResult> Regist(User entity)
|
||||
{
|
||||
if (entity.LoginCode.NotHas() || entity.Phone.NotHas())
|
||||
{
|
||||
return new ApiResult(ResultCode.C_LONGIN_NAME_ERROR, "账号或者手机号为空");
|
||||
}
|
||||
|
||||
if (this.Exist(m => m.LoginCode == entity.LoginCode || m.Phone == entity.Phone || m.TaoBao == entity.Phone))
|
||||
{
|
||||
return new ApiResult(ResultCode.C_ALREADY_EXISTS_ERROR, "该账号或者手机号被注册了");
|
||||
}
|
||||
entity.Password = HashPassword(entity.Password);
|
||||
entity.id_code="";
|
||||
|
||||
entity = await this.Add(entity);
|
||||
|
||||
await RandomAssignManager(entity.Id);
|
||||
|
||||
return new ApiResult(entity);
|
||||
}
|
||||
|
||||
public async Task<User> GetByPhone(string phone)
|
||||
{
|
||||
var entity = await this.Query(m => m.Phone == phone).FirstOrDefaultAsync();
|
||||
return entity;
|
||||
}
|
||||
|
||||
public async Task<ApiResult> UpdatePwd(int userId, string oldPwd, string newPwd)
|
||||
{
|
||||
var entity = await this.GetById(userId);
|
||||
if (newPwd.NotHas())
|
||||
{
|
||||
return new ApiResult(ResultCode.C_INVALID_ERROR, "新密码不能为空");
|
||||
}
|
||||
if (entity.Password != HashPassword(oldPwd))
|
||||
{
|
||||
return new ApiResult(ResultCode.C_INVALID_ERROR, "密码不正确");
|
||||
}
|
||||
entity.Password = HashPassword(newPwd);
|
||||
await this.Update(entity);
|
||||
return new ApiResult(entity);
|
||||
}
|
||||
|
||||
public async Task<ApiResult> UpdatePwd(User entity, string newPwd)
|
||||
{
|
||||
if (newPwd.NotHas())
|
||||
{
|
||||
return new ApiResult(ResultCode.C_INVALID_ERROR, "新密码不能为空");
|
||||
}
|
||||
entity.Password = HashPassword(newPwd);
|
||||
await this.Update(entity);
|
||||
return new ApiResult(ResultCode.C_SUCCESS,"重置成功");
|
||||
}
|
||||
|
||||
public async Task<ApiResult> UpdateAmount(UpdateAmountRequest request,String product="",String package="",String account="")
|
||||
{
|
||||
using (await _mutex1.LockAsync())
|
||||
{
|
||||
var entity = await this.GetById(request.UserId);
|
||||
if (entity == null)
|
||||
return new ApiResult(ResultCode.C_NOT_EXISTS_ERROR, "用户不存在");
|
||||
if (request.Amount <= 0)
|
||||
return new ApiResult(ResultCode.C_INVALID_ERROR, "金额必须大于0");
|
||||
|
||||
var remark = request.AttchInfo;
|
||||
if (product != "") {
|
||||
remark = product+"-"+package+"-"+account;
|
||||
}
|
||||
|
||||
var userScore = new UserScore()
|
||||
{
|
||||
UserId = request.UserId,
|
||||
UserName = entity.LoginCode,
|
||||
ScoreType = request.OpAmountType,
|
||||
ScoreValue = request.Amount,
|
||||
ScoreTypeName = request.OpAmountType.GetEnumDisplayName(),
|
||||
Remark = remark,
|
||||
OperateUserName = request.OperateUserName
|
||||
};
|
||||
|
||||
if (request.OpAmountType == ScoreType.ManagerDeduct || request.OpAmountType == ScoreType.Pay)
|
||||
{
|
||||
if (entity.RestAmount < request.Amount)
|
||||
{
|
||||
return new ApiResult(ResultCode.C_INVALID_ERROR, "余额不足");
|
||||
}
|
||||
userScore.RestAmount1 = entity.RestAmount;
|
||||
entity.RestAmount -= request.Amount;
|
||||
userScore.RestAmount2 = entity.RestAmount;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
userScore.RestAmount1 = entity.RestAmount;
|
||||
entity.RestAmount += request.Amount;
|
||||
userScore.RestAmount2 = entity.RestAmount;
|
||||
}
|
||||
|
||||
using (var tran = await m_DbContextBase.Database.BeginTransactionAsync())
|
||||
{
|
||||
try
|
||||
{
|
||||
await m_UserScoreService.Add(userScore);
|
||||
await this.Update(entity);
|
||||
tran.Commit();
|
||||
return new ApiResult(ResultCode.C_SUCCESS);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
tran.Rollback();
|
||||
LogHelper.Error("UpdateAmount", ex.Message);
|
||||
return new ApiResult(ResultCode.C_INVALID_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> RandomAssignManager(int userId)
|
||||
{
|
||||
var userEntity = await this.GetById(userId);
|
||||
var manage = await _dbContext.Set<Manager>()
|
||||
.Where(m => m.RoleId == 100).ToListAsync();
|
||||
|
||||
if (manage == null || manage.Count == 0)
|
||||
return false;
|
||||
|
||||
manage.ForEach(m =>
|
||||
{
|
||||
|
||||
if (!manangeDic.ContainsKey(m.Id))
|
||||
manangeDic[m.Id] = 1;
|
||||
|
||||
|
||||
});
|
||||
|
||||
var removeIds = manangeDic.Where(m => !manage.Select(p => p.Id).Contains(m.Key));
|
||||
|
||||
|
||||
foreach (var kv in removeIds)
|
||||
{
|
||||
manangeDic.TryRemove(kv.Key, out int data);
|
||||
}
|
||||
|
||||
var minKv = manangeDic.OrderBy(m => m.Value).FirstOrDefault();
|
||||
|
||||
//获取管理员信息
|
||||
var manger_info = manage.FirstOrDefault(m => m.Id == minKv.Key);
|
||||
|
||||
userEntity.ManagerId = minKv.Key;
|
||||
userEntity.ManagerName = manger_info.RealName;
|
||||
|
||||
await this.Update(userEntity);
|
||||
|
||||
manangeDic[minKv.Key] = manangeDic[minKv.Key] + 1;
|
||||
|
||||
if (manger_info.RoleId == 100 && manger_info.TenantId == 1 && DateTime.Now.Hour<23) {
|
||||
AliSmsService.Send("SMS_462001365", new { name = manger_info.RealName,phone=userEntity.Phone }, "聚IP", manger_info.Phone);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,22 @@
|
||||
using Hncore.Infrastructure.Service;
|
||||
using Hncore.Pass.BaseInfo.Models;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Pass.BaseInfo.Service
|
||||
{
|
||||
|
||||
public partial class WxAppService : ServiceBase<WxAppEntity>, IFindService
|
||||
{
|
||||
public WxAppService(UserDbContext dbContext, IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public async Task<WxAppEntity> GetApp(string appId,int appType=1)
|
||||
{
|
||||
return await this.Query(m => m.AppType == appType && m.Appid == appId).FirstOrDefaultAsync();
|
||||
}
|
||||
}
|
||||
using Hncore.Infrastructure.Service;
|
||||
using Hncore.Pass.BaseInfo.Models;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Pass.BaseInfo.Service
|
||||
{
|
||||
|
||||
public partial class WxAppService : ServiceBase<WxAppEntity>, IFindService
|
||||
{
|
||||
public WxAppService(UserDbContext dbContext, IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public async Task<WxAppEntity> GetApp(string appId,int appType=1)
|
||||
{
|
||||
return await this.Query(m => m.AppType == appType && m.Appid == appId).FirstOrDefaultAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,63 +1,63 @@
|
||||
|
||||
using Hncore.Infrastructure.Service;
|
||||
using Hncore.Pass.BaseInfo.Models;
|
||||
using Hncore.Pass.Sells.Service;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Pass.BaseInfo.Service
|
||||
{
|
||||
|
||||
public partial class WxAppUserService : ServiceBase<WxAppUserEntity>, IFindService
|
||||
{
|
||||
WxAppService m_WxAppService;
|
||||
CouponService couponService;
|
||||
public WxAppUserService(WxAppService _WxAppService, CouponService _couponService, UserDbContext dbContext, IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
|
||||
{
|
||||
m_WxAppService = _WxAppService;
|
||||
couponService = _couponService;
|
||||
}
|
||||
|
||||
public async Task<WxAppUserEntity> GetWxAppUserInfo(string appId,string openId, int appType=1)
|
||||
{
|
||||
var wxappInfo = await this.Query(false).FirstOrDefaultAsync(m => m.Appid == appId&&m.AppType==appType&&m.Openid==openId);
|
||||
|
||||
return wxappInfo;
|
||||
}
|
||||
|
||||
public async Task<WxAppUserEntity> Bind(WxAppUserEntity userInfo)
|
||||
{
|
||||
var wxUserInfo = await GetWxUser(userInfo.Appid, userInfo.Openid);
|
||||
if (wxUserInfo == null)
|
||||
{
|
||||
wxUserInfo = await this.Add(userInfo);
|
||||
}
|
||||
else //if (!string.IsNullOrEmpty(userInfo.Openid))
|
||||
{
|
||||
if (wxUserInfo.UserId == 0 && wxUserInfo.IsSubscribe == 1)
|
||||
{
|
||||
await couponService.Give(5, "", userInfo.UserId, 1, Hncore.Pass.Sells.Domain.Enums.CouponOriginType.MP, "¹Ø×¢¹«ÖÚºÅÔùËÍ");
|
||||
}
|
||||
wxUserInfo.UserId = userInfo.UserId;
|
||||
await this.Update(wxUserInfo);
|
||||
}
|
||||
return wxUserInfo;
|
||||
}
|
||||
|
||||
public async Task<WxAppUserEntity> GetWxUser(string appId, int userId)
|
||||
{
|
||||
return await this.Query(m => m.Appid == appId && m.UserId == userId).FirstOrDefaultAsync();
|
||||
}
|
||||
public async Task<WxAppUserEntity> GetWxUser(string appId, string openId)
|
||||
{
|
||||
return await this.Query(m => m.Appid == appId && m.Openid == openId).FirstOrDefaultAsync();
|
||||
}
|
||||
public WxAppUserEntity GetByOpenId(string openId)
|
||||
{
|
||||
return this.Query(m => m.Openid == openId).FirstOrDefault();
|
||||
}
|
||||
}
|
||||
|
||||
using Hncore.Infrastructure.Service;
|
||||
using Hncore.Pass.BaseInfo.Models;
|
||||
using Hncore.Pass.Sells.Service;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Pass.BaseInfo.Service
|
||||
{
|
||||
|
||||
public partial class WxAppUserService : ServiceBase<WxAppUserEntity>, IFindService
|
||||
{
|
||||
WxAppService m_WxAppService;
|
||||
CouponService couponService;
|
||||
public WxAppUserService(WxAppService _WxAppService, CouponService _couponService, UserDbContext dbContext, IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
|
||||
{
|
||||
m_WxAppService = _WxAppService;
|
||||
couponService = _couponService;
|
||||
}
|
||||
|
||||
public async Task<WxAppUserEntity> GetWxAppUserInfo(string appId,string openId, int appType=1)
|
||||
{
|
||||
var wxappInfo = await this.Query(false).FirstOrDefaultAsync(m => m.Appid == appId&&m.AppType==appType&&m.Openid==openId);
|
||||
|
||||
return wxappInfo;
|
||||
}
|
||||
|
||||
public async Task<WxAppUserEntity> Bind(WxAppUserEntity userInfo)
|
||||
{
|
||||
var wxUserInfo = await GetWxUser(userInfo.Appid, userInfo.Openid);
|
||||
if (wxUserInfo == null)
|
||||
{
|
||||
wxUserInfo = await this.Add(userInfo);
|
||||
}
|
||||
else //if (!string.IsNullOrEmpty(userInfo.Openid))
|
||||
{
|
||||
if (wxUserInfo.UserId == 0 && wxUserInfo.IsSubscribe == 1)
|
||||
{
|
||||
await couponService.Give(5, "", userInfo.UserId, 1, Hncore.Pass.Sells.Domain.Enums.CouponOriginType.MP, "¹Ø×¢¹«ÖÚºÅÔùËÍ");
|
||||
}
|
||||
wxUserInfo.UserId = userInfo.UserId;
|
||||
await this.Update(wxUserInfo);
|
||||
}
|
||||
return wxUserInfo;
|
||||
}
|
||||
|
||||
public async Task<WxAppUserEntity> GetWxUser(string appId, int userId)
|
||||
{
|
||||
return await this.Query(m => m.Appid == appId && m.UserId == userId).FirstOrDefaultAsync();
|
||||
}
|
||||
public async Task<WxAppUserEntity> GetWxUser(string appId, string openId)
|
||||
{
|
||||
return await this.Query(m => m.Appid == appId && m.Openid == openId).FirstOrDefaultAsync();
|
||||
}
|
||||
public WxAppUserEntity GetByOpenId(string openId)
|
||||
{
|
||||
return this.Query(m => m.Openid == openId).FirstOrDefault();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user