抖音实战~密码找回

在这里插入图片描述

1. 密码找回流程图

在这里插入图片描述

2. 前端源码
			/**
			 * 密码找回
			 */
	updatePassword() {
    
    
				var me = this;
				var mobile = me.mobile;
				// 提交前,手机号校验
				var reg = /^1[0-9]{
    
    10,10}$/;
				if (!mobile || !reg.test(mobile)) {
    
    
					uni.showToast({
    
    
						title: '请输入正确的手机号',
						icon: 'none'
					})
					return
				}

				if (!this.agree) {
    
    
					uni.showToast({
    
    
						title: '请先同意《隐私及服务协议》',
						icon: 'none'
					});
					return;
				}

				var serverUrl = app.globalData.serverUrl;
				// 密码找回
				var password = me.password;

				if (app.isStrEmpty(password)) {
    
    
					uni.showToast({
    
    
						title: "新密码不能为空",
						icon: "none"
					});
					return;
				}
				console.log("forgotPassword")
				// uni.showLoading()
				// 调用后端登录注册
				uni.request({
    
    
					method: "POST",
					url: serverUrl + "/user/forgotPassword",
					data: {
    
    
						"mobile": mobile,
						"smsCode": me.verifyCode,
						"newPassword": password
					},
					success(result) {
    
    
						console.log("result", result)
						var status = result.data.status;
						if (status != 200) {
    
    
							uni.showToast({
    
    
								title: result.data.msg,
								icon: "none",
								duration: 3000
							});
						}

						if (status == 200) {
    
    
							uni.navigateTo({
    
    
								url: '../loginRegist/loginRegist'
							})
							uni.showToast({
    
    
								title: result.data.msg,
								icon: "none",
								duration: 3000
							});
							// 登录成功,跳转登录页,关闭当前页
						}
					}
				});
			}
3. 后端
/**
     * 重置密码
     *
     * @param forgotPasswordBO 手机号 + 验证码+新密码
     */
    @PostMapping("forgotPassword")
    public GraceJSONResult forgotPassword(@Valid @RequestBody ForgotPasswordBO forgotPasswordBO,
                                          HttpServletRequest request) {
    
    
        String mobile = forgotPasswordBO.getMobile();
        String code = forgotPasswordBO.getSmsCode();
        String newPassword = forgotPasswordBO.getNewPassword();

        // 1. 从redis中获得验证码进行校验是否匹配
        String redisCode = redis.get(MOBILE_SMSCODE + ":" + mobile);
        if (StringUtils.isBlank(redisCode) || !redisCode.equalsIgnoreCase(code)) {
    
    
            return GraceJSONResult.errorCustom(ResponseStatusEnum.SMS_CODE_ERROR);
        }

        // 2. 查询数据库,判断用户是否存在
        Users user = userService.queryMobileIsExist(mobile);
        if (user == null) {
    
    
            return GraceJSONResult.errorCustom(ResponseStatusEnum.ACCOUNT_NOT_EXIST);
        }
        this.userService.forgotPassword(mobile, newPassword);
        //重置密码成功,将验证码删除
        redis.del(MOBILE_SMSCODE + ":" + mobile);
        return GraceJSONResult.ok(ResponseStatusEnum.RESET_PASSWORD_SUCCESS);
    }

拦截器放过

在这里插入图片描述