From 25037c64d8471dbaab774ab5815c770ca8af386f Mon Sep 17 00:00:00 2001 From: "DESKTOP-M4V1LH5\\Jin" Date: Tue, 20 Nov 2018 19:37:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=96=87=E7=AB=A0=E3=80=81?= =?UTF-8?q?=E5=95=86=E5=93=81webapi=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/api/APIBaseController.cs | 118 ++++++++++ .../Controllers/api/ArticleController.cs | 213 +++++++++++++++++ .../Controllers/api/ProductController.cs | 216 ++++++++++++++++++ COMCMS.Web/Startup.cs | 2 +- 4 files changed, 548 insertions(+), 1 deletion(-) create mode 100644 COMCMS.Web/Controllers/api/APIBaseController.cs create mode 100644 COMCMS.Web/Controllers/api/ArticleController.cs create mode 100644 COMCMS.Web/Controllers/api/ProductController.cs diff --git a/COMCMS.Web/Controllers/api/APIBaseController.cs b/COMCMS.Web/Controllers/api/APIBaseController.cs new file mode 100644 index 0000000..be9b404 --- /dev/null +++ b/COMCMS.Web/Controllers/api/APIBaseController.cs @@ -0,0 +1,118 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using COMCMS.Common; +using COMCMS.Web.Common; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using NewLife.Log; + +// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 + +namespace COMCMS.Web.Controllers.api +{ + [Produces("application/json")] + [Route("api/[controller]/[action]")] + [WebAPIHandleError] + public class APIBaseController : Controller + { + public APIBaseController() + { + string url = MyHttpContext.Current.Request.Path; + string querystring = MyHttpContext.Current.Request.QueryString.ToString(); + //XTrace.WriteLine($"访问API:{url}?{querystring}"); + } + #region 通用信息 + public ReJSON reJson = new ReJSON() { code = 40000 }; + #endregion + + #region 验证签名 + public bool CheckSignature(SortedDictionary pars, string signature) + { + if (!MySign.CheckSign(pars, signature)) + { + reJson.code = 40004; + reJson.message = "signature 错误!"; + return false; + } + //判断是否超时 + string timeStamp = pars["timeStamp"]; + //判断时间有效性 + DateTime postTime = Utils.StampToDateTime(timeStamp); + if (postTime < DateTime.UtcNow.AddSeconds(-120))//30秒有效期 + { + reJson.code = 40004; + reJson.message = "数据请求超时!"; + reJson.isReload = 1; + return false; + } + return true; + } + + #endregion + + #region 自动验证QueryString 的签名 + /// + /// 自动验证QueryString 的签名 + /// + /// + public bool AutoCheckQueryStringSignature() + { + var queryStrings = Request.Query; + List keys = new List(); + //string[] keys = HttpContext.Current.Request.QueryString.AllKeys; + if (queryStrings == null) + { + return false; + } + foreach (var item in queryStrings.Keys) + { + keys.Add(item); + } + SortedDictionary pars = new SortedDictionary(); + string signature = ""; + foreach (var k in keys) + { + if (k != "signature") + { + string v = Request.Query[k]; + pars.Add(k, v); + } + else + { + signature = Request.Query[k]; + } + } + //没有签名返回错误 + if (string.IsNullOrEmpty(signature)) + return false; + return CheckSignature(pars, signature); + } + #endregion + } + + #region 通用返回信息 + public class ReJSON + { + /// + /// 返回代码 0 为正确 + /// + public int code { get; set; } = 40000; + + /// + /// 提示语 + /// + public string message { get; set; } + /// + /// 是否重新加载 + /// + public int isReload { get; set; } = 0; + + /// + /// 数据详情 object + /// + public object detail { get; set; } + } + #endregion +} diff --git a/COMCMS.Web/Controllers/api/ArticleController.cs b/COMCMS.Web/Controllers/api/ArticleController.cs new file mode 100644 index 0000000..3f4c115 --- /dev/null +++ b/COMCMS.Web/Controllers/api/ArticleController.cs @@ -0,0 +1,213 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using COMCMS.Common; +using COMCMS.Core; +using COMCMS.Core.Models; +using XCode; +using System.Web; +using COMCMS.Web.Filter; +using NewLife.Log; +using static COMCMS.Web.Models.APIModels; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Senparc.Weixin.WxOpen; + +namespace COMCMS.Web.Controllers.api +{ + public class ArticleController : APIBaseController + { + #region 获取文章列表 + /// + /// 获取文章列表 + /// + /// + /// + /// + /// + [HttpGet] + [CheckFilter] + public object GetArticleList(int kid, int pagesize = 10, int page = 1, string random = "", string timeStamp = "", string signature = "") + { + var where = Article._.IsHide != 1; + if (kid > 0) + where &= Article._.KId == kid; + + if (page <= 0) page = 1; + //计算分页 + int numPerPage, currentPage, startRowIndex; + + numPerPage = pagesize; + currentPage = page; + startRowIndex = (currentPage - 1) * numPerPage; + + IList
list = Article.FindAll(where, Article._.Sequence.Asc(), null, startRowIndex, numPerPage); + long totalCount = Article.FindCount(where, Article._.Sequence.Asc(), null, startRowIndex, numPerPage); + List relist = new List(); + if (list != null && list.Count > 0) + { + foreach (var item in list) + { + relist.Add(new + { + id = item.Id, + title = item.Title, + subTitle = item.SubTitle, + pic = item.Pic, + description = item.Description, + addTime = item.AddTime.ToString("yyyy-MM-dd HH:mm"), + origin = item.Origin + }); + } + } + dynamic re = new { total = totalCount, page = page, list = relist }; + + reJson.code = 0; + reJson.message = "获取成功"; + reJson.detail = re; + return reJson; + + } + #endregion + + #region 获取文章列表 + /// + /// 获取文章列表 + /// + /// + /// + /// + /// + [HttpGet] + [CheckFilter] + public object GetArticleListWidthSub(int kid, int pagesize = 10, int page = 1, string key = "", string random = "", string timeStamp = "", string signature = "") + { + + var where = Article._.IsHide != 1; + if (kid > 0) + { + //获取下属 + List kids = new List(); + kids.Add(kid); + IList subkinds = ArticleCategory.FindByParentID(kid); + if (subkinds != null && subkinds.Count > 0) + { + foreach (var item in subkinds) + { + kids.Add(item.Id); + } + } + where &= Article._.KId.In(kids); + } + if (!string.IsNullOrEmpty(key)) + { + where &= Article._.Title.Contains(key); + } + if (page <= 0) page = 1; + //计算分页 + int numPerPage, currentPage, startRowIndex; + + numPerPage = pagesize; + currentPage = page; + startRowIndex = (currentPage - 1) * numPerPage; + + IList
list = Article.FindAll(where, Article._.Sequence.Asc(), null, startRowIndex, numPerPage); + long totalCount = Article.FindCount(where, Article._.Sequence.Asc(), null, startRowIndex, numPerPage); + List relist = new List(); + if (list != null && list.Count > 0) + { + foreach (var item in list) + { + relist.Add(new + { + id = item.Id, + title = item.Title, + subTitle = item.SubTitle, + pic = item.Pic, + description = item.Description, + addTime = item.AddTime.ToString("yyyy-MM-dd HH:mm"), + origin = item.Origin + }); + } + } + dynamic re = new { total = totalCount, page = page, list = relist }; + + reJson.code = 0; + reJson.message = "获取成功"; + reJson.detail = re; + return reJson; + + } + #endregion + + #region 获取文章详情 + /// + /// 获取文章详情 + /// + /// 文章id + /// + /// + /// + /// + [HttpGet] + [CheckFilter] + public object GetArticleDetail(int id, string random = "", string timeStamp = "", string signature = "") + { + //获取商品 + Article entity = Article.FindById(id); + if (entity == null || entity.IsHide == 1) + { + reJson.code = 40000; + reJson.message = "系统找不到本记录"; + return reJson; + } + entity.Hits++; + entity.Update(); + return entity; + } + #endregion + + #region 获取文章栏目详情,并带下一级栏目 + [HttpGet] + [CheckFilter] + public object GetArticleCategory(int id, string random = "", string timeStamp = "", string signature = "") + { + ArticleCategory entity = ArticleCategory.FindById(id); + if (entity == null) + { + reJson.message = "系统找不到本记录"; + return reJson; + } + + //获取下属栏目 + IList listsub = ArticleCategory.FindAll(ArticleCategory._.PId == entity.Id & ArticleCategory._.IsHide == 0, ArticleCategory._.Rank.Asc(), null, 0, 0); + + List reListSubs = new List(); + if (listsub != null && listsub.Count > 0) + { + foreach (var item in listsub) + { + reListSubs.Add(new + { + kindName = item.KindName, + id = item.Id + }); + } + } + dynamic detail = new + { + category = entity, + sublist = reListSubs + }; + + reJson.code = 0; + reJson.message = "获取成功!"; + reJson.detail = detail; + return reJson; + } + #endregion + } +} diff --git a/COMCMS.Web/Controllers/api/ProductController.cs b/COMCMS.Web/Controllers/api/ProductController.cs new file mode 100644 index 0000000..322343f --- /dev/null +++ b/COMCMS.Web/Controllers/api/ProductController.cs @@ -0,0 +1,216 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using COMCMS.Common; +using COMCMS.Core; +using COMCMS.Core.Models; +using XCode; +using System.Web; +using COMCMS.Web.Filter; +using NewLife.Log; +using static COMCMS.Web.Models.APIModels; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Senparc.Weixin.WxOpen; + +// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 + +namespace COMCMS.Web.Controllers.api +{ + public class ProductController : APIBaseController + { + #region 获取分类列表 + /// + /// 获取分类列表 + /// + /// + /// + /// + /// + /// + /// + [HttpGet] + [CheckFilter] + public object GetCategories(int level, int pid = 0, string random = "", string timeStamp = "", string signature = "") + { + var list = Category.GetListTree(pid, level, false, false); + + List relist = new List(); + if (list != null && list.Count > 0) + { + foreach (var item in list) + { + relist.Add(new + { + id = item.Id, + kindName = item.KindName, + subTitle = item.SubTitle, + pic = item.Pic, + banner = item.BannerImg, + description = item.Description + }); + } + } + reJson.code = 0; + reJson.message = "获取成功"; + reJson.detail = relist; + return reJson; + } + #endregion + + #region 获取商品列表 + /// + /// 获取商品列表 + /// + /// 分类ID + /// 第几页 + /// 每页显示数量 + /// + /// + /// + /// + [HttpGet] + [CheckFilter] + public object GetProductList(int kid, int page, int pageSize = 10, string random = "", string timeStamp = "", string signature = "") + { + var where = Product._.IsHide == 0; + if (kid > 0) + where &= Product._.KId == kid; + + if (page <= 0) page = 1; + //计算分页 + int numPerPage, currentPage, startRowIndex; + + numPerPage = pageSize; + currentPage = page; + startRowIndex = (currentPage - 1) * numPerPage; + + IList list = Product.FindAll(where, Product._.Sequence.Asc(), null, startRowIndex, numPerPage); + long totalCount = Product.FindCount(where, Product._.Sequence.Asc(), null, startRowIndex, numPerPage); + List relist = new List(); + if (list != null && list.Count > 0) + { + foreach (var item in list) + { + relist.Add(new + { + id = item.Id, + title = item.Title, + subTitle = item.SubTitle, + price = item.Price, + marketPrice = item.MarketPrice, + pic = item.Pic, + description = item.Description, + stock = item.Stock, + addTime = item.AddTime.ToString("yyyy-MM-dd HH:mm") + }); + } + } + dynamic re = new { total = totalCount, page = page, list = relist }; + + reJson.code = 0; + reJson.message = "获取成功"; + reJson.detail = re; + return reJson; + } + #endregion + + #region 商品详情 + /// + /// 获取商品详情 + /// + /// 商品id + /// + /// + /// + /// + [HttpGet] + [CheckFilter] + public object GetProductDetail(int id, string random = "", string timeStamp = "", string signature = "") + { + //获取商品 + Product entity = Product.FindById(id); + if (entity == null || entity.IsHide == 1) + { + reJson.code = 40000; + reJson.message = "系统找不到本记录"; + return reJson; + } + entity.Hits++; + entity.Update(); + return entity; + } + #endregion + + #region 搜索商品 + /// + /// 获取商品列表 + /// + /// 关键字 + /// 第几页 + /// 每页显示数量 + /// + /// + /// + /// + [HttpGet] + [CheckFilter] + public object GetSearchProductList(string key, int page, int pageSize = 10, int kid = 0, string random = "", string timeStamp = "", string signature = "") + { + var where = Product._.IsHide == 0; + + if (kid != 0) + { + where &= Product._.KId == kid; + } + + if (!string.IsNullOrEmpty(key)) + { + key = Utils.NoHTML(key); + where &= Product._.Title.Contains(key); + } + + if (page <= 0) page = 1; + //计算分页 + int numPerPage, currentPage, startRowIndex; + + numPerPage = pageSize; + currentPage = page; + startRowIndex = (currentPage - 1) * numPerPage; + + IList list = Product.FindAll(where, Product._.Sequence.Asc(), null, startRowIndex, numPerPage); + long totalCount = Product.FindCount(where, Product._.Sequence.Asc(), null, startRowIndex, numPerPage); + List relist = new List(); + if (list != null && list.Count > 0) + { + foreach (var item in list) + { + relist.Add(new + { + id = item.Id, + title = item.Title, + subTitle = item.SubTitle, + price = item.Price, + marketPrice = item.MarketPrice, + pic = item.Pic, + description = item.Description, + stock = item.Stock, + addTime = item.AddTime.ToString("yyyy-MM-dd HH:mm") + }); + } + } + dynamic re = new { total = totalCount, page = page, list = relist }; + + reJson.code = 0; + reJson.message = "获取成功"; + reJson.detail = re; + return reJson; + } + #endregion + + + } +} diff --git a/COMCMS.Web/Startup.cs b/COMCMS.Web/Startup.cs index 7b7bbba..4451f8d 100644 --- a/COMCMS.Web/Startup.cs +++ b/COMCMS.Web/Startup.cs @@ -47,7 +47,7 @@ namespace COMCMS.Web MenuRegister.Register(); //注入自己的HttpContext - services.AddSingleton(); + //services.AddSingleton(); //services.AddMvc().AddSessionStateTempDataProvider(); //添加Configuration到静态变量