Search in sources :

Example 1 with PostRoute

use of com.blade.mvc.annotation.PostRoute in project tale by otale.

the class InstallController method doInstall.

@PostRoute
@JSON
public RestResponse<?> doInstall(InstallParam installParam) {
    if (isRepeatInstall()) {
        return RestResponse.fail("请勿重复安装");
    }
    CommonValidator.valid(installParam);
    Users temp = new Users();
    temp.setUsername(installParam.getAdminUser());
    temp.setPassword(installParam.getAdminPwd());
    temp.setEmail(installParam.getAdminEmail());
    siteService.initSite(temp);
    String siteUrl = TaleUtils.buildURL(installParam.getSiteUrl());
    optionsService.saveOption("site_title", installParam.getSiteTitle());
    optionsService.saveOption("site_url", siteUrl);
    TaleConst.OPTIONS = Environment.of(optionsService.getOptions());
    return RestResponse.ok();
}
Also used : Users(com.tale.model.entity.Users) PostRoute(com.blade.mvc.annotation.PostRoute) JSON(com.blade.mvc.annotation.JSON)

Example 2 with PostRoute

use of com.blade.mvc.annotation.PostRoute in project tale by otale.

the class IndexController method upload.

/**
 * 上传文件接口
 */
@SysLog("上传附件")
@PostRoute("api/attach/upload")
@JSON
public RestResponse<?> upload(Request request) {
    log.info("UPLOAD DIR = {}", TaleUtils.UP_DIR);
    Users users = this.user();
    Integer uid = users.getUid();
    List<Attach> errorFiles = new ArrayList<>();
    List<Attach> urls = new ArrayList<>();
    Map<String, FileItem> fileItems = request.fileItems();
    if (null == fileItems || fileItems.size() == 0) {
        return RestResponse.fail("请选择文件上传");
    }
    fileItems.forEach((fileName, fileItem) -> {
        String fname = fileItem.getFileName();
        if ((fileItem.getLength() / 1024) <= TaleConst.MAX_FILE_SIZE) {
            String fkey = TaleUtils.getFileKey(fname);
            String ftype = fileItem.getContentType().contains("image") ? Types.IMAGE : Types.FILE;
            String filePath = TaleUtils.UP_DIR + fkey;
            try {
                Files.write(Paths.get(filePath), fileItem.getData());
                if (TaleUtils.isImage(new File(filePath))) {
                    String newFileName = TaleUtils.getFileName(fkey);
                    String thumbnailFilePath = TaleUtils.UP_DIR + fkey.replace(newFileName, "thumbnail_" + newFileName);
                    ImageUtils.cutCenterImage(CLASSPATH + fkey, thumbnailFilePath, 270, 380);
                }
            } catch (IOException e) {
                log.error("", e);
            }
            Attach attach = new Attach();
            attach.setFname(fname);
            attach.setAuthorId(uid);
            attach.setFkey(fkey);
            attach.setFtype(ftype);
            attach.setCreated(DateKit.nowUnix());
            attach.save();
            urls.add(attach);
            siteService.cleanCache(Types.SYS_STATISTICS);
        } else {
            Attach attach = new Attach();
            attach.setFname(fname);
            errorFiles.add(attach);
        }
    });
    if (errorFiles.size() > 0) {
        return RestResponse.fail().payload(errorFiles);
    }
    return RestResponse.ok(urls);
}
Also used : FileItem(com.blade.mvc.multipart.FileItem) Attach(com.tale.model.entity.Attach) ArrayList(java.util.ArrayList) Users(com.tale.model.entity.Users) IOException(java.io.IOException) File(java.io.File) PostRoute(com.blade.mvc.annotation.PostRoute) SysLog(com.tale.annotation.SysLog) JSON(com.blade.mvc.annotation.JSON)

Example 3 with PostRoute

use of com.blade.mvc.annotation.PostRoute in project tale by otale.

the class SystemController method saveProfile.

