Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
final edition
  • Loading branch information
suzxzd committed Jun 26, 2024
1 parent 75e8675 commit 92391cf
Show file tree
Hide file tree
Showing 17 changed files with 355 additions and 46 deletions.
5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified main/__pycache__/mysql.cpython-310.pyc
Binary file not shown.
Binary file modified main/__pycache__/mysql.cpython-311.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion main/client/Client.py
Expand Up @@ -305,7 +305,7 @@ def reset_password(self, answer, new_password):

####################################################################
def main():
SendMessage("1234","1234")
SecurityQuestion(userName="2222",question='1',answer='1',new_password='1234')



Expand Down
2 changes: 1 addition & 1 deletion main/mysql.py
Expand Up @@ -217,7 +217,7 @@ def set_security_question(user_name, question, answer):
return False

@staticmethod
def verify_security_answer(user_name, answer, new_password):
def verify_security_answer(user_name,answer, new_password):
try:
user = UserModel.get(UserModel.name == user_name)
security_question = SecurityQuestionModel.get(SecurityQuestionModel.user_id == user.id)
Expand Down
17 changes: 13 additions & 4 deletions main/server/Server.py
Expand Up @@ -195,8 +195,10 @@ def on_message(self, client, userdata, msg):
success = SecurityQuestionManage.set_security_question(user_name, question, answer)
if success == True:
code = 1
print("密保保存成功")
else:
code = 0
print("密保保存失败")
data = {
'action': 'set_question',
'code': code
Expand All @@ -218,11 +220,17 @@ def on_message(self, client, userdata, msg):
elif msg.topic == 'verify_security_answer':
user_name = data.get('userName')
answer = data.get('answer')
newPassword = data.get('new_password')
newPassword = data.get('newPassword')
returnTopic = data.get('returnTopic')
success = SecurityQuestionManage.verify_security_answer(user_name, answer, newPassword)
if success: code = 1
else: code = 0

print(user_name,"密码"+newPassword)
success = SecurityQuestionManage.verify_security_answer(user_name,answer, newPassword)
if success:
code = 1
print("密保验证成功")
else:
code = 0
print("密保验证失败")
data = {
'action': 'verify_security_answer',
'code': code
Expand Down Expand Up @@ -297,5 +305,6 @@ def main():
c=Like() #进程3,处理点赞
d=ChatAll() #进程4,处理所有消息
e=ChatSave() #进程5,处理消息保存
f=SecurityServer()

main()
Binary file modified su/myproject/mqtt/__pycache__/forms.cpython-310.pyc
Binary file not shown.
Binary file modified su/myproject/mqtt/__pycache__/views.cpython-310.pyc
Binary file not shown.
15 changes: 14 additions & 1 deletion su/myproject/mqtt/client/Client.py
Expand Up @@ -159,6 +159,7 @@ def on_message(self, client, userdata, msg): # 接受数据
messages = eval(msg.payload.decode('utf-8'))
# 读取数据
self.messages=messages #返回一个字典的列表,['id':id,'senderID':senderID,'message':message]存到类中
print(messages)


def publishGetAllMessage(self):
Expand Down Expand Up @@ -241,6 +242,10 @@ def __init__(self, userName, question=None, answer=None, new_password=None):
self.question = question
self.answer = answer
self.clientStart()
self.rightQuestion=None
self.request_security_question()
time.sleep(1)
self.flag=False
if userName and question and answer and new_password:
self.reset_password(answer,new_password)
elif userName and question and answer:
Expand All @@ -262,9 +267,14 @@ def on_message(self, client, userdata, msg):
if data.get('action') == 'set_question':
print(data.get('code'))
elif data.get('action') == 'request_question':
self.rightQuestion=data.get('question')
print(f"Security Question: {data.get('question')}")
elif data.get('action') == 'verify_security_answer':
print(data.get('code'))
if data.get('code')==1:
self.flag=True
print(f"密保验证成功,密码重制成功")
else:
print(f"密保验证失败")
return data

def set_security_question(self):
Expand All @@ -290,6 +300,9 @@ def request_security_question(self):
print(f'发布信息到 request_security_question 成功')

def reset_password(self, answer, new_password):
if self.rightQuestion!=self.question:
print("密保问题错误")
return
returnTopic = self.userName + "security"
data = {
'action': 'reset_password',
Expand Down
Binary file modified su/myproject/mqtt/client/__pycache__/Client.cpython-310.pyc
Binary file not shown.
8 changes: 7 additions & 1 deletion su/myproject/mqtt/forms.py
Expand Up @@ -5,4 +5,10 @@
class UserForm(forms.Form):
username = forms.CharField(max_length=100)
password = forms.CharField(widget=forms.PasswordInput)
confirm_password = forms.CharField(widget=forms.PasswordInput, required=False)
confirm_password = forms.CharField(widget=forms.PasswordInput, required=False)

class ForgetPasswordForm(forms.Form):
username = forms.CharField(max_length=150, required=True, widget=forms.TextInput(attrs={'placeholder': '用户名'}))
question = forms.CharField(max_length=255, required=True, widget=forms.TextInput(attrs={'placeholder': '密保问题'}))
answer = forms.CharField(max_length=255, required=True, widget=forms.TextInput(attrs={'placeholder': '密保答案'}))
password = forms.CharField(max_length=128, required=True, widget=forms.PasswordInput(attrs={'placeholder': '要重置的密码'}))
40 changes: 39 additions & 1 deletion su/myproject/mqtt/views.py
Expand Up @@ -72,7 +72,7 @@ def register(request):

#chat
def chat(request):
username = u.userName # 从session中获取username
username = u.userName
if username:
messages.info(request, f"欢迎回来 {username} (●’◡’●)")
return render(request, 'chat.html')
Expand Down Expand Up @@ -105,4 +105,42 @@ def sendchat(request):
return JsonResponse({'status': 'success','message': '发送成功'})
return JsonResponse({'status': 'failed'})

def setquestion(request):
if request.method == 'POST':
data = json.loads(request.body)
question = data.get('question')
answer = data.get('answer')
userName=u.userName
print("u.userName:"+u.userName)
print(question)
print(answer)
SecurityQuestion(u.userName,question=question,answer=answer)
time.sleep(2)
return JsonResponse({'status': 'success','message': '密保设置成功'})
return JsonResponse({'status': 'failed'})

from .forms import ForgetPasswordForm

def forgetpassword(request):
if request.method == 'POST':
form = ForgetPasswordForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
question = form.cleaned_data['question']
answer = form.cleaned_data['answer']
password = form.cleaned_data['password']

s=SecurityQuestion(userName=username,question=question,answer=answer,new_password=password)
time.sleep(2)
if s.flag:
messages.error(request, '密码重置成功')
return redirect('login')
else:
messages.error(request, '用户名或密保错误或未设置密保,请重试')
return redirect('forgetpassword')


else:
form = ForgetPasswordForm()

return render(request, 'forgetpassword.html', {'form': form})
Binary file modified su/myproject/myproject/__pycache__/urls.cpython-310.pyc
Binary file not shown.
2 changes: 2 additions & 0 deletions su/myproject/myproject/urls.py
Expand Up @@ -24,5 +24,7 @@
path('register/', views.register, name='register'),
path('getchats/', views.getchats, name='getchats'),
path('sendchat/', views.sendchat, name='sendchat'),
path('setquestion/', views.setquestion, name='setquestion'),
path('forgetpassword/',views.forgetpassword,name='forgetpassword')

]
108 changes: 92 additions & 16 deletions su/myproject/templates/chat.html
Expand Up @@ -17,26 +17,46 @@
<el-container>
<el-main style="position: relative;">
<el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect">
<el-menu-item index="1">热门问答</el-menu-item>
<el-menu-item index="1">讨论1区</el-menu-item>
<el-submenu index="2">
<template slot="title">最新回答</template>
<el-menu-item index="2-1">诊疗前沿</el-menu-item>
<el-menu-item index="2-2">科普专区</el-menu-item>
<el-menu-item index="2-3">专家评价</el-menu-item>
<template slot="title">讨论2区</template>
</el-submenu>
<el-menu-item index="3">我的关注</el-menu-item>
<el-menu-item index="4">我的收藏</el-menu-item>
<el-menu-item index="3">讨论2区</el-menu-item>
<el-menu-item index="4">讨论3区</el-menu-item>
<el-button v-if="!hasMessageTags" type="primary" class="login-button" @click="login">登入
</el-button>
</el-menu>


<!-- 消息显示开始 -->
<div class="messages-container">
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li {% if message.tags %} class="{{ message.tags }}" {% endif %}>{{ message }}</li>
<li class="{{ message.tags }}">
{{ message }}
</li>
{% endfor %}
</ul>
<el-button @click="openSecurityDialog">设置密保</el-button>
{% endif %}
</div>

<el-dialog :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
<el-form :model="securityForm">
<el-form-item label="密保问题">
<el-input v-model="securityForm.question"></el-input>
</el-form-item>
<el-form-item label="密保答案">
<el-input v-model="securityForm.answer"></el-input>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="setSecurityQuestion">确 定</el-button>
</span>
</el-dialog>

<!-- 消息显示结束 -->
<div class="line"></div>
<el-menu :default-active="activeIndex2" class="el-menu-demo" mode="horizontal" @select="handleSelect"
Expand Down Expand Up @@ -96,10 +116,18 @@ <h2 style="margin-bottom: 20px">聊天 {{ commentCount }}</h2>
activeIndex2: '1',
commentCount: 0,
commentContent: '',
commentList: []
commentList: [],
hasMessageTags: false,
dialogVisible: false,
securityForm: {
question: '',
answer: ''
}
},
created() {
this.fetchComments();
// 定时刷新评论区,每5秒刷新一次
setInterval(this.fetchComments, 1000);
},
methods: {
handleSelect(key, keyPath) {
Expand All @@ -124,7 +152,6 @@ <h2 style="margin-bottom: 20px">聊天 {{ commentCount }}</h2>
'X-CSRFToken': this.getCookie('csrftoken')
},
body: JSON.stringify({

message: this.commentContent,
})
})
Expand All @@ -136,8 +163,8 @@ <h2 style="margin-bottom: 20px">聊天 {{ commentCount }}</h2>
});
this.fetchComments();
this.commentContent = '';
}
else if (data.status === 'failed' && data.message) {

} else if (data.status === 'failed' && data.message) {
this.$alert(data.message, '提示', {
confirmButtonText: '确定',
});
Expand All @@ -162,9 +189,45 @@ <h2 style="margin-bottom: 20px">聊天 {{ commentCount }}</h2>
}
return cookieValue;
},
replyToComment(commentId) {
// 回复评论的处理逻辑,例如显示二级评论框
console.log('Reply to comment:', commentId);
openSecurityDialog() {
this.dialogVisible = true;
},
handleClose() {
this.dialogVisible = false;
},
setSecurityQuestion() {
fetch('/setquestion/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': this.getCookie('csrftoken')
},
body: JSON.stringify({
question: this.securityForm.question,
answer: this.securityForm.answer,
})
})
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
this.$alert(data.message, '提示', {
confirmButtonText: '确定',
});
} else if (data.status === 'failed' && data.message) {
this.$alert(data.message, '提示', {
confirmButtonText: '确定',
});
}
this.dialogVisible = false;
})
.catch(error => {
console.error('Error setting security question:', error);
this.dialogVisible = false;
});

},
login() {
window.location.href = '/login';
}
}
});
Expand All @@ -190,7 +253,20 @@ <h2 style="margin-bottom: 20px">聊天 {{ commentCount }}</h2>
}

.messages li {
margin-bottom: 5px;
margin-bottom: 23px;
text-align: center;
padding-top: 5px;
}

.el-menu-demo {
display: flex;
align-items: center;
justify-content: space-between;
}

.login-button {
margin-left: auto;
margin-top: 10px;
}
</style>
</body>
Expand Down

0 comments on commit 92391cf

Please sign in to comment.