using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Microsoft.AspNetCore.Cors; using Dapper; using System.Data; using System.Data.SqlClient; using System.Web; using System.Text; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using Dapper.Contrib.Extensions; using System.IO; using System.Threading; using System.Dynamic; using NPOI; using NPOI.HPSF; using NPOI.HSSF; using NPOI.HSSF.UserModel; using NPOI.XSSF; using NPOI.XSSF.UserModel; using NPOI.POIFS; using NPOI.Util; using Microsoft.Extensions.Configuration; using System.Security.Policy; using NPOI.SS.Formula.Functions; using static DbTableClass; using System.Runtime.InteropServices.ObjectiveC; using static System.Net.WebRequestMethods; using System.Diagnostics.Eventing.Reader; using static Journeys_WantHome.Controllers.AuthApiController; namespace Journeys_WantHome.Controllers { [Route("Api")] public class ApiController : ControllerBase { private readonly IHttpContextAccessor _httpContextAccessor; DbConn dbConn = new DbConn(); SqlConnection conn = new SqlConnection(GlobalClass.appsettings("ConnectionStrings:SQLConnectionString")); SqlConnection elabConn = new SqlConnection(GlobalClass.appsettings("ConnectionStrings:ElabConnectionString")); public ApiController(IHttpContextAccessor httpContextAccessor) { this._httpContextAccessor = httpContextAccessor; } [Route("kolAddEditDelGet")] public ActionResult kolAddEditDelGet(IFormCollection obj) { kolResult ret = new kolResult(); authToken token = new authToken(this._httpContextAccessor); if (token.user_isLogin == false) { HttpContext.Response.Cookies.Delete("token_key"); ret.ret = "no"; ret.err_code = "99999"; ret.message = "非登入狀態!"; return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); } string method = obj["method"].ToString(); string kol_uid = obj["kol_uid"].ToString(); string kol_name = obj["kol_name"].ToString(); string kol_descript = obj["kol_descript"].ToString(); string kol_contact1 = obj["kol_contact1"].ToString(); string kol_contact2 = obj["kol_contact2"].ToString(); double kol_men_ratio = double.Parse(obj["kol_men_ratio"].ToString()); double kol_women_ratio = double.Parse(obj["kol_women_ratio"].ToString()); double kol_13_17 = double.Parse(obj["kol_13_17"].ToString()); double kol_18_24 = double.Parse(obj["kol_18_24"].ToString()); double kol_25_34 = double.Parse(obj["kol_25_34"].ToString()); double kol_35_44 = double.Parse(obj["kol_35_44"].ToString()); double kol_45_54 = double.Parse(obj["kol_45_54"].ToString()); double kol_55_64 = double.Parse(obj["kol_55_64"].ToString()); double kol_65 = double.Parse(obj["kol_65"].ToString()); string kol_photo = obj["kol_photo"].ToString(); string kolMakeupStr = obj["kolMakeupStr"].ToString().TrimEnd(','); string kolStyleStr = obj["kolStyleStr"].ToString().TrimEnd(','); string kolFansTypeStr = obj["kolFansType"].ToString().TrimEnd(','); string mediaArrayJson = obj["mediaArrayJson"].ToString().TrimEnd(','); if (method == "") { ret.ret = "no"; ret.err_code = "0001"; ret.message = "無method參數!"; return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); } if (method == "add") { string err_msg = ""; if (kol_name == "") { err_msg = "請輸入KOL頻道名稱!\n"; } if (kol_photo == "") { err_msg += "請選擇KOL的照片!\n"; } if (err_msg != "") { ret.ret = "no"; ret.err_code = "0002"; ret.message = err_msg; return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); } kol newKol = new kol(); kol_uid = "kol_" + GlobalClass.CreateRandomCode(12); newKol.kol_uid = kol_uid; newKol.kol_name = kol_name; newKol.kol_descript = kol_descript; newKol.kol_photo = kol_photo; newKol.kol_contact1 = kol_contact1; newKol.kol_contact2 = kol_contact2; newKol.kol_men_ratio = kol_men_ratio; newKol.kol_women_ratio = kol_women_ratio; newKol.kol_13_17 = kol_13_17; newKol.kol_18_24 = kol_18_24; newKol.kol_25_34 = kol_25_34; newKol.kol_35_44 = kol_35_44; newKol.kol_45_54 = kol_45_54; newKol.kol_55_64 = kol_55_64; newKol.kol_65 = kol_65; newKol.kol_create_userId = token.user_id; newKol.kol_modify_userId = token.user_id; string[] kolMakeupArr = kolMakeupStr.Split(','); List kolMakeups = new List(); foreach (string makeup in kolMakeupArr) { optionItem item = conn.QueryFirstOrDefault("select * from optionItem where optionItem_ishide = 'N' and optionItem_uid = @optionItem_uid", new { optionItem_uid = makeup }); if (item != null) { kolMakeup makeupObj = new kolMakeup(); makeupObj.kolMakeup_uid = "kmk_" + GlobalClass.CreateRandomCode(12); makeupObj.kol_uid = kol_uid; makeupObj.option_uid = item.option_uid; makeupObj.optionItem_uid = item.optionItem_uid; kolMakeups.Add(makeupObj); } } string[] kolStyleArr = kolStyleStr.Split(","); List kolStyles = new List(); foreach (string style in kolStyleArr) { optionItem item = conn.QueryFirstOrDefault("select * from optionItem where optionItem_ishide = 'N' and optionItem_uid = @optionItem_uid", new { optionItem_uid = style }); if (item != null) { kolStyle styleObj = new kolStyle(); styleObj.kolStyle_uid = "ks_" + GlobalClass.CreateRandomCode(12); styleObj.kol_uid= kol_uid; styleObj.option_uid = item.option_uid; styleObj.optionItem_uid= item.optionItem_uid; kolStyles.Add(styleObj); } } string[] kolFansTypeArr = kolFansTypeStr.Split(","); List kolFansTypes = new List(); foreach(string fansType in kolFansTypeArr) { optionItem item = conn.QueryFirstOrDefault("select * from optionItem where optionItem_ishide = 'N' and optionItem_uid = @optionItem_uid", new { optionItem_uid = fansType }); if (item != null) { kolFansType fansObj = new kolFansType(); fansObj.kolFansType_uid = "kft_" + GlobalClass.CreateRandomCode(12); fansObj.kol_uid = kol_uid; fansObj.option_uid = item.option_uid; fansObj.optionItem_uid = item.optionItem_uid; kolFansTypes.Add(fansObj); } } dynamic mediaJsonObj; try { mediaJsonObj = JsonConvert.DeserializeObject(mediaArrayJson); } catch (Exception ex) { ret.ret = "no"; ret.err_code = "0003"; ret.message = "media json error" + ex.Message; return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); } List medias = new List(); foreach (dynamic item in mediaJsonObj) { kolMedia mediaObj = new kolMedia(); mediaObj.kolMedia_uid = "km_" + GlobalClass.CreateRandomCode(12); mediaObj.kol_uid = kol_uid; mediaObj.option_uid = item.option_uid; mediaObj.optionItem_uid = item.optionItem_uid; mediaObj.kolMedia_fansNum = int.Parse(item.kolMedia_fansNum.ToString()); mediaObj.kolMedia_accountName = item.kolMedia_accountName; mediaObj.kolMedia_displayName = item.kolMedia_displayName; mediaObj.kolMedia_url = item.kolMedia_url; medias.Add(mediaObj); } conn.Insert(kolMakeups); conn.Insert(kolStyles); conn.Insert(kolFansTypes); conn.Insert(medias); try { conn.Insert(newKol); conn.Close(); ret.kol = new kolDetial(kol_uid); } catch (Exception ex) { ret.ret = "no"; ret.err_code = "1001"; ret.message = ex.Message; return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); } ret.ret = "yes"; return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); } return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); } [Route("kolPhotoUpload")] [RequestFormLimits(MultipartBodyLengthLimit = int.MaxValue)] [RequestSizeLimit(int.MaxValue)] public ActionResult KolPhotoUpload([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 = "avatar_" + GlobalClass.CreateRandomCode(8) + Path.GetExtension(originFileName); string fullPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images/avatar/" + 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 = "/images/avatar/" + newFileName; newFile.size = file.Length; newFile.thumbnailUrl = "/images/avatar/" + newFileName; newFile.deleteUrl = "/images/avatar/" + 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("optionItemList")] public ActionResult OptionItemList(IFormCollection obj) { optionListResult ret = new optionListResult(); authToken token = new authToken(this._httpContextAccessor); if (token.user_isLogin == false) { HttpContext.Response.Cookies.Delete("token_key"); ret.ret = "no"; ret.err_code = "99999"; ret.message = "非登入狀態!"; return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); } string option_uid = obj["option_uid"].ToString(); if (option_uid == "") { ret.ret = "no"; ret.err_code = "00001"; ret.message = "無option_uid資料!"; return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); } ret.optionItems = conn.Query("select * from optionItem where optionItem_ishide = 'N' and option_uid = @option_uid order by optionItem_order ", new { option_uid = option_uid }).ToList(); ret.ret = "yes"; return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); } } public class kolResult { public string ret { get; set; } = ""; public string err_code { get; set; } = ""; public string message { get; set; } = ""; public kolDetial kol { get; set; } = new kolDetial(); } 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; } = ""; } }