Search in sources :

Example 11 with TipException

use of com.tale.exception.TipException in project tale by otale.

the class IndexController method upPwd.

/**
     * 修改密码
     */
@Route(value = "password", method = HttpMethod.POST)
@JSON
public RestResponse upPwd(@QueryParam String old_password, @QueryParam String password, Request request) {
    Users users = this.user();
    if (StringKit.isBlank(old_password) || StringKit.isBlank(password)) {
        return RestResponse.fail("请确认信息输入完整");
    }
    if (!users.getPassword().equals(Tools.md5(users.getUsername() + old_password))) {
        return RestResponse.fail("旧密码错误");
    }
    if (password.length() < 6 || password.length() > 14) {
        return RestResponse.fail("请输入6-14位密码");
    }
    try {
        Users temp = new Users();
        temp.setUid(users.getUid());
        String pwd = Tools.md5(users.getUsername() + password);
        temp.setPassword(pwd);
        usersService.update(temp);
        logService.save(LogActions.UP_PWD, null, request.address(), this.getUid());
        return RestResponse.ok();
    } catch (Exception e) {
        String msg = "密码修改失败";
        if (e instanceof TipException) {
            msg = e.getMessage();
        } else {
            LOGGER.error(msg, e);
        }
        return RestResponse.fail(msg);
    }
}
Also used : Users(com.tale.model.Users) TipException(com.tale.exception.TipException) TipException(com.tale.exception.TipException) JSON(com.blade.mvc.annotation.JSON) Route(com.blade.mvc.annotation.Route)

Example 12 with TipException

use of com.tale.exception.TipException in project tale by otale.

the class IndexController method comment.

/**
     * 评论操作
     */
@Route(value = "comment", method = HttpMethod.POST)
@JSON
public RestResponse comment(Request request, Response response, @QueryParam Integer cid, @QueryParam Integer coid, @QueryParam String author, @QueryParam String mail, @QueryParam String url, @QueryParam String text, @QueryParam String _csrf_token) {
    String ref = request.header("Referer");
    if (StringKit.isBlank(ref) || StringKit.isBlank(_csrf_token)) {
        return RestResponse.fail(ErrorCode.BAD_REQUEST);
    }
    if (!ref.startsWith(Commons.site_url())) {
        return RestResponse.fail("非法评论来源");
    }
    String token = cache.hget(Types.CSRF_TOKEN, _csrf_token);
    if (StringKit.isBlank(token)) {
        return RestResponse.fail(ErrorCode.BAD_REQUEST);
    }
    if (null == cid || StringKit.isBlank(author) || StringKit.isBlank(mail) || StringKit.isBlank(text)) {
        return RestResponse.fail("请输入完整后评论");
    }
    if (author.length() > 50) {
        return RestResponse.fail("姓名过长");
    }
    if (!TaleUtils.isEmail(mail)) {
        return RestResponse.fail("请输入正确的邮箱格式");
    }
    if (StringKit.isNotBlank(url) && !PatternKit.isURL(url)) {
        return RestResponse.fail("请输入正确的URL格式");
    }
    if (text.length() > 200) {
        return RestResponse.fail("请输入200个字符以内的评论");
    }
    String val = IPKit.getIpAddrByRequest(request.raw()) + ":" + cid;
    Integer count = cache.hget(Types.COMMENTS_FREQUENCY, val);
    if (null != count && count > 0) {
        return RestResponse.fail("您发表评论太快了,请过会再试");
    }
    author = TaleUtils.cleanXSS(author);
    text = TaleUtils.cleanXSS(text);
    author = EmojiParser.parseToAliases(author);
    text = EmojiParser.parseToAliases(text);
    Comments comments = new Comments();
    comments.setAuthor(author);
    comments.setCid(cid);
    comments.setIp(request.address());
    comments.setUrl(url);
    comments.setContent(text);
    comments.setMail(mail);
    comments.setParent(coid);
    try {
        commentsService.saveComment(comments);
        response.cookie("tale_remember_author", URLEncoder.encode(author, "UTF-8"), 7 * 24 * 60 * 60);
        response.cookie("tale_remember_mail", URLEncoder.encode(mail, "UTF-8"), 7 * 24 * 60 * 60);
        if (StringKit.isNotBlank(url)) {
            response.cookie("tale_remember_url", URLEncoder.encode(url, "UTF-8"), 7 * 24 * 60 * 60);
        }
        // 设置对每个文章30秒可以评论一次
        cache.hset(Types.COMMENTS_FREQUENCY, val, 1, 30);
        siteService.cleanCache(Types.C_STATISTICS);
        request.attribute("del_csrf_token", token);
        return RestResponse.ok();
    } catch (Exception e) {
        String msg = "评论发布失败";
        if (e instanceof TipException) {
            msg = e.getMessage();
        } else {
            LOGGER.error(msg, e);
        }
        return RestResponse.fail(msg);
    }
}
Also used : Comments(com.tale.model.Comments) TipException(com.tale.exception.TipException) TipException(com.tale.exception.TipException)

Example 13 with TipException

use of com.tale.exception.TipException in project tale by otale.

the class ContentsServiceImpl method updateArticle.

