def update(): ''' 更新菜单或权限 POST方法根据id返回数据 PUT方法更新菜单或权限 :return: ''' res_dir = request.get_json() if res_dir is None: return NO_PARAMETER() if request.method == "POST": menu_id = res_dir.get("id") if id: menu = Menu.query.get(menu_id) if menu: menu_data = model_to_dict(menu) return SUCCESS(menu_data) else: return ID_NOT_FOUND() else: PARAMETER_ERR() if request.method == "PUT": menu_id = res_dir.get("id") menu_name = res_dir.get("menu_name") if id and menu_name: model = Menu.query.get(menu_id) if model: token = request.headers["Authorization"] user = verify_token(token) model.parent_id = res_dir.get("parent_id") model.menu_name = res_dir.get("menu_name") model.menu_type = res_dir.get("menu_type") model.icon = res_dir.get("icon") model.is_frame = res_dir.get("is_frame") model.url = res_dir.get("url") model.route_name = res_dir.get("route_name") model.route_path = res_dir.get("route_path") model.route_component = res_dir.get("route_component") model.route_cache = res_dir.get("route_cache") model.perms = res_dir.get("perms") model.visible = res_dir.get("visible") model.order_num = res_dir.get("order_num") model.remark = res_dir.get("remark") model.update_by = user['name'] try: model.update() return SUCCESS() except Exception as e: print(f"更新菜单失败:{e}") return UPDATE_ERROR() else: return ID_NOT_FOUND() else: return PARAMETER_ERR()
def delete(): ''' 根据id删除菜单或权限 :return: ''' res_dir = request.get_json() if res_dir is None: return NO_PARAMETER() menu_id = res_dir.get("id") if menu_id: try: parent = Menu.query.filter_by(parent_id=menu_id).all() if parent: return DELETE_ERROR(msg="该菜单下存在子菜单,无法删除!") role = Role_Menu.query.filter_by(menu_id=menu_id).all() if role: return DELETE_ERROR(msg="该菜单已与角色关联,无法删除!") menu = Menu.query.get(menu_id) if menu: menu.delete() return SUCCESS() else: return ID_NOT_FOUND() except Exception as e: print(f"删除菜单失败:{e}") return DELETE_ERROR() else: return PARAMETER_ERR()
def create(): ''' 新建菜单或权限 :return: ''' res_dir = request.get_json() if res_dir is None: return NO_PARAMETER() # 根据token获取用户id try: token = request.headers["Authorization"] user = verify_token(token) menu = Menu() menu.parent_id = res_dir.get("parent_id"), menu.menu_name = res_dir.get("menu_name"), menu.menu_type = res_dir.get("menu_type"), menu.icon = res_dir.get("icon"), menu.is_frame = res_dir.get("is_frame"), menu.url = res_dir.get("url"), menu.route_name = res_dir.get("route_name"), menu.route_path = res_dir.get("route_path"), menu.route_component = res_dir.get("route_component"), menu.route_cache = res_dir.get("route_cache"), menu.perms = res_dir.get("perms"), menu.visible = res_dir.get("visible"), menu.order_num = res_dir.get("order_num"), menu.remark = res_dir.get("remark"), menu.create_by = user['name'], menu.save() return SUCCESS() except Exception as e: print(f"新建菜单失败:{e}") return CREATE_ERROR()
def check_token(): # 在请求头上拿到token token = request.headers["Authorization"] user = verify_token(token) if user: key = f"token_{user.get('name')}" redis_token = Redis.read(key) if redis_token == token: return SUCCESS(data=user.get('id')) else: return OTHER_LOGIN() else: return AUTH_ERR()
def logout(): ''' 注销方法:redis删除token :return: ''' try: token = request.headers["Authorization"] user = verify_token(token) if user: key = f"token_{user.get('name')}" redis_token = Redis.read(key) if redis_token: Redis.delete(key) return SUCCESS() else: return AUTH_ERR() except Exception as e: app.logger.error(f"注销失败") return REQUEST_ERROR()
def search(): data = request.get_data() data = str(data, 'utf-8') data = json.loads(data) if not data: return NO_PARAMETER() res = [] stus = T_students.query.filter_by(sname=data['content']).all() for stu in stus: dic = {} dic['sname'] = stu.sname dic['sno'] = stu.sno dic['ssex'] = stu.ssex dic['sclass'] = stu.sclass prize_dics = [] prize_list = T_prize.query.filter_by(sname=stu.sname).all() for prize in prize_list: prize_dic = {} prize_dic['name'] = prize.name prize_dic['p_time'] = prize.p_time prize_dic['level'] = prize.level prize_dic['award'] = prize.award prize_dic['img'] = prize.img prize_dics.append(prize_dic) dic['prize'] = prize_dics innovation_dics = [] innovation_list = T_innovation.query.filter_by(sname=stu.sname).all() for innovation in innovation_list: innovation_dic = {} innovation_dic['name'] = innovation.name innovation_dic['level'] = innovation.level innovation_dic['category'] = innovation.category innovation_dics.append(innovation_dic) dic['innovation'] = innovation_dics research_dics = [] research_list = T_research.query.filter_by(sname=stu.sname).all() for research in research_list: research_dic = {} research_dic['name'] = research.name research_dic['head'] = research.head research_dic['company'] = research.company research_dics.append(research_dic) dic['research'] = research_dics thesis_dics = [] thesis_list = T_thesis.query.filter_by(sname=stu.sname).all() for thesis in thesis_list: thesis_dic = {} thesis_dic['name'] = thesis.name thesis_dic['periodical'] = thesis.periodical thesis_dic['time'] = thesis.time thesis_dic['inclusion'] = thesis.inclusion thesis_dics.append(thesis_dic) dic['thesis'] = thesis_dics patent_dics = [] patent_list = T_patent.query.filter_by(sname=stu.sname).all() for patent in patent_list: patent_dic = {} patent_dic['name'] = patent.name patent_dic['category'] = patent.category patent_dic['num'] = patent.num patent_dic['time'] = patent.time patent_dic['f_inventor'] = patent.f_inventor patent_dics.append(patent_dic) dic['patent'] = patent_dics res.append(dic) return SUCCESS(data=res)
def test(): return SUCCESS()