def get_bet_number(game_serial, amount): redis_tool = RedisTools() if redis_tool.exists(game_serial): lock = redis_lock.Lock(redis_tool.redis_conn, 'bet_number', expire=1) if lock.acquire(): array_full = json.loads(redis_tool.get(game_serial)) length = len(array_full) if length <= 0: return 3000 if length < amount: return 3001 award_number_arr = random.sample(array_full, amount) # print(award_number_arr) # print(type(award_number_arr)) array_full = list( set(array_full).difference(set(award_number_arr))) if len(array_full) <= 0: redis_tool.delete(game_serial) else: redis_tool.set(game_serial, json.dumps(array_full)) lock.release() return award_number_arr return 3002
def check_authentication(code): env = get_conf('env') if env == 'dev' and code == '111111': return True result = False redis_tool = RedisTools() authentication_value = str(redis_tool.get('authentication'), encoding='utf8') if authentication_value == code: result = True authentication_value = generate_str() redis_tool.set('authentication', authentication_value) return result
def check_sms_email_vcode(self, vcode_key, vcode): email_config = get_sms_email(vcode_key) if email_config == {}: self.return_error(35034) redis = RedisTools() redis_key = self.get_sms_email_key(email_config['redis_key'], vcode) redis_value = redis.get(redis_key) if not redis_value: self.return_error(30036) if str(redis_value, encoding='utf-8') != str(vcode): self.return_error(30036) redis.delete(redis_key) return True
def generate_phase(phase_prefix): phase_serial = 1 redis_tool = RedisTools() today = time.strftime("%y%m%d", time.localtime()) phase_date = today if redis_tool.exists("phase_date"): phase_date = redis_tool.get("phase_date") if int(today) != int(phase_date): redis_tool.set("phase_date", today) redis_tool.set("phase_serial", 1) phase_date = today else: if redis_tool.exists("phase_serial"): phase_serial = int(redis_tool.get("phase_serial")) + 1 redis_tool.set("phase_serial", phase_serial) else: redis_tool.set("phase_serial", 1) else: redis_tool.set("phase_date", today) redis_tool.set("phase_serial", 1) return str(phase_prefix) + str( int(phase_date)) + str(phase_serial).zfill(4)
def get_recover_number(game_serial, numbers): redis_tool = RedisTools() if redis_tool.exists(game_serial): lock = redis_lock.Lock(redis_tool.redis_conn, 'bet_number', expire=1) if lock.acquire(): array_full = json.loads(redis_tool.get(game_serial)) array_full.extend(numbers) redis_tool.set(game_serial, json.dumps(array_full)) lock.release() return True return False
def check_vcode(self, vcode, code_type, mobile, user_type=_USER_TYPE_INVEST, register_by=None, direct_return_error=True): if register_by in [_REGISTER_BY_MOBILE]: self.check_mobile_all(mobile) elif register_by in [_REGISTER_BY_EMAIL]: self.check_email(mobile) elif register_by in [ _REGISTER_AUTHEN_MOBILE, _REGISTER_SET_PAYPD_MOBILE, _RESET_PWD_MOBILE, _REGISTER_AUTHEN_EMAIL, _REGISTER_SET_PAYPD_EMAIL, _RESET_PWD_EMAIL, _REGISTER_SET_EMAIL, _REGISTER_SET_MOBILE, _REGISTER_RESET_PAYPD_MOBILE, _REGISTER_RESET_PAYPD_EMAIL, ]: pass else: self.check_mobile(mobile) env = get_conf("env") if vcode == "111111" and env == 'dev': return True redis = RedisTools() redis_key = self.get_sms_key(mobile, code_type, user_type) check_result = redis.get(redis_key) if check_result: redis.delete(redis_key) return True else: if direct_return_error: self.return_aes_error(30059) else: return False
def check_vcode_picture(self, vcode_str, vcode_key='', vcode_type=_REQUEST_TYPE_REGISTER, user_type=_USER_TYPE_INVEST): env = get_conf('env') if vcode_str == "111111" and env == 'dev': return True if vcode_key == '': redis_key = self.vcode_picture_key + str(vcode_type) + ':' + str( user_type) + ':' + str(vcode_str).lower() else: redis_key = self.vcode_picture_key + str(vcode_type) + ':' + str( user_type) + ':' + str(vcode_key) redis_tools = RedisTools() redis_result = redis_tools.get(redis_key) raise_logger('check vcode' + ' ' + str(redis_key)) if redis_result is None: self.return_aes_error(30052) if str(redis_result, encoding='utf-8').lower() != str(vcode_str).lower(): self.return_aes_error(30060) redis_tools.delete(redis_key) return True
def get_access_token_by_user_id(self, user_id, user_type=_USER_TYPE_INVEST, source=ApiTokenModel.source_type_1): """ 内部方法,只能用于验证用户,不能返回 通过手机号获取用户token,先查看redis中是否有.如果没有在进行数据库查询 :param user_id: 用户user_id :param user_type: 用户类型 :return: """ self.check_api_token_source(source) access_token_key = self.make_access_token_redis_key( user_id, user_type, source) redis_tool = RedisTools() # 1.0 从redis中查取 access_token = redis_tool.get(access_token_key) if access_token: return str(access_token, encoding='utf-8') # 2.0 从数据库中查取 with self.session_scope() as session: if user_type == _USER_TYPE_INVEST: UserModel = InvestUserModel if user_type == _USER_TYPE_ADMIN: UserModel = AdminUserModel else: self.return_aes_error(10030) user = session.query(UserModel, ApiTokenModel.token, ApiTokenModel.expire_at, UserModel.user_id).\ outerjoin(ApiTokenModel, UserModel.user_id == ApiTokenModel.user_id)\ .filter( UserModel.user_id == user_id, ApiTokenModel.token_type == self.mysql_access_token_key, ApiTokenModel.user_type == user_type, ApiTokenModel.deleted == False, ApiTokenModel.source == source, ApiTokenModel.expire_at > datetime.datetime.now() ).first() if not user: self.return_aes_error(10031) if not user.token: # 用户token过期 self.return_aes_error(10032) end_time = int( time.mktime( time.strptime(str(user.expire_at), '%Y-%m-%d %H:%M:%S'))) now_time = int(time.time()) last_time = end_time - now_time if last_time <= 0: # 将之前的token删除 session.query(ApiTokenModel).filter_by( user_id=user.user_id, token_type=self.mysql_access_token_key, user_type=user_type, source=source, deleted=False).update({ ApiTokenModel.deleted: True, ApiTokenModel.deleted_at: datetime.datetime.now() }) session.commit() self.return_aes_error(10033) # 3.0将token插入到redis中, 应该是mysql的时间减去当前时间 redis_tool.set(access_token_key, user.token, ex=last_time) return user.token