SysAppUserController.java 8.0 KB
package com.skua.modules.app.user;

import cn.hutool.core.util.StrUtil;
import com.skua.core.api.ISysBaseAPI;
import com.skua.core.api.vo.LoginUser;
import com.skua.core.api.vo.Result;
import com.skua.core.aspect.annotation.AutoLog;
import com.skua.core.util.ConvertUtils;
import com.skua.core.util.PasswordUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.regex.Pattern;

/**
 * app用户管理
 * @author
 */
@Api(tags="APP用户管理")
@Slf4j
@RestController
@RequestMapping("/v1/app/sys/user")
public class SysAppUserController {

    /**校验手机号格式*/
    private static final Pattern PATTERN_PHONE = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");

    @Autowired
    private ISysBaseAPI sysBaseAPI;
    @Value("${skua.viewPath}")
    private String viewPath;

    @AutoLog(value = "修改用户名")
    @ApiOperation(value="修改用户信息", notes="用户管理")
    @PostMapping(value = "/changeUser")
    public Result<LoginUser> changeUser(@RequestBody LoginUser loginUser){
        Result<LoginUser> result = new Result<>();
        try{
            LoginUser oldUser = sysBaseAPI.getUserById(loginUser.getId());
            String newRealName = loginUser.getRealname();
            String newPassWord = loginUser.getPassword();
            String newPhone = loginUser.getPhone();
            String newEmail = loginUser.getEmail();
            String cid = loginUser.getCid();
            Date newBirthday = loginUser.getBirthday();
            if (oldUser == null){
                result.error500("未找到对应实体");
                return result;
            }
            if (StrUtil.isNotBlank(cid)){
                //修改手机cid
                oldUser.setCid(cid);
                sysBaseAPI.updateUser(oldUser);
                result.success("cid修改成功");
            }
            if (StrUtil.isNotBlank(newEmail)){
                //修改邮箱
                oldUser.setEmail(newEmail);
                sysBaseAPI.updateUser(oldUser);
                result.success("邮箱修改成功");
            }
            if (StrUtil.isNotBlank((CharSequence) newBirthday)){
                //修改生日
                oldUser.setEmail(String.valueOf(newBirthday));
                sysBaseAPI.updateUser(oldUser);
                result.success("生日修改成功");
            }
            if (StrUtil.isNotBlank(newRealName)){
                //修改用户名
                oldUser.setRealname(newRealName);
                sysBaseAPI.updateUser(oldUser);
                result.success("用户名修改成功");
            }
            if (StrUtil.isNotBlank(newPhone)){
                //修改手机号
                boolean matches = PATTERN_PHONE.matcher(newPhone).matches();
                if (!matches){
                    result.error500("手机号格式不正确");
                }else {
                    sysBaseAPI.updateUser(oldUser.setPhone(newPhone));
                    result.success("手机号修改成功");
                }
            }
            if (StrUtil.isNotBlank(newPassWord)){
                //修改密码
                String salt = ConvertUtils.randomGen(8);
                //md5密码盐
                oldUser.setSalt(salt);
                String passwordEncode = PasswordUtil.encrypt(oldUser.getUsername(), newPassWord, salt);
                oldUser.setPassword(passwordEncode);
                sysBaseAPI.updateUser(oldUser);
                result.success("密码修改完成!");
            }
        }catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    @AutoLog(value = "修改用户头像")
    @ApiOperation(value="修改用户头像", notes="用户管理")
    @PostMapping(value = "/changeUserAvatar")
    public Result<LoginUser> changeUserAvatar(@RequestBody LoginUser loginUser) {
        Result<LoginUser> result = new Result<LoginUser>();
        try {
            LoginUser oldUser = sysBaseAPI.getUserById(loginUser.getId());
            if(oldUser==null) {
                result.error500("未找到对应实体");
            }else {
                sysBaseAPI.updateUser(oldUser);
                oldUser.setAvatar(viewPath+oldUser.getAvatar());
                result.setResult(oldUser);
                result.success("修改成功!");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    @AutoLog(value = "根据id查询用户")
    @ApiOperation(value="根据id查询用户", notes="用户管理")
    @GetMapping(value = "/queryUserById")
    public Result<LoginUser> queryUserById(@RequestParam String userId) {
        Result<LoginUser> result = new Result<LoginUser>();
        try {
            LoginUser oldUser = sysBaseAPI.getUserById(userId);
            if(oldUser==null) {
                result.error500("未找到对应实体");
            }else {
                result.setResult(oldUser);
                result.success("查询成功!");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }



//    @AutoLog(value = "修改用户密码")
//    @ApiOperation(value="用户管理", notes="修改用户密码")
//    @PostMapping(value = "/changePassword")
//    public Result<LoginUser> changePassword(@RequestBody LoginUser sysUser) {
//        Result<LoginUser> result = new Result<LoginUser>();
//        String newPassword = sysUser.getPassword();
//        LoginUser oldUser = sysBaseAPI.getUserById(sysUser.getId());
//        if (oldUser == null) {
//            result.error500("未找到对应实体");
//        } else {
//            String salt = ConvertUtils.randomGen(8);
//
//            String passwordEncode = PasswordUtil.encrypt(oldUser.getUsername(), newPassword, salt);
//            oldUser.setPassword(passwordEncode);
//            sysBaseAPI.updateUser(oldUser);
//            result.success("密码修改完成!");
//        }
//        return result;
//    }
//
//    @AutoLog(value = "修改手机号")
//    @ApiOperation(value="用户管理", notes="修改手机号")
//    @PostMapping (value = "/changePhone")
//    public Result<LoginUser> changePhone(@RequestBody LoginUser sysUser){
//        Result<LoginUser> result = new Result<>();
//        LoginUser oldUser = sysBaseAPI.getUserById(sysUser.getId());
//        //修改手机号
//        boolean matches = PATTERN_PHONE.matcher(sysUser.getPhone()).matches();
//        if (!matches){
//            result.error500("手机号格式不正确");
//        }else {
//            sysBaseAPI.updateUser(oldUser.setPhone(sysUser.getPhone()));
//            result.success("手机号修改成功");
//        }
//        return result;
//    }
//
//    @AutoLog(value = "修改用户名")
//    @ApiOperation(value="用户管理", notes="修改用户信息")
//    @PostMapping(value = "/changeAvatar")
//    public Result<LoginUser> changeAvatar(@RequestBody LoginUser loginUser) {
//        Result<LoginUser> result = new Result<LoginUser>();
//        try {
//            LoginUser oldUser = sysBaseAPI.getUserById(loginUser.getId());
//            if(oldUser==null) {
//                result.error500("未找到对应实体");
//            }else {
//                sysBaseAPI.updateUser(loginUser);
//                loginUser.setAvatar(viewPath+loginUser.getAvatar());
//                result.setResult(loginUser);
//                result.success("修改成功!");
//            }
//        } catch (Exception e) {
//            log.error(e.getMessage(), e);
//            result.error500("操作失败");
//        }
//        return result;
//    }

}