com.jfinal.kit.Ret Java Examples

The following examples show how to use com.jfinal.kit.Ret. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: _TemplateController.java    From jpress with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void doEnable() {
    String tid = getPara("tid");
    Template template = TemplateManager.me().getTemplateById(tid);

    if (template == null) {
        renderJson(Ret.fail().set("message", "没有该模板"));
        return;
    }

    JPressOptions.set("web_template", template.getId());
    optionService.saveOrUpdate("web_template", template.getId());

    TemplateManager.me().setCurrentTemplate(template.getId());
    TemplateManager.me().clearCache();

    renderOkJson();
}
 
Example #2
Source File: ArticleApiController.java    From jpress with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void tagArticles() {
    String tag = getPara("tag");
    int count = getParaToInt("count", 10);
    if (StrUtil.isBlank(tag)) {
        renderFailJson();
        return;
    }

    ArticleCategory category = categoryService.findFirstByTypeAndSlug(ArticleCategory.TYPE_TAG, tag);
    if (category == null) {
        renderFailJson();
        return;
    }

    List<Article> articles = articleService.findListByCategoryId(category.getId(), null, "id desc", count);
    renderJson(Ret.ok().set("articles", articles));
}
 
Example #3
Source File: _ProductTagController.java    From jpress with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void doSave() {

        ProductCategory tag = getModel(ProductCategory.class, "category");

        String slug = tag.getTitle().contains(".")
                ? tag.getTitle().replace(".", "_")
                : tag.getTitle();

        //新增 tag
        if (tag.getId() == null) {
            ProductCategory indbTag = productCategoryService.findFirstByTypeAndSlug(ProductCategory.TYPE_TAG, slug);
            if (indbTag != null) {
                renderJson(Ret.fail().set("message", "该标签已经存在,不能新增。"));
                return;
            }
        }

        tag.setSlug(slug);
        saveCategory(tag);
    }
 
Example #4
Source File: ArticleUCenterController.java    From jpress with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void doDel() {

        Long id = getIdPara();
        if (id == null) {
            renderFailJson();
            return;
        }

        Article article = articleService.findById(id);
        if (article == null) {
            renderFailJson();
            return;
        }

        if (notLoginedUserModel(article)) {
            renderJson(Ret.fail().set("message", "非法操作"));
            return;
        }

        renderJson(articleService.deleteById(id) ? OK : FAIL);
    }
 
Example #5
Source File: _UserController.java    From jpress with GNU Lesser General Public License v3.0 6 votes vote down vote up
@EmptyValidate({
            @Form(name = "tag.title", message = "标签名称不能为空"),
    })
    public void doTagSave() {
        UserTag tag = getModel(UserTag.class, "tag");
        tag.setSlug(tag.getTitle());

        String slug = tag.getSlug();
        if (slug == null || slug.contains("-") || StrUtil.isNumeric(slug)) {
            renderJson(Ret.fail("message", "slug不能全是数字且不能包含字符:- "));
            return;
        }

        Object id = userTagService.saveOrUpdate(tag);
//        categoryService.doUpdateArticleCount(category.getId());


        renderOkJson();
    }
 
Example #6
Source File: OptionApiController.java    From jpress with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void index() {

        String keyPara = getPara("key");

        if (StrUtil.isBlank(keyPara)) {
            renderFailJson("key must not empty");
            return;
        }

        if (keyPara.contains(",")) {
            Set<String> keys = StrUtil.splitToSet(keyPara, ",");
            Map<String, String> data = new HashMap<>();
            for (String key : keys) {
                if (StrUtil.isNotBlank(key)) {
                    data.put(key, optionService.findByKey(key));
                }
            }
            renderJson(Ret.ok().set("values", data));
        } else {

            renderOkJson("value", optionService.findByKey(keyPara));
        }

    }
 
