|
@@ -0,0 +1,161 @@
|
|
|
+from django.shortcuts import render, redirect, HttpResponse
|
|
|
+from django.http import JsonResponse, HttpResponseRedirect
|
|
|
+from django.views.generic import View
|
|
|
+# from . import models
|
|
|
+from .models import User, Menu
|
|
|
+from django.core.serializers import serialize
|
|
|
+import json
|
|
|
+
|
|
|
+
|
|
|
+# Create your views here.
|
|
|
+
|
|
|
+def index(request):
|
|
|
+ data = {
|
|
|
+ 'name': {
|
|
|
+ 'firstname': 'Tom',
|
|
|
+ 'lastname': 'john'
|
|
|
+ },
|
|
|
+ 'age': 18,
|
|
|
+ 'sex': '男'
|
|
|
+ }
|
|
|
+ return JsonResponse(data, json_dumps_params={"ensure_ascii": False})
|
|
|
+
|
|
|
+# class cbv(View):
|
|
|
+# def get(self,request):
|
|
|
+# print(request)
|
|
|
+# print(request.GET.get('username'))
|
|
|
+# return JsonResponse({'method':'get','data':request.GET})
|
|
|
+
|
|
|
+# def post(self,request):
|
|
|
+# return JsonResponse({'method':'post','data':request.POST})
|
|
|
+# def put(self,request):
|
|
|
+# return JsonResponse({'method':'put'})
|
|
|
+# def delete(self,request):
|
|
|
+# return JsonResponse({'method':'delete'})
|
|
|
+
|
|
|
+# class extend_cbv(cbv):
|
|
|
+# # 重写post请求
|
|
|
+# def post(self,request):
|
|
|
+# return JsonResponse({'method':'post','msg':'重写post请求'},json_dumps_params={"ensure_ascii":False})
|
|
|
+
|
|
|
+
|
|
|
+def format_useinfo(user):
|
|
|
+ format_user = json.loads(
|
|
|
+ serialize('json', user, ensure_ascii=False)
|
|
|
+ )
|
|
|
+ for item in format_user:
|
|
|
+ res = item.get('fields')
|
|
|
+ res['userId'] = item.get('pk')
|
|
|
+ res['roles'] = [
|
|
|
+ {
|
|
|
+ 'roleName': res.get('roleName'), 'value': res.get('value')
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ res.pop('roleName')
|
|
|
+ res.pop('value')
|
|
|
+ return res
|
|
|
+
|
|
|
+
|
|
|
+def format_menus(data):
|
|
|
+ # 一级菜单列表(无父id)
|
|
|
+ menus = []
|
|
|
+ # 子菜单列表
|
|
|
+ childrens = []
|
|
|
+ for item in data:
|
|
|
+ item['fields']['id'] = item.get('pk')
|
|
|
+ # 将某些字段合并处理成前台需要的路由结构
|
|
|
+ item['fields']['meta'] = {
|
|
|
+ "title": item.get('fields').get('title'),
|
|
|
+ "affix": item.get('fields').get('affix'),
|
|
|
+ "icon": item.get('fields').get('icon'),
|
|
|
+ }
|
|
|
+ # 删除多余字段
|
|
|
+ item['fields'].pop('title')
|
|
|
+ item['fields'].pop('affix')
|
|
|
+ item['fields'].pop('icon')
|
|
|
+ if (item.get('fields').get('parent') == None):
|
|
|
+ menus.append(item.get('fields'))
|
|
|
+ else:
|
|
|
+ childrens.append(item.get('fields'))
|
|
|
+
|
|
|
+ for children in childrens:
|
|
|
+ # 将列表转化成树形菜单
|
|
|
+ getTree(children, menus, childrens)
|
|
|
+ return menus
|
|
|
+
|
|
|
+
|
|
|
+"""
|
|
|
+def getTree(children, menus, childrens)
|
|
|
+根据传递过来的父菜单id,递归设置各层次父菜单的子菜单列表
|
|
|
+:children: 单个子菜单
|
|
|
+:param menus: 父菜单列表 一级菜单
|
|
|
+:childrens: 子菜单列表,第二次调用
|
|
|
+"""
|
|
|
+
|
|
|
+
|
|
|
+def getTree(children, menus, childrens):
|
|
|
+ # 遍历父菜单
|
|
|
+ for m in menus:
|
|
|
+ # 当前菜单的id 和 子菜单的父id 是否匹配
|
|
|
+ if m.get('id') == children.get('parent'):
|
|
|
+ if not m.get('children'):
|
|
|
+ m['children'] = []
|
|
|
+ if not children in m['children']: # 判单子菜单是否已存在
|
|
|
+ m['children'].append(children)
|
|
|
+
|
|
|
+ if m.get('children'):
|
|
|
+ for children in childrens:
|
|
|
+ # 判断子菜单是否还有匹配的子菜单
|
|
|
+ getTree(children, m.get('children'), childrens)
|
|
|
+
|
|
|
+
|
|
|
+class login(View):
|
|
|
+
|
|
|
+ def post(self, request):
|
|
|
+ data = json.loads(request.body)
|
|
|
+ user = User.objects.all().filter(username=data.get(
|
|
|
+ 'username'), password=data.get('password'))
|
|
|
+ if user:
|
|
|
+ res = format_useinfo(user)
|
|
|
+ return JsonResponse({'code': 0, 'msg': 'success', 'result': res}, json_dumps_params={"ensure_ascii": False})
|
|
|
+ else:
|
|
|
+ return JsonResponse({'code': 1, 'error': '用户名或密码不正确'}, json_dumps_params={"ensure_ascii": False})
|
|
|
+
|
|
|
+
|
|
|
+class getUserInfoById(View):
|
|
|
+ def get(self, request):
|
|
|
+ userid = request.GET.get('userId')
|
|
|
+ user = User.objects.all().filter(id=userid)
|
|
|
+ if user:
|
|
|
+ # res = {'codeList': ['1000', '3000', '5000']}
|
|
|
+ res = format_useinfo(user)
|
|
|
+ return JsonResponse({'code': 0, 'result': res, })
|
|
|
+ else:
|
|
|
+ return JsonResponse({'code': 1, 'result': {'message': 'error'}}, json_dumps_params={"ensure_ascii": False})
|
|
|
+
|
|
|
+
|
|
|
+class add_user(View):
|
|
|
+ def post(self, request):
|
|
|
+ data = request.POST
|
|
|
+ if User.objects.all().filter(username=data.get('username')):
|
|
|
+ return JsonResponse({'code': 0, 'error': '该角色名已被创建'})
|
|
|
+ else:
|
|
|
+ User.objects.create(**data)
|
|
|
+ return JsonResponse({'code': 1, 'data': '角色创建成功'})
|
|
|
+
|
|
|
+
|
|
|
+class getMenuListById(View):
|
|
|
+ def get(self, request):
|
|
|
+ querySetObj = Menu.objects.all()
|
|
|
+
|
|
|
+ # querySet 转json对象
|
|
|
+ data = json.loads(serialize('json', querySetObj, ensure_ascii=False))
|
|
|
+ menus = format_menus(data)
|
|
|
+ return JsonResponse({'code': 0, 'result': menus})
|
|
|
+
|
|
|
+
|
|
|
+class add_menu(View):
|
|
|
+ def post(self, request):
|
|
|
+ data = request.POST
|
|
|
+ Menu.objects.create(**data)
|
|
|
+ return JsonResponse({'code': 1, 'error': '菜单添加成功'})
|