|
|
@ -1,8 +1,9 @@ |
|
|
|
# python对多文件编程及其不友好, 白写了这么多文件到处报错, 最后还是要写在一个文件里解决了 |
|
|
|
import os |
|
|
|
import sys |
|
|
|
import json |
|
|
|
from datetime import datetime # 处理时间 |
|
|
|
from flask import Flask |
|
|
|
from flask import Flask, request |
|
|
|
from flask_cors import CORS, cross_origin |
|
|
|
from flask_sqlalchemy import SQLAlchemy |
|
|
|
|
|
|
@ -46,57 +47,32 @@ class Win(db.Model): |
|
|
|
|
|
|
|
# 后端API |
|
|
|
# 获取每日屏幕使用总时间 |
|
|
|
@app.route('/getDayScreenUseTime', methods=['GET']) |
|
|
|
@app.route('/getDayScreenUseTime', methods=['POST']) |
|
|
|
@cross_origin() |
|
|
|
def getDayScreenUseTime(): |
|
|
|
datetimeStr = '2022-09-05' |
|
|
|
# datetimeStr = data.datetimeStr |
|
|
|
datetimeStr1 = json.loads(request.data) |
|
|
|
datetimeStr = datetimeStr1['datetimeStr'] |
|
|
|
struct_time = datetime.strptime(datetimeStr, "%Y-%m-%d").date() # 获取请求日期, struct_time数据类型为dateTime |
|
|
|
activityTimeList = Period.query.all() |
|
|
|
selectedTimeList = [] # 选中的对应今天日期的时间李彪 |
|
|
|
selectedTimeList = [] # 选中的对应今天日期的时间 |
|
|
|
for time in activityTimeList: |
|
|
|
if time.timeStart.date() == struct_time: |
|
|
|
if time.timeStart.date() == struct_time and time.timeEnd.date() == struct_time: |
|
|
|
selectedTimeList.append(time) |
|
|
|
|
|
|
|
timeAmount = 0 |
|
|
|
for time in selectedTimeList: |
|
|
|
time_diff = time.timeEnd - time.timeStart |
|
|
|
timeAmount = timeAmount + time_diff.total_seconds() |
|
|
|
if time.timeEnd.date() == time.timeStart.date(): |
|
|
|
time_diff = time.timeEnd - time.timeStart |
|
|
|
timeAmount = timeAmount + time_diff.total_seconds() |
|
|
|
|
|
|
|
timeAmount = int(timeAmount) |
|
|
|
m, s = divmod(timeAmount, 60) |
|
|
|
h, m = divmod(m, 60) |
|
|
|
|
|
|
|
return '{:d}小时{:02d}分{:02d}秒'.format(h, m, s) |
|
|
|
|
|
|
|
# 获取每日第一次屏幕使用时刻 |
|
|
|
@app.route('/getFirstScreenTime', methods=['GET']) |
|
|
|
@cross_origin() |
|
|
|
def getFirstScreenTime(): |
|
|
|
datetimeStr = '2022-09-05' |
|
|
|
struct_time = datetime.strptime(datetimeStr, "%Y-%m-%d").date() # 获取请求日期, struct_time数据类型为dateTime |
|
|
|
activityTimeList = Period.query.all() |
|
|
|
selectedTimeList = [] # 选中的对应今天日期的时间李彪 |
|
|
|
for time in activityTimeList: |
|
|
|
if time.timeStart.date() == struct_time: |
|
|
|
selectedTimeList.append(time) |
|
|
|
break; |
|
|
|
|
|
|
|
firstScreenTime = selectedTimeList[0].timeStart.strftime('%H点%M分%S秒') |
|
|
|
return firstScreenTime |
|
|
|
|
|
|
|
# 获取每日最后一次屏幕使用时刻 |
|
|
|
@app.route('/getLastScreenTime', methods=['GET']) |
|
|
|
@cross_origin() |
|
|
|
def getLastScreenTime(): |
|
|
|
datetimeStr = '2022-09-05' |
|
|
|
struct_time = datetime.strptime(datetimeStr, "%Y-%m-%d").date() # 获取请求日期, struct_time数据类型为dateTime |
|
|
|
activityTimeList = Period.query.all() |
|
|
|
selectedTimeList = [] # 选中的对应今天日期的时间李彪 |
|
|
|
for time in activityTimeList: |
|
|
|
if time.timeStart.date() == struct_time: |
|
|
|
selectedTimeList.append(time) |
|
|
|
|
|
|
|
lastScreenTime = selectedTimeList[-1].timeEnd.strftime('%H点%M分%S秒') |
|
|
|
return lastScreenTime |
|
|
|
|
|
|
|
# 持续时间 |
|
|
|
getDayScreenUseTimeM, getDayScreenUseTimeS = divmod(timeAmount, 60) # 每日屏幕使用时长, 分时秒 |
|
|
|
getDayScreenUseTimeH, getDayScreenUseTimeM = divmod(getDayScreenUseTimeM, 60) |
|
|
|
firstScreenTime = selectedTimeList[0].timeStart.strftime('%H时%M分%S秒') # 第一次屏幕使用时刻, |
|
|
|
lastScreenTime = selectedTimeList[-1].timeEnd.strftime('%H时%M分%S秒') # 最后一次屏幕使用时刻 |
|
|
|
screenTimeSpan = int((selectedTimeList[-1].timeEnd - selectedTimeList[0].timeStart).total_seconds()) # 持续时间 |
|
|
|
getScreenTimeSpanM, getScreenTimeSpanS = divmod(screenTimeSpan, 60) # 每日屏幕使用时长, 分时秒 |
|
|
|
getScreenTimeSpanH, getScreenTimeSpanM = divmod(getScreenTimeSpanM, 60) |
|
|
|
sendJson = {'getDayScreenUseTimeH': getDayScreenUseTimeH, 'getDayScreenUseTimeM': getDayScreenUseTimeM, 'getDayScreenUseTimeS': getDayScreenUseTimeS, 'firstScreenTime': firstScreenTime, 'lastScreenTime': lastScreenTime, 'getScreenTimeSpanH': getScreenTimeSpanH, 'getScreenTimeSpanM': getScreenTimeSpanM, 'getScreenTimeSpanS': getScreenTimeSpanS } |
|
|
|
return json.dumps(sendJson, indent=4) |