Example #7
Source File: _PageController.java    From jpress with GNU Lesser General Public License v3.0 6 votes vote down vote up
@EmptyValidate({
        @Form(name = "page.title", message = "标题不能为空"),
        @Form(name = "page.content", message = "内容不能为空")
})
public void doWriteSave() {
    SinglePage page = getModel(SinglePage.class, "page");

    if (!validateSlug(page)) {
        renderJson(Ret.fail("message", "slug不能包含该字符:- "));
        return;
    }

    if (StrUtil.isNotBlank(page.getSlug())) {
        SinglePage bySlug = sps.findFirstBySlug(page.getSlug());
        if (bySlug != null && bySlug.getId().equals(page.getId()) == false) {
            renderJson(Ret.fail("message", "该slug已经存在"));
            return;
        }
    }

    sps.saveOrUpdate(page);
    renderJson(Ret.ok().set("id", page.getId()));
}
 
Example #8
Source File: CartController.java    From jpress with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 对某个购物车商品 +1
 */
@EmptyValidate({
        @Form(name = "id", message = "id不能为空")
})
public void addcount() {
    UserCart userCart = cartService.findById(getPara("id"));
    if (notLoginedUserModel(userCart)) {
        renderFailJson();
        return;
    }

    userCart.setProductCount(userCart.getProductCount() + 1);
    cartService.update(userCart);
    renderJson(Ret.ok().set("shouldPayPrice", new DecimalFormat("0.00").format(userCart.getShouldPayPrice())));

}
 
Example #9
Source File: _UserController.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void doAddGroupRolePermission(long roleId, String groupId) {
    List<Long> permIds = new ArrayList<Long>();
    List<Permission> permissionList = permissionService.findListByNode(groupId.replace("...", ""));
    for (Permission permission : permissionList) {
        //先清空再添加
        if (!roleService.hasPermission(roleId, permission.getId())) {
            roleService.addPermission(roleId, permission.getId());
        }
        permIds.add(permission.getId());
    }
    renderJson(Ret.ok().set("permissionIds", permIds));
}
 
Example #10
Source File: _AttachmentController.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void doUplaodRootFile() {
    if (!isMultipartRequest()) {
        renderError(404);
        return;
    }

    UploadFile uploadFile = getFile();
    if (uploadFile == null) {
        renderJson(Ret.fail().set("message", "请选择要上传的文件"));
        return;
    }

    File file = uploadFile.getFile();
    if (AttachmentUtils.isUnSafe(file)) {
        file.delete();
        renderJson(Ret.fail().set("message", "不支持此类文件上传"));
        return;
    }

    File rootFile = new File(PathKit.getWebRootPath(), file.getName());
    if (rootFile.exists()) {
        file.delete();
        renderJson(Ret.fail().set("message", "该文件已经存在,请手动删除后再重新上传。"));
        return;
    }

    try {
        FileUtils.moveFile(file, rootFile);
        rootFile.setReadable(true, false);
        renderOkJson();
        return;
    } catch (IOException e) {
        e.printStackTrace();
    }

    renderJson(Ret.fail().set("message", "系统错误。"));
}
 
Example #11
Source File: _AddonController.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void doInstall() {
    String id = getPara("id");
    if (StrUtil.isBlank(id)) {
        renderJson(Ret.fail().set("message", "ID数据不能为空"));
        return;
    }
    if (AddonManager.me().install(id)) {
        renderOkJson();
    } else {
        renderJson(Ret.fail().set("message", "插件安装失败,请联系插件开发者。"));
    }
}
 
Example #12
Source File: _WechatController.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 微信菜单同步
 */
public void doMenuSync() {
    List<WechatMenu> wechatMenus = wechatMenuService.findAll();
    SortKit.toTree(wechatMenus);

    if (wechatMenus == null || wechatMenus.isEmpty()) {
        renderJson(Ret.fail().set("message", "微信菜单为空"));
        return;
    }

    JSONArray button = new JSONArray();
    for (WechatMenu wechatMenu : wechatMenus) {
        if (wechatMenu.hasChild()) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("name", wechatMenu.getText());
            List<WechatMenu> childMenus = wechatMenu.getChilds();
            JSONArray sub_buttons = new JSONArray();
            for (WechatMenu child : childMenus) {
                createJsonObjectButton(sub_buttons, child);
            }
            jsonObject.put("sub_button", sub_buttons);
            button.add(jsonObject);
        } else {
            createJsonObjectButton(button, wechatMenu);
        }
    }

    JSONObject wechatMenuJson = new JSONObject();
    wechatMenuJson.put("button", button);
    String jsonString = wechatMenuJson.toJSONString();

    ApiResult result = WechatApis.createMenu(jsonString);
    if (result.isSucceed()) {
        renderJson(Ret.ok().set("message", "微信菜单同步成功"));
    } else {
        renderJson(Ret.fail().set("message", "错误码:" + result.getErrorCode() + "," + result.getErrorMsg()));
    }

}
 
Example #13
Source File: _UserController.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
@EmptyValidate({
        @Form(name = "newPwd", message = "新密码不能为空"),
        @Form(name = "confirmPwd", message = "确认密码不能为空")
})
public void doUpdatePwd(long uid, String oldPwd, String newPwd, String confirmPwd) {

    User user = userService.findById(uid);
    if (user == null) {
        renderJson(Ret.fail().set("message", "该用户不存在"));
        return;
    }

    //超级管理员可以修改任何人的密码
    if (!roleService.isSupperAdmin(getLoginedUser().getId())) {
        if (StrUtil.isBlank(oldPwd)) {
            renderJson(Ret.fail().set("message", "旧密码不能为空"));
            return;
        }

        if (userService.doValidateUserPwd(user, oldPwd).isFail()) {
            renderJson(Ret.fail().set("message", "密码错误"));
            return;
        }
    }


    if (newPwd.equals(confirmPwd) == false) {
        renderJson(Ret.fail().set("message", "两次出入密码不一致"));
        return;
    }

    String salt = user.getSalt();
    String hashedPass = HashKit.sha256(salt + newPwd);

    user.setPassword(hashedPass);
    userService.update(user);

    renderOkJson();
}
 
Example #14
Source File: _UserController.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 删除用户
 */
public void doUserDel() {
    if (getLoginedUser().getId().equals(getIdPara())) {
        renderJson(Ret.fail().set("message", "不能删除自己"));
        return;
    }
    userService.deleteById(getIdPara());
    renderOkJson();
}
 
Example #15
Source File: _UserController.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 删除用户
 */
@EmptyValidate(@Form(name = "ids"))
public void doUserDelByIds() {
    Set<String> idsSet = getParaSet("ids");
    if (idsSet.contains(getLoginedUser().getId().toString())) {
        renderJson(Ret.fail().set("message", "删除的用户不能包含自己"));
        return;
    }
    render(userService.deleteByIds(idsSet.toArray()) ? OK : FAIL);
}
 
Example #16
Source File: ProductController.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 添加到购物车
 */
@Before(ProductValidate.class)
public void doAddCart() {

    Product product = ProductValidate.getThreadLocalProduct();
    User user = getLoginedUser();
    Long distUserId = CookieUtil.getLong(this, buildDistUserCookieName(product.getId()));
    UserCart userCart = product.toUserCartItem(user.getId(), distUserId, getPara("spec"));

    Object cartId = cartService.save(userCart);
    renderJson(Ret.ok().set("cartId",cartId));
}
 
Example #17
Source File: _UserController.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void doDelGroupRolePermission(long roleId, String groupId) {
    List<Long> permIds = new ArrayList<Long>();
    List<Permission> permissionList = permissionService.findListByNode(groupId.replace("...", ""));
    for (Permission permission : permissionList) {
        roleService.delPermission(roleId, permission.getId());
        permIds.add(permission.getId());
    }
    renderJson(Ret.ok().set("permissionIds", permIds));
}
 