@SysLog("保存个人信息")
@PostRoute("profile")
public RestResponse saveProfile(@Param String screenName, @Param String email) {
    Users users = this.user();
    if (StringKit.isNotBlank(screenName) && StringKit.isNotBlank(email)) {
        Users temp = new Users();
        temp.setScreenName(screenName);
        temp.setEmail(email);
        temp.updateById(users.getUid());
    }
    return RestResponse.ok();
}
Also used : Users(com.tale.model.entity.Users) PostRoute(com.blade.mvc.annotation.PostRoute) SysLog(com.tale.annotation.SysLog)

Example 4 with PostRoute

use of com.blade.mvc.annotation.PostRoute in project tale by otale.

the class SystemController method upPwd.

@SysLog("修改登录密码")
@PostRoute("password")
public RestResponse upPwd(@Param String old_password, @Param String password) {
    Users users = this.user();
    if (StringKit.isBlank(old_password) || StringKit.isBlank(password)) {
        return RestResponse.fail("请确认信息输入完整");
    }
    if (!users.getPassword().equals(EncryptKit.md5(users.getUsername() + old_password))) {
        return RestResponse.fail("旧密码错误");
    }
    if (password.length() < 6 || password.length() > 14) {
        return RestResponse.fail("请输入6-14位密码");
    }
    Users temp = new Users();
    String pwd = EncryptKit.md5(users.getUsername() + password);
    temp.setPassword(pwd);
    temp.updateById(users.getUid());
    optionsService.deleteOption(TaleConst.OPTION_SAFE_REMEMBER_ME);
    return RestResponse.ok();
}
Also used : Users(com.tale.model.entity.Users) PostRoute(com.blade.mvc.annotation.PostRoute) SysLog(com.tale.annotation.SysLog)

Example 5 with PostRoute

use of com.blade.mvc.annotation.PostRoute in project tale by otale.

the class AuthController method doLogin.

@SysLog("登录后台")
@PostRoute("login")
public RestResponse<?> doLogin(LoginParam loginParam, RouteContext context) {
    CommonValidator.valid(loginParam);
    Integer errorCount = cache.get(LOGIN_ERROR_COUNT);
    try {
        errorCount = null == errorCount ? 0 : errorCount;
        if (errorCount > 3) {
            return RestResponse.fail("您输入密码已经错误超过3次,请10分钟后尝试");
        }
        long count = new Users().where("username", loginParam.getUsername()).count();
        if (count < 1) {
            errorCount += 1;
            return RestResponse.fail("不存在该用户");
        }
        String pwd = EncryptKit.md5(loginParam.getUsername(), loginParam.getPassword());
        Users user = select().from(Users.class).where(Users::getUsername, loginParam.getUsername()).and(Users::getPassword, pwd).one();
        if (null == user) {
            errorCount += 1;
            return RestResponse.fail("用户名或密码错误");
        }
        context.session().attribute(TaleConst.LOGIN_SESSION_KEY, user);
        if (StringKit.isNotBlank(loginParam.getRememberMe())) {
            TaleUtils.setCookie(context, user.getUid());
        }
        Users temp = new Users();
        temp.setLogged(DateKit.nowUnix());
        temp.updateById(user.getUid());
        log.info("登录成功:{}", loginParam.getUsername());
        cache.set(LOGIN_ERROR_COUNT, 0);
        return RestResponse.ok();
    } catch (Exception e) {
        errorCount += 1;
        cache.set(LOGIN_ERROR_COUNT, errorCount, 10 * 60);
        String msg = "登录失败";
        if (e instanceof ValidatorException) {
            msg = e.getMessage();
        } else {
            log.error(msg, e);
        }
        return RestResponse.fail(msg);
    }
}
Also used : ValidatorException(com.blade.exception.ValidatorException) Users(com.tale.model.entity.Users) ValidatorException(com.blade.exception.ValidatorException) PostRoute(com.blade.mvc.annotation.PostRoute) SysLog(com.tale.annotation.SysLog)

Aggregations

PostRoute (com.blade.mvc.annotation.PostRoute)5 Users (com.tale.model.entity.Users)5 SysLog (com.tale.annotation.SysLog)4 JSON (com.blade.mvc.annotation.JSON)2 ValidatorException (com.blade.exception.ValidatorException)1 FileItem (com.blade.mvc.multipart.FileItem)1 Attach (com.tale.model.entity.Attach)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1