def create_app_auth(): data = request.data try: req_data = json.loads(data) except ValueError: return jsonify( ErrResponse(0, CodeConstant.code_null_param, AuthStrings.missing_param).__dict__) if not req_data \ or not AuthConstant.token_app_key in req_data \ or not AuthConstant.token_app_secret in req_data: return jsonify( ErrResponse(0, CodeConstant.code_null_param, AuthStrings.missing_param).__dict__) app_key = req_data[AuthConstant.token_app_key] app_secret = req_data[AuthConstant.token_app_secret] if app_key != Config.APP_KEY or app_secret != Config.APP_SECRET: return jsonify( ErrResponse(0, CodeConstant.code_err_param, AuthStrings.app_key_or_secret_error).__dict__) token = Auth.encode_app_token(app_key, app_secret) refresh_token = token auth_response = AuthResponse(token, refresh_token).__dict__ response = JKResponse(1, auth_response).__dict__ return jsonify(response)
def remove_account(): user_id = ReqAuthCommon.is_auth_user_ok(request) if user_id == -1: abort(401) req_data = ReqAccountCommon.is_post_param_ok(request) if not req_data: return jsonify( ErrResponse(0, CodeConstant.code_null_param, AuthStrings.missing_param).__dict__) account = Account.objects( account_id=req_data[AccountConstant.account_id]).first() user = User.objects(user_id=req_data[AccountConstant.user_id]).first() if not account or not user: return jsonify( ErrResponse(0, CodeConstant.code_already_exist, UserStrings.user_not_exist).__dict__) user.accounts.remove(account) user.update(accounts=user.accounts, update_time=time.mktime(datetime.now().timetuple())) account.delete() json_data = json_util.dumps({ 'code': 1, 'data': {} }, ensure_ascii=False, sort_keys=True, indent=4) return Response(json_data, mimetype='application/json')
def register(): if not ReqAuthCommon.is_auth_ok(request): abort(401) req_data = ReqUserCommon.is_post_param_ok(request) if not req_data: return jsonify(ErrResponse(0, CodeConstant.code_null_param, AuthStrings.missing_param).__dict__) if User.objects(username=req_data[UserConstant.username]): return jsonify(ErrResponse(0, CodeConstant.code_already_exist, UserStrings.user_is_exist).__dict__) count = User.objects().all().count() + 1 user = User(username=req_data[UserConstant.username], name=req_data[UserConstant.username], password=UserUtils.get_pwd(req_data[UserConstant.password]), user_id=count ).save() user.update(set__user_id=count) out_user = User.objects.only('user_id', 'username', 'create_time', 'update_time').get(user_id=count) token = Auth.encode_auth_token(user.user_id, time.mktime(datetime.now().timetuple())) data = out_user.to_mongo() data['token'] = token json_data = json_util.dumps({'code': 1, 'data': data}, ensure_ascii=False, sort_keys=True, indent=4) return Response(json_data, mimetype='application/json')
def update_account(): if (ReqAuthCommon.is_auth_ok(request) == False): abort(401) req_data = ReqDeviceCommon.is_patch_param_ok(request) if (req_data == False): return jsonify( ErrResponse(0, CodeConstant.code_null_param, AuthStrings.missing_param).__dict__) device_id = req_data[DeviceConstant.device_id] device = Device.objects(device_id=device_id).first() if not device: return jsonify( ErrResponse(0, CodeConstant.code_no_data, DeviceStrings.device_not_exist).__dict__) device_name = device.device_name if DeviceConstant.device_name in req_data: device_name = req_data[DeviceConstant.device_name] device_type = device.device_type if DeviceConstant.device_type in req_data: device_type = req_data[DeviceConstant.device_type] online = device.online if DeviceConstant.online in req_data: online = req_data[DeviceConstant.online] last_online = device.last_online if DeviceConstant.last_online in req_data: last_online = req_data[DeviceConstant.last_online] user_id = device.user_id if DeviceConstant.user_id in req_data: user_id = req_data[DeviceConstant.user_id] location = device.location if DeviceConstant.location in req_data: location = req_data[DeviceConstant.location] device.update(device_name=device_name,\ device_type=device_type,\ online=online,\ last_online=last_online,\ user_id=user_id,\ update_time=time.mktime(datetime.now().timetuple())) device = Device.objects(device_id=device_id).first() response = JKResponse(1, device).__dict__ return jsonify(response)
def login(): if not ReqAuthCommon.is_auth_ok(request): abort(401) req_data = ReqUserCommon.is_post_param_ok(request) if not req_data: return jsonify( ErrResponse(0, CodeConstant.code_null_param, AuthStrings.missing_param).__dict__) user = User.objects(username=req_data[UserConstant.username]).first() if not user: return jsonify( ErrResponse(0, CodeConstant.code_not_exist, UserStrings.user_not_exist).__dict__) if UserUtils.get_pwd(req_data[UserConstant.password]) != user.password: return jsonify( ErrResponse(0, CodeConstant.code_err_data, UserStrings.password_is_error).__dict__) account_list = [] for i in range(len(user.accounts)): if isinstance(user.accounts[i], Account): account_list.append({ 'account_id': user.accounts[i].account_id, 'username': user.accounts[i].username, 'password': user.accounts[i].password, 'user_id': user.accounts[i].user_id, 'sessionid': user.accounts[i].sessionid, 'phone_area': user.accounts[i].phone_area }) token = Auth.encode_auth_token(user.user_id, time.mktime(datetime.now().timetuple())) out_user = User.objects.only('user_id', 'username', 'create_time', 'update_time').get(user_id=user.user_id) data = out_user.to_mongo() data['token'] = token data['accounts_list'] = account_list json_data = json_util.dumps({ 'code': 1, 'data': data }, ensure_ascii=False, sort_keys=True, indent=4) return Response(json_data, mimetype='application/json')
def modify_password(): if not ReqAuthCommon.is_auth_ok(request): abort(401) req_data = ReqDeviceCommon.is_post_param_ok(request) if not req_data: return jsonify( ErrResponse(0, CodeConstant.code_null_param, AuthStrings.missing_param).__dict__) # if Device.objects(mac_addr=req_data[DeviceConstant.mac_addr]): # return jsonify(ErrResponse(0, CodeConstant.code_already_exist, DeviceStrings.device_is_exist).__dict__) # # count = Device.objects().all().count()+1 # device = Device(mac_addr=req_data[DeviceConstant.mac_addr], device_id=0).save(); # device.update(set__device_id=count) # # device_name = None # if DeviceConstant.device_name in req_data: # device_name = req_data[DeviceConstant.device_name] # # device_type = None # if DeviceConstant.device_type in req_data: # device_type = req_data[DeviceConstant.device_type] # # device.update(device_name=device_name, device_type=device_type, online=False) # response = JKResponse(1, Device.objects().first()).__dict__ return jsonify(response)
def create_account(): user_id = ReqAuthCommon.is_auth_user_ok(request) if user_id <= 0: abort(401) req_data = ReqUserCommon.is_post_param_ok(request) if not req_data: return jsonify( ErrResponse(0, CodeConstant.code_null_param, AuthStrings.missing_param).__dict__) if Account.objects(username=req_data[UserConstant.username]): return jsonify( ErrResponse(0, CodeConstant.code_already_exist, UserStrings.account_exist).__dict__) username = req_data[UserConstant.username] password = UserUtils.get_pwd(req_data[UserConstant.password]) phone_area = UserUtils.get_phone_area_by_name(username) wky = WKYInterface() account = wky.login(username, password, phone_area) if account.user_id is None or account.sessionid is None: return jsonify( ErrResponse(0, CodeConstant.code_err_bind, UserStrings.bind_error).__dict__) count = Account.objects().all().count() + 1 account.phone_area = phone_area account.username = username account.password = password account.account_id = 0 account.save() account.update(set__account_id=count) print 'user_id:' + str(user_id) user = User.objects(user_id=user_id).first() user.accounts.append(account) user.update(accounts=user.accounts, update_time=time.mktime(datetime.now().timetuple())) response = JKResponse(1, account).__dict__ return jsonify(response)
def get_account_by_id(device_id): if (ReqAuthCommon.is_auth_ok(request) == False): abort(401) devices = Device.objects(device_id=device_id).all() if not devices: return jsonify( ErrResponse(0, CodeConstant.code_no_data, DeviceStrings.device_not_exist).__dict__) response = JKResponse(1, devices).__dict__ return jsonify(response)
def draw_ltk(): user_id = ReqAuthCommon.is_auth_user_ok(request) if user_id == -1: abort(401) req_data = ReqDeviceCommon.is_post_param_with_wkb_ok(request) if not req_data: return jsonify(ErrResponse(0, CodeConstant.code_null_param, AuthStrings.missing_param).__dict__) account = Account.objects(account_id=req_data[DeviceConstant.account_id]).first() user = User.objects(user_id=req_data[DeviceConstant.user_id]).first() if not account or not user: return jsonify(ErrResponse(0, CodeConstant.code_already_exist, UserStrings.user_not_exist).__dict__) json_draw_wkb = getWKYIncome(account, req_data[DeviceConstant.draw_wkb]) if 'iRet' not in json_draw_wkb: return jsonify(ErrResponse(0, CodeConstant.code_err_data, DeviceStrings.device_draw_failed).__dict__) if json_draw_wkb['iRet'] != 0: return jsonify(ErrResponse(0, CodeConstant.code_err_data, json_draw_wkb['sMsg']).__dict__) json_data = json_util.dumps({'code': 1, 'data': {}}, ensure_ascii=False, sort_keys=True, indent=4) return Response(json_data, mimetype='application/json')
def check_and_update_account(account_id): wky = WKYInterface() account = Account.objects(account_id=account_id).first() if account is None: return month_income = wky.getMonthIncome('0', account.sessionid, account.user_id) json_month_income = json.loads(month_income) if 'iRet' in json_month_income and json_month_income['iRet'] == 0: return wky_account = wky.login(account.username, account.password, account.phone_area) if wky_account.user_id is None or wky_account.sessionid is None: return jsonify(ErrResponse(0, CodeConstant.code_err_bind, UserStrings.bind_error).__dict__) account.update(sessionid=wky_account.sessionid, update_time=time.mktime(datetime.now().timetuple()))
def get_device_list_by_user_id(): user_id = ReqAuthCommon.is_auth_user_ok(request) if user_id == -1: abort(401) req_data = ReqDeviceCommon.is_post_param_ok(request) if not req_data: return jsonify( ErrResponse(0, CodeConstant.code_null_param, Auth.missing_param).__dict__) user = User.objects(user_id=req_data[DeviceConstant.user_id]).first() if not user: return jsonify( ErrResponse(0, CodeConstant.code_not_exist, UserStrings.user_not_exist).__dict__) device_list = [] online_device_num = 0 offline_device_num = 0 total_income = 0 if len(user.accounts) == 0: json_data = json_util.dumps({ 'code': 1, 'data': {} }, ensure_ascii=False, sort_keys=True, indent=4) return Response(json_data, mimetype='application/json') for i in range(len(user.accounts)): if isinstance(user.accounts[i], Account): if user.accounts[i].device_info is None: getWKYDeviceInfo(user.accounts[i]) else: time_device_info = time.localtime( user.accounts[i].device_info.update_time) time_now = time.localtime( time.mktime(datetime.now().timetuple())) if user.accounts[i].device_info is None \ or time_now.tm_year > time_device_info.tm_year \ or time_now.tm_mon > time_device_info.tm_mon \ or time_now.tm_mday > time_device_info.tm_mday: getWKYDeviceInfo(user.accounts[i]) total_income += float(user.accounts[i].device_info.last_day_income) if user.accounts[i].device_info.status == "online": online_device_num += 1 else: offline_device_num += 1 device_list.append({ 'account_id': user.accounts[i].account_id, 'username': user.accounts[i].username, 'status': user.accounts[i].device_info.status, 'bind_time': user.accounts[i].device_info.bind_time, 'device_sn': user.accounts[i].device_info.device_sn, 'device_name': user.accounts[i].device_info.device_name, 'disconnect_time': user.accounts[i].device_info.disconnect_time, 'ip': user.accounts[i].device_info.ip, 'system_version': user.accounts[i].device_info.system_version, 'lan_ip': user.accounts[i].device_info.lan_ip, 'peerid': user.accounts[i].device_info.peerid, 'connect_time': user.accounts[i].device_info.connect_time, 'usb_capacity': user.accounts[i].device_info.usb_capacity, 'usb_used': user.accounts[i].device_info.usb_used, 'totalIncome': user.accounts[i].device_info.total_income, 'last_day_income': user.accounts[i].device_info.last_day_income, 'extract_coin': user.accounts[i].device_info.extract_coin, 'update_time': time.strftime("%Y-%m-%d %H:%M:%S", time_device_info) }) out_data = { 'device_list': device_list, 'online_num': online_device_num, 'offline_num': offline_device_num, 'total_income': round(total_income, 3), } json_data = json_util.dumps({ 'code': 1, 'data': out_data }, ensure_ascii=False, sort_keys=True, indent=4) return Response(json_data, mimetype='application/json')
def get_device_7days_income_list_by_user_id(): user_id = ReqAuthCommon.is_auth_user_ok(request) if user_id == -1: abort(401) req_data = ReqDeviceCommon.is_post_param_ok(request) if not req_data: return jsonify(ErrResponse(0, CodeConstant.code_null_param, Auth.missing_param).__dict__) user = User.objects(user_id=req_data[DeviceConstant.user_id]).first() if not user: return jsonify(ErrResponse(0, CodeConstant.code_not_exist, UserStrings.user_not_exist).__dict__) income7Days = ['0', '0', '0', '0', '0', '0', '0'] data7Days = ['', '', '', '', '', '', ''] total_income = 0.0 device_num = 0 current_month_income = 0.0 last_income = 0.0 total_outcome = 0.0 if len(user.accounts) == 0: json_data = json_util.dumps({'code': 1, 'data': {}}, ensure_ascii=False, sort_keys=True, indent=4) return Response(json_data, mimetype='application/json') for i in range(len(user.accounts)): if isinstance(user.accounts[i], Account): if user.accounts[i].income is None: getWKYIncome(user.accounts[i]) else: time_income = time.localtime(user.accounts[i].income.update_time) time_now = time.localtime(time.mktime(datetime.now().timetuple())) if user.accounts[i].income is None \ or time_now.tm_year > time_income.tm_year \ or time_now.tm_mon > time_income.tm_mon \ or time_now.tm_mday > time_income.tm_mday: getWKYIncome(user.accounts[i]) total_income += float(user.accounts[i].income.total_income) total_outcome += float(user.accounts[i].income.total_outcome) last_income += float(user.accounts[i].income.last_income) current_month_income += float(user.accounts[i].income.month_income) device_num += 1 for k in range(7): data7Days[k] = user.accounts[i].income.date_list[k] income7Days[k] = str(round(float(income7Days[k]) + float(user.accounts[i].income.income_list[k]), 4)) max_num = float(income7Days[0]) min_num = float(income7Days[0]) for k in range(len(income7Days)): if max_num < float(income7Days[k]): max_num = float(income7Days[k]) if min_num > float(income7Days[k]): min_num = float(income7Days[k]) if device_num == 0: device_num = 1 out_data = { 'last_income': str(round(last_income, 4)), 'last_average': str(round(last_income / device_num, 4)), 'current_month_income': str(round(current_month_income, 4)), 'total_income': str(round(total_income, 4)), 'total_outcome': str(round(total_outcome, 4)), 'remainder_coin': str(round(total_income - total_outcome, 4)), 'seven_days_date': data7Days, 'seven_days_income': income7Days, 'chart_min': min_num - 0.1, 'chart_max': max_num + 0.1 } json_data = json_util.dumps({'code': 1, 'data': out_data}, ensure_ascii=False, sort_keys=True, indent=4) return Response(json_data, mimetype='application/json')