296 lines
11 KiB
C#
296 lines
11 KiB
C#
using Models;
|
|
using Models.ModelItems;
|
|
using Models.View;
|
|
using Services.Extensions;
|
|
using Services.Manager;
|
|
using Services.Tool;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
using Newtonsoft.Json;
|
|
using static Services.Tool.HttpRequestHelp;
|
|
using System.Security.Cryptography;
|
|
|
|
namespace Services.Api
|
|
{
|
|
public class OTApi
|
|
{
|
|
/// <summary>
|
|
/// 登录
|
|
/// </summary>
|
|
/// <param name="keyword">账号</param>
|
|
/// <param name="password">密码</param>
|
|
/// <returns></returns>
|
|
public static ReturnResult<dynamic> GetUserLoginByApi(string keyword, string password, int appid)
|
|
{
|
|
// 登录
|
|
var isdl = GetLogo(keyword, password);
|
|
var res = new ReturnResult<dynamic>();
|
|
if (isdl.Status != 200)
|
|
{
|
|
res.Message = isdl.Message;
|
|
res.Status = isdl.Status;
|
|
return res;
|
|
}
|
|
var user = isdl.Data;
|
|
// 授权
|
|
res = GetAutho(user, appid);
|
|
return res;
|
|
}
|
|
static dynamic ResData(IEnumerable<IEnumerable<IGrouping<int, ResData>>> z)
|
|
{
|
|
List<dynamic> group = new List<dynamic>();
|
|
|
|
foreach (var item in z)
|
|
{
|
|
string groupname = "";
|
|
int groupid = 0;
|
|
List<dynamic> hoteles = new List<dynamic>();
|
|
foreach (var hotel in item)
|
|
{
|
|
string hotelName = string.Empty;
|
|
string Code = "";
|
|
int hotelId = 0;
|
|
List<dynamic> autho = new List<dynamic>();
|
|
foreach (ResData au in hotel)
|
|
{
|
|
if (string.IsNullOrEmpty(hotelName))
|
|
{
|
|
hotelName = au.HotelName;
|
|
Code = au.Code;
|
|
hotelId = au.HotelId;
|
|
}
|
|
groupname = au.HotelGroupsName;
|
|
groupid = au.GroupId;
|
|
autho.Add(new { au.AuthotypeId, au.AuthorityId, au.AuthorityName });
|
|
}
|
|
hoteles.Add(new { Auth = autho, HotelName = hotelName, Code = Code, HotelId = hotelId });
|
|
}
|
|
group.Add(new
|
|
{
|
|
HotelGroupsName = groupname,
|
|
HotelGroupsId = groupid,
|
|
Hotels = hoteles
|
|
}
|
|
);
|
|
}
|
|
return group;
|
|
}
|
|
/// <summary>
|
|
/// 验证登录
|
|
/// </summary>
|
|
/// <param name="keyword"></param>
|
|
/// <param name="password"></param>
|
|
/// <param name="token">是否返回token</param>
|
|
/// <returns></returns>
|
|
private static ReturnResult<UserInfo> GetLogo(string keyword, string password)
|
|
{
|
|
var res = new ReturnResult<UserInfo>();
|
|
try
|
|
{
|
|
//using (AuthorityDB DB = new AuthorityDB())
|
|
//{
|
|
UserInfo user = SqlSugarBase.Db.Queryable<UserInfo>().First(x=>x.Uid== keyword);
|
|
if (user == null)
|
|
{
|
|
res.Status = 25;
|
|
res.Message = "账号不存在~";
|
|
return res;
|
|
}
|
|
UserInfo usernew = new UserInfo() { CreateTime = user.CreateTime, Uid = keyword, Pwd = password };
|
|
usernew = usernew.ComputePasswordHash();
|
|
if (usernew.Pwd != user.Pwd)
|
|
{
|
|
res.Status = 50;
|
|
res.Message = "密码不正确~";
|
|
return res;
|
|
}
|
|
if (user.EndTime < DateTime.Now)
|
|
{
|
|
res.Status = 750;
|
|
res.Message = "授权过期~";
|
|
return res;
|
|
}
|
|
if (user.IsValid == 1)
|
|
{
|
|
res.Status = 100;
|
|
res.Message = "账号冻结~";
|
|
return res;
|
|
}
|
|
user.Pwd = null;
|
|
user.PwdSee = null;
|
|
res.Data = user;
|
|
res.Status = 200;
|
|
//if (token)
|
|
//{
|
|
// res.Message = TokenHelper.GetToken(new Dictionary<string, object>() { { "data", user } });
|
|
//}
|
|
//}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
res.Status = 500;
|
|
res.Data = null;
|
|
res.Message = ex.Message;
|
|
LogHelp.WriteExceptionLog(ex);
|
|
}
|
|
return res;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 微信小程序 密码账号登录
|
|
/// </summary>
|
|
/// <param name="keyword"></param>
|
|
/// <param name="password"></param>
|
|
/// <param name="appid"></param>
|
|
/// <returns></returns>
|
|
public static ReturnResult<dynamic> Wxlog(string keyword, string password, int appid, UserInfo userInfo = null, bool islong = false)
|
|
{
|
|
var resdata = new ReturnResult<dynamic>();
|
|
var result = new ReturnResult<UserInfo>();
|
|
if (userInfo == null){
|
|
result = OTApi.GetLogo(keyword, password);
|
|
}
|
|
else
|
|
{
|
|
result.Status = 200;
|
|
result.Data = userInfo;
|
|
Logs.WriteTimingPlanLog("用户名:" + keyword + "密码:" + password + "登录成功:" + result.Data );
|
|
|
|
}
|
|
//判断登录
|
|
if (result.Status != 200)
|
|
{
|
|
resdata.Message = result.Message;
|
|
resdata.Status = result.Status;
|
|
Logs.WriteTimingPlanLog("用户名:" + keyword + "密码:" + password + "登录失败,信息:" +resdata.Message);
|
|
|
|
return resdata;
|
|
}
|
|
var res = OTApi.GetAutho(result.Data, appid);
|
|
//需要用到写日志
|
|
resdata.Message = res.Message;
|
|
//判断授权
|
|
if (res.Status == 500)
|
|
{
|
|
resdata.Status = res.Status;
|
|
return resdata;
|
|
}
|
|
float day = 1;
|
|
if (islong)
|
|
{
|
|
day = 30;
|
|
}
|
|
resdata.Data = new {
|
|
error = Cache.CacheHelp.cacheSysErrorInfo.Where(x=>x.userid == result.Data.Id).ToList(),
|
|
autho = res.Data,
|
|
userinfo = result.Data,
|
|
token = TokenHelper.GetToken(new Dictionary<string, object>() { { "data", result.Data } }, day)
|
|
};
|
|
resdata.Status = 200;
|
|
return resdata;
|
|
}
|
|
/// <summary>
|
|
/// 授权权限
|
|
/// </summary>
|
|
/// <param name="user"></param>
|
|
/// <param name="appid"></param>
|
|
/// <returns></returns>
|
|
private static ReturnResult<dynamic> GetAutho(UserInfo user, int appid)
|
|
{
|
|
var res = new ReturnResult<dynamic>();
|
|
try
|
|
{
|
|
var app = Cache.CacheHelp.cacheSysApp.FirstOrDefault(x => x.Id == appid);
|
|
|
|
string appname = app == null ? "" : app.AppName;
|
|
if (app == null)
|
|
throw new Exception("App不存在~");
|
|
|
|
//var db = DB.Database.SqlQuery<ResData>(Help.Init(user.Id, appid)).ToList();
|
|
var db = SqlSugarBase.Db.Ado.SqlQuery<ResData>(Help.Init(user.Id, appid)).ToList();
|
|
//var name = db.Distinct().Select(Help.Init(user.Id, appid)).ToList();
|
|
//酒店父辈分组
|
|
var s = db.GroupBy(X => X.GroupId);
|
|
//酒权限分组
|
|
var z = s.Select(x => x.GroupBy(u => u.HotelId));
|
|
res.Data = ResData(z);
|
|
//返回用户Id 头像 权限数量
|
|
res.Status = 200;
|
|
res.Message = new ArrayList { user.Id, user.HeadImg, db.Count, appname };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
res.Status = 500;
|
|
res.Data = null;
|
|
res.Message = ex.Message;
|
|
LogHelp.WriteExceptionLog(ex);
|
|
}
|
|
return res;
|
|
}
|
|
/// <summary>
|
|
/// 微信小程序通过token登录
|
|
/// </summary>
|
|
/// <param name="token"></param>
|
|
/// <param name="appid"></param>
|
|
/// <returns></returns>
|
|
public static ReturnResult<dynamic> WxlogToken(string token,int appid,string UserAgent)
|
|
{
|
|
var app = Cache.CacheHelp.cacheSysApp.FirstOrDefault(x => x.Id == appid);
|
|
var resdata = new ReturnResult<dynamic>();
|
|
var userinfo = new UserInfo();
|
|
var chektoken = TokenHelper.CheckToken(token);
|
|
if (!string.IsNullOrEmpty(chektoken))
|
|
{
|
|
chektoken = chektoken.Replace("{{", "{").Replace("}}", "}");
|
|
userinfo = JsonConvert.DeserializeObject<UserInfo>(chektoken);
|
|
resdata = OTApi.Wxlog(userinfo.Uid, userinfo.Pwd, appid, userinfo);
|
|
|
|
}
|
|
else
|
|
{
|
|
resdata.Status = 1024;
|
|
resdata.Data = null;
|
|
resdata.Message = "token过期~";
|
|
}
|
|
|
|
var Ip = IPHelper.GetIP();
|
|
DbLog log = new DbLog() { Uid = userinfo.Uid, Ip = Ip, Client = UserAgent };
|
|
var Message = resdata.Message;
|
|
Task.Run(() =>
|
|
{
|
|
try
|
|
{
|
|
|
|
|
|
Data loc = HttpRequestHelp.GetIp($@"https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query={log.Ip}&co=&resource_id=6006&oe=utf8");
|
|
if (loc != null && loc.status == 0)
|
|
{
|
|
if (loc.data.Count > 0)
|
|
{
|
|
log.location = loc.data[0].location;
|
|
}
|
|
}
|
|
DbLogServer.WriteDbLog($"{userinfo.Uid}请求应用 {(app == null ? "app[名称获取失败]":app.AppName)}({appid}) 授权" + (resdata.Status == 200 ? "请求成功~" : "请求失败~"), 0, log);
|
|
if (resdata.Status == 200)
|
|
DBlog.WirtDB(Message[2], Message[0], appid);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelp.WriteExceptionLog(ex);
|
|
}
|
|
});
|
|
if (resdata.Status == 200)
|
|
{
|
|
resdata.Message = "请求成功~";
|
|
}
|
|
return resdata;
|
|
}
|
|
}
|
|
}
|