def captcha(self): telephone = self.get_argument("telephone", '') role = self.get_argument("role", None) user = UserDao(session=self.session) user_id = user.get_user_id_by_tel_and_role(telephone, role) if not user_id: # 判断手机号是否存在 result = { 'result': False, 'msg': err_msgs['TEL_OR_ROLE_NOT_EXIST'] } self.set_status(400) return result # if not user.exist_role_group(user_id, role): # 判断用户是否存在该角色 # result = { # 'result': False, # 'msg': err_msgs['ROLE_NOT_EXIST'] # } # self.set_status(400) # return result if setting.SMS_SEND: send_res = AuthCode().send_msg(telephone) if send_res['result']: code = send_res['code'] else: result = send_res self.set_status(400) return result else: code = '123456' r_captcha = self.redis_captcha(telephone) self.redis.set_value(r_captcha, code) self.redis.expire(r_captcha, setting.CAPTCHA_EXPIRE_TIME) result = {'result': True, 'msg': "OK"} return result
def verify_args(self,captcha=None,telephone=None,v_id=None,v_code=None,role=None,platform=None,app_version=None,m_platform=None, syn_uuid=None): ''' 判断短信验证码和图形验证码是否正确 :param captcha: 用户填写的手机验证码 :param telephone: 手机号 :param v_id: 图形验证码id :param v_code: 用户填写的图形验证码 :param app_version :param m_platform :param syn_uuid :return: ''' authcode = self.redis.get_value(self.redis_captcha(telephone)) verify_code = self.redis.get_value(self.redis_verify(v_id)) user = UserDao(session=self.session) user_id = user.get_user_id_by_tel_and_role(telephone,role) result = {} if not authcode: # 判断短信验证码是否失效 result.update({ 'result': False, 'msg': err_msgs['SMS_TIMEOUT'], }) self.set_status(417) elif authcode and authcode != captcha: # 判断短信验证码是否一致 if self.redis.error_num_over(telephone): pass #连续输入错误5次锁死该用户 result.update({ 'result': False, 'msg': err_msgs['SMS_ERR'], }) self.set_status(400) elif v_id and not verify_code:# 判断图形验证码是否失效 result.update({ 'result': False, 'msg': err_msgs['VERIFY_TIMEOUT'], }) self.set_status(417) elif verify_code and verify_code.lower() != v_code.lower():# 判断图形验证码是否一致 result.update({ 'result': False, 'msg': err_msgs['VERIFY_ERR'], }) self.set_status(400) elif not user_id: # 判断用户 result.update({ 'result': False, 'msg': err_msgs['TEL_OR_ROLE_NOT_EXIST'] }) self.set_status(404) elif platform == setting.PLATFORM_MOBILE and (not app_version or not m_platform or not syn_uuid): result.update({ 'result': False, 'msg': err_msgs['PARAMS_MISSING'] }) self.set_status(400) else: result.update({ 'user_id':user_id, 'result': True, }) # 删除redis中的短信验证码信息 self.redis.del_key(self.redis_captcha(telephone)) # 删除redis中的图形验证码信息 self.redis.del_key(self.redis_verify(v_id)) # 删除用户验证码输入错误相关的信息 self.redis.del_key(self.redis_err_num(telephone)) return result