Search in sources :

Example 11 with UserInfo

use of com.ch999.haha.admin.entity.UserInfo in project haha by hahafreeasair666.

the class UserInfoApi method updateAvatar.

/**
 * 修改用户信息
 *
 * @param type             要修改的类型有{avatar,name,mobile,description,pwd}
 * @param userInfoUpdateVO
 * @return
 */
@PostMapping("/updateUserInfo/{type}/v1")
public Result<String> updateAvatar(@PathVariable("type") String type, UserInfoUpdateVO userInfoUpdateVO, HttpServletResponse httpResponse) {
    // 设置ajax跨域可访问
    httpResponse.addHeader("Access-Control-Allow-Origin", "*");
    UserInfoBO loginUser = userComponent.getLoginUser();
    if (loginUser.getId() == null) {
        return Result.error("unLogin", "请登录后再进行操作");
    }
    UserInfo userInfo = new UserInfo(loginUser.getId());
    userInfo.setPicPath(userInfoBORepository.findOne(loginUser.getId()).getUserInfo().getPicPath());
    switch(type) {
        case "avatar":
            if (userInfoUpdateVO.getFile() == null) {
                return Result.error("null", "请选择图片");
            }
            if (userComponent.updateUserAvatar(loginUser.getId(), userInfoUpdateVO.getFile())) {
                return Result.success();
            }
            return Result.error("error", "头像更改失败");
        case "name":
            if (StringUtils.isBlank(userInfoUpdateVO.getName())) {
                return Result.error("null", "用户名不能为空");
            } else if (!userInfoUpdateVO.getName().equals(loginUser.getUserInfo().getUsername()) && !userComponent.checkCanUse(type, userInfoUpdateVO.getName())) {
                return Result.error("null", "昵称修改失败该昵称已被占用");
            }
            userInfo.setUsername(userInfoUpdateVO.getName());
            break;
        case "description":
            if (StringUtils.isBlank(userInfoUpdateVO.getDescription())) {
                return Result.error("null", "个性签名不能为空");
            }
            userInfo.setAutograph(userInfoUpdateVO.getDescription());
            break;
        case "mobile":
            if (StringUtils.isBlank(userInfoUpdateVO.getMobile())) {
                return Result.error("null", "请输入要修改的手机号码");
            } else if (!userInfoUpdateVO.getMobile().matches(Phone.CK)) {
                return Result.error("error", "请输入正确的的电话号码");
            } else if (!userInfoUpdateVO.getMobile().equals(loginUser.getUserInfo().getMobile()) && !userComponent.checkCanUse(type, userInfoUpdateVO.getMobile())) {
                return Result.error("error", "手机号修改失败该号码已被占用");
            }
            userInfo.setMobile(userInfoUpdateVO.getMobile());
            break;
        case "pwd":
            if (StringUtils.isBlank(userInfoUpdateVO.getOldPwd())) {
                return Result.error("null", "请输入原密码");
            } else if (!userInfoService.selectById(loginUser.getId()).getPwd().equals(userInfoUpdateVO.getOldPwd())) {
                return Result.error("error", "原密码输入错误,密码修改失败");
            } else if (StringUtils.isBlank(userInfoUpdateVO.getNewPwd1()) || StringUtils.isBlank(userInfoUpdateVO.getNewPwd2()) || !userInfoUpdateVO.getNewPwd2().equals(userInfoUpdateVO.getNewPwd1())) {
                return Result.error("error", "新密码未输入或两次密码输入不一致,密码修改失败");
            }
            userInfo.setPwd(userInfoUpdateVO.getNewPwd1());
            break;
        default:
            return Result.error("error", "不支持该类型的修改");
    }
    // 修改数据库
    userInfoService.updateById(userInfo);
    UserInfo userInfo1 = userInfoService.selectById(loginUser.getId());
    // 修改缓存
    UserInfoBO one = userInfoBORepository.findOne(loginUser.getId());
    if (one != null) {
        one.setUserInfo(userInfo1);
    } else {
        one = new UserInfoBO(loginUser.getId(), userInfo1);
    }
    userInfoBORepository.save(one);
    return Result.success();
}
Also used : UserInfo(com.ch999.haha.admin.entity.UserInfo) UserInfoBO(com.ch999.haha.admin.document.redis.UserInfoBO)

Aggregations

UserInfo (com.ch999.haha.admin.entity.UserInfo)10 UserInfoBO (com.ch999.haha.admin.document.redis.UserInfoBO)5 NewsCommentBO (com.ch999.haha.admin.document.mongo.NewsCommentBO)3 CommentZanBO (com.ch999.haha.admin.document.redis.CommentZanBO)3 NewsCommentRepository (com.ch999.haha.admin.repository.mongo.NewsCommentRepository)3 CommentZanRepository (com.ch999.haha.admin.repository.redis.CommentZanRepository)3 NewsCommentService (com.ch999.haha.admin.service.NewsCommentService)3 UserInfoService (com.ch999.haha.admin.service.UserInfoService)3 CommentReplyVO (com.ch999.haha.admin.vo.CommentReplyVO)3 PageVO (com.ch999.haha.admin.vo.PageVO)3 PageableVo (com.ch999.haha.common.PageableVo)3 java.util (java.util)3 Collectors (java.util.stream.Collectors)3 Resource (javax.annotation.Resource)3 CollectionUtils (org.apache.commons.collections.CollectionUtils)3 Page (org.springframework.data.domain.Page)3 Pageable (org.springframework.data.domain.Pageable)3 Service (org.springframework.stereotype.Service)3 StopWatch (org.springframework.util.StopWatch)3 EntityWrapper (com.baomidou.mybatisplus.mapper.EntityWrapper)1