def get_code(): """ 获取图片验证码 :return: """ data = {"msg_type": "s", "http_status": 200} code = create_img_code() data['code'] = code return data
def call_verification(code_url_obj, code): """ 记录调用次数,并查看是否有调用权限 :return: """ # 记录调用 if current_user.is_authenticated: user_id = current_user.str_id else: user_id = None mdbs["sys"].db.sys_call_record.insert_one({ "type": "api", "req_path": request.path, "ip": request.remote_addr, "user_id": user_id, "time": time.time() }) # 查找1分钟内本IP的调用次数 freq = mdbs["sys"].db.sys_call_record.find({ "type": "api", "req_path": request.path, "ip": request.remote_addr, "user_id": user_id, "time": { "$gte": time.time() - 60 } }).count(True) if freq: if freq > get_config("verify_code", "MAX_NUM_SEND_SAMEIP_PERMIN"): # 大于单位时间最大调用次数访问验证 data = { 'msg': gettext( "The system detects that your network is sending verification codes frequently." " Please try again later!"), 'msg_type': "w", "custom_status": 401 } return False, data elif freq > get_config("verify_code", "MAX_NUM_SEND_SAMEIP_PERMIN_NO_IMGCODE") + 1: # 已超过单位时间无图片验证码情况下的最大调用次数, 验证图片验证码 # 检验图片验证码 r = verify_image_code(code_url_obj, code) if not r: data = { 'msg': gettext("Image verification code error, email not sent"), 'msg_type': "e", "custom_status": 401 } # 验证错误,开启验证码验证 data["open_img_verif_code"] = True data["code"] = create_img_code() return False, data elif freq > get_config("verify_code", "MAX_NUM_SEND_SAMEIP_PERMIN_NO_IMGCODE"): # 如果刚大于单位时间内,无图片验证码情况下的最大调用次数, 返回图片验证码验证码 data = { 'msg': gettext( "The system detected that your operation is too frequent and" " you need to verify the picture verification code"), 'msg_type': "w", "custom_status": 401 } data["open_img_verif_code"] = True data["code"] = create_img_code() return False, data return True, ""