懒人记时 代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 regels
4.0 KiB

2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
  1. # python对多文件编程及其不友好, 白写了这么多文件到处报错, 最后还是要写在一个文件里解决了
  2. import os
  3. import sys
  4. import json
  5. from datetime import datetime # 处理时间
  6. from flask import Flask, request
  7. from flask_cors import CORS, cross_origin
  8. from flask_sqlalchemy import SQLAlchemy
  9. app = Flask(__name__)
  10. cors = CORS(app)
  11. app.config['CORS_HEADERS'] = 'Content-Type'
  12. prefix = "sqlite:///"
  13. path = '../appTime/AppTime-Realease/data.db'
  14. app.config['SQLALCHEMY_DATABASE_URI'] = prefix + path
  15. app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
  16. # 在扩展类实例化前加载配置
  17. db = SQLAlchemy(app)
  18. # 数据库模型
  19. class App(db.Model):
  20. id = db.Column(db.Integer, primary_key=True) # App id
  21. process = db.Column(db.Text) # appName
  22. text = db.Column(db.Text) # app窗口名称
  23. tagId = db.Column(db.Integer, unique=True) # 标签id
  24. class Period(db.Model):
  25. timeStart = db.Column(db.DateTime, primary_key=True) # 开始时间
  26. timeEnd = db.Column(db.DateTime) # 结束时间
  27. winId = db.Column(db.Integer) # 活动id
  28. class Tag(db.Model):
  29. id = db.Column(db.Integer, primary_key=True) # 标签id
  30. text = db.Column(db.Text) # 标签文本
  31. class Win(db.Model):
  32. id = db.Column(db.Integer, primary_key=True) # 活动id
  33. appId = db.Column(db.Integer) # App id
  34. text = db.Column(db.Text, nullable=True) # 活动窗口名称
  35. tagId = db.Column(db.Integer, nullable=True) # 标签id
  36. # 后端API
  37. # 获取每日屏幕使用总时间
  38. @app.route('/getDayScreenUseTime', methods=['POST'])
  39. @cross_origin()
  40. def getDayScreenUseTime():
  41. datetimeStr1 = json.loads(request.data)
  42. datetimeStr = datetimeStr1['datetimeStr']
  43. struct_time = datetime.strptime(datetimeStr, "%Y-%m-%d").date() # 获取请求日期, struct_time数据类型为dateTime
  44. activityTimeList = Period.query.all()
  45. selectedTimeList = [] # 选中的对应今天日期的时间
  46. for time in activityTimeList:
  47. if time.timeStart.date() == struct_time and time.timeEnd.date() == struct_time:
  48. selectedTimeList.append(time)
  49. if (len(selectedTimeList) == 0):
  50. sendJson = {'getDayScreenUseTimeH': 0, 'getDayScreenUseTimeM': 0, 'getDayScreenUseTimeS': 0, 'firstScreenTime': '', 'lastScreenTime': '', 'getScreenTimeSpanH': 0, 'getScreenTimeSpanM':0, 'getScreenTimeSpanS': 0 }
  51. else:
  52. timeAmount = 0
  53. for time in selectedTimeList:
  54. if time.timeEnd.date() == time.timeStart.date():
  55. time_diff = time.timeEnd - time.timeStart
  56. timeAmount = timeAmount + time_diff.total_seconds()
  57. timeAmount = int(timeAmount)
  58. getDayScreenUseTimeM, getDayScreenUseTimeS = divmod(timeAmount, 60) # 每日屏幕使用时长, 分时秒
  59. getDayScreenUseTimeH, getDayScreenUseTimeM = divmod(getDayScreenUseTimeM, 60)
  60. firstScreenTime = selectedTimeList[0].timeStart.strftime('%H时%M分%S秒') # 第一次屏幕使用时刻,
  61. lastScreenTime = selectedTimeList[-1].timeEnd.strftime('%H时%M分%S秒') # 最后一次屏幕使用时刻
  62. screenTimeSpan = int((selectedTimeList[-1].timeEnd - selectedTimeList[0].timeStart).total_seconds()) # 持续时间
  63. getScreenTimeSpanM, getScreenTimeSpanS = divmod(screenTimeSpan, 60) # 每日屏幕使用时长, 分时秒
  64. getScreenTimeSpanH, getScreenTimeSpanM = divmod(getScreenTimeSpanM, 60)
  65. sendJson = {'getDayScreenUseTimeH': getDayScreenUseTimeH, 'getDayScreenUseTimeM': getDayScreenUseTimeM, 'getDayScreenUseTimeS': getDayScreenUseTimeS, 'firstScreenTime': firstScreenTime, 'lastScreenTime': lastScreenTime, 'getScreenTimeSpanH': getScreenTimeSpanH, 'getScreenTimeSpanM': getScreenTimeSpanM, 'getScreenTimeSpanS': getScreenTimeSpanS }
  66. return json.dumps(sendJson, indent=4)
  67. @app.route('/getDayScreenUseTime', methods=['POST'])
  68. @cross_origin()
  69. def getDayScreenUseTime():
  70. datetimeStr1 = json.loads(request.data)
  71. datetimeStr = datetimeStr1['datetimeStr']
  72. struct_time = datetime.strptime(datetimeStr, "%Y-%m-%d").date() # 获取请求日期, struct_time数据类型为dateTime
  73. return "1"