def get(self, *args, **kwargs): """ get请求,通过用户id获取该用户的详细信息的json :param args: :param kwargs: /user_id :return: 成功返回用户详细信息的json,未找到该用户返回status_24 参数错误返回status_22 """ # api日志记录 SpiderApi.request(kwargs['c_user_id'], self.request.remote_ip, self.request.method, self.request.uri) user_id = kwargs['user_id'] if user_id: user_obj = UserDB() result = user_obj.find(user_id) user_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请求,获取用户列表 :param args: page: 第几页 per_page: 每页显示页数 :param kwargs: :return: 成功返回201和用户列表,未找到数据返回status_24 参数错误返回status_22 """ # api日志记录 SpiderApi.request(kwargs['c_user_id'], self.request.remote_ip, self.request.method, self.request.uri) request_data = kwargs['request_data'] page = request_data.get('page') per_page = request_data.get('per_page') if page and per_page: limit = int(per_page) offset = (int(page) - 1) * limit user_obj = UserDB() result = user_obj.findall(offset, limit) user_obj.close() if result: SpiderApi.response(201) self.write_json(result, 201) else: SpiderApi.response(errors.status_24) raise HTTPError(**errors.status_24) else: SpiderApi.response(errors.status_22) raise HTTPError(**errors.status_22)
def delete(self, *args, **kwargs): """ delete请求,通过用户id,直接删除用户 :param args: :param kwargs: /user_id :return: 成功返回204,未找到返回status_24 参数错误返回status_22 """ # api日志记录 SpiderApi.request(kwargs['c_user_id'], self.request.remote_ip, self.request.method, self.request.uri) user_id = kwargs['user_id'] if user_id: user_obj = UserDB() result = user_obj.remove(id=user_id) user_obj.close() if result: SpiderApi.response(204) self.write_json(None, 204) 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): request_data = kwargs['request_data'] name = request_data.get('username') password = request_data.get('password') if name and password: factory_id = kwargs['c_factory_id'] role = 1 user_obj = UserDB() # 用户名唯一性检测 uniqueness = user_obj.find(username=name) if uniqueness: raise HTTPError(**errors.status_33) result = user_obj.insert(username=name, user_role=role, password=password, factory_id=factory_id) if result: token = genTokenSeq(result, role, 7200) rf_token = genRfTokenSeq(result, role, 108000) self.write_json( { 'token': token, 'rf_token': rf_token, 'role': role }, 201) else: raise HTTPError(**errors.status_31) else: 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)
def put(self, *args, **kwargs): """ put请求,通过工厂id,用户id,和修改的信息,更新用户列表 工厂id默让不让修改 :param args: name: 用户名 role: 角色 password: 密码 :param kwargs: /user_id :return: 成功返回200和修改的用户的id,未找到对象返回status_24 参数错误返回status_22 """ # api日志记录 SpiderApi.request(kwargs['c_user_id'], self.request.remote_ip, self.request.method, self.request.uri) user_id = kwargs['user_id'] request_data = kwargs['request_data'] name = request_data.get('name') role = request_data.get('role') password = request_data.get('password') if user_id and name and role and password: user_obj = UserDB() result = user_obj.update(user_id, username=name, password=password, user_role=role) user_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 put(self, *args, **kwargs): """ put请求,用户通过该接口直接修改基本信息 通过解析传递过来的token中的用户id和修改的用户id做对比,相同才能够然他修改 该接口只能修改用户名和密码 :param args: name: 用户名 password: 密码 :param kwargs: /user_id :return: 成功返回用户id和200,未找到对象返回status_24 参数错误返回status_22, 不是修改自己的信息返回status_29 """ # api日志记录 SpiderApi.request(kwargs['c_user_id'], self.request.remote_ip, self.request.method, self.request.uri) user_id = kwargs['user_id'] if kwargs['c_user_id'] == user_id: request_data = kwargs['request_data'] name = request_data.get('name') password = request_data.get('password') if user_id and name and password: user_obj = UserDB() old_user_name = user_obj.find(id=user_id)['username'] if old_user_name != name: # 用户名唯一性检测 uniqueness = user_obj.find(username=name) if uniqueness: SpiderApi.response(errors.status_33) raise HTTPError(**errors.status_33) result = user_obj.update(user_id, username=name, password=password) user_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) else: SpiderApi.response(errors.status_29) raise HTTPError(**errors.status_29)
def post(self, *args, **kwargs): """ post请求,单机版添加用户,并绑定用户所属公司 :param args: name: 工厂名称 role: 角色 password: 密码 factory_id: 所属工厂id :param kwargs: :return: 成功返回201和用户的id,未能成功插入返回status_24 参数错误返回status_22 """ # api日志记录 SpiderApi.request(kwargs['c_user_id'], self.request.remote_ip, self.request.method, self.request.uri) request_data = kwargs['request_data'] name = request_data.get('name') role = request_data.get('role') password = request_data.get('password') factory_id = request_data.get('factory_id') if name and role and password and factory_id: user_obj = UserDB() # 用户名唯一性检测 uniqueness = user_obj.find(username=name) if uniqueness: raise HTTPError(**errors.status_33) result = user_obj.insert(username=name, user_role=int(role), password=password, factory_id=factory_id) user_obj.close() if result: SpiderApi.response(201) self.write_json(result, 201) else: SpiderApi.response(errors.status_24) raise HTTPError(**errors.status_24) else: SpiderApi.response(errors.status_22) raise HTTPError(**errors.status_22)