AppVersionController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * App版本管理
  3. */
  4. using System;
  5. using System.Web;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Linq;
  9. using System.Threading.Tasks;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.AspNetCore.Http;
  12. using Microsoft.Extensions.Logging;
  13. using Microsoft.Extensions.Options;
  14. using MySystem.BsModels;
  15. using Library;
  16. using LitJson;
  17. using MySystemLib;
  18. namespace MySystem.Areas.Admin.Controllers
  19. {
  20. [Area("Admin")]
  21. [Route("Admin/[controller]/[action]")]
  22. public class AppVersionController : BaseController
  23. {
  24. public AppVersionController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  25. {
  26. OtherMySqlConn.connstr = ConfigurationManager.AppSettings["BsSqlConnStr"].ToString();
  27. }
  28. #region App版本管理列表
  29. /// <summary>
  30. /// 根据条件查询App版本管理列表
  31. /// </summary>
  32. /// <returns></returns>
  33. public IActionResult Index(AppVersion data, string right)
  34. {
  35. ViewBag.RightInfo = RightInfo;
  36. ViewBag.right = right;
  37. string Condition = "";
  38. if (!string.IsNullOrEmpty(Condition))
  39. {
  40. Condition = Condition.TrimEnd(',');
  41. Condition = ", where: {" + Condition + "}";
  42. }
  43. ViewBag.Condition = Condition;
  44. return View();
  45. }
  46. #endregion
  47. #region 根据条件查询App版本管理列表
  48. /// <summary>
  49. /// App版本管理列表
  50. /// </summary>
  51. /// <returns></returns>
  52. public JsonResult IndexData(AppVersion data, string Kind = "default", int page = 1, int limit = 30)
  53. {
  54. Dictionary<string, string> Fields = new Dictionary<string, string>();
  55. string condition = " and SeoKeyword='" + Kind + "'";
  56. Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).IndexData("AppVersion", Fields, "Id desc", "0", page, limit, condition);
  57. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  58. foreach (Dictionary<string, object> dic in diclist)
  59. {
  60. dic["Status"] = dic["Status"].ToString() == "1" ? "开启" : "关闭";
  61. }
  62. return Json(obj);
  63. }
  64. #endregion
  65. #region 增加App版本管理
  66. /// <summary>
  67. /// 增加或修改App版本管理信息
  68. /// </summary>
  69. /// <returns></returns>
  70. public IActionResult Add(string right)
  71. {
  72. ViewBag.RightInfo = RightInfo;
  73. ViewBag.right = right;
  74. return View();
  75. }
  76. #endregion
  77. #region 增加App版本管理
  78. /// <summary>
  79. /// 增加或修改App版本管理信息
  80. /// </summary>
  81. /// <returns></returns>
  82. [HttpPost]
  83. public string Add(AppVersion data)
  84. {
  85. Dictionary<string, object> Fields = new Dictionary<string, object>();
  86. Fields.Add("TerminalKind", data.TerminalKind); //终端类型
  87. Fields.Add("VersionNum", data.VersionNum); //版本号
  88. Fields.Add("Title", data.Title); //标题
  89. Fields.Add("Info", data.Info); //更新信息
  90. Fields.Add("ConfirmText", data.ConfirmText); //确定按钮文字
  91. Fields.Add("CancelText", data.CancelText); //取消按钮文字
  92. Fields.Add("DownloadUrl", data.DownloadUrl); //更新地址
  93. Fields.Add("SeoTitle", data.SeoTitle);
  94. Fields.Add("SeoKeyword", data.SeoKeyword);
  95. Fields.Add("SeoDescription", data.SeoDescription);
  96. int Id = new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Add("AppVersion", Fields, 0);
  97. AddSysLog(data.Id.ToString(), "AppVersion", "add");
  98. bsdb.SaveChanges();
  99. SetRedis();
  100. return "success";
  101. }
  102. #endregion
  103. #region 修改App版本管理
  104. /// <summary>
  105. /// 增加或修改App版本管理信息
  106. /// </summary>
  107. /// <returns></returns>
  108. public IActionResult Edit(string right, int Id = 0)
  109. {
  110. ViewBag.RightInfo = RightInfo;
  111. ViewBag.right = right;
  112. AppVersion editData = bsdb.AppVersion.FirstOrDefault(m => m.Id == Id) ?? new AppVersion();
  113. ViewBag.data = editData;
  114. return View();
  115. }
  116. #endregion
  117. #region 修改App版本管理
  118. /// <summary>
  119. /// 增加或修改App版本管理信息
  120. /// </summary>
  121. /// <returns></returns>
  122. [HttpPost]
  123. public string Edit(AppVersion data)
  124. {
  125. Dictionary<string, object> Fields = new Dictionary<string, object>();
  126. Fields.Add("TerminalKind", data.TerminalKind); //终端类型
  127. Fields.Add("VersionNum", data.VersionNum); //版本号
  128. Fields.Add("Title", data.Title); //标题
  129. Fields.Add("Info", data.Info); //更新信息
  130. Fields.Add("ConfirmText", data.ConfirmText); //确定按钮文字
  131. Fields.Add("CancelText", data.CancelText); //取消按钮文字
  132. Fields.Add("DownloadUrl", data.DownloadUrl); //更新地址
  133. Fields.Add("SeoTitle", data.SeoTitle);
  134. Fields.Add("SeoKeyword", data.SeoKeyword);
  135. Fields.Add("SeoDescription", data.SeoDescription);
  136. new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Edit("AppVersion", Fields, data.Id);
  137. AddSysLog(data.Id.ToString(), "AppVersion", "update");
  138. bsdb.SaveChanges();
  139. SetRedis();
  140. return "success";
  141. }
  142. #endregion
  143. #region 删除App版本管理信息
  144. /// <summary>
  145. /// 删除App版本管理信息
  146. /// </summary>
  147. /// <returns></returns>
  148. public string Delete(string Id)
  149. {
  150. string[] idlist = Id.Split(new char[] { ',' });
  151. AddSysLog(Id, "AppVersion", "del");
  152. foreach (string subid in idlist)
  153. {
  154. int id = int.Parse(subid);
  155. new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Delete("AppVersion", id);
  156. }
  157. bsdb.SaveChanges();
  158. SetRedis();
  159. return "success";
  160. }
  161. #endregion
  162. #region 开启
  163. /// <summary>
  164. /// 开启
  165. /// </summary>
  166. /// <returns></returns>
  167. public string Open(string Id)
  168. {
  169. string[] idlist = Id.Split(new char[] { ',' });
  170. AddSysLog(Id, "AppVersion", "open");
  171. foreach (string subid in idlist)
  172. {
  173. int id = int.Parse(subid);
  174. Dictionary<string, object> Fields = new Dictionary<string, object>();
  175. Fields.Add("Status", 1);
  176. new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Edit("AppVersion", Fields, id);
  177. }
  178. bsdb.SaveChanges();
  179. SetRedis();
  180. return "success";
  181. }
  182. #endregion
  183. #region 关闭
  184. /// <summary>
  185. /// 关闭
  186. /// </summary>
  187. /// <returns></returns>
  188. public string Close(string Id)
  189. {
  190. string[] idlist = Id.Split(new char[] { ',' });
  191. AddSysLog(Id, "AppVersion", "close");
  192. foreach (string subid in idlist)
  193. {
  194. int id = int.Parse(subid);
  195. Dictionary<string, object> Fields = new Dictionary<string, object>();
  196. Fields.Add("Status", 0);
  197. new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Edit("AppVersion", Fields, id);
  198. }
  199. bsdb.SaveChanges();
  200. SetRedis();
  201. return "success";
  202. }
  203. #endregion
  204. #region 排序
  205. /// <summary>
  206. /// 排序
  207. /// </summary>
  208. /// <param name="Id"></param>
  209. public string Sort(int Id, int Sort)
  210. {
  211. new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Sort("AppVersion", Sort, Id);
  212. AddSysLog(Id.ToString(), "AppVersion", "sort");
  213. SetRedis();
  214. return "success";
  215. }
  216. #endregion
  217. #region 导入数据
  218. /// <summary>
  219. /// 导入数据
  220. /// </summary>
  221. /// <param name="ExcelData"></param>
  222. public string Import(string ExcelData)
  223. {
  224. ExcelData = HttpUtility.UrlDecode(ExcelData);
  225. JsonData list = JsonMapper.ToObject(ExcelData);
  226. for (int i = 1; i < list.Count; i++)
  227. {
  228. JsonData dr = list[i];
  229. string TerminalKind = dr["终端类型"].ToString();
  230. string VersionNum = dr["版本号"].ToString();
  231. string Title = dr["标题"].ToString();
  232. string Info = dr["更新信息"].ToString();
  233. string ConfirmText = dr["确定按钮文字"].ToString();
  234. string CancelText = dr["取消按钮文字"].ToString();
  235. string DownloadUrl = dr["更新地址"].ToString();
  236. bsdb.AppVersion.Add(new AppVersion()
  237. {
  238. CreateDate = DateTime.Now,
  239. UpdateDate = DateTime.Now,
  240. TerminalKind = TerminalKind, //终端类型
  241. VersionNum = VersionNum, //版本号
  242. Title = Title, //标题
  243. Info = Info, //更新信息
  244. ConfirmText = ConfirmText, //确定按钮文字
  245. CancelText = CancelText, //取消按钮文字
  246. DownloadUrl = DownloadUrl, //更新地址
  247. });
  248. bsdb.SaveChanges();
  249. }
  250. AddSysLog("0", "AppVersion", "Import");
  251. return "success";
  252. }
  253. #endregion
  254. #region 导出Excel
  255. /// <summary>
  256. /// 导出Excel
  257. /// </summary>
  258. /// <returns></returns>
  259. public JsonResult ExportExcel(AppVersion data)
  260. {
  261. Dictionary<string, string> Fields = new Dictionary<string, string>();
  262. Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).IndexData("AppVersion", Fields, "Id desc", "0", 1, 20000, "", "TerminalKind,Version,Title,Info,ConfirmText,CancelText,DownloadUrl", false);
  263. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  264. Dictionary<string, object> result = new Dictionary<string, object>();
  265. result.Add("Status", "1");
  266. result.Add("Info", "Excel报表-" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + ".xlsx");
  267. result.Add("Obj", diclist);
  268. Dictionary<string, object> ReturnFields = new Dictionary<string, object>();
  269. ReturnFields.Add("TerminalKind", "终端类型");
  270. ReturnFields.Add("Version", "版本号");
  271. ReturnFields.Add("Title", "标题");
  272. ReturnFields.Add("Info", "更新信息");
  273. ReturnFields.Add("ConfirmText", "确定按钮文字");
  274. ReturnFields.Add("CancelText", "取消按钮文字");
  275. ReturnFields.Add("DownloadUrl", "更新地址");
  276. result.Add("Fields", ReturnFields);
  277. AddSysLog("0", "AppVersion", "ExportExcel");
  278. return Json(result);
  279. }
  280. #endregion
  281. #region 同步数据到redis
  282. public string SyncAll(string Kind = "default")
  283. {
  284. // List<AppVersion> appvers = bsdb.AppVersion.Where(m => m.SeoKeyword == Kind).ToList();
  285. // RedisDbconn.Instance.Clear("AppVersion:" + Kind + "");
  286. // RedisDbconn.Instance.AddList("AppVersion:" + Kind + "", appvers.ToArray());
  287. SetRedis();
  288. return "success";
  289. }
  290. #endregion
  291. #region 设置缓存
  292. private void SetRedis()
  293. {
  294. int count = bsdb.AppVersion.Count();
  295. if(count < 1)
  296. {
  297. return;
  298. }
  299. List<string> Roles = new List<string>();
  300. Roles.Add("consumer");
  301. Roles.Add("merchant");
  302. Roles.Add("creater");
  303. Roles.Add("cyb");
  304. foreach (string Role in Roles)
  305. {
  306. List<AppVersion> list = bsdb.AppVersion.Where(m => m.SeoKeyword == Role).OrderByDescending(m => m.Sort).ThenByDescending(m => m.Id).ToList();
  307. RedisDbconn.Instance.AddList("AppVersion", list.ToArray());
  308. AppVersion ver = list.Where(m => m.TerminalKind == "ios").FirstOrDefault();
  309. if (ver != null)
  310. {
  311. RedisDbconn.Instance.Set("AppVersion:Last:" + Role + ":ios", ver);
  312. }
  313. ver = list.Where(m => m.TerminalKind == "android").FirstOrDefault();
  314. if (ver != null)
  315. {
  316. RedisDbconn.Instance.Set("AppVersion:Last:" + Role + ":android", ver);
  317. }
  318. }
  319. }
  320. #endregion
  321. }
  322. }