123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Http;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- using System.Web;
- using MySystem.BsModels;
- using LitJson;
- using Library;
- using Microsoft.IdentityModel.Tokens;
- using System.Text;
- using System.Security.Claims;
- using System.IdentityModel.Tokens.Jwt;
- namespace MySystem.Areas.Api.Controllers
- {
- [Area("Api")]
- [Route("Api/[controller]/[action]")]
- public class SystemSetController : Admin.Controllers.BaseController
- {
- public SystemSetController(IHttpContextAccessor accessor, ILogger<Admin.Controllers.BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
- {
- }
- #region APP启动初始数据
- public JsonResult Detail(string value)
- {
- value = HttpUtility.UrlDecode(value);
- function.WriteLog(DateTime.Now.ToString() + ":APP启动初始数据:" + Request.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString() + "APP接口IP监控");
- JsonData data = JsonMapper.ToObject(value);
- string apptype = data["apptype"].ToString();
- string version = data["version"].ToString();
- string CheckSignResult = CheckSign(value, new string[] { });
- if (CheckSignResult != "1")
- {
- return Json(new AppResultJson() { Status = "-1", Info = CheckSignResult });
- }
- else
- {
- return DetailDo(value);
- }
- }
- public JsonResult DetailDo(string value)
- {
- JsonData data = JsonMapper.ToObject(value);
- string Kind = data["Kind"].ToString();
- string apptype = data["apptype"].ToString();
- string version = data["version"].ToString();
- Dictionary<string, object> Obj = new Dictionary<string, object>();
- List<Dictionary<string, object>> PageSDK = new List<Dictionary<string, object>>();
- SystemSet set = RedisDbconn.Instance.Get<SystemSet>("SystemSet");
- List<FileUpdateInfo> files = MysqlDbconn.Instance.GetFileList(Kind).OrderBy(m => m.Path).OrderBy(m => m.FileName).ToList();
- foreach (FileUpdateInfo subfile in files)
- {
- string Tag = function.MD5_16(subfile.Id.ToString());
- string Url = subfile.Path;
- string FileName = subfile.FileName;
- int ver = subfile.VersionNum;
- Dictionary<string, object> row = new Dictionary<string, object>();
- row.Add("Url", Url);
- row.Add("DownloadUrl", "skin/app/" + Kind + "/" + Url);
- row.Add("FileName", FileName);
- row.Add("Version", ver);
- row.Add("Tag", Tag);
- PageSDK.Add(row);
- }
- List<Dictionary<string, string>> AfterStartPhotos = new List<Dictionary<string, string>>();
- string StartAdPhoto = "static/images/startadphoto.png";
- if (!string.IsNullOrEmpty(set.StartAdPhoto))
- {
- StartAdPhoto = set.StartAdPhoto;
- StartAdPhoto = StartAdPhoto.Substring(StartAdPhoto.IndexOf("static"));
- }
- string StartAdUrl = "";
- Dictionary<string, string> after = new Dictionary<string, string>();
- after.Add("Url", "");
- after.Add("Photo", "static/images/afterstartphoto1.png");
- AfterStartPhotos.Add(after);
- after = new Dictionary<string, string>();
- after.Add("Url", "");
- after.Add("Photo", "static/images/afterstartphoto2.png");
- AfterStartPhotos.Add(after);
- after = new Dictionary<string, string>();
- after.Add("Url", "");
- after.Add("Photo", "static/images/afterstartphoto3.png");
- AfterStartPhotos.Add(after);
- Obj.Add("PageSDK", PageSDK); //页面所需资源引用
- List<Dictionary<string, string>> HtmlVersion = new List<Dictionary<string, string>>();
- List<PageUpdateInfo> pages = MysqlDbconn.Instance.GetPageList(Kind).ToList();
- foreach (PageUpdateInfo page in pages)
- {
- Dictionary<string, string> item = new Dictionary<string, string>();
- string DataId = function.MD5_16(page.ModulePath);
- item.Add("Id", page.Id.ToString());
- item.Add("DataId", DataId);
- item.Add("ModuleVersion", page.ModuleVersion.ToString());
- item.Add("FileName", page.ModulePath);
- HtmlVersion.Add(item);
- }
- Obj.Add("HtmlVersion", HtmlVersion);
- Obj.Add("IsPageSDKZip", "0");
- Obj.Add("PageSDKZip", "/Areas/Admin/Assets/TemplateFile/Areas.zip");
- Obj.Add("StartAdPhoto", string.IsNullOrEmpty(set.StartAdPhoto) ? "" : StartAdPhoto);
- Obj.Add("StartAdUrl", StartAdUrl);
- Obj.Add("AfterStartPhotos", AfterStartPhotos);
- Obj.Add("IsLeftSide", "0"); //是否侧滑
- Obj.Add("LeftSideUrl", "http://www.baidu.com/"); //侧滑页面地址
- if (apptype == "ios")
- {
- Obj.Add("AppClose", "0");
- int check = MysqlDbconn.Instance.GetAppVersionList(Kind).Count(m => m.TerminalKind == "ios");
- if (check > 0)
- {
- AppVersion ver = MysqlDbconn.Instance.GetAppVersionList(Kind).Where(m => m.TerminalKind == "ios").OrderByDescending(m => m.Id).FirstOrDefault();
- if (ver.Status == 1 && versionToNumber(version) < versionToNumber(ver.VersionNum))
- {
- Obj.Add("AppStatus", "1");
- Obj.Add("AppTitle", ver.Title);
- Obj.Add("AppInfo", ver.Info);
- Obj.Add("AppBtn1", ver.ConfirmText);
- Obj.Add("AppBtn2", ver.CancelText);
- Obj.Add("AppBtn1Url", ver.DownloadUrl);
- }
- else
- {
- Obj.Add("AppStatus", "0");
- }
- }
- else
- {
- Obj.Add("AppStatus", "0");
- }
- }
- else
- {
- int check = MysqlDbconn.Instance.GetAppVersionList(Kind).Count(m => m.TerminalKind == "android");
- if (check > 0)
- {
- AppVersion ver = MysqlDbconn.Instance.GetAppVersionList(Kind).Where(m => m.TerminalKind == "android").OrderByDescending(m => m.Id).FirstOrDefault();
- if (ver.Status == 1 && versionToNumber(version) < versionToNumber(ver.VersionNum))
- {
- Obj.Add("AppStatus", "1");
- Obj.Add("AppTitle", ver.Title);
- Obj.Add("AppInfo", ver.Info);
- Obj.Add("AppBtn1", ver.ConfirmText);
- Obj.Add("AppBtn2", ver.CancelText);
- Obj.Add("AppBtn1Url", ver.DownloadUrl);
- }
- else
- {
- Obj.Add("AppStatus", "0");
- }
- }
- else
- {
- Obj.Add("AppStatus", "0");
- }
- }
- Obj.Add("ConnectErrUrl", ""); //网络错误页面
- return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
- }
- #endregion
- #region APP启动初始数据2
- public JsonResult Detail2(string value)
- {
- value = HttpUtility.UrlDecode(value);
- function.WriteLog(DateTime.Now.ToString() + ":APP启动初始数据:" + Request.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString() + "APP接口IP监控");
- JsonData data = JsonMapper.ToObject(value);
- string apptype = data["apptype"].ToString();
- string version = data["version"].ToString();
- string CheckSignResult = CheckSign(value, new string[] { });
- if (CheckSignResult != "1")
- {
- return Json(new AppResultJson() { Status = "-1", Info = CheckSignResult });
- }
- else
- {
- return Detail2Do(value);
- }
- }
- public JsonResult Detail2Do(string value)
- {
- JsonData data = JsonMapper.ToObject(value);
- string Kind = data["Kind"].ToString();
- string apptype = data["apptype"].ToString();
- string version = data["version"].ToString();
- Dictionary<string, object> Obj = new Dictionary<string, object>();
- List<Dictionary<string, object>> PageSDK = new List<Dictionary<string, object>>();
- SystemSet set = RedisDbconn.Instance.Get<SystemSet>("SystemSet");
- List<FileUpdateInfo> files = MysqlDbconn.Instance.GetFileList(Kind).OrderBy(m => m.Path).OrderBy(m => m.FileName).ToList();
- foreach (FileUpdateInfo subfile in files)
- {
- string Tag = function.MD5_16(subfile.Id.ToString());
- string Url = subfile.Path;
- string FileName = subfile.FileName;
- int ver = subfile.VersionNum;
- Dictionary<string, object> row = new Dictionary<string, object>();
- row.Add("Url", Url);
- row.Add("DownloadUrl", OssHost + "skin/app/" + Kind + "/" + Url);
- row.Add("FileName", FileName);
- row.Add("Version", ver);
- row.Add("Tag", Tag);
- PageSDK.Add(row);
- }
- List<Dictionary<string, string>> AfterStartPhotos = new List<Dictionary<string, string>>();
- string StartAdPhoto = "static/images/startadphoto.png";
- if (!string.IsNullOrEmpty(set.StartAdPhoto))
- {
- StartAdPhoto = set.StartAdPhoto;
- StartAdPhoto = StartAdPhoto.Substring(StartAdPhoto.IndexOf("static"));
- }
- string StartAdUrl = "";
- Dictionary<string, string> after = new Dictionary<string, string>();
- after.Add("Url", "");
- after.Add("Photo", "static/images/afterstartphoto1.png");
- AfterStartPhotos.Add(after);
- after = new Dictionary<string, string>();
- after.Add("Url", "");
- after.Add("Photo", "static/images/afterstartphoto2.png");
- AfterStartPhotos.Add(after);
- after = new Dictionary<string, string>();
- after.Add("Url", "");
- after.Add("Photo", "static/images/afterstartphoto3.png");
- AfterStartPhotos.Add(after);
- Obj.Add("PageSDK", PageSDK); //页面所需资源引用
- List<Dictionary<string, string>> HtmlVersion = new List<Dictionary<string, string>>();
- List<PageUpdateInfo> pages = MysqlDbconn.Instance.GetPageList(Kind).ToList();
- foreach (PageUpdateInfo page in pages)
- {
- Dictionary<string, string> item = new Dictionary<string, string>();
- string DataId = function.MD5_16(page.ModulePath);
- item.Add("Id", page.Id.ToString());
- item.Add("DataId", DataId);
- item.Add("ModuleVersion", page.ModuleVersion.ToString());
- item.Add("FileName", page.ModulePath);
- HtmlVersion.Add(item);
- }
- Obj.Add("HtmlVersion", HtmlVersion);
- Obj.Add("IsPageSDKZip", "0");
- Obj.Add("PageSDKZip", "/Areas/Admin/Assets/TemplateFile/Areas.zip");
- Obj.Add("StartAdPhoto", string.IsNullOrEmpty(set.StartAdPhoto) ? "" : StartAdPhoto);
- Obj.Add("StartAdUrl", StartAdUrl);
- Obj.Add("AfterStartPhotos", AfterStartPhotos);
- Obj.Add("IsLeftSide", "0"); //是否侧滑
- Obj.Add("LeftSideUrl", "http://www.baidu.com/"); //侧滑页面地址
- if (apptype == "ios")
- {
- Obj.Add("AppClose", "0");
- int check = MysqlDbconn.Instance.GetAppVersionList(Kind).Count(m => m.TerminalKind == "ios");
- if (check > 0)
- {
- AppVersion ver = MysqlDbconn.Instance.GetAppVersionList(Kind).Where(m => m.TerminalKind == "ios").OrderByDescending(m => m.Id).FirstOrDefault();
- if (ver.Status == 1 && versionToNumber(version) < versionToNumber(ver.VersionNum))
- {
- Obj.Add("AppStatus", "1");
- Obj.Add("AppTitle", ver.Title);
- Obj.Add("AppInfo", ver.Info);
- Obj.Add("AppBtn1", ver.ConfirmText);
- Obj.Add("AppBtn2", ver.CancelText);
- Obj.Add("AppBtn1Url", ver.DownloadUrl);
- }
- else
- {
- Obj.Add("AppStatus", "0");
- }
- }
- else
- {
- Obj.Add("AppStatus", "0");
- }
- }
- else
- {
- int check = MysqlDbconn.Instance.GetAppVersionList(Kind).Count(m => m.TerminalKind == "android");
- if (check > 0)
- {
- AppVersion ver = MysqlDbconn.Instance.GetAppVersionList(Kind).Where(m => m.TerminalKind == "android").OrderByDescending(m => m.Id).FirstOrDefault();
- if (ver.Status == 1 && versionToNumber(version) < versionToNumber(ver.VersionNum))
- {
- Obj.Add("AppStatus", "1");
- Obj.Add("AppTitle", ver.Title);
- Obj.Add("AppInfo", ver.Info);
- Obj.Add("AppBtn1", ver.ConfirmText);
- Obj.Add("AppBtn2", ver.CancelText);
- Obj.Add("AppBtn1Url", ver.DownloadUrl);
- }
- else
- {
- Obj.Add("AppStatus", "0");
- }
- }
- else
- {
- Obj.Add("AppStatus", "0");
- }
- }
- Obj.Add("ConnectErrUrl", ""); //网络错误页面
- return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
- }
- #endregion
- #region 附加方法
-
- List<string> SystemFilesV2;
- private void GetSystemFilesV2(string Kind = "default")
- {
- SystemFilesV2 = new List<string>();
- string dataFilePath = function.getPath("/wwwroot/skin/app/" + Kind + "/");
- System.IO.FileSystemInfo info = new System.IO.DirectoryInfo(dataFilePath);
- scanV2(info);
- }
- private void scanV2(System.IO.FileSystemInfo info)
- {
- if (!info.Exists) return;
- System.IO.DirectoryInfo dir = info as System.IO.DirectoryInfo;
- //不是目录
- if (dir == null) return;
- System.IO.FileSystemInfo[] files = dir.GetFileSystemInfos();
- for (int i = 0; i < files.Length; i++)
- {
- System.IO.FileInfo file = files[i] as System.IO.FileInfo;
- //是文件
- if (file != null)
- {
- string filename = file.FullName.Replace("\\", "/");
- SystemFilesV2.Add(filename.Substring(filename.LastIndexOf("/")));
- }
- else scanV2(files[i]);
- }
- }
- private int versionToNumber(string version)
- {
- string[] versionlist = version.Split('.');
- string left = versionlist[0];
- string mid = versionlist[1];
- if (mid.Length == 1) mid = "00" + mid;
- if (mid.Length == 2) mid = "0" + mid;
- string right = versionlist[2];
- if (right.Length == 1) right = "00" + right;
- if (right.Length == 2) right = "0" + right;
- string result = left + mid + right;
- return int.Parse(result);
- }
- #endregion
- #region 生成APP配置文件
- public string makeAppInitData(string version, string apptype, string Kind = "default")
- {
- string url = Host + "Api/APPSetInfo/List?value={\"Kind\":\"" + Kind + "\",\"PageSize\":\"10\",\"PageNum\":\"1\",\"version\":\"" + version + "\",\"apptype\":\"" + apptype + "\"}";
- string AppInfoList = function.GetWebRequest(url);
- string SystemSet = function.GetWebRequest(Host + "Api/SystemSet/DetailDo?value={\"Kind\":\"" + Kind + "\",\"version\":\"" + version + "\",\"apptype\":\"" + apptype + "\"}");
- string GotoPages = "{";
- string PageInfoList = "{";
- string LibFile = "{";
- List<PageUpdateInfo> newpages = MysqlDbconn.Instance.GetPageList(Kind).OrderBy(m => m.Id).ToList();
- foreach (PageUpdateInfo page in newpages)
- {
- string FileName = page.ModulePath.Split('.')[0];
- PageInfoList += "\"page" + FileName + "\":" + function.GetWebRequest(Host + "Api/PageUpdateInfo/CheckDo?value={\"Kind\":\"" + Kind + "\",\"Url\":\"" + FileName + "\",\"version\":\"" + version + "\",\"apptype\":\"" + apptype + "\"}&ismakefile=0") + ",";
- GotoPages += "\"page" + FileName + "\":\"" + function.CheckNull(page.GotoPages).Trim(',') + "\",";
- string PagePath = "template/app/" + Kind + "/" + page.ModulePath;
- string pageContent = PublicFunction.GetNetFileContent(OssHost + PagePath);
- pageContent = dbconn.Encrypt3DES(pageContent, "*ga34|^7");
- LibFile += "\"page" + FileName + "\":\"" + pageContent + "\",";
- }
- PageInfoList = PageInfoList.TrimEnd(',');
- PageInfoList += "}";
- GotoPages = GotoPages.TrimEnd(',');
- GotoPages += "}";
- LibFile = LibFile.TrimEnd(',');
- LibFile += "}";
- return AppInfoList + "#cut#" + SystemSet + "#cut#" + PageInfoList + "#cut#" + GotoPages + "#cut#" + dbconn.Encrypt3DES(LibFile, "*ga34|^7");
- }
- #endregion
- #region 验证获取jwt的token
- public JsonResult AppCheck(string value)
- {
- value = HttpUtility.UrlDecode(value);
- JsonData data = JsonMapper.ToObject(value);
- string CheckSignResult = CheckSign(value, new string[] { "uuid", "salt" });
- if (CheckSignResult != "1")
- {
- return Json(new AppResultJson() { Status = "-1", Info = CheckSignResult });
- }
- else
- {
- string uuid = data["uuid"].ToString();
- string salt = data["salt"].ToString();
- string u = function.MD5_16(uuid + salt);
- Dictionary<string, object> Obj = new Dictionary<string, object>();
- //生成jwt令牌
- Obj.Add("Token", AppToken(u));
- return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
- }
- }
- public JsonResult AppCheckTest(string value)
- {
- value = DesDecrypt(value);
- JsonData data = JsonMapper.ToObject(value);
- string uuid = data["uuid"].ToString();
- string salt = data["salt"].ToString();
- string u = function.MD5_16(uuid + salt);
- Dictionary<string, object> Obj = new Dictionary<string, object>();
- //生成jwt令牌
- Obj.Add("Token", AppToken(u));
- return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
- }
- public string AppToken(string u)
- {
- string test = function.get_Random(10);
- var securityKey = new SigningCredentials(new SymmetricSecurityKey(Encoding.ASCII.GetBytes(JwtSecret)), SecurityAlgorithms.HmacSha256);
- var claims = new Claim[] {
- new Claim(JwtRegisteredClaimNames.Iss,JwtIss),
- new Claim(JwtRegisteredClaimNames.Aud,test),
- new Claim("Guid", Guid.NewGuid().ToString("D")),
- new Claim(ClaimTypes.Role,"system"),
- new Claim(ClaimTypes.Role,"admin"),
- };
- SecurityToken securityToken = new JwtSecurityToken(
- signingCredentials: securityKey,
- expires: DateTime.Now.AddDays(1),//过期时间
- claims: claims,
- audience: test,
- issuer: u
- );
- RedisDbconn.Instance.Set("utoken:" + u, test);
- //生成jwt令牌
- return new JwtSecurityTokenHandler().WriteToken(securityToken);
- }
- #endregion
- #region app公用包授权
- public JsonResult Auth(string value)
- {
- value = HttpUtility.UrlDecode(value);
- JsonData data = JsonMapper.ToObject(value);
- string CheckSignResult = CheckSign(value, new string[] { "appkey" });
- if (CheckSignResult != "1")
- {
- return Json(new AppResultJson() { Status = "-1", Info = CheckSignResult });
- }
- else
- {
- string appkey = data["appkey"].ToString();
- Dictionary<string, object> Obj = new Dictionary<string, object>();
- if (appkey == "kJweFaqV0Wid7Am0")
- {
- string IsCopyCheck = "2";
- string salt = "BG35JK46";
- string deskey = "*ga34|^7";
- string SignKey = function.MD532(appkey);
- string Sign = function.MD532(IsCopyCheck + salt + deskey + SignKey);
- Obj.Add("SignKey", SignKey);
- Obj.Add("Sign", Sign);
- }
- else
- {
- Obj.Add("SignKey", "");
- Obj.Add("Sign", "");
- }
- return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
- }
- }
- #endregion
- // #region ios配置文件
- // [Route("/apple-app-site-association")]
- // public Dictionary<string, object> iosSetting()
- // {
- // Dictionary<string, object> result = new Dictionary<string, object>();
- // Dictionary<string, object> webcredentials = new Dictionary<string, object>();
- // List<string> apps = new List<string>();
- // apps.Add("TKFY2BZ6Z7.com.cyb888.cyb");
- // webcredentials.Add("apps", apps);
- // result.Add("webcredentials", webcredentials);
- // Dictionary<string, object> applinks = new Dictionary<string, object>();
- // applinks.Add("apps", new List<string>());
- // List<Dictionary<string, object>> details = new List<Dictionary<string, object>>();
- // Dictionary<string, object> detail = new Dictionary<string, object>();
- // detail.Add("appID", "TKFY2BZ6Z7.com.cyb888.cyb");
- // detail.Add("paths", "/ios/*");
- // details.Add(detail);
- // applinks.Add("details", details);
- // result.Add("applinks", applinks);
- // return result;
- // }
- // #endregion
- #region 检查签名是否合法,合法返回1,不合法返回提示信息
- /// <summary>
- /// 检查签名是否合法,合法返回1,不合法返回提示信息
- /// </summary>
- /// <param name="value">请求的参数(json字符串)</param>
- /// <param name="signField">要签名的字段</param>
- /// <returns></returns>
- private string CheckSign(string value, string[] signField)
- {
- JsonData json = JsonMapper.ToObject(value);
- Dictionary<string, string> dic = new Dictionary<string, string>();
- for (int i = 0; i < signField.Length; i++)
- {
- dic.Add(signField[i], json[signField[i]].ToString());
- }
- string sign = json["sign"].ToString(); //客户端签名字符串
- return new Sign().sign(dic, sign);
- }
- #endregion
- }
- }
|