public void OnAuthorization(AuthorizationContext filterContext) { if (filterContext == null) { filterContext.Result = new CustomJsonResult() { Data = new ReturnResult(-102, "程序异常,filterContext为null!") }; } var tokenString = filterContext.HttpContext.Request["accessToken"]; var clientId = Convert.ToInt32(filterContext.HttpContext.Request["clientId"]); accessToken = tokenService.GetAccessToken(tokenString); if (null == accessToken) { filterContext.Result = new CustomJsonResult() { Data = new ReturnResult(11, "参数assessToken错误") }; return; } if (accessToken.ClientID != clientId) { filterContext.Result = new CustomJsonResult() { Data = new ReturnResult(12, "参数clientId错误") }; } if (accessToken.Expires < DateTime.Now.Epoch()) { filterContext.Result = new CustomJsonResult() { Data = new ReturnResult(13, "assessToken已过期,请刷新或者重新登录") }; } accessToken.ClientIp = filterContext.HttpContext.Request.GetIp(); accessToken.User = memberService.GetMember(accessToken.UserKeyId); if (null == accessToken.User) { filterContext.Result = new CustomJsonResult() { Data = new ReturnResult(14, "用户已失效,请重新登录") }; } }
public ReturnResult<OauthToken> CreateAccessToken(TdMemberDataContract memberInfo) { if (null == memberInfo || memberInfo.UserKeyId < 1) { return new ReturnResult<OauthToken>(101, null, "参数memberInfo错误"); } OauthToken accessToken = new OauthToken(); accessToken.ClientID = 10001;//目前默认为10001 accessToken.UserKeyId = memberInfo.UserKeyId; accessToken.AccessToken = CreateToken(); accessToken.Expires = DateTime.Now.Epoch() + ACCESS_TOKEN_EXPIRES; accessToken.Scope = "all";//权限默认 accessToken.User = memberInfo; RefreshTokenDataContract refreshToken = new RefreshTokenDataContract(); refreshToken.ClientID = accessToken.ClientID; refreshToken.UserKeyID = accessToken.UserKeyId; refreshToken.RefreshToken = CreateToken(); refreshToken.Expires = DateTime.Now.Epoch() + REFRESH_TOKEN_EXPIRES; accessToken.RefreshToken = refreshToken.RefreshToken; TokenRepository.Instance.InsertAccessToken(accessToken); TokenRepository.Instance.InsertRefreshToken(refreshToken); return new ReturnResult<OauthToken>(accessToken); }
public JsonResult AddAttention(OauthToken token, long attentionUserKeyId) { var model = new ArticleAttentionDataContract(); model.UserKeyId = token.UserKeyId; model.AttentionUserKeyId = attentionUserKeyId; model.AttentionTime = DateTime.Now; return Json(articleAttentionService.AddArticleAttention(model)); }
public JsonResult CollectArticle(OauthToken token, long articleId, int categoryId) { var model = new ArticleCollectDataContract() { UserKeyId = token.UserKeyId, ArticleID = articleId, CollectTime = DateTime.Now }; return Json(articleCollectService.AddArticleCollect(model, categoryId), JsonRequestBehavior.AllowGet); }
public JsonResult GetAttention(OauthToken token, long userKeyId, int page = 1, int pageSize = PAGESIZE) { return Json(articleAttentionService.GetArticleAttention(userKeyId, page, pageSize)); }
public JsonResult DeleteCollect(OauthToken token, long collectId) { return Json(articleCollectService.DeleteArticleCollect(token.UserKeyId, collectId), JsonRequestBehavior.AllowGet); }
public JsonResult UploadImage(OauthToken token, string content) { try { if (string.IsNullOrWhiteSpace(content)) { return Json(new ReturnResult<string>(101, null, "参数content不能为空")); } if (!content.StartsWith("data:image/")) { return Json(new ReturnResult<string>(102, null, "参数content格式错误")); } string imageData = content.Split(',')[1]; var fileName = string.Format("{0}.jpg", Guid.NewGuid().ToString()); var filePath = string.Format("Upload/images/{0}", Guid.NewGuid().ToString("N").CutString(2).ToLower()); var serverPath = Server.MapPath("~/" + filePath); byte[] imageBytes = Convert.FromBase64String(imageData); if (!System.IO.Directory.Exists(serverPath)) { System.IO.Directory.CreateDirectory(serverPath); } using (MemoryStream ms = new MemoryStream(imageBytes)) { Bitmap bmp = new Bitmap(ms); bmp.Save(serverPath + "/" + fileName); } string url = string.Format("http://{0}/{1}/{2}", Request.Url.Authority, filePath, fileName); return Json(new ReturnResult<string>(0, url, "success")); } catch (Exception e) { return Json(new ReturnResult<string>(103, null, "数据解析错误," + e.ToString())); } }
public JsonResult SetCount(OauthToken token, long articleId, int articleCategoryId, int type) { if (type != 2 && type != 3 && type != 6) { return Json(new ReturnResult<bool>(1, false, "参数type错误")); } return Json(articleContentService.UpdateArticleCount(articleId, articleCategoryId, token.UserKeyId, (ArticleCountEnum)type)); }
public JsonResult PublishComment(OauthToken token, long articleId, int articleCategoryId, string content) { var comment = new ArticleCommentDataContract() { ArticleID = articleId, CommentContent = System.Web.HttpUtility.UrlDecode(content) }; comment.UserKeyId = token.UserKeyId; return Json(articleCommentService.InsertArticleComment(comment, articleCategoryId)); }
public JsonResult Publish(OauthToken token, int categoryId, string title, string content) { try { content = Server.UrlDecode(content); if (string.IsNullOrWhiteSpace(content)) { return Json(new ReturnResult<string>(101, null, "参数content不能为空")); } #region 图片解析与上传 try { Regex re = new Regex("<img( ||.*?)src=('|\"|)([^\"|^\']+)('|\"|>| )", RegexOptions.IgnoreCase); MatchCollection matches = re.Matches(content); foreach (Match mh in matches) { string base64Str = mh.Groups[3].Value;//src里面的路径 if (!string.IsNullOrWhiteSpace(base64Str) && base64Str.StartsWith("data:image/")) { string imageData = base64Str.Split(',')[1]; var fileName = string.Format("{0}.jpg", Guid.NewGuid().ToString()); var filePath = string.Format("Upload/images/{0}", Guid.NewGuid().ToString("N").CutString(2).ToLower()); var serverPath = Server.MapPath("~/" + filePath); byte[] imageBytes = Convert.FromBase64String(imageData); if (!System.IO.Directory.Exists(serverPath)) { System.IO.Directory.CreateDirectory(serverPath); } using (MemoryStream ms = new MemoryStream(imageBytes)) { Bitmap bmp = new Bitmap(ms); bmp.Save(serverPath + "/" + fileName); } content = content.Replace(base64Str, string.Format("http://{0}/{1}/{2}", Request.Url.Authority, filePath, fileName)); } } } catch (Exception e) { return Json(new ReturnResult<string>(102, null, "数据解析错误," + e.ToString())); } #endregion ArticleContentDataContract aritcle = new ArticleContentDataContract(); aritcle.CategoryId = categoryId; aritcle.Title = title; aritcle.UserKeyId = token.UserKeyId; aritcle.Author = token.User.NickName ?? token.User.UserName; aritcle.ArticleContent = content; return Json(articleContentService.PublishArticle(aritcle)); } catch (Exception e) { return Json(new ReturnResult<string>(103, null, "数据解析错误," + e.ToString())); } }
public JsonResult GetNoVerify(OauthToken token, int categoryId, int page = 1, int pageSize = PAGESIZE) { return Json(articleContentService.GetNoVerifyArticle(categoryId, token.UserKeyId, page, pageSize)); }
public JsonResult GetMy(OauthToken token, int categoryId, int page = 1, int pageSize = PAGESIZE) { return Json(articleContentService.GetArticleContentList(categoryId, token.UserKeyId, null, page, pageSize)); }
public JsonResult GetCollectArticleCount(OauthToken token) { return Json(articleCollectService.GetArticleCollectCount(token.UserKeyId), JsonRequestBehavior.AllowGet); }
public JsonResult GetCollectArticle(OauthToken token, int page = 1, int pageSize = PAGESIZE) { return Json(articleCollectService.GetArticleCollect(token.UserKeyId, page, pageSize), JsonRequestBehavior.AllowGet); }
public JsonResult GetCurr(OauthToken token) { return Json(new ReturnResult<TdMemberDataContract>(token.User), JsonRequestBehavior.AllowGet); }
public JsonResult Get(OauthToken token, long userKeyId) { var user = memberService.GetMember(userKeyId); return Json(new ReturnResult<TdMemberDataContract>(user), JsonRequestBehavior.AllowGet); }