实现用户手机号码的重置功能
添加了新的API端点,允许用户重置其关联的手机号码。实现包括在SysProfileController中添加新的更新方法,使用正则表达式验证手机号码格式,并处理相关的业务逻辑。此外,更新了SysUserMapper接口和SysUserServiceImpl类,以支持数据库操作的变更。master
parent
d62d8d48b6
commit
4981ed3c6b
|
@ -15,6 +15,7 @@ import com.muyu.common.system.domain.SysUser;
|
||||||
import com.muyu.common.system.domain.LoginUser;
|
import com.muyu.common.system.domain.LoginUser;
|
||||||
import com.muyu.system.domain.resp.ProfileResp;
|
import com.muyu.system.domain.resp.ProfileResp;
|
||||||
import com.muyu.system.service.SysUserService;
|
import com.muyu.system.service.SysUserService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
@ -26,6 +27,7 @@ import java.util.Arrays;
|
||||||
*
|
*
|
||||||
* @author muyu
|
* @author muyu
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/user/profile")
|
@RequestMapping("/user/profile")
|
||||||
public class SysProfileController extends BaseController {
|
public class SysProfileController extends BaseController {
|
||||||
|
@ -105,6 +107,45 @@ public class SysProfileController extends BaseController {
|
||||||
return error("修改密码异常,请联系管理员");
|
return error("修改密码异常,请联系管理员");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重置手机号
|
||||||
|
*/
|
||||||
|
@Log(title = "重置手机号", businessType = BusinessType.UPDATE)
|
||||||
|
@RequestMapping(value = "/updatePhonenumber/{phonenumber}",method = RequestMethod.POST)
|
||||||
|
public Result updatePhonenumber(@PathVariable(value = "phonenumber") String phonenumber) {
|
||||||
|
try {
|
||||||
|
if (!isValidPhoneNumber(phonenumber)) {
|
||||||
|
return error("手机号格式不正确");
|
||||||
|
}
|
||||||
|
String username = SecurityUtils.getUsername();
|
||||||
|
SysUser user = userService.selectUserByUserName(username);
|
||||||
|
if (user == null) {
|
||||||
|
return error("用户不存在");
|
||||||
|
}
|
||||||
|
if (userService.updateUserPhonenumber(username, phonenumber) > 0) {
|
||||||
|
LoginUser loginUser = SecurityUtils.getLoginUser();
|
||||||
|
loginUser.getSysUser().setPhonenumber(phonenumber);
|
||||||
|
tokenService.setLoginUser(loginUser);
|
||||||
|
return success();
|
||||||
|
} else {
|
||||||
|
return error("修改手机号失败,请联系管理员");
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 记录异常日志
|
||||||
|
log.error("更新手机号时发生异常", e);
|
||||||
|
return error("系统异常,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isValidPhoneNumber(String phoneNumber) {
|
||||||
|
// 正则表达式校验手机号格式
|
||||||
|
String regex = "^1[3-9]\\d{9}$";
|
||||||
|
return phoneNumber.matches(regex);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 头像上传
|
* 头像上传
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -294,6 +294,11 @@ public class SysUserController extends BaseController {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据用户编号获取授权角色
|
* 根据用户编号获取授权角色
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -160,4 +160,6 @@ public interface SysUserMapper extends BaseMapper<SysUser> {
|
||||||
|
|
||||||
SysUser findByPhone(String phonenumber);
|
SysUser findByPhone(String phonenumber);
|
||||||
|
|
||||||
|
|
||||||
|
int updateUserPhonenumber(@Param("username") String username, @Param("phonenumber") String phonenumber);
|
||||||
}
|
}
|
||||||
|
|
|
@ -244,4 +244,7 @@ public interface SysUserService extends IService<SysUser> {
|
||||||
String sendCode(String phonenumber);
|
String sendCode(String phonenumber);
|
||||||
|
|
||||||
String checkCode(String phonenumber, String code);
|
String checkCode(String phonenumber, String code);
|
||||||
|
|
||||||
|
|
||||||
|
int updateUserPhonenumber(String username, String phonenumber);
|
||||||
}
|
}
|
||||||
|
|
|
@ -586,6 +586,10 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int updateUserPhonenumber(String username, String phonenumber) {
|
||||||
|
return userMapper.updateUserPhonenumber(username,phonenumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -291,6 +291,12 @@
|
||||||
where user_name = #{userName}
|
where user_name = #{userName}
|
||||||
</update>
|
</update>
|
||||||
|
|
||||||
|
<update id="updateUserPhonenumber">
|
||||||
|
update sys_user
|
||||||
|
set phonenumber = #{phonenumber}
|
||||||
|
where user_name = #{username}
|
||||||
|
</update>
|
||||||
|
|
||||||
|
|
||||||
<delete id="deleteUserById" parameterType="Long">
|
<delete id="deleteUserById" parameterType="Long">
|
||||||
update sys_user
|
update sys_user
|
||||||
|
|
Loading…
Reference in New Issue