def get(self, *args, **kwargs): """ get请求,通过工厂id获取工厂的信息json :param args: :param kwargs: /factory_id :return: 成功返回200和设备信息的json,未找到设备信息返回status_24 参数错误返回status_22 """ # api日志记录 SpiderApi.request(kwargs['c_user_id'], self.request.remote_ip, self.request.method, self.request.uri) factory_id = kwargs['parameter'] if factory_id: factory_obj = FactoryDB() result = factory_obj.find(factory_id) factory_obj.close() if result: SpiderApi.response(200) self.write_json(result) else: SpiderApi.response(errors.status_24) raise HTTPError(**errors.status_24) else: SpiderApi.response(errors.status_22) raise HTTPError(**errors.status_22)
def post(self, *args, **kwargs): """ post请求,通过用户名和密码获取token,rf_token以及用户的角色 token的有效期为两小时,rf_token的有效期为30小时 :param args: username: 用户名 password: 密码 :param kwargs: :return: 成功返回token,rf_token,role的json以及201,密码验证错误返回status_23 没有该用户返回status_26,缺少参数返回status_22 """ # 请求数据检查 content_type = self.request.headers.get('Content-Type') if content_type != 'application/json': raise HTTPError(**errors.status_34) try: request_data = json.loads(self.request.body) except: raise HTTPError(**errors.status_35) username = request_data.get("username") password = request_data.get("password") # api日志记录 SpiderApi.request(username, self.request.remote_ip, self.request.method, self.request.uri) if username and password: user = UserDB().find(username=username) if user: encrypt_password = Encryption.generate_password( password, parser.auth_salt) if encrypt_password == user["password"]: token = genTokenSeq(user["_id"], user["user_role"], 7200) rf_token = genRfTokenSeq(user["_id"], user["user_role"], 108000) SpiderApi.response(201) # 获取工厂名称 factory_obj = FactoryDB() factory_name = factory_obj.find( id=user["factory_id"])['name'] self.write_json( { 'token': token, 'rf_token': rf_token, 'role': user["user_role"], 'factory_id': user["factory_id"], "factory_name": factory_name, "username": user['username'], "user_id": user["_id"] }, 201) else: SpiderApi.response(errors.status_23) raise HTTPError(**errors.status_23) else: SpiderApi.response(errors.status_26) raise HTTPError(**errors.status_26) else: SpiderApi.response(errors.status_22) raise HTTPError(**errors.status_22)