Example #18
Source File: _UserController.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void doUpdateUserRoles() {

        Long userId = getParaToLong("userId");

        if (getLoginedUser().getId().equals(userId)) {
            renderJson(Ret.fail().set("message", "不能修改自己的角色"));
            return;
        }

        Long[] roleIds = getParaValuesToLong("roleId");
        roleService.doResetUserRoles(userId, roleIds);
        renderOkJson();
    }
 
Example #19
Source File: _AddonController.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void doUninstall() {
    String id = getPara("id");
    if (StrUtil.isBlank(id)) {
        renderJson(Ret.fail().set("message", "ID数据不能为空"));
        return;
    }
    if (AddonManager.me().uninstall(id)) {
        renderOkJson();
    } else {
        renderFailJson();
    }
}
 
Example #20
Source File: _AddonController.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void doStop() {
    String id = getPara("id");
    if (StrUtil.isBlank(id)) {
        renderJson(Ret.fail().set("message", "ID数据不能为空"));
        return;
    }
    AddonManager.me().stop(id);
    renderOkJson();
}
 
Example #21
Source File: AddonManager.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Ret upgrade(File newAddonFile, String oldAddonId) {
    AddonInfo oldAddon = AddonManager.me().getAddonInfo(oldAddonId);
    if (oldAddon == null) {
        return failRet("升级失败:无法读取旧的插件信息,可能该插件不存在。");
    }

    AddonInfo addon = AddonUtil.readSimpleAddonInfo(newAddonFile);
    if (addon == null || StrUtil.isBlank(addon.getId())) {
        return failRet("升级失败:无法读取新插件配置文件。");
    }

    if (!addon.getId().equals(oldAddonId)) {
        return failRet("升级失败:新插件的ID和旧插件不一致。");
    }

    if (addon.getVersionCode() <= oldAddon.getVersionCode()) {
        return failRet("升级失败:新插件的版本号必须大于已安装插件的版本号。");
    }

    boolean upgradeSuccess = false;
    try {
        upgradeSuccess = doUpgrade(newAddonFile, addon, oldAddon);
    } catch (Exception ex) {
        LOG.error(ex.toString(), ex);
        upgradeSuccess = false;
    } finally {
        if (!upgradeSuccess) {
            doUpgradeRollback(addon, oldAddon);
        }
    }

    return upgradeSuccess ? Ret.ok() : failRet("插件升级失败,请联系管理员。");
}
 
Example #22
Source File: PermissionInterceptor.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void render(Invocation inv) {
    if (RequestUtil.isAjaxRequest(inv.getController().getRequest())) {
        inv.getController().renderJson(Ret.fail().set("message", "您没有权限操作此功能。"));
    } else {
        inv.getController().render(NO_PERMISSION_VIEW);
    }
}
 
Example #23
Source File: _ProductTagController.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void saveCategory(ProductCategory category) {
        if (!validateSlug(category)) {
            renderJson(Ret.fail("message", "slug不能全是数字且不能包含字符:- "));
            return;
        }

        Object id = productCategoryService.saveOrUpdate(category);
//        productCategoryService.updateCount(category.getId());

        Menu displayMenu = menuService.findFirstByRelatives("product_category", id);
        Boolean isDisplayInMenu = getParaToBoolean("displayInMenu");
        if (isDisplayInMenu != null && isDisplayInMenu) {
            if (displayMenu == null) {
                displayMenu = new Menu();
            }

            displayMenu.setUrl(category.getUrl());
            displayMenu.setText(category.getTitle());
            displayMenu.setType(Menu.TYPE_MAIN);
            displayMenu.setOrderNumber(category.getOrderNumber());
            displayMenu.setRelativeTable("product_category");
            displayMenu.setRelativeId((Long) id);

            if (displayMenu.getPid() == null) {
                displayMenu.setPid(0L);
            }

            if (displayMenu.getOrderNumber() == null) {
                displayMenu.setOrderNumber(99);
            }

            menuService.saveOrUpdate(displayMenu);

        } else if (displayMenu != null) {
            menuService.delete(displayMenu);
        }

        renderOkJson();
    }
 
