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 online( cls, openid, # 上线时间 self.ctime ctime, # 地址 结构体对象 address, # 余额 分 balance, # 计费 价格 分钟 / 分 charge_mode): access_token = redis_cache_client.get(WECHAT_ACCESS_TOKEN_KEY) if access_token is None: log.error("access_token 为None,刷新token进程异常!!!") return online_time = ctime.strftime('%Y-%m-%d %H:%M:%S') address_str = address.get_full_address() account_url = url_for("wechat.menu", name="account", _external=True) log.info("当前用户中心地址: url = {}".format(account_url)) if WechatTemplate.online(access_token, openid, online_time, address_str, balance, charge_mode, url=account_url): log.info("发送微信上机通知成功: openid = {}".format(openid)) else: log.warn("发送微信上机通知失败: openid = {}".format(openid))
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 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 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 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 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 get_device_deploy_list(device_id, page, size): # 获取数据总数目 total = 0 result_list = list() # 根据设备ID过滤 query = Deploy.query.filter(Deploy.device_id == device_id) # 获取部署信息列表 item_paginate = query.paginate(page=page, per_page=size, error_out=False) if item_paginate is None: log.warn("获取部署信息翻页查询失败: device_id = {} page = {} size = {}".format( device_id, page, size)) return package_result(total, result_list) item_list = item_paginate.items if item_list is None: log.warn("部署信息分页查询失败: device_id = {} page = {} size = {}".format( device_id, page, size)) return package_result(total, result_list) return package_result(item_paginate.total, [item.to_dict() for item in item_list])
def create(user_id, amount, transaction_id, pay_time): amount = int(amount) user_id = int(user_id) recharge = Recharge(user_id=user_id, amount=amount, transaction_id=transaction_id, pay_time=pay_time) # 账户总额增加 user = User.get(user_id) if user is None: log.warn("当前充值用户信息不存在: user_id = {}".format(user_id)) return None, False try: user.balance_account += amount user.total_account += amount user.utime = datetime.now() db.session.add(user) db.session.add(recharge) db.session.commit() # 发送充值成功通知 TemplateService.recharge_remind(user.openid, pay_time, amount) except IntegrityError: log.error("主键重复: user_id = {} amount = {}".format(user_id, amount)) db.session.rollback() return None, False except Exception as e: log.error("未知插入错误: user_id = {} amount = {}".format( user_id, amount)) log.exception(e) return None, False return recharge, True
def create(username, password, name, address_id=Maintain.ALL_ADDRESS_ID): # 如果地址信息不正确,则选用所有地址可用 if Address.get(address_id) is None: log.warn("当前传入地址ID没有找到相应地址信息,默认调整为全部地区: address_id = {}".format( address_id)) address_id = Maintain.ALL_ADDRESS_ID maintain = Maintain(username=username, name=name, address_id=address_id) maintain.password = password try: db.session.add(maintain) db.session.commit() except IntegrityError: log.error("主键重复: username = {} name = {} address_id = {}".format( username, name, address_id)) db.session.rollback() return None, False except Exception as e: log.error("未知插入错误: username = {} name = {} address_id = {}".format( username, name, address_id)) log.exception(e) return None, False return maintain, True
def find_list_by_keyword(page, size, keyword): query = Maintain.query # 先通过ID查找 try: maintain_id = int(keyword) pagination = query.filter(Maintain.id == maintain_id).paginate( page=page, per_page=size, error_out=False) if pagination is not None and pagination.total > 0: return pagination.total, pagination.items except Exception as e: log.warn("通过ID查找失败: keyword = {}".format(keyword)) log.exception(e) # 再通过用户名查找 pagination = query.filter(Maintain.username == keyword).paginate( page=page, per_page=size, error_out=False) if pagination is not None and pagination.total > 0: return pagination.total, pagination.items # 再通过姓名查找 pagination = query.filter(Maintain.name == keyword).paginate( page=page, per_page=size, error_out=False) if pagination is not None and pagination.total > 0: return pagination.total, pagination.items return 0, []
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_wechat_user_info(openid): # 默认设置是未关注状态 subscribe, nick_name, head_img_url = 0, '', '' if openid is None: log.error("openid 为None,未知异常!!!") return subscribe, nick_name, head_img_url access_token = redis_cache_client.get(WECHAT_ACCESS_TOKEN_KEY) if access_token is None: log.error("access_token 为None,刷新token进程异常!!!") return subscribe, nick_name, head_img_url url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token={}&openid={}&lang=zh_CN'.format( access_token, openid) try: resp = requests.get(url, verify=False, timeout=30) if resp.status_code != 200: log.error("获取用户信息访问状态码不正确: status_code = {} url = {}".format( resp.status_code, url)) return subscribe, nick_name, head_img_url log.info("当前获取的用户信息为: text = {}".format(resp.text)) json_data = json.loads(resp.text) errcode = json_data.get('errcode') if errcode is not None: log.error("获取用户信息错误码不正常: {}".format(resp.text)) return subscribe, nick_name, head_img_url subscribe = json_data.get('subscribe') if subscribe is None: log.error("获取用户信息关注状态不正常: {}".format(resp.text)) return 0, nick_name, head_img_url # 如果用户关注了 才去获取昵称和头像信息 if subscribe == 1: # 获得用户昵称 和头像信息 nick_name = json_data.get('nickname', '') head_img_url = json_data.get('headimgurl', '') if isinstance(nick_name, basestring): nick_name = nick_name.strip() else: nick_name = '' if isinstance(head_img_url, basestring): head_img_url = head_img_url.strip() else: head_img_url = '' log.info( "当前用户关注了公众号, 能够获取昵称和头像: openid = {} nick_name = {} head_img_url = {}" .format(openid, nick_name, head_img_url)) else: log.warn( "当前用户并没有关注公众号,无法获取用户信息: openid = {} subscribe = {}".format( openid, subscribe)) except Exception as e: log.error("访问微信用户链接失败: url = {}".format(url)) log.exception(e) return subscribe, nick_name, head_img_url
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 recharge_remind(cls, openid, pay_time, account): access_token = redis_cache_client.get(WECHAT_ACCESS_TOKEN_KEY) if access_token is None: log.error("access_token 为None,刷新token进程异常!!!") return recharge_time = pay_time.strftime('%Y-%m-%d %H:%M:%S') if WechatTemplate.recharge_remind(access_token, openid, recharge_time, account): log.info("发送微信充值通知成功: openid = {}".format(openid)) else: log.warn("发送微信充值通知失败: openid = {}".format(openid))
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 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_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 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_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 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 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 verify_password(username, password): result = u'登录成功!' maintain = MaintainService.get_maintain_by_username(username) if maintain is None: result = u'账户不存在!' log.error("当前维护人员ID没有找到相关信息: username = {}".format(username)) return False, result is_success = maintain.verify_password(password) if is_success: log.info("当前维护人员登录成功: username = {} password = {}".format( username, password)) else: result = u'密码错误!' log.warn("当前维护人员密码错误: username = {} password = {}".format( username, password)) return is_success, result
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 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 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 get_dynamic_proxy(host): try: r = requests.get('http://{host}:{port}/proxy/{h}'.format( h=host, host=REMOTE_PROXY_CONF['host'], port=REMOTE_PROXY_CONF['port']), timeout=10) if r is None or r.status_code != 200 or 'failed' in r.text or 'False' in r.text: log.warn("动态代理服务异常, 重试...") return None proxies = 'http://{host}'.format(host=r.text) log.info('鲲鹏 ip = {}'.format(proxies)) return proxies except Exception as e: log.error("动态代理访问异常:") log.exception(e) return None
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 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!" })