def validate(self, attrs): # 1. 获取发送短信的凭证 access_token, 并校验 access_token = attrs['access_token'] # 2.从user.User模型中调用验证access_token的方法 mobile = User.check_sms_code_token(access_token) if not mobile: return serializers.ValidationError('无效或错误的access_toekn') # 3. 判断60s内是否发送过短信 redis_conn = get_redis_connection('verify_code') send_flag = redis_conn.get('send_flag_%s' % mobile) if send_flag: raise serializers.ValidationError('请求次数过于频繁') self.mobile = mobile return attrs