master
BINGWU 11 months ago
parent bedff6d160
commit 7875121b51

@ -0,0 +1,5 @@
def handleEmpty(count, msg):
if count > 0:
return {"code": 2000, "msg": msg + "成功"}
else:
return {"code": 1000, "msg": msg + "失败,数据不存在"}

@ -0,0 +1,9 @@
def handleErrorMethod(reqMethod, method):
if reqMethod == method:
return [True]
else:
return [False, {
'msg': '请求方式应为:' + method,
'code': 1000
}]

@ -0,0 +1,7 @@
import jwt
def decode_jwt_token(encoded_jwt):
# 关闭过期时间检验
de_code = jwt.decode(encoded_jwt, "JWT_SECRET_KEY", algorithms=['HS256'])
return de_code

@ -0,0 +1,14 @@
def get_jwt_token(user_name, role_data='default'):
"""
生成jwt-token
:param unit_name:
:param role_data:
:return:
"""
payload = {
'exp': datetime.utcnow() + timedelta(seconds=3600), # 单位秒
'iat': datetime.utcnow(),
'data': {'username': user_name, 'role_data': role_data}
}
encoded_jwt = jwt.encode(payload, "JWT_SECRET_KEY", algorithm='HS256')
return str(encoded_jwt, encoding='utf8')

@ -1,17 +0,0 @@
from pymongo import MongoClient
from bson.objectid import ObjectId
def test():
# 连接到 MongoDB 数据库
client = MongoClient('mongodb://localhost:27017/')
db = client['employee-information-management-system']
collection = db['user_model']
# 获取文档的 _id 字段的值
document_id = '660195d50d5cb377410134f7' # 替换为你要查找的文档的 _id
document = collection.find_one({'_id': ObjectId(document_id)})
if document:
document_id = document['_id']
print(document)
else:
print("Document not found.")

@ -1,44 +1,75 @@
from django.shortcuts import render, HttpResponse
from serve.models.user import UserModel
from django.http import JsonResponse
import json
from django.urls import path
from serve.utils.connectdb import connectdb
from bson.objectid import ObjectId
from serve.middleware.handleErrorMethod import handleErrorMethod
from serve.middleware.handleEmpty import handleEmpty
collection = connectdb('user_model')
def createUser(request):
print(request.method)
data = json.loads(request.body)
print(data.get('name'))
UserModel.objects.create(username='154782456', password='123456', subjects=[
{
'subjectName': "语文",
'subjectScore': 90
}]
)
return HttpResponse("插入成功")
result = handleErrorMethod(request.method, 'POST')
if result[0]:
data = json.loads(request.body)
print(data)
data1 = {
'username': 'Alice',
'password': '123',
'subjects': [
{
'subjectName': '1234',
'subjectScore': 90
}
]
}
collection.insert_one(data1)
return JsonResponse({"code": 2000, "msg": "添加成功"})
else:
return JsonResponse(result[1])
def deleteUser(request):
result = UserModel.objects.filter(id="ly").first().delete()
print(result)
return HttpResponse('删除成功')
result = handleErrorMethod(request.method, 'DELETE')
if result[0]:
_ids = ['660188e85f5269231b716281']
filter_criteria = {'_id': {'$in': [ObjectId(_id) for _id in _ids]}}
deleteResult = collection.delete_many(filter_criteria)
res = handleEmpty(deleteResult.deleted_count, '删除')
return JsonResponse(res)
else:
return JsonResponse(result[1])
def updateUser(request):
result = UserModel.objects.filter(id='1').first().update(username="ly")
return HttpResponse(result)
result = handleErrorMethod(request.method, 'PUT')
if result[0]:
_id = '6601b0749157ae8a26e2af7b'
update_data = {
'username': 'juuuujjj',
'password': 'hhhhuuuuh3'
}
updateResult = collection.update_one({'_id': ObjectId(_id)}, {"$set": update_data})
res = handleEmpty(updateResult.modified_count, '更新')
return JsonResponse(res)
else:
return JsonResponse(result[1])
def getUser(request):
# user = UserModel.objects.get(id='660195d50d5cb377410134f7')
user = collection.find_one({'_id': ObjectId('660195d50d5cb377410134f7')})
user['_id'] = str(user['_id'])
# 返回 JSON 响应
return JsonResponse(user)
result = handleErrorMethod(request.method, 'GET')
if result[0]:
_id = '660195d50d5cb377419134f7' # 替换为你要查找的文档的 _id
user = collection.find_one({'_id': ObjectId(_id)})
if user:
user['_id'] = str(user['_id'])
return JsonResponse(user)
else:
return JsonResponse({"code": 1000, "msg": "数据不存在"})
else:
return JsonResponse(result[1])
user_url = [

Loading…
Cancel
Save