Example #24
Source File: UserMustLoginedInterceptor.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void intercept(Invocation inv) {

    if (UserInterceptor.getThreadLocalUser() == null) {
        if (RequestUtil.isAjaxRequest(inv.getController().getRequest())) {
            inv.getController().renderJson(Ret.fail("message", "用户未登录"));
        } else {
            inv.getController().redirect("/user/login");
        }
        return;
    }

    inv.invoke();

}
 
Example #25
Source File: AdminControllerBase.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
@NotAction
public void renderErrorForNoPermission() {
    if (isAjaxRequest()) {
        renderJson(Ret.fail().set("message", "您没有权限操作此功能。"));
    } else {
        render(NO_PERMISSION_VIEW);
    }
}
 
Example #26
Source File: _AddonController.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void renderFail(String msg, UploadFile... uploadFiles) {
    renderJson(Ret.fail()
            .set("success", false)
            .setIfNotBlank("message", msg));

    for (UploadFile ufile : uploadFiles) {
        deleteFileQuietly(ufile.getFile());
    }
}
 
Example #27
Source File: _TemplateController.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void doUninstall() {
    String tid = getPara("tid");
    Template template = TemplateManager.me().getTemplateById(tid);

    if (template == null) {
        renderJson(Ret.fail().set("message", "没有该模板"));
        return;
    }

    template.uninstall();
    renderOkJson();
}
 
Example #28
Source File: _AdminController.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Clear
@EmptyValidate({
        @Form(name = "user", message = "账号不能为空"),
        @Form(name = "pwd", message = "密码不能为空"),
        @Form(name = "captcha", message = "验证码不能为空"),
})
@CaptchaValidate(form = "captcha",message = "验证码不正确,请重新输入")
public void doLogin(String user, String pwd) {

    if (!JPressHandler.getCurrentTarget().equals(JPressConfig.me.getAdminLoginAction())) {
        renderError(404);
        return;
    }

    if (StrUtil.isBlank(user) || StrUtil.isBlank(pwd)) {
        throw new RuntimeException("你当前的编辑器(idea 或者 eclipse)可能有问题,请参考文档:http://www.jfinal.com/doc/3-3 进行配置");
    }

    User loginUser = userService.findByUsernameOrEmail(user);
    if (loginUser == null) {
        renderJson(Ret.fail("message", "用户名不正确。"));
        return;
    }

    if (!roleService.hasAnyRole(loginUser.getId())) {
        renderJson(Ret.fail("message", "您没有登录的权限。"));
        return;
    }

    Ret ret = userService.doValidateUserPwd(loginUser, pwd);

    if (ret.isOk()) {
        CookieUtil.put(this, JPressConsts.COOKIE_UID, loginUser.getId());
    }

    renderJson(ret);
}
 
Example #29
Source File: CartController.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void querySelectedItemCountAndPrice() {

        long count = cartService.querySelectedCount(getLoginedUser().getId());
        BigDecimal price = BigDecimal.ZERO;


        List<UserCart> userCarts = cartService.findSelectedListByUserId(getLoginedUser().getId());
        if (userCarts != null) {
            for (UserCart cart : userCarts) {
                price = price.add(cart.getShouldPayPrice());
            }
        }

        renderJson(Ret.ok().set("count", count).set("price", new DecimalFormat("0.00").format(price)));
    }
 
Example #30
Source File: _AddonController.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void doStart() {
    String id = getPara("id");
    if (StrUtil.isBlank(id)) {
        renderJson(Ret.fail().set("message", "ID数据不能为空"));
        return;
    }

    if (AddonManager.me().start(id)) {
        renderOkJson();
    } else {
        renderJson(Ret.fail().set("message", "该插件启动时出现异常,启动失败。"));
    }

}