using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using Dapper; using System.Data.SqlClient; using static Bremen_ESG.Controllers.ApiController; using static DbTableClass; using SixLabors.Fonts.Tables.AdvancedTypographic; using Dapper.Contrib.Extensions; namespace Bremen_ESG.Controllers { [Route("BackEndApi")] public class BackEndApiController : ControllerBase { private readonly IHttpContextAccessor _httpContextAccessor; DbConn dbConn = new DbConn(); SqlConnection conn = new SqlConnection(GlobalClass.appsettings("ConnectionStrings:SQLConnectionString")); public BackEndApiController(IHttpContextAccessor httpContextAccessor) { this._httpContextAccessor = httpContextAccessor; } [Route("updateTags")] public ActionResult UpdateTags(IFormCollection obj) { updatTagResult ret = new updatTagResult(); authToken token = new authToken(this._httpContextAccessor); if (token.user_isLogin == false) { HttpContext.Response.Cookies.Delete("token_key"); return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); } string search = obj["search"].ToString(); if (search.Length < 2) { ret.ret = "no"; return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); } tags tag = conn.QueryFirstOrDefault("select * from tags where tag_text = @tag_text", new { tag_text = search }); if (tag == null) { tags newTag = new tags(); newTag.tag_uid = "tag_" + GlobalClass.CreateRandomCode(12); newTag.tag_text = search; conn.Insert(newTag); ret.data.id = newTag.tag_uid; ret.data.text = search; ret.ret = "yes"; } else { ret.data.id = tag.tag_uid; ret.data.text = search; ret.ret = "yes"; } return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); } [Route("queryTags")] public ActionResult QueryTags(IFormCollection obj) { tagListResult ret = new tagListResult(); authToken token = new authToken(this._httpContextAccessor); if (token.user_isLogin == false) { HttpContext.Response.Cookies.Delete("token_key"); return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); } string search = obj["search"].ToString(); search = "%" + search + "%"; List tagList = conn.Query("select * from tags where tag_text like @tag_text", new { tag_text = search }).ToList(); foreach (tags tag in tagList) { optionData item = new optionData(); item.id = tag.tag_uid; item.text = tag.tag_text; ret.data.Add(item); } return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); } [Route("mainPhotoUpload")] [RequestFormLimits(MultipartBodyLengthLimit = int.MaxValue)] [RequestSizeLimit(int.MaxValue)] public ActionResult MainPhotoUpload([FromForm(Name = "avatar")] IFormFile file) { authToken token = new authToken(this._httpContextAccessor); if (token.user_isLogin == false) { List files = new List(); errFile newFile = new errFile(); newFile.name = ""; newFile.size = 0; newFile.error = "尚未登入"; files.Add(newFile); fileResult obj = new fileResult(); obj.files = files; return Content(JsonConvert.SerializeObject(files), "application/json;charset=utf-8"); } string originFileName = file.FileName; string newFileName = "mainPhoto_" + GlobalClass.CreateRandomCode(8) + Path.GetExtension(originFileName); string fullPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/upload/main/" + newFileName); try { using (var stream = new FileStream(fullPath, FileMode.Create)) { file.CopyTo(stream); } List files = new List(); uploadFile newFile = new uploadFile(); newFile.name = originFileName; newFile.url = "/upload/main/" + newFileName; newFile.size = file.Length; newFile.thumbnailUrl = "/upload/main/" + newFileName; newFile.deleteUrl = "/upload/main/" + newFileName; files.Add(newFile); fileResult obj = new fileResult(); obj.files = files; return Content(JsonConvert.SerializeObject(obj), "application/json;charset=utf-8"); } catch (Exception ex) { List files = new List(); errFile newFile = new errFile(); newFile.name = originFileName; newFile.size = file.Length; newFile.error = ex.Message; files.Add(newFile); fileResult obj = new fileResult(); obj.files = files; return Content(JsonConvert.SerializeObject(files), "application/json;charset=utf-8"); } } //後台登入 [Route("signin")] public ActionResult Signin(IFormCollection obj) { result ret = new result(); string input_ID = obj["id"].ToString(); string input_PWD = obj["pwd"].ToString(); string sys_ID = GlobalClass.appsettings("Admin:id"); string sys_PWD = GlobalClass.Sha256(GlobalClass.appsettings("Admin:pwd")); if (input_ID == sys_ID && input_PWD == sys_PWD) { DbConn dbConn = new DbConn(); SqlConnection conn = dbConn.sqlConnection(); string token_key = GlobalClass.CreateRandomCode(24); int effCount = conn.Execute("insert into token (token_key, user_uid, user_id, user_perm, token_expireddate) values (@token_key, @user_uid, @user_id, @user_perm, @token_expireddate)", new { token_key = token_key, user_uid = "system", user_id = input_ID, user_perm = "system", token_expireddate = DateTime.Now.AddMinutes(20) }); CookieOptions options = new CookieOptions(); options.Secure = true; options.Expires = DateTime.Now.AddMinutes(30); HttpContext.Response.Cookies.Delete("token_key"); _httpContextAccessor.HttpContext.Response.Cookies.Append("token_key", token_key, options); dbConn.closeConn(); ret.ret = "yes"; } else { ret.ret = "no"; ret.err_code = "0001"; ret.message = "帳號或密碼錯誤!"; } return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); } [Route("newsList")] public ActionResult NewsList(IFormCollection obj) { newResult ret = new newResult(); authToken token = new authToken(this._httpContextAccessor); if (token.user_isLogin == false) { HttpContext.Response.Cookies.Delete("token_key"); ret.ret = "no"; ret.err_code = "9999"; ret.message = "非登入狀態!"; return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); } DbConn dbConn = new DbConn(); SqlConnection conn = dbConn.sqlConnection(); ret.newsList = conn.Query("select * from news order by news_sn desc").ToList(); ret.ret = "yes"; return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); } public class newResult { public string ret = "no"; public string err_code = "0000"; public string message = ""; public List newsList = new List(); } public class fileResult { public object files = new object(); } public class uploadFile { public string name { get; set; } = ""; public long size { get; set; } = 0; public string url { get; set; } = ""; public string thumbnailUrl { get; set; } = ""; public string deleteUrl { get; set; } = ""; public string deleteType { get; set; } = "DELETE"; } public class errFile { public string name { get; set; } = ""; public long size { get; set; } = 0; public string error { get; set; } = ""; } public class updatTagResult { public string ret { get; set; } = "no"; public string err_code { get; set; } = "0000"; public string message { get; set; } = ""; public optionData data = new optionData(); } public class tagListResult { public List data = new List(); } public class optionData { public string id { get; set; } = ""; public string text { get; set; } = ""; } } }