Commit 9c59730d by Aeolus

update

parent 33c765f0
...@@ -8,9 +8,10 @@ from config.base_config import MONGO_DATABASE_URI ...@@ -8,9 +8,10 @@ from config.base_config import MONGO_DATABASE_URI
from config.commen_config import LOGIN_TYPE from config.commen_config import LOGIN_TYPE
from models.base_model import db 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 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.jwt_util import verify_jwt, generate_jwt
from utils.my_response import BaseResponse from utils.my_response import BaseResponse
...@@ -28,31 +29,79 @@ def test(): ...@@ -28,31 +29,79 @@ def test():
@tallyman_route.route('/edit_password', methods=['GET', 'POST']) @tallyman_route.route('/edit_password', methods=['GET', 'POST'])
def run_tallyman_edit_password(): def run_tallyman_edit_password():
json_data = request.get_json() json_data = request.get_json()
user_name = json_data.get('name', None)
password = json_data.get('password', None) password = json_data.get('password', None)
tallyman_info = g.user tallyman_info = g.user
if not tallyman_info: 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: if user_name:
tallyman_info.user_name = user_name tallyman_info.user_name = user_name
if password: if password:
tallyman_info.password = password tallyman_info.password = password
db.session.add(tallyman_info) db.session.add(tallyman_info)
db.session.commit() 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() return BaseResponse()
@tallyman_route.route('/add_account', methods=['GET', 'POST']) @tallyman_route.route('/add_account', methods=['GET', 'POST'])
def add_user(): def add_user():
admin = g.user
if g.user.level != 1:
return jsonify({"error_code": "500", "error_message": "没有权限"})
json_data = request.get_json() json_data = request.get_json()
user_name = json_data['name'] if 'name' in json_data else 'SSW' user_name = json_data['name'] if 'name' in json_data else 'SSW'
phone = json_data['phone'] if 'phone' in json_data else None phone = json_data['phone'] if 'phone' in json_data else None
level = int(json_data['level']) if 'level' in json_data else 2 level = int(json_data['level']) if 'level' in json_data else 2
password = json_data['password'] if 'password' in json_data else None password = json_data['password'] if 'password' in json_data else None
comment = json_data['comment'] if 'comment' in json_data else '' 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() tallyman = TallymanAccount.query.filter_by(phone=phone).first()
if tallyman: if tallyman:
...@@ -78,7 +127,6 @@ def add_user(): ...@@ -78,7 +127,6 @@ def add_user():
db.session.add(tallyman) db.session.add(tallyman)
db.session.commit() db.session.commit()
machine_list = json_data.get("machine_list", [])
if not machine_list: if not machine_list:
return BaseResponse() return BaseResponse()
...@@ -101,14 +149,14 @@ def delete_user(): ...@@ -101,14 +149,14 @@ def delete_user():
tallyman = TallymanAccount.query.filter_by(phone=phone).first() tallyman = TallymanAccount.query.filter_by(phone=phone).first()
if not tallyman: if not tallyman:
return BaseResponse() return BaseResponse()
tallyman.status = -1
db.session.add(tallyman)
agent_spot_info = TallymanMachine.query.filter_by(user_id=tallyman.id).all() agent_spot_info = TallymanMachine.query.filter_by(user_id=tallyman.id).all()
for info in agent_spot_info: for info in agent_spot_info:
info.status = -1 info.status = -1
db.session.add(info) db.session.add(info)
tallyman.status = -1
db.session.add(tallyman)
db.session.commit() db.session.commit()
return BaseResponse() return BaseResponse()
...@@ -155,7 +203,16 @@ def run_tallyman_login(): ...@@ -155,7 +203,16 @@ def run_tallyman_login():
@tallyman_route.route('/machine_list', methods=['GET', 'POST']) @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(): 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) return BaseResponse(data=machine_info)
...@@ -8,10 +8,14 @@ logger = logging.getLogger(__name__) ...@@ -8,10 +8,14 @@ logger = logging.getLogger(__name__)
class TallymanService(object): class TallymanService(object):
@classmethod @classmethod
def get_machine_info(cls, tallyman): def get_machine_list(cls, tallyman):
machine_infos = Machine.query(Machine).join(TallymanMachine, machine_infos = Machine.query.join(TallymanMachine,
TallymanMachine.machine_no == Machine.machine_no).filter( TallymanMachine.machine_no == Machine.machine_no).filter(
user_id=tallyman.id, status=1).all() 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 = [] return_data = []
for tmp_machine in machine_infos: for tmp_machine in machine_infos:
...@@ -24,3 +28,16 @@ class TallymanService(object): ...@@ -24,3 +28,16 @@ class TallymanService(object):
Hatch.status == 2).count() Hatch.status == 2).count()
return_data.append(cur_machine) return_data.append(cur_machine)
return return_data 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 @file: error_code.py """ ### 通用错误相关 Param_Invalid_Error = { "error_code": "500", "error_message": "params is invalid, 参数无效" } TOKEN_NOT_VALID_ERROR = { "error_code": "1001", "error_message": "无效的token" } TOKEN_NOT_PROVIDED_ERROR = { "error_code": "1002", "error_message": "token未提供" } TOKEN_EXPIRE_ERROR = { "error_code": "1003", "error_message": "token超时" } PHONE_NOT_BINDING_ERROR = { "error_code": "1004", "error_message": "未绑定手机号" } PHONE_NOT_NULL_ERROR = { "error_code": "1005", "error_message": "手机号为空" } PHONE_NOT_VALID_ERROR = { "error_code": "1006", "error_message": "无效的手机号" } USER_ALREADY_REGISTER_ERROR = { "error_code": "1007", "error_message": "用户已注册" } VERIFICATION_CODE_NULL_ERROR = { "error_code": "1008", "error_message": "验证码为空" } VERIFICATION_CODE_INVALID_ERROR = { "error_code": "1009", "error_message": "验证码已失效" } VERIFICATION_CODE_ERROR = { "error_code": "1010", "error_message": "验证码错误" } PASSWORD_ERROR = { "error_code": "1011", "error_message": "账号或密码错误" } ## 微信登陆相关 WX_LOGIN_DATA_ERROR = { "error_code": "3001", "error_message": "微信登录数据错误" } WX_LOGIN_CODE_ERROR = { "error_code": "3002", "error_message": "微信登录code值错误" } WX_OPENID_NOT_GET_ERROR = { "error_code": "3003", "error_message": "微信OpenId获取失败,请刷新重试" } WX_SESSION_KEY_ERROR = { "error_code": "3004", "error_message": "session key error" } ### 微信支付相关 WE_MINIAPP_PAY_FAIL = { "error_code": "3101", "error_message": "小程序下单失败" } ### 消息推送相关 WXBizMsgCrypt_OK = { "error_code": "0", "error_message": "WXBizMsgCrypt_OK" } WXBizMsgCrypt_ValidateSignature_Error = { "error_code": "4001", "error_message": "验证签名错误" } WXBizMsgCrypt_ParseXml_Error = { "error_code": "4002", "error_message": "解析xml错误" } WXBizMsgCrypt_ComputeSignature_Error = { "error_code": "4003", "error_message": "计算签名错误" } WXBizMsgCrypt_IllegalAesKey = { "error_code": "4004", "error_message": "Aes key非法错误" } WXBizMsgCrypt_ValidateAppid_Error = { "error_code": "4005", "error_message": "appid错误" } WXBizMsgCrypt_EncryptAES_Error = { "error_code": "4006", "error_message": "aes加密错误" } WXBizMsgCrypt_DecryptAES_Error = { "error_code": "4007", "error_message": "aes解密错误" } WXBizMsgCrypt_IllegalBuffer = { "error_code": "4008", "error_message": "illegal buffer" } WXBizMsgCrypt_EncodeBase64_Error = { "error_code": "4009", "error_message": "base64加密错误" } WXBizMsgCrypt_DecodeBase64_Error = { "error_code": "4010", "error_message": "base64解密错误" } WXBizMsgCrypt_GenReturnXml_Error = { "error_code": "4011", "error_message": "gen return xml error" } MACHINE_NOT_EXIST_ERROR = { "error_code": '5001', "error_message": "机柜不存在" } MACHINE_IS_USE_ERROR = { "error_code": '5002', "error_message": "已有他人正在租借中,请稍后" } MACHINE_IS_NOT_ONLINE_ERROR = { "error_code": '5003', "error_message": "机柜不在线" } MACHINE_ADD_ERROR = { "error_code": '5004', "error_message": "机柜添加失败" } MACHINE_NO_DUPLICATE_ERROR = { "error_code": '5005', "error_message": "machine_no duplicate,机柜编号重复" } MACHINE_EDIT_ERROR = { "error_code": '5006', "error_message": "machine edit error, 机柜修改错误" } HATCH_NOT_EXIST_ERROR = { "error_code": "5007", "error_message": "no hatch, 没有商品信息" } HATCH_NOT_ALL_EXIST_ERROR = { "error_code": "5008", "error_message": "no all hatch, 存在已售出商品" } HATCH_COUNT_ERROR = { "error_code": "5009", "error_message": "hatch count error, 商品数量错误,检查数量" } ### 订单相关 RENT_ORDER_NOT_BACK_ERROR = { "error_code": '6101', "error_message": "有未归还的订单" } RENT_ORDER_NOT_TAKE_ERROR = { "error_code": '6102', "error_message": "有未取货的订单" } RENT_ORDER_NUMBER_MAX = { "error_code": '6103', "error_message": "订单数量达到上限" } TAKE_CODE_NOT_VALID = { "error_code": '6104', "error_message": "取货码错误请确认手机号及取货码是否匹配" } CODE_CANCEL_ERROR = { "error_code": '6105', "error_message": "取货码已取消" } CODE_USED_ERROR = { "error_code": '6108', "error_message": "取货码已使用" } NO_POWER_ERROR = { "error_code": '6106', "error_message": "没有可租借设备" } NO_RENT_RECORD = { "error_code": '6107', "error_message": "订单不存在" } CODE_USED_ERROR = { "error_code": '6108', "error_message": "取货码已使用" } RENT_ORDER_NUMBER_LIMIT = { "error_code": '6109', "error_message": "机柜只允许租借一台" } REFUND_NOT_RENT_INFO = { "error_code": "6301", "error_message": "没有该订单信息" } REFUND_BACK_TIME_ERROR = { "error_code": "6302", "error_message": "归还时间异常" } REFUND_NOT_PRODUCTION_INFO = { "error_code": "6303", "error_message": "没有该讲解器信息" } REFUND_MONEY_IS_ZERO = { "error_code": "6304", "error_message": "退款金额为零" } REFUND_NO_DUPLICATE = { "error_code": "6305", "error_message": "退款单号重复" } TALLYMAN_ACCOUNT_EXIST = { "error_code": "7001", "error_message": "tallyman account exist, 补货员账号已存在" } #!usr/bin/env python # -*- coding:utf-8 _*- """ @version: author:Aeolus @file: error_code.py """ ### 通用错误相关 Param_Invalid_Error = { "error_code": "500", "error_message": "params is invalid, 参数无效" } TOKEN_NOT_VALID_ERROR = { "error_code": "1001", "error_message": "无效的token" } TOKEN_NOT_PROVIDED_ERROR = { "error_code": "1002", "error_message": "token未提供" } TOKEN_EXPIRE_ERROR = { "error_code": "1003", "error_message": "token超时" } PHONE_NOT_BINDING_ERROR = { "error_code": "1004", "error_message": "未绑定手机号" } PHONE_NOT_NULL_ERROR = { "error_code": "1005", "error_message": "手机号为空" } PHONE_NOT_VALID_ERROR = { "error_code": "1006", "error_message": "无效的手机号" } USER_ALREADY_REGISTER_ERROR = { "error_code": "1007", "error_message": "用户已注册" } VERIFICATION_CODE_NULL_ERROR = { "error_code": "1008", "error_message": "验证码为空" } VERIFICATION_CODE_INVALID_ERROR = { "error_code": "1009", "error_message": "验证码已失效" } VERIFICATION_CODE_ERROR = { "error_code": "1010", "error_message": "验证码错误" } PASSWORD_ERROR = { "error_code": "1011", "error_message": "账号或密码错误" } ## 微信登陆相关 WX_LOGIN_DATA_ERROR = { "error_code": "3001", "error_message": "微信登录数据错误" } WX_LOGIN_CODE_ERROR = { "error_code": "3002", "error_message": "微信登录code值错误" } WX_OPENID_NOT_GET_ERROR = { "error_code": "3003", "error_message": "微信OpenId获取失败,请刷新重试" } WX_SESSION_KEY_ERROR = { "error_code": "3004", "error_message": "session key error" } ### 微信支付相关 WE_MINIAPP_PAY_FAIL = { "error_code": "3101", "error_message": "小程序下单失败" } ### 消息推送相关 WXBizMsgCrypt_OK = { "error_code": "0", "error_message": "WXBizMsgCrypt_OK" } WXBizMsgCrypt_ValidateSignature_Error = { "error_code": "4001", "error_message": "验证签名错误" } WXBizMsgCrypt_ParseXml_Error = { "error_code": "4002", "error_message": "解析xml错误" } WXBizMsgCrypt_ComputeSignature_Error = { "error_code": "4003", "error_message": "计算签名错误" } WXBizMsgCrypt_IllegalAesKey = { "error_code": "4004", "error_message": "Aes key非法错误" } WXBizMsgCrypt_ValidateAppid_Error = { "error_code": "4005", "error_message": "appid错误" } WXBizMsgCrypt_EncryptAES_Error = { "error_code": "4006", "error_message": "aes加密错误" } WXBizMsgCrypt_DecryptAES_Error = { "error_code": "4007", "error_message": "aes解密错误" } WXBizMsgCrypt_IllegalBuffer = { "error_code": "4008", "error_message": "illegal buffer" } WXBizMsgCrypt_EncodeBase64_Error = { "error_code": "4009", "error_message": "base64加密错误" } WXBizMsgCrypt_DecodeBase64_Error = { "error_code": "4010", "error_message": "base64解密错误" } WXBizMsgCrypt_GenReturnXml_Error = { "error_code": "4011", "error_message": "gen return xml error" } MACHINE_NOT_EXIST_ERROR = { "error_code": '5001', "error_message": "机柜不存在" } MACHINE_IS_USE_ERROR = { "error_code": '5002', "error_message": "已有他人正在租借中,请稍后" } MACHINE_IS_NOT_ONLINE_ERROR = { "error_code": '5003', "error_message": "机柜不在线" } MACHINE_ADD_ERROR = { "error_code": '5004', "error_message": "机柜添加失败" } MACHINE_NO_DUPLICATE_ERROR = { "error_code": '5005', "error_message": "machine_no duplicate,机柜编号重复" } MACHINE_EDIT_ERROR = { "error_code": '5006', "error_message": "machine edit error, 机柜修改错误" } HATCH_NOT_EXIST_ERROR = { "error_code": "5007", "error_message": "no hatch, 没有商品信息" } HATCH_NOT_ALL_EXIST_ERROR = { "error_code": "5008", "error_message": "no all hatch, 存在已售出商品" } HATCH_COUNT_ERROR = { "error_code": "5009", "error_message": "hatch count error, 商品数量错误,检查数量" } ### 订单相关 RENT_ORDER_NOT_BACK_ERROR = { "error_code": '6101', "error_message": "有未归还的订单" } RENT_ORDER_NOT_TAKE_ERROR = { "error_code": '6102', "error_message": "有未取货的订单" } RENT_ORDER_NUMBER_MAX = { "error_code": '6103', "error_message": "订单数量达到上限" } TAKE_CODE_NOT_VALID = { "error_code": '6104', "error_message": "取货码错误请确认手机号及取货码是否匹配" } CODE_CANCEL_ERROR = { "error_code": '6105', "error_message": "取货码已取消" } CODE_USED_ERROR = { "error_code": '6108', "error_message": "取货码已使用" } NO_POWER_ERROR = { "error_code": '6106', "error_message": "没有可租借设备" } NO_RENT_RECORD = { "error_code": '6107', "error_message": "订单不存在" } CODE_USED_ERROR = { "error_code": '6108', "error_message": "取货码已使用" } RENT_ORDER_NUMBER_LIMIT = { "error_code": '6109', "error_message": "机柜只允许租借一台" } REFUND_NOT_RENT_INFO = { "error_code": "6301", "error_message": "没有该订单信息" } REFUND_BACK_TIME_ERROR = { "error_code": "6302", "error_message": "归还时间异常" } REFUND_NOT_PRODUCTION_INFO = { "error_code": "6303", "error_message": "没有该讲解器信息" } REFUND_MONEY_IS_ZERO = { "error_code": "6304", "error_message": "退款金额为零" } REFUND_NO_DUPLICATE = { "error_code": "6305", "error_message": "退款单号重复" } TALLYMAN_ACCOUNT_EXIST = { "error_code": "7001", "error_message": "tallyman account exist, 补货员账号已存在" } TALLYMAN_ACCOUNT_NOT_EXIST = { "error_code": "7002", "error_message": "tallyman account not exist, 补货员账号不存在" }
\ No newline at end of file \ 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": '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) #!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 \ 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