Commit 9c59730d by Aeolus

update

parent 33c765f0
......@@ -8,9 +8,10 @@ from config.base_config import MONGO_DATABASE_URI
from config.commen_config import LOGIN_TYPE
from models.base_model import db
from models.models import TallymanAccount, TallymanMachine, TallymanLoginRecord
from models.models import TallymanAccount, TallymanMachine, TallymanLoginRecord, Machine
from service.tallyman_service import TallymanService
from utils.error_code import TALLYMAN_ACCOUNT_EXIST, PHONE_NOT_VALID_ERROR, TOKEN_NOT_VALID_ERROR, PASSWORD_ERROR
from utils.error_code import TALLYMAN_ACCOUNT_EXIST, PHONE_NOT_VALID_ERROR, TOKEN_NOT_VALID_ERROR, PASSWORD_ERROR, \
TALLYMAN_ACCOUNT_NOT_EXIST
from utils.jwt_util import verify_jwt, generate_jwt
from utils.my_response import BaseResponse
......@@ -28,31 +29,79 @@ def test():
@tallyman_route.route('/edit_password', methods=['GET', 'POST'])
def run_tallyman_edit_password():
json_data = request.get_json()
user_name = json_data.get('name', None)
password = json_data.get('password', None)
tallyman_info = g.user
if not tallyman_info:
return BaseResponse(**ACCOUNT_NOT_EXISTS_ERROR)
return jsonify(TALLYMAN_ACCOUNT_NOT_EXIST)
if password:
tallyman_info.password = password
db.session.add(tallyman_info)
db.session.commit()
return BaseResponse()
@tallyman_route.route('/edit_account', methods=['GET', 'POST'])
def run_tallyman_edit_account():
admin = g.user
if g.user.level != 1:
return jsonify({"error_code": "500", "error_message": "没有权限"})
json_data = request.get_json()
phone = json_data.get('phone', None)
user_name = json_data.get('name', None)
password = json_data.get('password', None)
machine_list = json_data.get('machine_list', None)
tallyman_info = TallymanAccount.query.filter_by(phone=phone).first()
if not tallyman_info:
return jsonify(TALLYMAN_ACCOUNT_NOT_EXIST)
if user_name:
tallyman_info.user_name = user_name
if password:
tallyman_info.password = password
db.session.add(tallyman_info)
db.session.commit()
if not machine_list:
return BaseResponse()
old_machine_list = Machine.query.join(TallymanMachine,
TallymanMachine.machine_no == Machine.machine_no).filter(
TallymanMachine.user_id == tallyman_info.id).all()
old_machine_dict = {}
for i in old_machine_list:
old_machine_dict[i.machine_no] = i
for i in machine_list:
if old_machine_dict.get(i):
old_machine_dict[i].status = 1
db.session.add(old_machine_dict[i])
else:
model = TallymanMachine()
model.user_id = tallyman_info.id
model.machine_no = i
model.status = 1
db.session.add(model)
db.session.commit()
return BaseResponse()
@tallyman_route.route('/add_account', methods=['GET', 'POST'])
def add_user():
admin = g.user
if g.user.level != 1:
return jsonify({"error_code": "500", "error_message": "没有权限"})
json_data = request.get_json()
user_name = json_data['name'] if 'name' in json_data else 'SSW'
phone = json_data['phone'] if 'phone' in json_data else None
level = int(json_data['level']) if 'level' in json_data else 2
password = json_data['password'] if 'password' in json_data else None
comment = json_data['comment'] if 'comment' in json_data else ''
machine_list = json_data.get("machine_list", [])
tallyman = TallymanAccount.query.filter_by(phone=phone).first()
if tallyman:
......@@ -78,7 +127,6 @@ def add_user():
db.session.add(tallyman)
db.session.commit()
machine_list = json_data.get("machine_list", [])
if not machine_list:
return BaseResponse()
......@@ -101,14 +149,14 @@ def delete_user():
tallyman = TallymanAccount.query.filter_by(phone=phone).first()
if not tallyman:
return BaseResponse()
tallyman.status = -1
db.session.add(tallyman)
agent_spot_info = TallymanMachine.query.filter_by(user_id=tallyman.id).all()
for info in agent_spot_info:
info.status = -1
db.session.add(info)
tallyman.status = -1
db.session.add(tallyman)
db.session.commit()
return BaseResponse()
......@@ -155,7 +203,16 @@ def run_tallyman_login():
@tallyman_route.route('/machine_list', methods=['GET', 'POST'])
def get_tallyman_machine_list():
machine_info = TallymanService.get_machine_list(g.user)
return BaseResponse(data=machine_info)
@tallyman_route.route('/machine_info', methods=['GET', 'POST'])
def get_agent_module_list():
machine_info = TallymanService.get_machine_info(g.user)
json_data = request.get_json()
machine_no = json_data["machine_no"]
machine_info = TallymanService.get_machine_info(g.user, machine_no)
return BaseResponse(data=machine_info)
......@@ -8,10 +8,14 @@ logger = logging.getLogger(__name__)
class TallymanService(object):
@classmethod
def get_machine_info(cls, tallyman):
machine_infos = Machine.query(Machine).join(TallymanMachine,
TallymanMachine.machine_no == Machine.machine_no).filter(
user_id=tallyman.id, status=1).all()
def get_machine_list(cls, tallyman):
machine_infos = Machine.query.join(TallymanMachine,
TallymanMachine.machine_no == Machine.machine_no).filter(
TallymanMachine.user_id == tallyman.id, TallymanMachine.status == 1).all()
# machine_infos = db.session.query(Machine).join(TallymanMachine,
# TallymanMachine.machine_no == Machine.machine_no).filter(
# TallymanMachine.user_id == tallyman.id, TallymanMachine.status == 1).all()
return_data = []
for tmp_machine in machine_infos:
......@@ -24,3 +28,16 @@ class TallymanService(object):
Hatch.status == 2).count()
return_data.append(cur_machine)
return return_data
@classmethod
def get_machine_info(cls, tallyman, machine_no):
machine_info = Machine.query.join(
TallymanMachine,
TallymanMachine.machine_no == Machine.machine_no
).filter(
TallymanMachine.user_id == tallyman.id, TallymanMachine.status == 1,
Machine.machine_no == machine_no
).first()
if not machine_info:
return None
#!usr/bin/.env python # -*- coding:utf-8 _*- """ @version: author:Aeolus @time: 2021/03/30 @file: jwt_util.py @function: @modify: """ import jwt from flask import current_app def generate_jwt(payload, expiry, secret=None): """ 生成jwt :param payload: dict 载荷 :param expiry: datetime 有效期 :param secret: 密钥 :return: jwt """ _payload = {'exp': expiry} _payload.update(payload) if not secret: secret = current_app.config['SECRET_KEY'] token = jwt.encode(_payload, secret, algorithm='HS256') return token def verify_jwt(token, secret=None): """ 检验jwt :param token: jwt :param secret: 密钥 :return: dict: payload """ if not secret: secret = current_app.config['SECRET_KEY'] try: payload = jwt.decode(token, secret, algorithms=['HS256']) except jwt.PyJWTError: payload = None return payload if __name__ == '__main__': import time from config.env_path_config import env_path from dotenv import load_dotenv load_dotenv(dotenv_path=env_path, verbose=True, override=True) import os SECRET_KEY = os.getenv('SECRET_KEY') token = generate_jwt({"user_id": 1}, time.time() + 6000, SECRET_KEY) # token = generate_jwt({"user_no": 'SK000001'}, time.time() + 6000, SECRET_KEY) print(token) # for i in range(10): # result = verify_jwt(token, 'secret') # print(result) # print(time.time()) # time.sleep(1)
\ No newline at end of file
#!usr/bin/.env python # -*- coding:utf-8 _*- """ @version: author:Aeolus @time: 2021/03/30 @file: jwt_util.py @function: @modify: """ import jwt from flask import current_app def generate_jwt(payload, expiry, secret=None): """ 生成jwt :param payload: dict 载荷 :param expiry: datetime 有效期 :param secret: 密钥 :return: jwt """ _payload = {'exp': expiry} _payload.update(payload) if not secret: secret = current_app.config['SECRET_KEY'] token = jwt.encode(_payload, secret, algorithm='HS256') return token def verify_jwt(token, secret=None): """ 检验jwt :param token: jwt :param secret: 密钥 :return: dict: payload """ if not secret: secret = current_app.config['SECRET_KEY'] try: payload = jwt.decode(token, secret, algorithms=['HS256']) except jwt.PyJWTError: payload = None return payload if __name__ == '__main__': import time from config.env_path_config import env_path from dotenv import load_dotenv load_dotenv(dotenv_path=env_path, verbose=True, override=True) import os SECRET_KEY = os.getenv('SECRET_KEY') # token = generate_jwt({"user_id": 1}, time.time() + 6000, SECRET_KEY) token = generate_jwt({"user_no": 'SK000007'}, time.time() + 6000, SECRET_KEY) print(token) # for i in range(10): # result = verify_jwt(token, 'secret') # print(result) # print(time.time()) # time.sleep(1)
\ No newline at end of file
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment