-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
336 lines (289 loc) · 12.2 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#cSpell:ignore jsonify shunvpevtnnhbhjb Weixin rsplit nums xlsx uzpvggxhqdllbgef
from flask import Flask,request,jsonify,send_from_directory
from flask_mail import Message,Mail
import insert,ast,query,json,update,tools
import os
from tools import mapGender,mapType
UPLOAD_FOLDER = './music/'
ALLOWED_EXTENSIONS = set(['mp3'])
app=Flask(__name__)
# app.config.update( #这种写法至少在python3.7,无法正确运行,另外学姐的授权码也有问题
# DEBUG = True,
# MAIL_SERVER='smtp.qq.com',
# MAIL_PROT=25,
# MAIL_USE_TLS = False,
# MAIL_USE_SSL = False,
# MAIL_USERNAME = "[email protected]",
# MAIL_PASSWORD = "uzpvggxhqdllbgef",
# MAIL_DEBUG = False
# )
#shunvpevtnnhbhjb
with open('config.json') as f:
config = json.loads(f.read())
app.config['MAIL_SERVER'] = 'smtp.qq.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_DEBUG'] = True
app.config['MAIL_USERNAME'] = config['smtp']
app.config['MAIL_PASSWORD'] = config['auth_code']
mail=Mail(app)
# 判断文件是否合法
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
#插入设备信息 api for 微信小程序,但是不使用了,微信小程序请求被注释掉了
@app.route('/addDevice',methods=['POST'])
def addDevice():
device_mac = request.form['device_mac']
device_id=insert.insertDevice({'device_mac':device_mac})
if device_id !=None:
return jsonify({'status':1})
else:
return jsonify({'status':0})
#查询设备 api for 终端
@app.route('/queryDevice',methods=['POST'])
def queryDevice():
device_mac = request.form['device_mac']
result = query.getDevice(device_mac)
if result !=None:
return jsonify({'status':1})
else:
return jsonify({'status':0})
#同步设备端所有病人信息至云端 api for 微信小程序
@app.route('/syncAllPatientsInfo',methods=['POST'])
def syncAllPatientsInfo():
patients = request.form['patients']
device_mac = request.form['device_mac']
# data_string = request.get_json()
# data = json.loads(data_string)
# device_mac = data['device_mac']
device_id = insert.insertDevice({'device_mac': device_mac})
if device_id != None:
try:
for patient in ast.literal_eval(patients):
doctor_id=insert.insertDoctor(patient['doctor'],device_mac)
if doctor_id != None:
patient_id=insert.insertPatient(patient,device_mac)
if patient_id !=None:
insert.insertTreatments(patient.get('treat',None),patient_id)
insert.insertGrades(patient.get('grade',None),patient_id)
return jsonify({'status':1})
except:
return jsonify({'status':0})
else:
return jsonify({'status':2})
#获取所有患者信息 api for 终端
@app.route('/queryConsultationList',methods=['POST'])
def queryConsultationList():
data_json = request.get_json()
data = json.loads(data_json)
device_mac = data['device_mac']
print('device_mac',device_mac)
try:
patients_info = query.getAllPatientsInfo(device_mac)
print('patient_info',patients_info)
return jsonify({'patients_info':patients_info})
except:
return jsonify({'status':0})
#小程序获取所有患者信息 api for 微信小程序
@app.route('/queryConsultationListWeixin',methods=['POST'])
def queryConsultationListWeixin():
device_mac = request.form['device_mac']
try:
patients_info = query.getAllPatientsInfo(device_mac)
return jsonify({'patients_info':patients_info,'status':1})
except:
return jsonify({'status':0})
#获取指定姓名的患者信息 api for 微信
@app.route('/querySearchConsultationList',methods=['POST'])
def querySearchConsultationList():
device_mac = request.form['device_mac']
search_name=request.form['search_name']
print('device_mac',device_mac)
print('search_name',search_name)
try:
patients_info = query.getPatientInfoDependName(device_mac,search_name)
print(patients_info)
return jsonify({'patients_info':patients_info,'status':1})
except:
return jsonify({'status':0})
#统计每天治疗人数 api for 微信小程序
@app.route('/countTreatmentsPatientNumber',methods=['POST'])
def countTreatmentsPatientNumber():
device_mac=request.form['device_mac']
try:
gender_nums=query.getTreatmentPatientNumber(device_mac)
return jsonify(gender_nums)
except:
return jsonify({'status': 0})
#统计性别人数占比 api for 微信小程序
@app.route('/countGendersProportion',methods=['POST'])
def countGendersProportion():
device_mac = request.form['device_mac']
try:
gender_percent = query.getGenderPatientProportion(device_mac)
return jsonify(gender_percent)
except:
return jsonify({'status': 0})
#统计第一分型人数占比 api for 微信小程序
@app.route('/countTypesProportion',methods=['POST'])
def countTypesProportion():
device_mac = request.form['device_mac']
try:
type_percent = query.getTypePatientProportion(device_mac)
return jsonify(type_percent)
except:
return jsonify({'status': 0})
#统计年龄占比 api for 微信小程序
@app.route('/countAgesProportion',methods=['POST'])
def countAgesProportion():
device_mac = request.form['device_mac']
try:
age_percent = query.getAgePatientProportion(device_mac)
return jsonify(age_percent)
except:
return jsonify({'status': 0})
#统计每位医生的患者数 api for 微信小程序
@app.route('/countPerDoctorPatientNumber',methods=['POST'])
def countPerDoctorPatientNumber():
device_mac=request.form['device_mac']
try:
perdoctor_patient_nums=query.getPerDoctorPatientNumber(device_mac)
return jsonify(perdoctor_patient_nums)
except:
return jsonify({'status': 0})
#统计每种类型音乐数 api for 微信小程序
@app.route('/countPerMusicNumber',methods=['POST'])
def countPerMusicNumber():
try:
permusic_nums=query.getPerMusicNumber()
return jsonify(permusic_nums)
except:
return jsonify({'status': 0})
#统计整体疗效 api for 微信小程序
@app.route('/countResultAll',methods=['POST'])
def countResultAll():
device_mac = request.form['device_mac']
try:
result_all=query.getResultAll(device_mac)
return jsonify(result_all)
except:
return jsonify({'status': 0})
# #下载单条数据
# @app.route('/downloadSingleData',methods=['POST'])
# def downloadSingleData():
# patient_id=request.form['patient_id']
#发送单条信息邮件 api for 微信小程序
@app.route('/sendMailSingle',methods=['POST'])
def sendMailSingle():
patient_id=request.form['patient_id']
recipient_mail = request.form['recipient_mail'] # 需要为list类型
filename, workbook_filepath=query.singlePatientToExcel(patient_id)
msg = Message("嗨,这是单条数据 ", sender='[email protected]', recipients=[recipient_mail])
msg.body = "单条数据" # msg.body 邮件正文
with app.open_resource(workbook_filepath) as fp:
msg.attach(filename, "text/xlsx", fp.read()) # msg.attach 邮件附件添加,msg.attach("文件名", "类型", 读取文件)
try:
mail.send(msg)
return jsonify({'status':1})
except:
return jsonify({'status':0})
#发送所有信息邮件 api for 微信小程序
@app.route('/sendMailAll',methods=['POST'])
def sendMailAll():
device_mac=request.form['device_mac']
recipient_mail=request.form['recipient_mail'] #需要为list类型
print('patient_id',device_mac)
print('recipient_mail',recipient_mail)
filename,filepath = query.allPatientToExcel(device_mac)
msg = Message("嗨,这是所有数据 ", sender='[email protected]', recipients=[recipient_mail])
msg.body = "所有数据" # msg.body 邮件正文
with app.open_resource(filepath) as fp:
msg.attach(filename, "text/xlsx", fp.read()) # msg.attach 邮件附件添加,msg.attach("文件名", "类型", 读取文件)
try:
mail.send(msg)
return jsonify({'status':1})
except:
return jsonify({'status':0})
#更新单条病人信息 api for 微信
@app.route('/updatePatientInfo',methods=['POST'])
def updatePatientInfo():
device_mac = request.form['device_mac']
patient_name = request.form['patient_name']
patient_age = request.form['patient_age']
patient_gender = request.form['patient_gender']
patient_doctor_category1 = request.form['patient_doctor_category1']
patient_doctor_category2 = request.form['patient_doctor_category2']
patient_self_reported = request.form['patient_self_reported']
patient_medical_history = request.form['patient_medical_history']
patient_examination = request.form['patient_examination']
if patient_doctor_category2 == 'null':
patient_info = {'patient_name':patient_name, 'patient_age':patient_age, 'patient_gender':int(patient_gender),
'patient_doctor_category1':int(patient_doctor_category1), 'patient_doctor_category2':None,
'patient_self_reported':patient_self_reported, 'patient_medical_history':patient_medical_history,
'patient_examination':patient_examination}
else:
patient_info = {'patient_name': patient_name, 'patient_age': patient_age, 'patient_gender': int(patient_gender),
'patient_doctor_category1': int(patient_doctor_category1),
'patient_doctor_category2': int(patient_doctor_category2),
'patient_self_reported': patient_self_reported,
'patient_medical_history': patient_medical_history,
'patient_examination': patient_examination}
a = update.updatePerPatient(patient_info,device_mac)
if a == 1:
return jsonify({'status':1})
else:
return jsonify({'status':0})
#上传mp3文件 api for 终端
@app.route('/uploadMusicFile',methods=['POST'])
def uploadMusicFile():
music_file = request.files['file']
music_type = request.form['type']
if music_file and allowed_file(music_file.filename):
file_name = music_file.filename
previous_file_path = os.path.join(UPLOAD_FOLDER, file_name)
music_file.save(previous_file_path)
human_no_type_res = insert.insertAndUpdateMusic(file_name, music_type)
if human_no_type_res != None:
suffix = file_name.rsplit('.',1)[1]
new_file_name = human_no_type_res + '.'+suffix
new_file_path = os.path.join(UPLOAD_FOLDER,new_file_name)
if os.path.isfile(new_file_path):
os.remove(new_file_path)
os.rename(previous_file_path, new_file_path)
return jsonify({'status': 1})
else:
return jsonify({'status': 0})
#文件没有上传成功
else:
return jsonify({'status':0})
#返回所有音乐信息 api for 终端
@app.route('/queryAllMusic',methods=['POST'])
def queryAllMusic():
data_json = request.get_json()
print(data_json)
musics_info = query.getAllMusicInfo()
return jsonify({'music':musics_info})
#下载指定音乐 api for 终端
#后期还得改,现在为了测试,只下载了一首音乐
@app.route('/downloadCertainMusic',methods=['POST','GET'])
def downloadCertainMusic():
# data_json = request.get_json() #在这里
# print('data',data_json)
# data = json.loads(data_json)
# time = data['time']
#music_names = query.getCertainMusic()
music_names = ["0021.mp3"]
target_filepath, filename = tools.getZipfile(music_names)
return send_from_directory(target_filepath, filename, as_attachment=True)
# return send_from_directory('E:\\mp3ZipFiles', '20190327102507.zip',
# as_attachment=True)
@app.route('/test',methods=['POST','GET'])
def test():
return send_from_directory('./music/','0021.mp3', as_attachment=True)
if __name__=='__main__':
app.run(
debug=True,
host='0.0.0.0',
port=80
)