代码拉取完成,页面将自动刷新
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)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。