-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
412 lines (338 loc) · 11.5 KB
/
app.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
'''
Author: Resunoon
Date: 2020-10-07 18:20:37
LastEditTime: 2020-12-24 18:05:30
LastEditors: Resunoon
Description:
FilePath: \Pycar\app.py
最后写亿行
'''
from common import common
import os
import time
import pymysql
import datetime
import psutil
from flask import Flask, render_template, request, session, make_response, redirect
import module.loginobject as loginobject
import module.config as db
app = Flask(__name__, static_url_path='/',
static_folder='./static', template_folder='./templates')
app.secret_key = os.urandom(24)
# 路由模块开始 #
###############
# 404页面
@app.errorhandler(404)
def pagenotfound(error):
return redirect('/')
# 登录页面
@app.route("/", methods=["GET", "POST"])
def login():
if request.method == 'GET':
return render_template('login.html')
elif request.method == 'POST':
# 登录准备
username = request.form.get('username')
password = request.form.get('password')
lgobj = loginobject.loginobj(username, password)
if username == '' or password == '':
return redirect('/')
if username == 'admin' and password == '123':
session['username'] = 'admin'
return redirect('/admin')
# 开始登录, 由于学校奇怪的防御机制, 很多时候不知道为什么会出错, 所以多试几次
trialtime = 3
for i in range(0, trialtime):
# 需要验证码
if lgobj.checkcaptcha() == True:
return '''
用户认证失败,请去信息门户输入验证码登录一遍, 3秒后跳转
<script type="text/javascript">function jump(){
window.location.href='https://www.nuist.edu.cn/';}
setTimeout(jump,3000);</script>'''
# endif
# 登录成功
if lgobj.login() == True:
# 自动登录开关打开, 保存永久cookie
if request.form.get('rememberme') == 'on':
session['username'] = username
session.permanent = True
app.permanent_session_lifetime = datetime.timedelta(
days=365*4)
return redirect('/index')
else:
# 临时登录保存session 5分钟
session['username'] = username
session.permanent = True
app.permanent_session_lifetime = datetime.timedelta(
minutes=5)
return redirect('/index')
time.sleep(1)
else:
return '错误的请求方式'
return '登录失败'
# 二维码页面(主页面)
@app.route('/index', methods=["GET"])
def index():
if session.get("username") is None:
return redirect('/')
# 获取订单号
code = getcode()
# 插入订单号
insertorder(code)
# 获得学生钱包信息
money = getstumoney()
# 获得公告
cast = getbroadcast()
return render_template('index.html', secretcode=code, index="active", money=money, cast=cast)
# 二维码页面(主页面)调试界面
@app.route('/index/debug', methods=["GET"])
def indexdebug():
if session.get("username") is None:
return redirect('/')
# 获取订单号
code = getcode()
# 插入订单号
insertorder(code)
# 获得学生钱包信息
money = getstumoney()
return render_template('indexdebug.html', secretcode=code, index="active", money=money)
# 校车页面
@app.route('/map', methods=["GET"])
def map():
return render_template('map.html', map="active")
# 充值页面
@app.route('/charge')
def charge():
if session.get("username") is None:
return redirect('/')
return render_template('charge.html')
# 查看充值记录
@app.route('/chargehistory')
def chargehistory():
if session.get("username") is None:
return redirect('/')
history = getchargehistory(session.get('username'))
return render_template("chargehistory.html", my="active", chargeinfo=history)
# 查看乘车记录
@app.route('/takehistory')
def tekehistory():
if session.get("username") is None:
return redirect('/')
takeinfo = gettakehistory(session.get('username'))
return render_template("takehistory.html", my="active", takeinfo=takeinfo)
# 后台主页面
@app.route('/admin')
def admin():
if session.get("username") is None:
return redirect('/')
if not session.get("username") == 'admin':
return redirect('/')
cpu = getcpustat()
memory = getmemorystat()
return render_template('admin.html', cpu=cpu, memory=memory, admin="active")
# 后台订单页面
@app.route("/order")
def order():
if session.get("username") is None:
return redirect('/')
if not session.get("username") == 'admin':
return redirect('/')
orders = getorder()
return render_template("order.html", orders=orders, order="active")
# 后台公告查询页面
@app.route("/broadcast")
def broadcast():
if session.get("username") is None:
return redirect('/')
if not session.get("username") == 'admin':
return redirect('/')
broad = getbroadcast()
return render_template("broadcast.html", broadcast="active", broad=broad)
# 后台公告发布页面
@app.route("/addbroadcast")
def addbroadcast():
if session.get("username") is None:
return redirect('/')
if not session.get("username") == 'admin':
return redirect('/')
return render_template("addbroadcast.html", broadcast="active")
# 我的
@app.route('/my')
def my():
if session.get("username") is None:
return redirect('/')
return render_template("my.html", my="active")
# 发布公告API
@app.route("/addbroadcastapi", methods=["POST"])
def addbroadcastapi():
if session.get("username") is None:
return redirect('/')
if not session.get("username") == 'admin':
return redirect('/')
addcast(request.form.get("title"), request.form.get("content"))
return render_template("broadcast.html", broadcast="active")
# 充值API
@app.route('/charging', methods=['POST'])
def charging():
if session.get("username") is None:
return redirect('/')
amount = request.form.get('amount')
if amount is not None and amount.isdigit() and int(amount) > 0:
conn = connectsql()
# 连接失败
if conn is None:
session['username'] = ''
return redirect('/')
c = conn.cursor()
money = getstumoney()
sql = f"UPDATE stufinance SET wallet = {money+int(amount)} WHERE username = '{session.get('username')}'"
c.execute(sql)
conn.commit()
timedate = str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
sql = f"insert into rechargeinfo (username, rechargevalue, rechargetime) values ('{session.get('username')}', '{amount}', '{timedate}')"
c.execute(sql)
conn.commit()
disconnectsql(conn, c)
return redirect('/index')
return redirect('/charge')
# 登录退出API
@app.route("/logout", methods=['GET'])
def logout():
if session.get('username') is None:
return redirect('/')
else:
session['username'] = None
return redirect('/')
# debug 扫码
@app.route("/scan", methods=["POST"])
def scan():
if session.get('username') is None:
return redirect('/')
secretcode = request.form.get('secretcode')
conn = connectsql()
c = conn.cursor()
sql = f"select wallet from stufinance where username = {session.get('username')}"
c.execute(sql)
money = c.fetchone()[0]
sql = f"update stufinance set wallet = {money-1} where username = {session.get('username')}"
c.execute(sql)
sql = f"UPDATE orderinfo set payid='1' where orderid='{secretcode}'"
c.execute(sql)
taketime = str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
sql = f"insert into takebushistory (username, taketime) values ({session.get('username')},'{taketime}')"
c.execute(sql)
conn.commit()
disconnectsql(conn, c)
return redirect("/index/debug")
# 路由模块结束 #
###############
# 模组模块 #
###########
# 连接数据库
def connectsql() -> object:
try:
conn = pymysql.connections.Connection(
host=db.database_host,
user=db.database_user,
passwd=db.database_password,
db=db.database,
port=db.database_port,
charset='utf8')
return conn
except:
return None
# 断开数据库
def disconnectsql(conn, c):
try:
conn.close()
c.close()
return True
except:
return False
# 处理的函数 #
#############
# 主页面获得订单号
def getcode() -> str:
return str(time.time()) + session.get('username')
# 插入订单号
def insertorder(code) -> bool:
conn = connectsql()
c = conn.cursor()
orderid = code
orderdate = str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
payid = 0
sql = "INSERT INTO orderinfo (orderid,orderdate,payid) VALUES ('%s','%s','%s')" % (
orderid, orderdate, payid)
c.execute(sql)
conn.commit()
disconnectsql(conn, c)
return True
# 获得学生钱
def getstumoney() -> int:
conn = connectsql()
c = conn.cursor()
sql = f"SELECT wallet FROM stufinance where username = {session.get('username')}"
if c.execute(sql) is not 0:
money = c.fetchone()[0]
else:
# 发现没有学生信息
sql = f"INSERT INTO stufinance(username,takecount,wallet) VALUES('{session.get('username')}', '0', '0')"
c.execute(sql)
conn.commit()
money = 0
disconnectsql(conn, c)
return int(money)
# 获得订单
def getorder():
conn = connectsql()
c = conn.cursor()
sql = "select * from orderinfo where payid = 1"
c.execute(sql)
result = c.fetchall()
disconnectsql(conn, c)
return result
# 获得公告
def getbroadcast():
conn = connectsql()
c = conn.cursor()
sql = "select * from broadcast"
c.execute(sql)
result = c.fetchall()
disconnectsql(conn, c)
return result
# 添加公告
def addcast(title, content):
conn = connectsql()
c = conn.cursor()
curtime = str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
sql = f"insert into broadcast (title, info, date) values ('{title}', '{content}', '{curtime}')"
c.execute(sql)
conn.commit()
disconnectsql(conn, c)
return True
# 获得充值历史
def getchargehistory(username):
conn = connectsql()
c = conn.cursor()
sql = f"select * from rechargeinfo where username = {username}"
c.execute(sql)
disconnectsql(conn, c)
return c.fetchall()
# 获得乘车信息
def gettakehistory(username):
conn = connectsql()
c = conn.cursor()
sql = f"select * from takebushistory where username = {username}"
c.execute(sql)
disconnectsql(conn, c)
return c.fetchall()
# 获得cpu信息
def getcpustat() -> int:
data = psutil.virtual_memory()
return int(data.available/data.total*100)
# 获得内存信息
def getmemorystat() -> int:
return int(psutil.cpu_percent(interval=1))
if __name__ == '__main__':
app.run(debug=True, host='localhost')