service_state.py 4.0 KB
# coding=utf-8
#author:        4N
#createtime:    2021/9/14
#email:         nheweijun@sina.com

import requests
import configure
from app.util.component.UserCheck import UserCheck
from app.util.component.ApiTemplate import ApiTemplate
from .models import Service,db
from .util.ServiceType import ServiceType

class Api(ApiTemplate):
    api_name = "修改服务状态"
    def process(self):
        res = {}
        try:
            guid = self.para.get("guid")
            state = int(self.para.get("state"))
            s_type = self.para.get("type")

            if s_type in [ServiceType.tile_service.value,ServiceType.map_service.value]:
                service = Service.query.filter_by(guid=guid).one_or_none()
                # 验证权限
                UserCheck.verify(service.creator)

                if service:
                    service.state = state


                    operate = "startService" if state == 1 else "stopService"
                    server_type = "tileserver" if s_type == "电子地图" else "mapserver"
                    dmap_engine_url = "{}/dmap/api/manager/{}?servicename={}&servername={}".format(configure.dmap_engine,
                                                                                                   operate,service.name,
                                                                                                   server_type)
                    state_result = requests.get(dmap_engine_url)
                    if state_result.status_code == 200:
                        if state_result.json().get("status") == "false":
                            raise Exception("修改服务状态失败!")
                    else:
                        raise Exception("修改服务状态失败!")
                    db.session.commit()
                    res["result"] = True
                else:
                    raise Exception("服务不存在!")

            elif s_type == ServiceType.image_service.value:
                user_req: requests.Response = requests.post("{}/API/Service/Info".format(self.para.get("url")),
                                                            data={"guid": self.para.get("guid")})

                if user_req.status_code == 200:
                    if not user_req.json().get("result"):
                        raise Exception("服务不存在!")
                else:
                    raise Exception("影像服务器连接失败!")
                # 验证权限
                UserCheck.verify(user_req.json().get("data").get("service").get("creator"))

                url = "{}/API/Service/State".format(self.para.get("url"))
                response: requests.Response = requests.post(url, data=self.para)
                if response.status_code == 200:
                    if not response.json().get("result"):
                        raise Exception("由于{},影像地图修改失败!".format(response.json().get("msg")))
                else:
                    raise Exception("影像服务器连接失败!")
                res["result"] = True

        except Exception as e:
            raise e
        return res


    api_doc = {
        "tags": ["服务接口"],
        "parameters": [
            {"name": "guid",
             "in": "formData",
             "type": "string",
             "description": "guid"},
            {"name": "type",
             "in": "formData",
             "type": "string",
             "enum":[st.value for st in ServiceType],
             "description": "type"},
            {"name": "url",
             "in": "formData",
             "type": "string",
             "description": "url"},
            {"name": "state",
             "in": "formData",
             "type": "int",
             "description": "state","enum":[0,1]},
        ],
        "responses": {
            200: {
                "schema": {
                    "properties": {
                    }
                }
            }
        }
    }