from flask import jsonify import TokenUtils from data import MySQLTool from returnTemp import get_template, get_error_message import time class MoneyRecord: def __init__(self, action, data, logger): self.action = action self.data = data self.logger = logger def money_record_router(self): if self.action == "insert": return self.insert() elif self.action == "check": return self.check() elif self.action == "delete": return self.deleteSomeItem() else: return self.test() def insert(self): print("---insert---") return insert(self.data) def check(self): print("---check----") return check(self.data) def deleteSomeItem(self): print("---deleteSomeItem----") return deleteSomeItem(self.data) def insert(data): # 检查参数是否存在 required_fields = ['cash_type', 'des', 'kind', 'location', 'mount', 'paytime', 'uid'] missing_fields = [field for field in required_fields if field not in data] if missing_fields: return get_template(2000, get_error_message(2000), {}) token = data["token"] uid = data["uid"] mount = data["mount"] if token == "" or uid == "" or mount == "": # 三个值必须存在 template = get_template(2000, get_error_message(2000), {}) return template if not TokenUtils.verify_jwt_token(uid, token): return get_template(2001, get_error_message(2001), {}) # 获取支付信息 kind = data["kind"] mount = data["mount"] des = data["des"] paytime = data["paytime"] cash_type = data["cash_type"] location = data["location"] noteid = get_order_code() # 插入数据 mysql_tool.connect() columns = ['cash_type', 'des', 'kind', 'location', 'mount', 'paytime', 'uid', 'noteid'] values = [cash_type, des, kind, location, mount, paytime, uid, noteid] insert_result = mysql_tool.insert(table='cash', columns=columns, values=values) print(insert_result) mysql_tool.disconnect() return get_template(0, "记录成功", {}) def deleteSomeItem(data): required_fields = ['token', 'uid', 'noteid'] missing_fields = [field for field in required_fields if field not in data] if missing_fields: return get_template(2000, get_error_message(2000), {}) token = data["token"] uid = data["uid"] noteid = data["noteid"] # 检查参数 if token == "" or uid == "" or noteid == "": # 三个值必须存在 template = get_template(2000, get_error_message(2000), {}) return template if not TokenUtils.verify_jwt_token(uid, token): return get_template(2001, get_error_message(2001), {}) mysql_tool.connect() result = mysql_tool.select(table='cash', columns='*', conditions={"uid": uid, "noteid": noteid}) if len(result): delete_result = mysql_tool.delete(table='cash', conditions={"uid": uid, "noteid": noteid}) if delete_result == 1: return get_template(0, get_error_message(0), {}) else: return get_template(2002, get_error_message(2002), {}) else: return get_template(1006, get_error_message(1006), {}) def check(data): # 检查参数是否存在 required_fields = ['token', 'uid', 'start', 'end', 'type'] missing_fields = [field for field in required_fields if field not in data] if missing_fields: return get_template(2000, get_error_message(2000), {}) token = data["token"] uid = data["uid"] start = data["start"] end = data["end"] kindtype = data["type"] if token == "" or uid == "" or start == "" or end == "" or kindtype == "": # 三个值必须存在 template = get_template(2000, get_error_message(2000), {}) return template if not TokenUtils.verify_jwt_token(uid, token): return get_template(2001, get_error_message(2001), {}) # 组装查询参数 mysql_tool.connect() conditions = {} if kindtype != 0: conditions['kind'] = kindtype conditions['uid'] = uid result = mysql_tool.select(table='cash', columns='*', conditions=conditions) final_result = [] i_start = int(start) i_end = int(end) required_fields = ['id', 'cash_type', 'des', 'kind', 'location', 'mount', 'paytime', 'uid', 'noteid'] for r in result: if i_start <= r[6] <= i_end: temp = {} for inx, val in enumerate(r): temp[required_fields[inx]] = val final_result.append(temp) mysql_tool.disconnect() return get_template(0, get_error_message(0), {"record": final_result}) mysql_tool = MySQLTool(host='localhost', user='root', password='wyt615115@', database='apiserver') # 生成订单号 def get_order_code(): # 年月日时分秒+time.time()的后7位 order_no = str(time.strftime('%Y%m%d%H%M%S', time.localtime(time.time())) + str(time.time()).replace('.', '')[-7:]) return order_no