def search_list(cls, _user_id=None): total, item_list = cls.search_item_list(_user_id=_user_id) if total <= 0 or item_list is None: return success(package_result(0, [])) return success( package_result(total, [item.to_dict() for item in item_list]))
def search_list(): log.info("获取维护人员列表信息...") total, item_list = Maintain.search_item_list() if total <= 0 or item_list is None: return success(package_result(0, [])) result_list = [] for item in item_list: # item_dict = item.to_dict() # while True: # # # 判断是否所有大厅 # if item.address_id == Maintain.ALL_ADDRESS_ID: # full_address = Maintain.ALL_ADDRESS_STR # break # # # 获取地址信息 # address = Address.get(item.address_id) # if address is None: # log.warn("当前地址不存在: maintain_id = {} address_id = {}".format(item.id, item.address_id)) # full_address = '当前地址不存在!' # break # # full_address = address.get_full_address() # break # # item_dict['address'] = full_address result_list.append(MaintainService.to_address_dict(item)) return success(package_result(total, result_list))
def edit_role(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") role_id = request.json.get('id') if not isinstance(role_id, int): return fail(HTTP_OK, u"角色ID数据类型不正确!") role = Role.get(role_id) if role is None: log.warn("当前角色ID没有查找到对应的角色信息: id = {}".format(role_id)) return fail(HTTP_OK, u"当前角色ID不存在!") name = request.json.get('name') if not isinstance(name, basestring): return fail(HTTP_OK, u"角色名称数据类型不正确!") # 如果当前角色名称没变 则直接返回成功 if role.name == name: return success(role.to_dict()) if Role.get_by_name(name) is not None: log.warn("当前角色名称已经存在: {}".format(name)) return fail(HTTP_OK, u"当前角色名称已经存在,不能重复") role.name = name if not role.save(): return fail(HTTP_OK, u"当前角色信息存储错误!") return success(role.to_dict())
def login(): if g.admin is not None and g.admin.is_authenticated: return success(u"账户已经登录!") if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") username = request.json.get('username', None) password = request.json.get('password', None) if username is None or password is None: log.warn("用户账号密码没有传过来...username = {} password = {}".format( username, password)) return fail(HTTP_OK, u"没有用户密码信息!") # 获取是否需要记住密码 is_remember = request.json.get('remember', False) admin = Admin.query.authenticate(username, password) if admin is not None: # 判断账户是否被停用 if not admin.is_active(): return fail(HTTP_OK, u'账户已被暂停使用,请联系管理员') if is_remember: login_user(admin, remember=True) else: login_user(admin, remember=False) log.info("用户登录成功: {} {}".format(username, password)) return success(u'登录成功') admin = Admin.get_by_username(username) if admin is None: return fail(HTTP_OK, u'用户不存在') return fail(HTTP_OK, u'用户名或密码错误,请重新登陆!')
def new_charge(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") # json = { # 'name': 'xxx', # 'charge_mode': 'xxxx', # } name = request.json.get('name', None) charge_mode = request.json.get('charge_mode', None) if name is None or not isinstance(charge_mode, int) or charge_mode <= 0: log.warn("参数错误: name = {} charge_mode = {}".format(name, charge_mode)) return fail(HTTP_OK, u"name or charge_mode 参数错误!") if ChargeService.find_by_name(name) is not None: log.warn("费率命名冲突, 数据库中已存在名字相同的费率模板: name = {}".format(name)) return fail(HTTP_OK, u"费率命名冲突, 数据库中已存在名字相同的费率模板: name = {}".format(name)) charge, is_success = ChargeService.create(name, charge_mode) if not is_success: log.warn("创建费率模板失败: name = {} charge_mode = {}".format( name, charge_mode)) return fail( HTTP_OK, u"创建费率模板失败: name = {} charge_mode = {}".format(name, charge_mode)) return success(charge.to_dict())
def get_user_by_id(user_id): # 先通过手机号码查找 user = UserService.get_user_by_mobile(user_id) if user is not None: return success(user.to_dict()) try: a_id = int(user_id) user = User.get(a_id) if user is not None: return success(user.to_dict()) except Exception as e: log.error("用户ID信息无法转换为 int 类型: user_id = {}".format(user_id)) log.exception(e) return success(None)
def device_game_add(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") username = request.json.get('username') password = request.json.get('password') name = request.json.get('game') version = request.json.get('version') if not isinstance(name, basestring) or not isinstance(version, basestring): log.error( "参数错误: username = {} password = {} game = {} version = {}".format( username, password, name, version)) return fail(HTTP_OK, u"参数错误") # 先判断能否登录 is_success, msg = AdminService.verify_authentication(username, password) if not is_success: return fail(HTTP_OK, msg) # 开始更新游戏信息 if not DeviceGameService.add_device_game(name, version): return fail(HTTP_OK, u"游戏更新失败,请重试!") return success(u'游戏更新成功!')
def get_role_list(): ''' page: 当前页码 size: 每页读取数目, 最大不超过50项 ''' if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") page = request.json.get('page') size = request.json.get('size') if not isinstance(page, int) or \ not isinstance(size, int): log.warn("请求参数错误: page = {} size = {}".format(page, size)) return fail(HTTP_OK, u"请求参数错误") # 请求参数必须为正数 if page <= 0 or size <= 0: msg = "请求参数错误: page = {} size = {}".format(page, size) log.error(msg) return fail(HTTP_OK, msg) if size > 50: log.info("翻页最大数目只支持50个, 当前size超过50 size = {}!".format(size)) size = 50 return success(Role.find_role_list(page, size))
def new_admin(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") # 判断当前管理员是否为超级管理员,只有超级管理员才有修改管理员信息的权限 if current_user.role.name != Role.SUPER_ADMIN: return fail(HTTP_OK, u"没有操作权限,只有超级管理员才能够编辑管理员信息...!") name = request.json.get('name', None) username = request.json.get('username', None) role_id = request.json.get("role_id", None) password = request.json.get("password", None) if name is None or username is None or role_id is None or password is None: return fail(HTTP_OK, u"添加管理员参数不正确...!") # 查找当前用户名是否已经被占用了 if Admin.get_by_username(username) is not None: return fail(HTTP_OK, u"该用户名已被使用...!") # 创建并添加管理员 admin, is_success = Admin.create(username, password, name, role_id) if admin is None: log.warn("管理员信息添加失败") return fail(HTTP_OK, u"管理员信息添加失败!") log.info("管理员信息添加成功: {}".format(admin.to_dict())) return success(admin.to_dict())
def delete_admin(): # 只支持修改 名称 与 启用状态 if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") # 判断当前管理员是否为超级管理员,只有超级管理员才有修改管理员信息的权限 if current_user.role.name != Role.SUPER_ADMIN: return fail(HTTP_OK, u"没有操作权限,只有超级管理员才能够编辑管理员信息...!") a_id = request.json.get('id', None) if a_id is None: log.warn("没有传入管理员id信息") return fail(HTTP_OK, u"没有传入管理员id信息!") if g.admin.id == a_id: return fail(HTTP_OK, u"不能删除自身账户信息") admin = Admin.get(a_id) if admin is None: log.warn("当前ID信息不存在: id = {}".format(a_id)) return fail(HTTP_OK, u"当前ID信息不存在!") # 判断存储是否正确 if not admin.delete(): log.warn("管理员信息删除失败!!!") return fail(HTTP_OK, u"管理员信息删除失败!") log.info("管理员信息删除成功: {}".format(admin.to_dict())) return success(admin.to_dict())
def get_proxy(): if not request.is_json: log.warn("参数错误...") return fail() # { # 'username': '******', # 'password': '******', # 'type': 'static' or 'dynamic' // 这个参数保留,目前不一定用 # } username = request.json.get('username') password = request.json.get('password') if not isinstance(username, basestring): log.error("用户错误,不是字符串: username = {} type = {}".format(username, type(username))) return fail('用户名类型错误!') if not isinstance(password, basestring): log.error("密码错误,不是字符串: password = {} type = {}".format(password, type(password))) return fail('密码类型错误!') # 判断当前用户是否在redis中 origin_password = redis.get(username) if origin_password is None: log.error("当前用户不存在: username = {} password = {}".format(username, password)) return fail('当前用户不存在!') if origin_password != password: log.error("密码错误: username = {} password = {}".format(username, password)) return fail('密码错误!') # 请求动态代理服务 proxy = get_dynamic_proxy(LOCAL_HOST) return success(proxy)
def delete_charges(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") id_list = request.json.get('list', None) if not isinstance(id_list, list): log.warn("参数错误: id_list = {}".format(id_list)) return fail(HTTP_OK, u"传入不是id列表") result_list = [] for charge_id in id_list: charge = Charge.get(charge_id) if charge is None: log.warn("当前ID设备信息不存在: {}".format(charge_id)) continue if not charge.delete(): log.warn("费率信息删除失败: {}".format( json.dumps(charge.to_dict(), ensure_ascii=False))) continue result_list.append(charge_id) # 如果当前费率有被成功删除的,则需要更新redis中的费率信息 # if len(result_list) > 0: # charge = ChargeService.update_charge_to_redis() # if charge is not None: # log.info("完成一次 redis 中费率更新: 最新费率 charge_mode = {}".format(charge.charge_mode)) # else: # log.info("更新费率到redis失败!") return success(result_list)
def delete_user(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") result_list = [] user_list = request.json.get('list', None) if not isinstance(user_list, list): log.warn("没有传入ID列表信息..") return fail(HTTP_OK, u"传入ID参数错误") for user_id in user_list: if not isinstance(user_id, int): log.warn("当前用户ID数据类型错误: {}".format(user_id)) continue user = User.get(user_id) if user is None: log.warn("当前用户ID查找用户失败: {}".format(user_id)) continue if user.deleted: log.warn("当前用户信息已经被删除: {}".format(user_id)) continue if not user.delete(): log.warn("用户信息删除失败: {}".format( json.dumps(user.to_dict(), ensure_ascii=False))) continue result_list.append(user.id) log.info("删除用户信息成功: {}".format(result_list)) return success(result_list)
def update_game(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") game = request.json.get('game') version = request.json.get('version') md5 = request.json.get('md5') if not isinstance(game, basestring) or \ not isinstance(version, basestring) or \ not isinstance(md5, basestring): log.error("参数错误: game = {} version = {} md5 = {}".format( game, version, md5)) return fail(HTTP_OK, u"参数错误") # 更新游戏列表 if not GameListService.update_game_list(game, version): return fail(HTTP_OK, u"更新游戏列表失败!") game_manage = GameVersionManageService.get_game_info(game, version) if game_manage is None: game_manage, is_success = GameVersionManageService.create( game, version, md5) if not is_success: return fail(HTTP_OK, u"游戏更新失败,请重试!") else: if not GameVersionManageService.update_game_info(game_manage, md5): return fail(HTTP_OK, u"游戏更新失败,请重试!") # 开始更新游戏信息 if not DeviceGameService.add_device_game(game, version): return fail(HTTP_OK, u"游戏更新失败,请重试!") return success(u'游戏更新成功!')
def delete_address(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") id_list = request.json.get('list', None) if not isinstance(id_list, list): log.warn("参数错误: id_list = {}".format(id_list)) return fail(HTTP_OK, u"传入不是id列表") result_list = [] for address_id in id_list: # 查找地址是否已经存在 address = Address.get(address_id) if address is None: log.warn("地址信息不存在: {}".format(address_id)) continue # 如果改地址管理的设备数目不为0 则不能删除 if address.device_num > 0: log.warn("当前地址关联的设备数不为0,不能删除: address_id = {}".format(address_id)) continue # 判断是否删除成功 if not address.delete(): log.warn("地址信息删除失败: {}".format( json.dumps(address.to_dict(), ensure_ascii=False))) continue result_list.append(address_id) return success(result_list)
def get_jsapi_signature(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") url = request.json.get('url') if url is None: log.warn("没有传入url 参数: {}".format( json.dumps(request.json, ensure_ascii=False))) return fail(HTTP_OK, u"没有传入url参数!") jsapi_ticket = redis_cache_client.get(WECHAT_JSAPI_TICKET_KEY) if jsapi_ticket is None: log.warn("没有jsapi_ticket: user_id = {}".format(g.user_id)) return fail(HTTP_OK, u'没有jsapi_ticket') import time timestamp = int(time.time()) nonceStr = wx_pay.nonce_str(31) signature = gen_jsapi_signature(timestamp, nonceStr, jsapi_ticket, url) config = { 'debug': settings.DEBUG, 'appId': settings.WECHAT_APP_ID, 'timestamp': str(timestamp), 'nonceStr': nonceStr, 'signature': signature, 'jsApiList': ['scanQRCode'] } log.info("当前user_id获取的wx.config = {}".format( json.dumps(config, ensure_ascii=False))) return success(config)
def get_user_info(): user = get_current_user(g.user_id) if user is None: log.warn("当前user_id没有获得用户信息: {}".format(g.user_id)) return fail(HTTP_OK, u'没有当前用户信息') if user.deleted: log.warn("当前user_id用户已经被删除: {}".format(g.user_id)) return fail(HTTP_OK, u'当前用户已经被删除') # 判断昵称或头像是否已经获取到了 if user.head_img_url == '' or user.nick_name == '': # 先判断token是否存在 subscribe, nick_name, head_img_url = get_wechat_user_info(user.openid) if head_img_url == '' or nick_name == '': log.error( "再次更新用户ID = {} 头像与昵称失败: head_img_url = {} nick_name = {}". format(user.id, head_img_url, nick_name)) else: # 存储用户昵称和头像信息 if UserService.save_nick_and_head(user, nick_name, head_img_url): log.info( "重新更新用户昵称与头像成功: user_id = {} head = {} nike = {}".format( user.id, user.head_img_url, user.nick_name)) # # 存储用户信息 # user.head_img_url = head_img_url # user.nick_name = nick_name # if user.save(): # log.info("重新更新用户昵称与头像成功: user_id = {} head = {} nike = {}".format( # user.id, user.head_img_url, user.nick_name)) return success(user.to_dict())
def check_connect(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") device_code = request.json.get('device_code') if device_code is None: return fail(HTTP_OK, u"not have device_code!!!") # 获得设备使用状态 device_status = DeviceService.get_device_status(device_code) if device_status is None: return success({ 'status': -1, 'device_status': device_status, 'msg': "not deploy" }) # 保持心跳 DeviceService.keep_device_heart(device_code) # 从维护状态跳转到空闲状态 if device_status == DeviceStatus.STATUS_MAINTAIN: log.info("当前状态为维护状态,设备已经有心跳需要重新设置空闲状态!") DeviceService.status_transfer(device_code, device_status, DeviceStatus.STATUE_FREE) # 重新获得设备状态 device_status = DeviceService.get_device_status(device_code) device_code_key = RedisClient.get_device_code_key(device_code) record_key = redis_cache_client.get(device_code_key) if record_key is None: return success({ 'status': 0, 'device_status': device_status, 'msg': "not login" }) return success({ "status": 1, "token": record_key, 'device_status': device_status, "msg": "login successed!" })
def geetest(): # 初始化极验验证码 gt = GeetestLib(settings.PC_GEETEST_ID, settings.PC_GEETEST_KEY) status = gt.pre_process(g.openid) session[gt.GT_STATUS_SESSION_KEY] = status response_str = gt.get_response_str() log.info("极验验证码初始化状态: response_str = {}".format(response_str)) return success(json.loads(response_str))
def get_address(location): if not isinstance(location, basestring) or location.strip() == '': log.warn("传入参数有误: location = {}".format(location)) return fail(HTTP_OK, u"参数错误!") address_list = Address.find_address_by_location(location) if len(address_list) > 0: return success(address_list) try: a_id = int(location) address = Address.get(a_id) if address is not None: return success([address.to_dict()]) except Exception as e: log.error("地址信息无法转换为 int 类型: location = {}".format(location)) log.exception(e) return success([])
def get_role(role_info): if not isinstance(role_info, basestring) or role_info.strip() == '': log.warn("传入参数有误: location = {}".format(role_info)) return fail(HTTP_OK, u"参数错误!") role = Role.get_by_name(role_info) if role is not None: return success([role.to_dict()]) try: a_id = int(role_info) role = Role.get(a_id) if role is not None: return success([role.to_dict()]) except Exception as e: log.error("角色名称信息无法转换为 int 类型: role_info = {}".format(role_info)) log.exception(e) return success([])
def get_online_status(): user_key = RedisClient.get_user_key(g.user_id) record_key = redis_cache_client.get(user_key) if record_key is None: return fail(HTTP_OK, u'当前用户没有上机信息', 0) charging = redis_cache_client.get(record_key) if charging is None: return fail(HTTP_OK, u'当前用户没有上机信息', 0) return success(WindowsService.get_current_time_charging(charging))
def delete_role(role_id): role = Role.get(role_id) if role is None: log.warn("通过当前ID没有查到角色信息: role_id = {}".format(role_id)) return fail(HTTP_OK, u"角色信息不存在!") if not role.delete(): log.warn("设备信息删除失败: {}".format( json.dumps(role.to_dict(), ensure_ascii=False))) return fail(HTTP_OK, u"角色设备信息失败!") return success(role.id)
def device_need_update(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") device_id = request.json.get('device_id') if device_id is None: log.warn("参数错误...") return fail(HTTP_OK, u"参数错误!") return success(DeviceGameService.is_device_need_update(device_id))
def search_by_keyword(page, size, keyword): # 请求参数必须为正数 if page <= 0 or size <= 0: msg = "请求参数错误: page = {} size = {}".format(page, size) log.error(msg) return fail(HTTP_OK, msg) if size > 50: log.info("翻页最大数目只支持50个, 当前size超过50 size = {}!".format(size)) size = 50 total, item_list = MaintainService.find_list_by_keyword( page, size, keyword) if total <= 0 or not isinstance(item_list, list): return success(package_result(0, [])) return success( package_result( total, [MaintainService.to_address_dict(item) for item in item_list]))
def wechat_offline(): user_key = RedisClient.get_user_key(g.user_id) record_key = redis_cache_client.get(user_key) if record_key is None: return success({ 'status': 0, 'msg': "logout failed! reason: user device is already offline" }) charging = redis_cache_client.get(record_key) if charging is None: return success({ 'status': 0, 'msg': "logout failed! reason: user device is already offline" }) return WindowsService.do_offline(charging)
def get_device_game_list(device_id, page=0, size=0): # 如果page 和 size为0 则返回全部游戏数据 if page <= 0 or size <= 0: item_list = DeviceGame.query.filter( DeviceGame.device_id == device_id).all() return success( package_result(len(item_list), [item.to_full_dict() for item in item_list])) query = DeviceGame.query # 再通过用户名查找 pagination = query.filter(DeviceGame.device_id == device_id).paginate( page=page, per_page=size, error_out=False) if pagination is None or pagination.total <= 0: return success(package_result(0, [])) # return pagination.total, pagination.items return success( package_result(pagination.total, [item.to_full_dict() for item in pagination.items]))
def get_device_by_id(device_id): device = None while True: try: # 先通过设备mac地址查找 device = DeviceService.get_device_by_code(device_id) if device is not None: break a_id = int(device_id) device = DeviceService.get_device_by_id(a_id) except Exception as e: log.error("设备信息无法转换为 int 类型: device_id = {}".format(device_id)) log.exception(e) break if device is None: return success(None) # 获取设备最新存活状态 device.alive = DeviceService.get_device_alive_status(device.device_code) return success(device.to_dict())
def keep_alive(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") record_key = request.json.get('token') if record_key is None: return fail(HTTP_OK, u"not have token!!!") device_code = request.json.get('device_code') if device_code is None: log.error( "无法保持心跳, 没有传入device_code: record_key = {}".format(record_key)) return fail(HTTP_OK, u"not have device_code!!!") # 保持心跳 DeviceService.keep_device_heart(device_code) charging = redis_cache_client.get(record_key) if charging is None: return success({ "status": 0, "msg": "keepalive failed!reason:token invalid" }) # 获得keep_alive_key 更新最新存活时间 user_online_key = RedisClient.get_user_online_key(record_key) # 设置最新存活时间 最多存在五分钟 redis_cache_client.setex(user_online_key, settings.MAX_LOST_HEART_TIME, int(time.time())) return success({ "status": 1, "msg": "keepalive success", "data": WindowsService.get_current_time_charging(charging) })
def request_code(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") mobile = request.json.get('mobile', None) if mobile is None: return fail(HTTP_OK, u'手机号不能为空') if not isinstance(mobile, basestring): log.warn("当前手机号: mobile = {}".format(mobile)) log.warn("当前手机号的类型: mobile type = {}".format(type(mobile))) return fail(HTTP_OK, u'手机号不合法') mobile = mobile.strip() if len(mobile) != 11: log.warn("当前手机号: mobile = {}".format(mobile)) return fail(HTTP_OK, u'手机号不合法') if sms_client.mobile_reach_rate_limit(mobile): return fail(HTTP_OK, u'验证码已发送') gt = GeetestLib(settings.PC_GEETEST_ID, settings.PC_GEETEST_KEY) challenge = request.json.get(gt.FN_CHALLENGE) validate = request.json.get(gt.FN_VALIDATE) seccode = request.json.get(gt.FN_SECCODE) status = session.get(gt.GT_STATUS_SESSION_KEY) if status: result = gt.success_validate(challenge, validate, seccode, g.openid) else: result = gt.failback_validate(challenge, validate, seccode) log.info("滑动验证码参数: mobile = {} {} {}".format(mobile, gt.FN_CHALLENGE, challenge)) log.info("滑动验证码参数: mobile = {} {} {}".format(mobile, gt.FN_VALIDATE, validate)) log.info("滑动验证码参数: mobile = {} {} {}".format(mobile, gt.FN_SECCODE, seccode)) log.info("当前滑动验证码破解情况: result = {}".format(result)) if result <= 0: log.info("当期手机号滑动验证码破解失败,不进行短信验证码请求: {}".format(mobile)) return fail(HTTP_OK, u'滑动验证码识别失败,无法请求短信验证码!') # 通过手机号请求验证码 if sms_client.request_sms(mobile): return success() return fail(HTTP_OK, u"发送验证码请求失败!")