@Override
public void updateArticle(Contents contents) {
    if (null == contents || null == contents.getCid()) {
        throw new TipException("文章对象不能为空");
    }
    if (StringKit.isBlank(contents.getTitle())) {
        throw new TipException("文章标题不能为空");
    }
    if (contents.getTitle().length() > TaleConst.MAX_TITLE_COUNT) {
        throw new TipException("文章标题最多可以输入" + TaleConst.MAX_TITLE_COUNT + "个字符");
    }
    if (StringKit.isBlank(contents.getContent())) {
        throw new TipException("文章内容不能为空");
    }
    if (contents.getContent().length() > TaleConst.MAX_TEXT_COUNT)
        throw new TipException("文章内容最多可以输入" + TaleConst.MAX_TEXT_COUNT + "个字符");
    if (null == contents.getAuthor_id()) {
        throw new TipException("请登录后发布文章");
    }
    int time = DateKit.getCurrentUnixTime();
    contents.setModified(time);
    Integer cid = contents.getCid();
    contents.setContent(EmojiParser.parseToAliases(contents.getContent()));
    activeRecord.update(contents);
    if (!StringKit.equals(contents.getType(), Types.PAGE)) {
        String sql = "delete from t_relationships where cid = ?";
        activeRecord.execute(sql, cid);
    }
    metasService.saveMetas(cid, contents.getTags(), Types.TAG);
    metasService.saveMetas(cid, contents.getCategories(), Types.CATEGORY);
}
Also used : TipException(com.tale.exception.TipException)

Example 14 with TipException

use of com.tale.exception.TipException in project tale by otale.

the class ContentsServiceImpl method publish.

@Override
public Integer publish(Contents contents) {
    if (null == contents)
        throw new TipException("文章对象为空");
    if (StringKit.isBlank(contents.getTitle()))
        throw new TipException("文章标题不能为空");
    if (contents.getTitle().length() > TaleConst.MAX_TITLE_COUNT) {
        throw new TipException("文章标题最多可以输入" + TaleConst.MAX_TITLE_COUNT + "个字符");
    }
    if (StringKit.isBlank(contents.getContent()))
        throw new TipException("文章内容不能为空");
    // 最多可以输入5w个字
    if (contents.getContent().length() > TaleConst.MAX_TEXT_COUNT)
        throw new TipException("文章内容最多可以输入" + TaleConst.MAX_TEXT_COUNT + "个字符");
    if (null == contents.getAuthor_id())
        throw new TipException("请登录后发布文章");
    if (StringKit.isNotBlank(contents.getSlug())) {
        if (contents.getSlug().length() < 5) {
            throw new TipException("路径太短了");
        }
        if (!TaleUtils.isPath(contents.getSlug()))
            throw new TipException("您输入的路径不合法");
        int count = activeRecord.count(new Take(Contents.class).eq("type", contents.getType()).eq("slug", contents.getSlug()));
        if (count > 0)
            throw new TipException("该路径已经存在,请重新输入");
    }
    contents.setContent(EmojiParser.parseToAliases(contents.getContent()));
    int time = DateKit.getCurrentUnixTime();
    contents.setCreated(time);
    contents.setModified(time);
    String tags = contents.getTags();
    String categories = contents.getCategories();
    Integer cid = activeRecord.insert(contents);
    metasService.saveMetas(cid, tags, Types.TAG);
    metasService.saveMetas(cid, categories, Types.CATEGORY);
    return cid;
}
Also used : Take(com.blade.jdbc.core.Take) Contents(com.tale.model.Contents) TipException(com.tale.exception.TipException)

Example 15 with TipException

use of com.tale.exception.TipException in project tale by otale.

the class MetasServiceImpl method saveMeta.

@Override
public void saveMeta(String type, String name, Integer mid) {
    if (StringKit.isNotBlank(type) && StringKit.isNotBlank(name)) {
        Metas metas = activeRecord.one(new Take(Metas.class).eq("type", type).eq("name", name));
        if (null != metas) {
            throw new TipException("已经存在该项");
        } else {
            if (null != mid) {
                metas = new Metas();
                metas.setMid(mid);
                metas.setName(name);
                activeRecord.update(metas);
            } else {
                metas = new Metas();
                metas.setType(type);
                metas.setName(name);
                activeRecord.insert(metas);
            }
        }
    }
}
Also used : Take(com.blade.jdbc.core.Take) Metas(com.tale.model.Metas) TipException(com.tale.exception.TipException)

Aggregations

TipException (com.tale.exception.TipException)24 JSON (com.blade.mvc.annotation.JSON)12 Route (com.blade.mvc.annotation.Route)12 Users (com.tale.model.Users)10 Contents (com.tale.model.Contents)7 Comments (com.tale.model.Comments)4 Take (com.blade.jdbc.core.Take)3 Config (com.blade.kit.base.Config)3 Attach (com.tale.model.Attach)2 Metas (com.tale.model.Metas)2 File (java.io.File)2 IOException (java.io.IOException)2 FileItem (com.blade.mvc.multipart.FileItem)1 RestResponse (com.blade.mvc.view.RestResponse)1 BackResponse (com.tale.dto.BackResponse)1 ArrayList (java.util.ArrayList)1