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 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 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 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 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 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_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_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 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_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 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 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 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 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 decorator(*args, **kwargs): user_id_cookie = session.get('u_id') if user_id_cookie is None: log.warn("当前session中没有u_id 信息,需要登录...") return fail(HTTP_OK, u'当前用户没有登录', -1) user_id = decode_user_id(user_id_cookie) if user_id is None: log.warn( "当前用户信息被篡改,需要重新登录: user_id_cookie = {}".format(user_id_cookie)) return fail(HTTP_OK, u'当前用户登录信息被篡改, 不能登录', -1) g.user_id = int(user_id) log.info("当前访问用户ID为: user_id = {}".format(g.user_id)) return func(*args, **kwargs)
def get_device_game_state(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") device_code = request.json.get('device_code') if not isinstance(device_code, basestring): return fail(HTTP_OK, u"参数类型错误!") update_state = DeviceService.get_update_state(device_code) if update_state is None: log.error("当前设备号没有获取到任何设备信息: {}".format(device_code)) return fail(HTTP_OK, u'当前设备号信息不正确,无法获取更新状态信息') return success(update_state)
def search_maintain(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") keyword = request.json.get('keyword') if not isinstance(keyword, basestring) and not isinstance(keyword, int): return fail(HTTP_OK, u"参数错误!") page = request.json.get('page') size = request.json.get('size') if not isinstance(page, int) or not isinstance(size, int): return fail(HTTP_OK, u"参数错误!") return MaintainService.search_by_keyword(page, size, keyword)
def update_maintain(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") maintain_id = request.json.get('id') name = request.json.get('name') password = request.json.get('password') address_id = request.json.get('address_id') is_success = MaintainService.update_maintain(maintain_id, name, password, address_id) if not is_success: return fail(HTTP_OK, u"更新失败!") return success(u'更新维护人员信息成功!', id=maintain_id)
def state_maintain(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") state = request.json.get('state') if state not in Maintain.STATUS_VALUES: return fail(HTTP_OK, u"参数不正确!") maintain_id = request.json.get('id') if not isinstance(maintain_id, int): return fail(HTTP_OK, u"参数不正确!") if not MaintainService.state_maintain(maintain_id, state): return fail(HTTP_OK, u"维护人员使用状态设置失败!") return success(u"维护人员使用状态设置成功!", id=maintain_id)
def http_server_error(e): log.warn('{addr} request: {method}, ' 'url: {url}'.format(addr=get_remote_addr(), method=request.method, url=request.url)) log.warn("{}".format(request.headers)) log.exception(e) return fail(HTTP_SERVER_ERROR)
def get_game_md5(): 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') if not isinstance(game, basestring) or \ not isinstance(version, basestring): log.error("参数错误: game = {} version = {}".format(game, version)) return fail(HTTP_OK, u"参数错误") game_manage = GameVersionManageService.get_game_info(game, version) if game_manage is None: return fail(HTTP_OK, u'没有当前游戏版本记录') return success(game_manage.to_dict())
def http_bad_request(e): log.warn('{addr} request: {method}, ' 'url: {url}'.format(addr=get_remote_addr(), method=request.method, url=request.url)) log.warn("{}".format(request.headers)) log.exception(e) return fail(HTTP_BAD_REQUEST)
def http_not_found(e): log.warn('{addr} request: {method}, ' 'url: {url}'.format(addr=get_remote_addr(), method=request.method, url=request.url)) log.warn("{}".format(request.headers)) log.exception(e) return fail(HTTP_NOT_FOUND)
def add_role(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") name = request.json.get('name') if not isinstance(name, basestring): return fail(HTTP_OK, u"角色名称数据类型不正确!") if Role.get_by_name(name) is not None: return fail(HTTP_OK, u"当前角色名称已经存在!") role, is_success = Role.create(name) if not is_success: return fail(HTTP_OK, u"角色创建失败!") return success(role.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 modify_game_update_time(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") time_str = request.json.get('time') if time_str is None: log.error("修改时间参数错误: time = None") return fail(HTTP_OK, u"参数错误") # 判断时间格式是否正确 if not is_valid_time(time_str): log.error("时间格式错误: time = {}".format(time_str)) return fail(HTTP_OK, u"时间格式错误: %H:%M:%S") # 设置时间 DeviceGameService.set_game_update_time(time_str, redis_cache_client) return success(u"时间更新成功!")
def wechat_check(): openid = session.get('openid', None) # refresh_token = session.get('refresh_token', None) # 如果两个关键的token都存在 则正常进入下面的流程 if openid is None: log.warn("当前用户没有openid..") return fail(HTTP_OK, u"当前用户没有openid!", -1) user = get_current_user_by_openid(openid) if user is None: log.info("当前openid没有注册用户信息: {}".format(openid)) return fail(HTTP_OK, u"当前openid没有注册!", 0) if user.deleted: log.info("当前openid没有注册用户信息: {}".format(openid)) return fail(HTTP_OK, u"当前用户已经被删除!", -2) return success()
def delete_maintain(): 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 maintain_id in id_list: if not MaintainService.delete_maintain(maintain_id): log.warn("当前维护人员删除失败: maintain_id = {}".format(maintain_id)) continue result_list.append(maintain_id) return success(result_list)
def windows_offline(): if not request.is_json: log.warn("参数错误...") return fail(HTTP_OK, u"need application/json!!") token = request.json.get('token') if token is None: return fail(HTTP_OK, u"not have token!!!") charging = redis_cache_client.get(token) if charging is None: return success({ 'status': 0, 'msg': "logout failed! reason: user device is already offline" }) return WindowsService.do_offline(charging)
def recharge(account): if account <= 0: log.warn("充值金额不正确: account = {}".format(account)) return fail(HTTP_OK, u"充值金额数目不正确,需要正整数!") try: log.info("当前支付用户ID = {}".format(g.user_id)) pay_data = wx_pay.js_pay_api( openid=g.openid, # 付款用户openid body=u'Account Recharge', # 例如:饭卡充值100元 total_fee=account, # total_fee 单位是 分, 100 = 1元 attach=str(g.user_id), ) except WxPayError as e: log.error("支付失败: openid = {} user_id = {}".format(g.openid, g.user_id)) log.exception(e) return fail(HTTP_OK, e.message) log.info("当前支付结果: {}".format(json.dumps(pay_data, ensure_ascii=False))) return success(pay_data)