1 Star 1 Fork 0

ManiacRaTT/HospitalFrontDesk

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
view_chat.py 27.13 KB
一键复制 编辑 原始数据 按行查看 历史
ManiacRaTT 提交于 2023-04-02 19:41 . sql
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
import json
import math
import smtplib
from datetime import datetime
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
from math import floor
from peewee import *
from tornado.websocket import WebSocketHandler
import logging
import ctrl_base
from werkzeug.security import generate_password_hash, check_password_hash
import tornado.web
from database.XRedis import redis_0
from database.Xmongodb import mongo
from database.xES import es, return_body
from database.xMySQL_1 import PatientPatient, DoctorDoctor, objects, DrugDrugCategory, PatientPatientFrontFeedback, \
DoctorDoctorFrontFeedback, DoctorDoctorSections, DoctorDoctorDepartments, DoctorDoctorScore, \
DoctorDoctorRegistrationPrice, PatientPatientContactUs, NewsNewsImg, NewsNewsBlog, NewsNewsCategory, NewsNewsTag
from util.check_login import auth_login_redirect, ban_check
from util.dfa import DFA
from util.utils import GetInformation, emailisValid, generate_verification_code, GetDataSensitive, GetFlowNumber, \
is_all_english, generate_page_list, page_tool
from decimal import *
import requests
dfa = DFA()
class chat_index(ctrl_base.BaseHandler):
@tornado.web.authenticated
@auth_login_redirect
@ban_check
async def get(self):
current_type, current_username, current_user_id, find_0, find_0_0, find_0_1 = await self.get_base_config()
cursor_private = None
contact_chat = []
cursor_private_count = 0
if 'Patient' in str(current_type):
cursor_private = mongo.collection_private_name.find({"$or":[{'patient': current_user_id},{"patient.patient_id": current_user_id}]}).sort([("time",-1)])
cursor_private_count = await mongo.collection_private_name.count_documents({"$or":[{'patient': current_user_id},{"patient.patient_id": current_user_id}]})
elif 'Doctor' in str(current_type):
cursor_private = mongo.collection_private_name.find({'doctor': current_user_id}).sort([("time",-1)])
cursor_private_count = await mongo.collection_private_name.count_documents({'doctor': current_user_id})
if cursor_private_count <= 0:
pass
else:
async for document in cursor_private:
contact_chat.append(document)
await self.render("chat_index.html", find_0=find_0, current_type=current_type,
current_username=current_username, current_user_id=current_user_id, find_0_0=find_0_0,
find_0_1=find_0_1,contact_chat=contact_chat,key='common')
class chat_private(ctrl_base.BaseHandler):
@tornado.web.authenticated
@auth_login_redirect
@ban_check
async def get(self):
current_type, current_username, current_user_id, find_0, find_0_0, find_0_1 = await self.get_base_config()
msg = []
contact_chat = []
img = '/static/img/doctor.svg'
user_id = None
cursor_private = None
cursor_private_count = 0
key = self.get_argument('key')
to_name = None
if 'Patient' in str(current_type):
user_id = str(key).replace('{}_'.format(current_user_id), '')
doctor = await objects.get(DoctorDoctor, doctor_id=user_id)
to_name = 'DOCTOR '+doctor.name
cursor_private = mongo.collection_private_name.find({"$or":[{'patient': current_user_id},{"patient.patient_id": current_user_id}]}).sort([("time",-1)])
cursor_private_count = await mongo.collection_private_name.count_documents({"$or":[{'patient': current_user_id},{"patient.patient_id": current_user_id}]})
if doctor.img == '':
img = '/static/img/doctor.svg'
else:
img = 'http://localhost:8081/' + doctor.img
await mongo.collection_private_name.update_one \
({'name': key}, {'$set': {'p_count':0}})
elif 'Doctor' in str(current_type):
user_id = str(key).replace('_{}'.format(current_user_id), '')
to_name = 'PATIENT ' + current_username
doctor = await objects.get(DoctorDoctor, doctor_id=current_user_id)
cursor_private = mongo.collection_private_name.find({'doctor': current_user_id}).sort([("time",-1)])
cursor_private_count = await mongo.collection_private_name.count_documents({'doctor': current_user_id})
if doctor.img == '':
img = '/static/img/doctor.svg'
else:
img = 'http://localhost:8081/' + doctor.img
await mongo.collection_private_name.update_one \
({'name': key}, {'$set': {'d_count': 0}})
count = await mongo.collection_private_name.count_documents({'name':key})
if count == 0:
if 'Patient' in str(current_type):
await mongo.collection_private_name.insert_one({'name':key,
'patient':current_user_id,
'doctor':user_id,
'img':img,
'd_count':0,
'p_count':0,
'time':datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
'msg':''})
elif 'Doctor' in str(current_type):
await mongo.collection_private_name.insert_one({'name':key,
'patient':user_id,
'doctor':current_user_id,
'img':img,
'd_count':0,
'p_count':0,
'time': datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
'msg': ''
})
if cursor_private_count <= 0:
contact_chat.append({'name':key,
'type': 'private',
'patient':current_user_id,
'doctor':user_id,
'img':img,
'd_count':0,
'p_count':0,
'time': datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
'msg': ''
})
else:
async for document in cursor_private:
contact_chat.append(document)
record_count = await mongo[key].count_documents({})
history = []
if record_count > 0:
record = mongo[key].find({})
async for document in record:
if document['user_id'] == current_user_id:
if 'Doctor' in document['user_id']:
document.update({'own_type': 'own_msg'})
else:
document.update({'own_type': 'own_msg'})
else:
if 'Doctor' in document['user_id']:
document.update({'own_type': 'doc_msg'})
else:
document.update({'own_type': 'msg'})
history.append(document)
await self.render("chat.html", find_0=find_0, current_type=current_type,
current_username=current_username, current_user_id=current_user_id, find_0_0=find_0_0,
find_0_1=find_0_1,msg=msg,key=key,contact_chat=contact_chat
,chat_type='private',to_name=to_name,history=history)
class chat_group(ctrl_base.BaseHandler):
@tornado.web.authenticated
@auth_login_redirect
@ban_check
async def get(self):
current_type, current_username, current_user_id, find_0, find_0_0, find_0_1 = await self.get_base_config()
msg = []
contact_chat = []
img = '/static/img/group_chat.svg.svg'
cursor_private = None
cursor_private_count = 0
key = self.get_argument('key')
user_id = str(key).replace('Group_', '')
doctor = await objects.get(DoctorDoctor, doctor_id=user_id)
to_name = 'GROUP {} {}'.format(doctor.doctor_id,doctor.name)
count = await mongo.collection_private_name.count_documents({'name': key})
if count == 0:
if 'Patient' in str(current_type):
await mongo.collection_private_name.insert_one({'name': key,
'patient': [{'patient_id':current_user_id,'count':0}],
'doctor': user_id,
'img': img,
'd_count': 0,
'p_count': 0,
'time': datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
'msg': ''})
elif 'Doctor' in str(current_type):
await mongo.collection_private_name.insert_one({'name': key,
'patient': None,
'doctor': current_user_id,
'img': img,
'd_count': 0,
'p_count': 0,
'time': datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
'msg': ''
})
else:
if 'Patient' in str(current_type):
a = await mongo.collection_private_name.find_one({'name': key})
if a['patient'] is None:
new = [{'patient_id':current_user_id,'count':0}]
await mongo.collection_private_name.update_one \
({'name': key}, {'$set': {'patient': new}})
else:
count = await mongo.collection_private_name.count_documents(
{'name': key, "patient.patient_id": current_user_id})
if count >0:
pass
else:
await mongo.collection_private_name.update_one \
({'name': key}, {'$addToSet': {'patient': {'patient_id': current_user_id, 'count': 0}}})
if 'Patient' in str(current_type):
cursor_private = mongo.collection_private_name.find({"$or":[{'patient': current_user_id},{"patient.patient_id": current_user_id}]}).sort([("time",-1)])
cursor_private_count = await mongo.collection_private_name.count_documents({"$or":[{'patient': current_user_id},{"patient.patient_id": current_user_id}]})
if doctor.img == '':
img = '/static/img/doctor.svg'
else:
img = 'http://localhost:8081/' + doctor.img
await mongo.collection_private_name.update_one \
({'name': key,'patient.patient_id':current_user_id}, {'$set': {'patient.$.count': 0}})
elif 'Doctor' in str(current_type):
cursor_private = mongo.collection_private_name.find({'doctor': current_user_id}).sort([("time",-1)])
cursor_private_count = await mongo.collection_private_name.count_documents({'doctor': current_user_id})
if doctor.img == '':
img = '/static/img/doctor.svg'
else:
img = 'http://localhost:8081/' + doctor.img
await mongo.collection_private_name.update_one \
({'name': key}, {'$set': {'d_count': 0}})
if cursor_private_count <= 0:
contact_chat.append({'name': key,
'type':'group',
'patient': current_user_id,
'doctor': user_id,
'img': img,
'd_count': 0,
'p_count': 0,
'time': '',
'msg': ''
})
else:
async for document in cursor_private:
contact_chat.append(document)
record_count = await mongo[key].count_documents({})
history = []
if record_count > 0:
record = mongo[key].find({})
async for document in record:
if document['user_id'] == current_user_id:
if 'Doctor' in document['user_id']:
document.update({ 'own_type': 'own_msg'})
else:
document.update({'own_type': 'own_msg'})
else:
if 'Doctor' in document['user_id']:
document.update({'own_type': 'doc_msg'})
else:
document.update({'own_type': 'msg'})
history.append(document)
await self.render("chat.html", find_0=find_0, current_type=current_type,
current_username=current_username, current_user_id=current_user_id, find_0_0=find_0_0,
find_0_1=find_0_1,msg=msg,key=key,chat_type='group',to_name=to_name
,contact_chat=contact_chat,history=history)
class chat(tornado.websocket.WebSocketHandler):
users = dict()
user_id_list = dict()
online_user = {}
def createMessage(self, type, user, usertype,user_img,data):
message = {
"type": type,
"user": user,
"user_type": usertype,
"img": user_img,
"msg": data,
"create_time":datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
return json.dumps(message)
async def get_user_info(self,user_id):
type = self.get_secure_cookie('type')
username = ''
type = str(type).replace("b'", "").replace("'", "")
img = '/static/img/logo.png'
if type == 'doctor':
doc = await objects.get(DoctorDoctor, doctor_id=user_id)
username = doc.name
if doc.img == '':
img = '/static/img/doctor.svg'
else:
img = 'http://localhost:8081/' + doc.img
if type == 'patient':
pat = await objects.get(PatientPatient,patient_id=user_id)
username = pat.username
img = '/static/img/logo.png'
return username,img,type
def get_chat_type(self,key):
_type_ = ''
if 'Group' in key:
_type_ = 'group'
elif 'Patient' in key:
_type_ = 'private'
return _type_
@ban_check
async def open(self, *args, **kwargs):
# 当进入到chat.html页面时,会主动出发该函数
user_id = str(self.get_secure_cookie('user_id')).replace("b'", "").replace("'", "")
username, img ,type= await self.get_user_info(user_id)
key = self.get_argument('key')
key = str(key).replace("<%=", "").replace("%>", "")
chat_type = self.get_chat_type(key)
if key == 'common':
pass
else:
if key in self.users:
self.users[key].append(self)
self.user_id_list[key].append(user_id)
else:
self.users[key] = [self]
self.user_id_list[key] = [user_id]
self.online_user.update({'{}'.format(user_id):self})
if chat_type == 'private':
if len(self.users[key]) > 2:
self.redirect('/error_500')
if key == 'common':
pass
else:
for user in self.users[key]:
user.write_message(self.createMessage('enter', username, type, img, 'ENTER'))
@ban_check
async def on_message(self, message):
# 当用户发送消息时,将用户发送的消息反馈到页面
key = self.get_argument('key')
key = str(key).replace("<%=","").replace("%>","")
chat_type = self.get_chat_type(key)
if chat_type == 'private':
if len(self.users[key]) > 2:
self.redirect('/error_500')
user_id = str(self.get_secure_cookie('user_id')).replace("b'", "").replace("'", "")
username, img ,type= await self.get_user_info(user_id)
message = dfa.filter_all(str(message))
if chat_type == 'group':
await mongo[key].insert_one({'msg': message,
'user_id': user_id,
'username': username,
'type': type,
'img': img,
'time': datetime.now()})
await mongo.collection_private_name.update_one \
({'name': key}, {'$set': {'time': datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 'msg': message}})
list_member = []
member = await mongo.collection_private_name.find_one({'name': key})
if member['patient'] is None:
pass
else:
for i in member['patient']:
list_member.append(i['patient_id'])
for i in list_member:
if i in self.user_id_list[key]:
pass
else:
check_count = await mongo.collection_private_name.find_one({"name":key,"patient.patient_id": i})
count = 0
for k in check_count['patient']:
if k['patient_id'] == i:
count = int(k['count']) + 1
else:
pass
mongo.collection_private_name.update_one(
{'name': key, "patient.patient_id": i},
{ '$set': {"patient.$.count": count}}
)
doctor_id = str(key).replace('Group_','')
if doctor_id in self.user_id_list[key]:
pass
else:
p = await mongo.collection_private_name.find_one({'name': key})
n = p['d_count']
await mongo.collection_private_name.update_one \
({'name': key}, {'$set': {'d_count': int(n) + 1}})
elif chat_type == 'private':
await mongo[key].insert_one({'msg': message,
'user_id': user_id,
'username': username,
'type': type,
'img': img,
'time': datetime.now()})
if type == 'doctor':
user_list = self.user_id_list[key]
if len(user_list) == 2:
await mongo.collection_private_name.update_one \
({'name': key}, {'$set': {'time': datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 'msg': message}})
else:
p = await mongo.collection_private_name.find_one({'name': key})
n = p['p_count']
await mongo.collection_private_name.update_one \
({'name': key}, {'$set': {'time': datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 'msg': message,'p_count':int(n)+1}})
elif type == 'patient':
user_list = self.user_id_list[key]
if len(user_list) == 2:
await mongo.collection_private_name.update_one \
({'name': key}, {'$set': {'time': datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 'msg': message}})
else:
p = await mongo.collection_private_name.find_one({'name': key})
n = p['d_count']
await mongo.collection_private_name.update_one \
({'name': key}, {'$set': {'time': datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 'msg': message, 'd_count': int(n) + 1}})
for user in self.users[key]:
if user == self:
user.write_message(self.createMessage('own_msg', username,type, img, message))
else:
if type == 'doctor' and chat_type == 'group':
user.write_message(self.createMessage('doc_msg', username, type, img, message))
else:
user.write_message(self.createMessage('msg', username,type,img, message))
if chat_type == 'group':
user_list_type = 'group'
else:
user_list_type = 'private'
for user in self.online_user:
if len(message) > 20:
message = '{}...'.format(message[:20])
p = await mongo.collection_private_name.find_one({'name': key})
if p['d_count'] > 99:
d_count = '99+'
else:
d_count = p['d_count']
if p['p_count'] > 99:
p_count = '99+'
else:
p_count = p['p_count']
if user in self.user_id_list[key]:
if 'Doctor' in user:
to_user = str(key).replace('_{}'.format(user),'')
self.online_user[user].write_message(self.createMessage('own_user_list', user, user_list_type, p['img'],
{'information':message,'count':d_count,'key':key,'to_user':to_user}))
else:
if user_list_type == 'private':
self.online_user[user].write_message(
self.createMessage('own_user_list', user, user_list_type,'/static/img/logo.png',
{'information': message, 'count': p_count,'key':key}))
else:
_p_ = await mongo.collection_private_name.find_one({'name': key,"patient.patient_id":user})
count = 0
for o in _p_['patient']:
if o['patient_id'] == user:
count = o['count']
if count > 99:
count = '99+'
self.online_user[user].write_message(
self.createMessage('own_user_list', user, user_list_type,p['img'],
{'information': message, 'count': count,'key':key}))
else:
if 'Doctor' in user:
to_user = str(key).replace('_{}'.format(user), '')
self.online_user[user].write_message(self.createMessage('user_list', user, user_list_type, p['img'],
{'information':message,'count':d_count,'key':key,'to_user':to_user}))
else:
if user_list_type == 'private':
self.online_user[user].write_message(
self.createMessage('user_list', user, user_list_type,'/static/img/logo.png',
{'information': message, 'count': p_count,'key':key}))
else:
_p_ = await mongo.collection_private_name.find_one({'name': key,"patient.patient_id":user})
count = 0
for o in _p_['patient']:
if o['patient_id'] == user:
count = o['count']
if count > 99:
count = '99+'
self.online_user[user].write_message(
self.createMessage('user_list', user, user_list_type,p['img'],
{'information': message, 'count': count,'key':key}))
def on_close(self):
# 当用户关闭浏览器时,删除用户
user_id = str(self.get_secure_cookie('user_id')).replace("b'", "").replace("'", "")
username = str(self.get_secure_cookie('username')).replace("b'", "").replace("'", "")
type = str(self.get_secure_cookie('type')).replace("b'", "").replace("'", "")
key = self.get_argument('key')
key = str(key).replace("<%=", "").replace("%>", "")
for i in list(self.online_user.keys()):
if self.online_user[i] == self:
del self.online_user[i]
if key == 'common':
pass
else:
if len(self.users[key]) == 1:
del self.users[key]
del self.user_id_list[key]
else:
self.users[key].remove(self)
self.user_id_list[key].remove(user_id)
for user in self.users[key]:
user.write_message(self.createMessage('enter', username, type, None, 'QUIT'))
def check_origin(self, origin):
return True
class chat_group_quit(ctrl_base.BaseHandler):
@tornado.web.authenticated
@auth_login_redirect
@ban_check
async def post(self):
key = self.get_argument('key')
user_id = str(self.get_secure_cookie('user_id')).replace("b'", "").replace("'", "")
print(key,user_id)
message = {}
if 'Doctor' in user_id:
self.redirect('error_500')
else:
await mongo.collection_private_name.update_one(
{'name': key,"patient.patient_id":user_id},
{ "$pull": { "patient": { "patient_id": user_id}}}
)
message['code'] = 200
await self.finish(message)
return
class chat_check_count_all(ctrl_base.BaseHandler):
# @tornado.web.authenticated
# @auth_login_redirect
# @ban_check
async def get(self):
user_id = str(self.get_secure_cookie('user_id')).replace("b'", "").replace("'", "")
user = await mongo.user.find_one({'user':user_id})
current_type = str(self.get_secure_cookie('type')).replace("b'", "").replace("'", "")
count = 0
if current_type == 'doctor':
p = mongo.collection_private_name.find({"doctor":user_id})
async for document in p:
count = count+document['d_count']
elif current_type == 'patient':
p = mongo.collection_private_name.find({"$or": [{'patient': user_id}, {"patient.patient_id": user_id}]})
async for document in p:
if 'Group' in document['name']:
for i in document['patient']:
if i['patient_id'] == user_id:
count = count + i['count']
elif 'Patient' in document['name']:
count = count + document['p_count']
if user is not None:
if user['status'] is False or user['is_online'] is False:
count = -1
message = {'count':count}
await self.finish(message)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/maniacratt/HospitalFrontDesk.git
git@gitee.com:maniacratt/HospitalFrontDesk.git
maniacratt
HospitalFrontDesk
HospitalFrontDesk
master

搜索帮助