diff --git a/README.md b/README.md index 0476eab..c4586cc 100644 --- a/README.md +++ b/README.md @@ -29,4 +29,6 @@ pip insatll mysqlclient 用户评论 +用户更新个人信息 + 未完待续 \ No newline at end of file diff --git "a/Server01/static/img/avatar/10-\351\207\221\346\231\272\347\247\200.jpg" "b/Server01/static/img/avatar/10-\351\207\221\346\231\272\347\247\200.jpg" new file mode 100644 index 0000000..81c2c29 Binary files /dev/null and "b/Server01/static/img/avatar/10-\351\207\221\346\231\272\347\247\200.jpg" differ diff --git a/Server01/static/img/post/30-avatar.jpg b/Server01/static/img/post/30-avatar.jpg new file mode 100644 index 0000000..eb44714 Binary files /dev/null and b/Server01/static/img/post/30-avatar.jpg differ diff --git "a/Server01/static/img/post/33-\345\274\240\345\250\234\350\213\261.jpg" "b/Server01/static/img/post/33-\345\274\240\345\250\234\350\213\261.jpg" new file mode 100644 index 0000000..8ebbfc2 Binary files /dev/null and "b/Server01/static/img/post/33-\345\274\240\345\250\234\350\213\261.jpg" differ diff --git "a/Server01/static/img/post/33-\346\241\245\346\234\254\347\216\257\345\245\210.jpg" "b/Server01/static/img/post/33-\346\241\245\346\234\254\347\216\257\345\245\210.jpg" new file mode 100644 index 0000000..d881476 Binary files /dev/null and "b/Server01/static/img/post/33-\346\241\245\346\234\254\347\216\257\345\245\210.jpg" differ diff --git a/Server01/util/__pycache__/verifyJWT.cpython-39.pyc b/Server01/util/__pycache__/verifyJWT.cpython-39.pyc index 80ab71c..9c71a9e 100644 Binary files a/Server01/util/__pycache__/verifyJWT.cpython-39.pyc and b/Server01/util/__pycache__/verifyJWT.cpython-39.pyc differ diff --git a/Server01/util/verifyJWT.py b/Server01/util/verifyJWT.py index 30908aa..931f895 100644 --- a/Server01/util/verifyJWT.py +++ b/Server01/util/verifyJWT.py @@ -15,8 +15,7 @@ def wrapper(request, *args, **kwargs): token = request.headers.get('Authorization').split(' ')[1] verify_payload = jwt.decode(token, SECRET_KEY, ['HS256'], verify=True) # 检查数据库是否存在该用户 - if models.User.objects.filter(username=verify_payload.get('username'), - id=verify_payload.get('user_id')).exists(): + if models.User.objects.filter(id=verify_payload.get('user_id')).exists(): return view_func(request, verify_payload, *args, **kwargs) return JsonResponse({'error': '未授权访问'}, status=401) except exceptions.ExpiredSignatureError: @@ -45,7 +44,7 @@ def create_token(user): payload = { 'user_id': user.id, 'username': user.username, - 'exp': datetime.datetime.utcnow() + datetime.timedelta(minutes=30) # 设置过期时间 + 'exp': datetime.datetime.utcnow() + datetime.timedelta(minutes=60) # 设置过期时间 } result = jwt.encode(payload=payload, key=SECRET_KEY, algorithm='HS256', headers=headers) return result diff --git a/Server01/views/__pycache__/post.cpython-39.pyc b/Server01/views/__pycache__/post.cpython-39.pyc index 0ea910e..14b1b92 100644 Binary files a/Server01/views/__pycache__/post.cpython-39.pyc and b/Server01/views/__pycache__/post.cpython-39.pyc differ diff --git a/Server01/views/__pycache__/user.cpython-39.pyc b/Server01/views/__pycache__/user.cpython-39.pyc index 7ecd431..debfb7a 100644 Binary files a/Server01/views/__pycache__/user.cpython-39.pyc and b/Server01/views/__pycache__/user.cpython-39.pyc differ diff --git a/Server01/views/post.py b/Server01/views/post.py index 62c85c7..4a937ec 100644 --- a/Server01/views/post.py +++ b/Server01/views/post.py @@ -1,15 +1,15 @@ import json from django.http import JsonResponse -from webServer.settings import TIME_ZONE, SYSTEM_PATH + import Server01.models as models -from Server01.util.verifyJWT import authenticate_request from Server01.util.auxiliaryFuction import convert_to_timezone, combine_index_post +from Server01.util.verifyJWT import authenticate_request +from webServer.settings import TIME_ZONE, SYSTEM_PATH - - -def upload_post(request): +@authenticate_request +def upload_post(request, payload): file = request.FILES['file'] id = request.POST.get('id') file_path = SYSTEM_PATH + '/webServer/Server01/static/img/post/' + str(id) + '-' + file.name @@ -89,4 +89,3 @@ def query_post_index(request): return JsonResponse({'info': list(combine_index_post(posts))}, status=200) return JsonResponse({'info': []}, status=200) - diff --git a/Server01/views/user.py b/Server01/views/user.py index 257e929..5703ca6 100644 --- a/Server01/views/user.py +++ b/Server01/views/user.py @@ -3,8 +3,9 @@ from django.http import JsonResponse import Server01.models as models -from Server01.util.auxiliaryFuction import check_email, combine_index_post +from Server01.util.auxiliaryFuction import check_email, combine_index_post, check_and_delete from Server01.util.verifyJWT import create_token, authenticate_request +from webServer.settings import SYSTEM_PATH # 用户登录 @@ -102,3 +103,33 @@ def unfollow(request, payload): user.following.remove(unfollow_user) return JsonResponse({'info': '成功取消关注'}, status=200) return JsonResponse({'error': '非法的操作'}, status=401) + + +@authenticate_request +def update_user_info(request, payload): + data = json.loads(request.body) + user_id = payload['user_id'] + user = models.User.objects.filter(id=user_id).first() + user.username = data['username'] + user.signature = data['signature'] + user.save() + return JsonResponse({'info': '修改成功'}, status=200) + + +@authenticate_request +def update_avatar(request, payload): + file = request.FILES['file'] + id = payload['user_id'] + file_path = SYSTEM_PATH + '/webServer/Server01/static/img/avatar/' + str(id) + '-' + file.name + check_and_delete(id, SYSTEM_PATH + '/webServer/Server01/static/img/avatar/') + with open(file_path, 'wb') as destination: + for chunk in file.chunks(): + destination.write(chunk) + result = { + 'filename': file.name, + 'filepath': 'http://localhost:8000/static/img/avatar/' + str(id) + '-' + file.name, + } + user = models.User.objects.filter(id=id).first() + user.avatar = 'http://localhost:8000/static/img/avatar/' + str(id) + '-' + file.name + user.save() + return JsonResponse({'info': result}, status=200) diff --git a/webServer/__pycache__/settings.cpython-39.pyc b/webServer/__pycache__/settings.cpython-39.pyc index 2e5ec24..a1ccf67 100644 Binary files a/webServer/__pycache__/settings.cpython-39.pyc and b/webServer/__pycache__/settings.cpython-39.pyc differ diff --git a/webServer/__pycache__/urls.cpython-39.pyc b/webServer/__pycache__/urls.cpython-39.pyc index 3025d33..2b67977 100644 Binary files a/webServer/__pycache__/urls.cpython-39.pyc and b/webServer/__pycache__/urls.cpython-39.pyc differ diff --git a/webServer/urls.py b/webServer/urls.py index 8819ce1..9c413fa 100644 --- a/webServer/urls.py +++ b/webServer/urls.py @@ -26,6 +26,8 @@ path('focus/', user.focusOn), path('user/focus/', user.get_user_focus), path('user/unfollow/', user.unfollow), + path('user/update/', user.update_user_info), + path('user/avatar/', user.update_avatar), # 帖子相关 path('upload/', post.upload_post), path('upload/info/', post.upload_post_info),