# coding=utf-8 import datetime import logging import json import random import time from mongoengine import DoesNotExist from django.views.decorators.http import require_POST from apilib.utils_json import JsonResponse from apps.web.api.ft_north.constant import RESPONSE_CODE from apps.web.api.ft_north.models import FengTuTechnologyNorther from apps.web.api.ft_north.utils import get_coding_info , get_device_status, generate_json_token,parse_json_token logger = logging.getLogger(__name__) @require_POST def getToken(request): """ 通过账号密码获取token """ logger.debug("[queryToken] request body = {}".format(request.body)) if not request.body: return JsonResponse({"success": False,"code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1001)"}) try: data = json.loads(request.body).get("data") except Exception as e: logger.exception(e) return JsonResponse({"success": False,"code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1002)"}) appid = data.get("appid") appSecret = data.get("appSecret") logger.debug("[queryToken] appid = {}, appSecret = {}".format(appid, appSecret)) if not all((appid, appSecret)): return JsonResponse({"success": False,"code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1003)"}) try: norther = FengTuTechnologyNorther.objects.filter(appId=appid, appSecret=appSecret).first() # type: FengTuTechnologyNorther except DoesNotExist: return JsonResponse({"success": False,"code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1004)"}) except Exception as e: return JsonResponse({"success": False,"code": RESPONSE_CODE.SYS_ERROR, "message": u"系统错误"}) expire = int(time.time() + 60 * 60 * 24) * 1000 token = generate_json_token(data=norther.get_token_data(), expire=expire) resultData = { "token": token, "expiresTime": expire } return JsonResponse({ "success": True, "code": 200, "message": "请求成功", "data": resultData, }) def getOrder(request): """ 查询订单数量 :param request: :return: """ # 验证身份 token = request.META.get('HTTP_AUTHORIZATION', "").replace("Bearer", "").strip() logger.info('[getOrder] , token = {}'.format(token)) # 验证身份 tokenData = parse_json_token(token) if not tokenData: return JsonResponse({"success":False, "code": RESPONSE_CODE.ERROR_TOKEN, "message": u"请求参数错误(1001)"}) try: data = json.loads(request.body).get("data") except Exception as e: logger.exception(e) return JsonResponse({"success":False, "code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1004)"}) # 验证参数 logger.debug("[queryStationsInfo] request body = {}".format(request.body)) if not request.body: return JsonResponse({"success":False, "code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1003)"}) # 查询参数 deviceNo = str(data.get('deviceNo')) startTime = data.get('startTime',"2015-01-01 00:00:00") endTime = data.get("endTime",datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) result = { "orderNum": random.randint(10, 200) } resultData = json.dumps(result) return JsonResponse({ "success": True, "message": u"请求成功", "code": u"200", "data": resultData, }) def getCoding(request): """ 查询端口状态 :param request: :return: """ # 验证身份 token = request.META.get('HTTP_AUTHORIZATION', "").replace("Bearer", "").strip() logger.info('[getOrder] , token = {}'.format(token)) # 验证身份 tokenData = parse_json_token(token) if not tokenData: return JsonResponse({"success": False, "code": RESPONSE_CODE.ERROR_TOKEN, "message": u"请求参数错误(1001)"}) try: data = json.loads(request.body).get("data") except Exception as e: logger.exception(e) return JsonResponse({"success": False, "code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1004)"}) # 验证参数 logger.debug("[queryStationsInfo] request body = {}".format(request.body)) if not request.body: return JsonResponse({"success": False, "code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1003)"}) devNo = str(data.get('deviceNo')) codingInfo = get_coding_info(devNo) if not codingInfo: return JsonResponse({ "success": False, "message": u"设备未启动", "code": u"303" }) resultData = json.dumps(codingInfo) return JsonResponse({ "success": True, "message": u"请求成功", "code": u"200", "data": resultData, }) def getDeviceInfo(request): """ 查询设备状态 :param request: :return: """ # 验证身份 token = request.META.get('HTTP_AUTHORIZATION', "").replace("Bearer", "").strip() logger.info('[getOrder] , token = {}'.format(token)) # 验证身份 tokenData = parse_json_token(token) if not tokenData: return JsonResponse({"success": False, "code": RESPONSE_CODE.ERROR_TOKEN, "message": u"请求参数错误(1001)"}) try: data = json.loads(request.body).get("data") except Exception as e: logger.exception(e) return JsonResponse({"success": False, "code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1004)"}) # 验证参数 logger.debug("[queryStationsInfo] request body = {}".format(request.body)) if not request.body: return JsonResponse({"success": False, "code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1003)"}) devNo = str(data.get('deviceNo')) deviceStatus = get_device_status(devNo) result = { "deviceStatus": deviceStatus } resultData = json.dumps(result) return JsonResponse({ "success": True, "message": u"请求成功", "code": u"200", "data": resultData, })