|
@@ -57,6 +57,28 @@ def format_useinfo(user):
|
|
|
return res
|
|
|
|
|
|
|
|
|
+def format_uselist(list):
|
|
|
+ json_user = json.loads(
|
|
|
+ serialize('json', list, ensure_ascii=False)
|
|
|
+ )
|
|
|
+ res = []
|
|
|
+ for item in json_user:
|
|
|
+ item['fields'].pop('value')
|
|
|
+ item['fields'].pop('token')
|
|
|
+ user = item.get('fields')
|
|
|
+ user['id'] = item.get('pk')
|
|
|
+ # res['roles'] = [
|
|
|
+ # {
|
|
|
+ # 'roleName': res.get('roleName'), 'value': res.get('value')
|
|
|
+ # }
|
|
|
+ # ]
|
|
|
+ # res.pop('roleName')
|
|
|
+ # res.pop('value')
|
|
|
+ # res.pop('password')
|
|
|
+ res.append(user)
|
|
|
+ return res
|
|
|
+
|
|
|
+
|
|
|
def format_login_result(user):
|
|
|
format_user = json.loads(
|
|
|
serialize('json', user, ensure_ascii=False)
|
|
@@ -64,6 +86,7 @@ def format_login_result(user):
|
|
|
for item in format_user:
|
|
|
res = {}
|
|
|
res['userId'] = item.get('pk')
|
|
|
+ res['menus'] = item.get('menus')
|
|
|
res['token'] = item.get('fields').get('token')
|
|
|
res['roles'] = [
|
|
|
{
|
|
@@ -73,31 +96,36 @@ def format_login_result(user):
|
|
|
return res
|
|
|
|
|
|
|
|
|
-def format_menus(data):
|
|
|
- # 一级菜单列表(无父id)
|
|
|
+def format_menus(querySetObj, mode):
|
|
|
+ # querySet 转json对象
|
|
|
+ data = json.loads(serialize('json', querySetObj, ensure_ascii=False))
|
|
|
+ # 定义一级菜单列表(无父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):
|
|
|
+ # mode True -- 否将某些字段合并处理成前台路由需要的数据结构;
|
|
|
+ if mode:
|
|
|
+ item['fields']['meta'] = {
|
|
|
+ "title": item['fields'].pop('title'),
|
|
|
+ "affix": item['fields'].pop('affix'),
|
|
|
+ "icon": 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)
|
|
|
+
|
|
|
+ else: # False -- 不必转成树结构, menuName 语言包需要到前台转中文
|
|
|
+ item['fields']["menuName"] = item['fields'].pop("title")
|
|
|
+ item['fields'].pop('affix')
|
|
|
menus.append(item.get('fields'))
|
|
|
- else:
|
|
|
- childrens.append(item.get('fields'))
|
|
|
|
|
|
- for children in childrens:
|
|
|
- # 将列表转化成树形菜单
|
|
|
- getTree(children, menus, childrens)
|
|
|
return menus
|
|
|
|
|
|
|
|
@@ -126,6 +154,7 @@ def getTree(children, menus, childrens):
|
|
|
getTree(children, m.get('children'), childrens)
|
|
|
|
|
|
|
|
|
+# 请求---------------------------------------------------------------------
|
|
|
class login(View):
|
|
|
|
|
|
def post(self, request):
|
|
@@ -133,11 +162,7 @@ class login(View):
|
|
|
user = User.objects.all().filter(username=data.get(
|
|
|
'username'), password=data.get('password'))
|
|
|
if user:
|
|
|
- print('-------user---------')
|
|
|
- print(user)
|
|
|
res = format_login_result(user)
|
|
|
- print('---------------res--------')
|
|
|
- print(res)
|
|
|
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})
|
|
@@ -155,6 +180,16 @@ class getUserInfoById(View):
|
|
|
return JsonResponse({'code': 1, 'result': {'message': 'error'}}, json_dumps_params={"ensure_ascii": False})
|
|
|
|
|
|
|
|
|
+class getUserList(View):
|
|
|
+ def get(self, request):
|
|
|
+ users = User.objects.all()
|
|
|
+ if users:
|
|
|
+ res = format_uselist(users)
|
|
|
+ return JsonResponse({'code': 0, 'result': res, }, json_dumps_params={"ensure_ascii": False})
|
|
|
+ else:
|
|
|
+ return JsonResponse({'code': 1, 'result': {'message': 'no role,create role'}}, json_dumps_params={"ensure_ascii": False})
|
|
|
+
|
|
|
+
|
|
|
class add_user(View):
|
|
|
def post(self, request):
|
|
|
data = request.POST
|
|
@@ -168,13 +203,17 @@ class add_user(View):
|
|
|
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)
|
|
|
+ menus = format_menus(querySetObj, True)
|
|
|
return JsonResponse({'code': 0, 'result': menus})
|
|
|
|
|
|
|
|
|
+class getAllMenuList(View):
|
|
|
+ def get(self, request):
|
|
|
+ querySetObj = Menu.objects.all()
|
|
|
+ menus = format_menus(querySetObj, False)
|
|
|
+ return JsonResponse({'code': 0, 'result': {'format': True, 'menus': menus}})
|
|
|
+
|
|
|
+
|
|
|
class add_menu(View):
|
|
|
def post(self, request):
|
|
|
data = request.POST
|