start.py文件
from core.src import run
if __name__ == ‘__main__‘:
run()
conf目录下settings.p
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
LOG_PATH= r‘%s\logs\access.log‘ % BASE_DIR
USER_DATA_PATH=os.path.join(BASE_DIR,‘db‘,‘user_data‘)
core目录下src.py
import os
import json
import time
user_data = {
‘username‘: None,
‘is_login‘: False
}
from conf import settings
from lib import common
def register():
print(‘注册‘.center(50, ‘*‘))
if user_data[‘is_login‘]:
print(‘已经登录‘)
return
while True:
username = input(‘请输入用户名‘).strip()
path = settings.BASE_DIR
path_file = os.path.join(path, ‘db‘, ‘%s.json‘ % username)
if os.path.exists(path_file):
print(‘用户已存在,请重新输入‘)
continue
password = input(‘请输入密码‘).strip()
user_dic = {‘username‘: username, ‘password‘: password, ‘balance‘: 15000, ‘locked‘: False, ‘flow‘: [],
‘shopping_cart‘: {}}
with open(path_file, ‘w‘, encoding=‘utf-8‘)as f:
json.dump(user_dic, f)
print(‘注册成功‘)
break
from conf import settings
def login():
print(‘登录‘.center(50, ‘*‘))
count = 0
if user_data[‘is_login‘]:
print(‘已经登录‘)
return
while True:
username = input(‘请输入用户名:>>‘).strip()
path = settings.BASE_DIR
path_file = os.path.join(path, ‘db‘, ‘%s.json‘ % username)
if not os.path.exists(path_file):
print(‘用户不存在,请重新输入‘)
continue
with open(path_file, ‘r‘, encoding=‘utf-8‘)as f:
data = json.load(f)
if data.get(‘locked‘):
print(‘用户账号已经锁定,请联系管理员‘)
break
password = input(‘请输入密码‘).strip()
if password == data.get(‘password‘):
print(‘登录成功‘)
user_data[‘username‘] = username
user_data[‘is_login‘] = True
break
else:
print(‘密码错误‘)
count += 1
if count == 3:
data[‘locked‘] = True
with open(path_file, ‘w‘, encoding=‘utf-8‘)as f:
json.dump(data, f)
@common.outter
def transfer():
print(‘转账‘.center(50, ‘*‘))
while True:
to_user = input(‘请输入转账的账户用户名:>>‘).strip()
path = settings.BASE_DIR
to_path_file = os.path.join(path, ‘db‘, ‘%s.json‘ % to_user)
if not os.path.exists(to_path_file):
print(‘用户不存在‘)
continue
transfer_money = int(input(‘请输入转账金额:>>‘).strip())
from_path_file = os.path.join(path, ‘db‘, ‘%s.json‘ % user_data[‘username‘])
with open(from_path_file, ‘r‘, encoding=‘utf-8‘)as f:
from_user_dic = json.load(f)
if from_user_dic[‘balance‘] >= transfer_money:
with open(to_path_file, ‘r‘, encoding=‘utf-8‘) as f1:
to_user_dic = json.load(f1)
to_user_dic[‘balance‘] += transfer_money
to_user_dic[‘flow‘].append(‘收到了%s的一笔转账%s元‘ %(from_user_dic[‘username‘], transfer_money))
with open(to_path_file, ‘w‘, encoding=‘utf-8‘) as f2:
json.dump(to_user_dic, f2, ensure_ascii=False)
from_user_dic[‘balance‘] -= transfer_money
from_user_dic[‘flow‘].append(‘向%s转了一笔钱%s元‘ % (to_user_dic[‘username‘], transfer_money))
with open(from_path_file, ‘w‘, encoding=‘utf-8‘) as f3:
json.dump(from_user_dic, f3, ensure_ascii=False)
print("转账成功")
break
else:
print("没钱")
@common.outter
def withdraw():
print(‘提现‘.center(50, ‘*‘))
while True:
money = input(‘请输入提现金额:>>‘).strip()
if not money: continue
if not money.isdigit():
print(‘输入金额不是数字‘)
continue
money = int(money)
path = settings.BASE_DIR
path_file = os.path.join(path, ‘db‘, ‘%s.json‘ % user_data[‘username‘])
with open(path_file, ‘r‘, encoding=‘utf-8‘)as f:
user_dic = json.load(f)
now = time.strftime(‘%Y-%m-%d %X‘)
if user_dic[‘balance‘] >= money * 1.05:
user_dic[‘balance‘] -= money * 1.05
user_dic[‘flow‘].append(‘%s在%s提现了%s元‘ % (user_data[‘username‘], now, money))
with open(path_file, ‘w‘, encoding=‘utf-8‘)as f:
json.dump(user_dic, f, ensure_ascii=False)
print(‘恭喜提现成功‘)
break
else:
print(‘提现失败,请检查金额‘)
continue
@common.outter
def check_balance():
print(‘查看余额‘.center(50, ‘*‘))
while True:
money = input(‘请输入需要查询用户的用户名:>>‘).strip()
path = settings.BASE_DIR
path_file = os.path.join(path, ‘db‘, ‘%s.json‘ % money)
if not os.path.exists(path_file):
print(‘用户不存在‘)
continue
path = settings.BASE_DIR
path_file = os.path.join(path, ‘db‘, ‘%s.json‘ % user_data[‘username‘])
with open(path_file, ‘r‘, encoding=‘utf-8‘)as f:
user_dic = json.load(f)
user_dic[‘flow‘].append(‘%s查询余额‘ % user_data[‘username‘])
print(user_dic[‘balance‘])
break
@common.outter
def pay():
print(‘充值‘.center(50, ‘*‘))
while True:
money = input(‘请输入充值金额‘)
if not money.isdigit():
print(‘输入金额不是数字‘)
continue
money = int(money)
path = settings.BASE_DIR
path_file = os.path.join(path, ‘db‘, ‘%s.json‘ % user_data[‘username‘])
if not os.path.exists(path_file):
print(‘用户不存在,请重新输入‘)
continue
with open(path_file, ‘r‘, encoding=‘utf-8‘)as f:
user_dic = json.load(f)
now = time.strftime(‘%Y-%m-%d %X‘)
user_dic[‘balance‘] += money
user_dic[‘flow‘].append(‘%s在%s充值了%s元‘ % (user_data[‘username‘], now, money))
with open(path_file, ‘w‘, encoding=‘utf-8‘)as f:
json.dump(user_dic, f, ensure_ascii=False)
print(‘充值成功‘)
break
@common.outter
def check_flow():
print(‘查看流水‘.center(50, ‘*‘))
while True:
money = input(‘请输入需要查询用户的用户名:>>‘).strip()
path = settings.BASE_DIR
path_file = os.path.join(path, ‘db‘, ‘%s.json‘ % money)
if not os.path.exists(path_file):
print(‘用户不存在‘)
continue
path = settings.BASE_DIR
path_file = os.path.join(path, ‘db‘, ‘%s.json‘ % user_data[‘username‘])
with open(path_file, ‘r‘, encoding=‘utf-8‘)as f:
user_dic = json.load(f)
print(user_dic[‘flow‘])
break
@common.outter
def add_cart():
print(‘加入购物车‘.center(50, ‘*‘))
shopping_cart = {}
while True:
goods = [
[‘xxa‘, 100],
[‘xbb‘, 200],
[‘xxc‘, 300]
]
for k, v in enumerate(goods):
print(‘%s %s %s‘ % (k, v[0], v[1]))
num = input(‘请输入序号:>>(q to quit)‘)
if num.isdigit():
num = int(num)
goods_name = goods[num][0]
goods_price = goods[num][1]
if goods_name not in shopping_cart:
shopping_cart[goods_name] = {‘price‘: goods_price, ‘count‘: 1}
else:
shopping_cart[goods_name][‘count‘] += 1
print(‘请继续购物‘)
elif num == ‘q‘:
if shopping_cart:
path = settings.BASE_DIR
path_file = os.path.join(path, ‘db‘, ‘%s.json‘ % user_data[‘username‘])
with open(path_file, ‘r‘, encoding=‘utf-8‘)as f:
user_dic = json.load(f)
user_dic[‘shopping_cart‘] = shopping_cart
with open(path_file, ‘w‘, encoding=‘utf-8‘)as f:
json.dump(user_dic, f, ensure_ascii=False)
print(‘欢迎下次再来‘)
break
else:
print(‘输入非法数字,请重新输入‘)
@common.outter
def check_shop_car():
print(‘查看购物车‘.center(50, ‘*‘))
while True:
money = input(‘请输入需要查询用户的用户名:>>‘).strip()
path = settings.BASE_DIR
path_file = os.path.join(path, ‘db‘, ‘%s.json‘ % money)
if not os.path.exists(path_file):
print(‘用户不存在‘)
continue
path = settings.BASE_DIR
path_file = os.path.join(path, ‘db‘, ‘%s.json‘ % user_data[‘username‘])
with open(path_file, ‘r‘, encoding=‘utf-8‘)as f:
user_dic = json.load(f)
print(user_dic[‘shopping_cart‘])
break
func_dic = {
‘0‘: [‘注册‘, register],
‘1‘: [‘登录‘, login],
‘2‘: [‘转账‘, transfer],
‘3‘: [‘提现‘, withdraw],
‘4‘: [‘查看余额‘, check_balance],
‘5‘: [‘充值‘, pay],
‘6‘: [‘查看流水‘, check_flow],
‘7‘: [‘加入购物车‘, add_cart],
‘8‘: [‘查看购物车‘, check_shop_car]
}
def run():
while True:
for k in func_dic:
print(k, func_dic[k][0])
choice = input(‘请输入对应指令:>>‘).strip()
if choice not in func_dic:
print(‘输入错误指令,请重新输入‘)
continue
else:
print(func_dic[choice][1]())
lib目录下common.py
from core import src
def outter(func):
def wrapper(*args,**kwargs):
if src.user_data[‘is_login‘]:
res= func(*args,**kwargs)
return res
else:
src.login()
return wrapper