帖子功能加入[发布/更新/评论/置顶/加精/删除]

This commit is contained in:
2018-01-09 23:16:16 +08:00
parent f2cc75b5bd
commit cb3a29991e
20 changed files with 376 additions and 67 deletions

View File

@@ -6,12 +6,15 @@ import java.util.Map;
/**
* Created by Lxyer lxy208@126.com at 2016/8/4 0:13.
*/
public class JsonBean {
public class JBean {
public int code;//全局状态码 -1失败1成功2未登录
public String msg;
public Object obj;
public JsonBean(int code){
public static final JBean success = new JBean(1, "操作成功");
public static final JBean error = new JBean(-1, "操作失败");
public JBean(int code){
this.code = code;
if (code == 1)
this.msg = "操作成功";
@@ -21,11 +24,11 @@ public class JsonBean {
this.msg = "未登录,请前往登录";
}
public JsonBean(int code, String msg){
public JBean(int code, String msg){
this.code = code;
this.msg = msg;
}
public JsonBean(int code, String msg, Object obj){
public JBean(int code, String msg, Object obj){
this.code = code;
this.msg = msg;
this.obj = obj;
@@ -35,7 +38,7 @@ public class JsonBean {
return msg;
}
public JsonBean setMsg(String msg) {
public JBean setMsg(String msg) {
this.msg = msg;
return this;
}
@@ -44,7 +47,7 @@ public class JsonBean {
return code;
}
public JsonBean setCode(int code) {
public JBean setCode(int code) {
this.code = code;
if (code == 1)
this.msg = "操作成功";
@@ -53,12 +56,12 @@ public class JsonBean {
return this;
}
public JsonBean setCode(int code, String msg) {
public JBean setCode(int code, String msg) {
this.code = code;
this.msg = msg;
return this;
}
public JsonBean setCode(int code, String msg, Object obj) {
public JBean setCode(int code, String msg, Object obj) {
this.code = code;
this.msg = msg;
this.obj = obj;
@@ -69,11 +72,11 @@ public class JsonBean {
return obj;
}
public JsonBean setObj(Object obj) {
public JBean setObj(Object obj) {
this.obj = obj;
return this;
}
public <K, V> JsonBean set(Object k, Object v){
public <K, V> JBean set(Object k, Object v){
if (!(obj instanceof Map)){
obj = new HashMap();
}
@@ -86,35 +89,29 @@ public class JsonBean {
下面的是一些快速创建JsonBean的方式
使用中可以使用new 同时也可以直接使用
*/
public static JsonBean success(){
return new JsonBean(1, "操作成功");
public static JBean success(Object obj){
return new JBean(1, "操作成功",obj);
}
public static JsonBean success(Object obj){
return new JsonBean(1, "操作成功",obj);
public static JBean success(int code, String msg){
return new JBean(1, msg);
}
public static JsonBean success(int code, String msg){
return new JsonBean(1, msg);
}
public static JsonBean success(int code, String msg, Object obj){
return new JsonBean(1, msg, obj);
public static JBean success(int code, String msg, Object obj){
return new JBean(1, msg, obj);
}
public static JsonBean error(){
return new JsonBean(-1, "操作失败");
public static JBean error(int code, String msg){
return new JBean(code, msg);
}
public static JsonBean error(int code, String msg){
return new JsonBean(code, msg);
public static JBean error(int code, String msg, Object obj){
return new JBean(code, msg, obj);
}
public static JsonBean error(int code, String msg, Object obj){
return new JsonBean(code, msg, obj);
}
public static JsonBean error(String msg){
return new JsonBean(-1, msg);
public static JBean error(String msg){
return new JBean(-1, msg);
}
@Override
public String toString() {
return "JsonBean{" +
return "JBean{" +
"code=" + code +
", msg='" + msg + '\'' +
", obj=" + obj +

View File

@@ -3,6 +3,7 @@ package com.lxyer.config.interceptor;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.lxyer.config.JBean;
import com.lxyer.model.User;
import javax.servlet.http.HttpServletRequest;
@@ -22,7 +23,8 @@ public class LoginInterceptor implements Interceptor {
User user = controller.getSessionAttr("user");
if (user == null){
if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))){
controller.renderJson(new JBean(-1,"请登录后再尝试"));
return;
}else {
controller.redirect("/user/login");
}

View File

@@ -1,6 +1,7 @@
package com.lxyer.config.route;
import com.jfinal.config.Routes;
import com.lxyer.controller.CommentController;
import com.lxyer.controller.HomeController;
import com.lxyer.controller.JieController;
import com.lxyer.controller.UserController;
@@ -16,5 +17,6 @@ public class SiteRoute extends Routes {
add("/", HomeController.class);
add("/user", UserController.class);
add("/jie", JieController.class);
add("/comment", CommentController.class);
}
}

View File

@@ -0,0 +1,58 @@
package com.lxyer.controller;
import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;
import com.lxyer.config.JBean;
import com.lxyer.config.interceptor.LoginInterceptor;
import com.lxyer.model.Comment;
import com.lxyer.service.CommentService;
/**
* Created by JUECHENG at 2018/1/8 23:07.
*/
public class CommentController extends IController {
CommentService service = CommentService.me;
/**
* 评论列表
*/
public void list(){
Kv kv = getParams("contentId");
Page<Comment> page = Comment.dao.findPage(getPn(), getPs(), kv);
renderJBean(page);
}
/**
* 评论详情
*/
public void info(){
}
/**
* 评论保存
*/
@Before(LoginInterceptor.class)
public void save(){
Comment comment = getModel(Comment.class);
service.save(comment, getUserId());
renderJBean(JBean.success);
}
/**
* todo:更新状态
*/
public void update_status(){
}
/**
* todo:评论点赞
*/
public void support(){
}
}

View File

@@ -2,7 +2,6 @@ package com.lxyer.controller;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.lxyer.model.Content;
import com.lxyer.model.User;

View File

@@ -6,6 +6,7 @@ import com.jfinal.plugin.activerecord.*;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.lxyer.config.E;
import com.lxyer.config.JBean;
import com.lxyer.model.User;
import java.util.ArrayList;
@@ -51,6 +52,10 @@ public class IController extends Controller {
return kv;
}
public void renderJBean(Object obj){
renderJson(new JBean(1, null, obj));
}
public int getPn(){
return getParaToInt("pn", 1);
}
@@ -121,4 +126,11 @@ public class IController extends Controller {
return str;
}
/**
* todo:文件上传
*/
public void upFile(){
}
}

View File

@@ -1,9 +1,12 @@
package com.lxyer.controller;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.kit.Kv;
import com.lxyer.config.JsonBean;
import com.jfinal.plugin.activerecord.Page;
import com.lxyer.config.JBean;
import com.lxyer.config.interceptor.LoginInterceptor;
import com.lxyer.model.Comment;
import com.lxyer.model.Content;
import com.lxyer.service.ContentService;
@@ -12,35 +15,39 @@ import java.util.List;
/**
* Created by JUECHENG at 2018/1/7 16:48.
*/
@Before(LoginInterceptor.class)
public class JieController extends IController{
private ContentService contentService = ContentService.me;
private ContentService service = ContentService.me;
private int userId;
/**
* 帖子详情
*/
@Clear(LoginInterceptor.class)
public void index(){
int contentId = getParaToInt(0);
//ContentInfo content = contentService.contentInfo(sessionid, contentid);
//Sheet<CommentInfo> comments = commentService.commentQuery(request.getSessionid(false) ,contentid, new Flipper().limit(30));
Content content = Content.dao.findFirst(Kv.by("contentId", contentId));
//评论
Page<Comment> comments = Comment.dao.findPage(getPn(), getPs(), Kv.by("contentId", contentId));
//热议
List<Content> hotReply = Content.dao.findPage(1, 8, Kv.by("order", "replyNum DESC")).getList();
setAttr("bean", content);
setAttr("comments", comments);
setAttr("hotReply", hotReply);
//todo: 访问量+1
render("detail.html");
}
/**
* 添加/修改帖子
*/
@Before(LoginInterceptor.class)
public void add(){
setAttr("bean", Content.dao.findById(getParaToInt()));
@@ -50,13 +57,62 @@ public class JieController extends IController{
/**
* 帖子保存
*/
@Before(LoginInterceptor.class)
public void save() {
Content content = getModel(Content.class);
contentService.save(content, getUserId());
service.save(content, getUserId());
renderJson(JsonBean.success());
renderJson(JBean.success);
}
/**
* 帖子删除
*/
public void del(){
JBean bean = new JBean(1);
try {
service.del(getParaToInt("contentId"), getUserId());
} catch (Exception e) {
bean.setCode(-1, e.getMessage());
}
renderJson(bean);
}
/**
* 帖子收藏
*/
public void collect(){
JBean bean = new JBean(1);
Integer contentId = getParaToInt("contentId");
Integer status = getParaToInt("ok", 1);
try {
service.collect(contentId, getUserId(), status);
} catch (Exception e) {
bean.setCode(-1, e.getMessage());
}
renderJson(bean);
}
/**
* 帖子加精/置顶
*/
public void set(){
JBean bean = new JBean(1);
Integer contentId = getParaToInt("id");
String field = getPara("field");
Integer v = getParaToInt("v");
try {
service.upField(contentId, field, v, getUserId());
} catch (Exception e) {
bean.setCode(-1, e.getMessage());
}
renderJson(bean);
}
}

View File

@@ -1,6 +1,6 @@
package com.lxyer.controller;
import com.lxyer.config.JsonBean;
import com.lxyer.config.JBean;
import com.lxyer.model.User;
import com.lxyer.service.UserService;
@@ -15,7 +15,7 @@ public class UserController extends IController {
* 注册
*/
public void create(){
JsonBean bean = new JsonBean(1);
JBean bean = new JBean(1);
String email = getPara("email");
String pwd = getPara("pwd");
String nickname = getPara("nickname");
@@ -39,10 +39,10 @@ public class UserController extends IController {
return;
}else if ("out".equals(para)){
removeSessionAttr("user");
renderJson(JsonBean.success());
renderJson(JBean.success);
return;
}
JsonBean bean = new JsonBean(1);
JBean bean = new JBean(1);
String username = getPara("username");
String pwd = getPara("pwd");
@@ -73,7 +73,7 @@ public class UserController extends IController {
* 修改密码
*/
public void repwd(){
JsonBean bean = new JsonBean(1);
JBean bean = new JBean(1);
String pwd = getPara("pwd");
try {

View File

@@ -7,5 +7,15 @@ import com.lxyer.model.base.BaseActLog;
*/
@SuppressWarnings("serial")
public class ActLog extends BaseActLog<ActLog> {
public static ActLog dao = new ActLog().dao();
@Override
public String sqlSpace() {
return "actLog";
}
@Override
public ActLog getDao() {
return dao;
}
}

View File

@@ -7,5 +7,15 @@ import com.lxyer.model.base.BaseComment;
*/
@SuppressWarnings("serial")
public class Comment extends BaseComment<Comment> {
public static final Comment dao = new Comment().dao();
@Override
public String sqlSpace() {
return "comment";
}
@Override
public Comment getDao() {
return dao;
}
}

View File

@@ -1,5 +1,7 @@
package com.lxyer.model;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.lxyer.model.base.BaseContent;
/**
@@ -18,4 +20,8 @@ public class Content extends BaseContent<Content> {
public Content getDao() {
return dao;
}
public static void upReplyNum(int contentId){
Db.update(Db.getSqlPara("content.upReplyNum", Kv.by("contentId", contentId)));
}
}

View File

@@ -7,7 +7,7 @@ import com.jfinal.plugin.activerecord.IBean;
* Generated by JFinal, do not modify this file.
*/
@SuppressWarnings("serial")
public abstract class BaseActLog<M extends BaseActLog<M>> extends Model<M> implements IBean {
public abstract class BaseActLog<M extends BaseActLog<M>> extends Model<M> implements IBean,IModel<M> {
public void setLogid(java.lang.Integer logid) {
set("logid", logid);

View File

@@ -7,7 +7,7 @@ import com.jfinal.plugin.activerecord.IBean;
* Generated by JFinal, do not modify this file.
*/
@SuppressWarnings("serial")
public abstract class BaseComment<M extends BaseComment<M>> extends Model<M> implements IBean {
public abstract class BaseComment<M extends BaseComment<M>> extends Model<M> implements IBean,IModel<M> {
public void setCommentId(java.lang.Integer commentId) {
set("commentId", commentId);

View File

@@ -0,0 +1,25 @@
package com.lxyer.service;
import com.lxyer.model.Comment;
import com.lxyer.model.Content;
/**
* Created by JUECHENG at 2018/1/9 11:59.
*/
public class CommentService {
public static final CommentService me = new CommentService();
public void save(Comment comment, Integer userId) {
if (comment.getCommentId() == null){
comment.setUserId(userId);
comment.setCreateTime(System.currentTimeMillis());
comment.save();
}else {
comment.update();
}
//更新评论数
Content.upReplyNum(comment.getContentId());
}
}

View File

@@ -1,12 +1,21 @@
package com.lxyer.service;
import com.lxyer.model.Content; /**
import com.jfinal.kit.Kv;
import com.lxyer.model.ActLog;
import com.lxyer.model.Content;
/**
* Created by JUECHENG at 2018/1/7 16:49.
*/
public class ContentService {
public static final ContentService me = new ContentService();
/**
* 帖子保存
* @param content
* @param userId
*/
public void save(Content content, int userId) {
if (content.getContentId() == null) {
content.setCreateTime(System.currentTimeMillis());
@@ -16,4 +25,76 @@ public class ContentService {
content.update();
}
}
/**
* 删除帖子
* @param contentId
* @param userId
* @throws Exception
*/
public void del(Integer contentId, Integer userId) throws Exception {
Content content = Content.dao.findById(contentId);
if (content == null || content.getStatus() == -1) return;
//安全校验
if (userId != 10_0001 && content.getUserId() != userId)
throw new Exception("操作失败:无权操作");
content.setStatus(-1);
content.update();
}
/**
* 帖子收藏
* @param contentId
* @param userId
* @param status
* @throws Exception
*/
public void collect(Integer contentId, Integer userId, int status) throws Exception {
Content content = Content.dao.findById(contentId);
if (content == null)
throw new Exception("操作失败,未查询到相关内容");
Kv kv = Kv.by("tid", contentId).set("userId", userId).set("cate", 2);//cate:2收藏
ActLog actLog = ActLog.dao.findFirst(kv);
if (actLog == null){
actLog = new ActLog();
actLog.setCate(2);
actLog.setTid(contentId);
actLog.setUserId(userId);
actLog.setCreateTime(System.currentTimeMillis());
actLog.save();
}else if (actLog.getStatus() != status){
actLog.setStatus(status == 1 ? 1 : -1);
actLog.setCreateTime(System.currentTimeMillis());
actLog.update();
}
//更新收藏数
}
/**
* 帖子置顶/加精
* @param contentId
* @param field
* @param v
* @param userId
* @throws Exception
*/
public void upField(Integer contentId, String field, Integer v, int userId) throws Exception {
Content content = Content.dao.findById(contentId);
if (content == null)
throw new Exception("操作失败,未查询到相关内容");
if ("top".equals(field) && userId != 10_0001)
throw new Exception("操作失败:无权限进行此操作");
content.set(field, v);
content.update();
}
}