MoneyRecord.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from flask import jsonify
  2. import TokenUtils
  3. from data import MySQLTool
  4. from returnTemp import get_template, get_error_message
  5. class MoneyRecord:
  6. def __init__(self, action, data, logger):
  7. self.action = action
  8. self.data = data
  9. self.logger = logger
  10. def money_record_router(self):
  11. if self.action == "insert":
  12. return self.insert()
  13. elif self.action == "check":
  14. return self.check()
  15. else:
  16. return self.test()
  17. def insert(self):
  18. print("---insert---")
  19. return insert(self.data)
  20. def check(self):
  21. print("---check----")
  22. return check(self.data)
  23. def insert(data):
  24. # 检查参数是否存在
  25. required_fields = ['cash_type', 'des', 'kind', 'location', 'mount', 'paytime', 'uid']
  26. missing_fields = [field for field in required_fields if field not in data]
  27. if missing_fields:
  28. return get_template(2000, get_error_message(2000), {})
  29. token = data["token"]
  30. uid = data["uid"]
  31. mount = data["mount"]
  32. if token == "" or uid == "" or mount == "":
  33. # 三个值必须存在
  34. template = get_template(2000, get_error_message(2000), {})
  35. return template
  36. if not TokenUtils.verify_jwt_token(uid, token):
  37. return get_template(2001, get_error_message(2001), {})
  38. # 获取支付信息
  39. kind = data["kind"]
  40. mount = data["mount"]
  41. des = data["des"]
  42. paytime = data["paytime"]
  43. cash_type = data["cash_type"]
  44. location = data["location"]
  45. # 插入数据
  46. mysql_tool.connect()
  47. columns = ['cash_type', 'des', 'kind', 'location', 'mount', 'paytime', 'uid']
  48. values = [cash_type, des, kind, location, mount, paytime, uid]
  49. insert_result = mysql_tool.insert(table='cash', columns=columns, values=values)
  50. print(insert_result)
  51. mysql_tool.disconnect()
  52. return get_template(0, "记录成功", {})
  53. mysql_tool = MySQLTool(host='localhost', user='root', password='wyt615115@', database='apiserver')
  54. def check(data):
  55. # 检查参数是否存在
  56. required_fields = ['token', 'uid', 'start', 'end', 'type']
  57. missing_fields = [field for field in required_fields if field not in data]
  58. if missing_fields:
  59. return get_template(2000, get_error_message(2000), {})
  60. token = data["token"]
  61. uid = data["uid"]
  62. start = data["start"]
  63. end = data["end"]
  64. kindtype = data["type"]
  65. if token == "" or uid == "" or start == "" or end == "" or kindtype == "":
  66. # 三个值必须存在
  67. template = get_template(2000, get_error_message(2000), {})
  68. return template
  69. if not TokenUtils.verify_jwt_token(uid, token):
  70. return get_template(2001, get_error_message(2001), {})
  71. # 组装查询参数
  72. mysql_tool.connect()
  73. conditions = {}
  74. if kindtype != 0:
  75. conditions['kind'] = kindtype
  76. conditions['uid'] = uid
  77. result = mysql_tool.select(table='cash', columns='*', conditions=conditions)
  78. final_result = []
  79. i_start = int(start)
  80. i_end = int(end)
  81. required_fields = ['id','cash_type', 'des', 'kind', 'location', 'mount', 'paytime', 'uid']
  82. for r in result:
  83. if i_start <= r[6] <= i_end:
  84. temp = {}
  85. for inx, val in enumerate(r):
  86. temp[required_fields[inx]] = val
  87. final_result.append(temp)
  88. mysql_tool.disconnect()
  89. return get_template(0, get_error_message(0), {"record": final_result})