# 导入模块
import os # 系统模块
from datetime import * # 时间模块
import pymysql # 数据库模块
import qrcode # 支付二维码模块
# 定义变量
is_login = False # 定义登录状态
info = [] # 记录登录后信息
sql = '' # sql语句
cursor = '' # 游标
connect = '' # 数据库连接参数
# ============================================= 主模块 =============================================#
# 启动
def start(): # :start启动
tag_map = { # 定义启动选项的字典
'0': exit, # 退出
'1': login, # 登录
'2': register, # 注册
'3': forget_pwd # 忘记密码
}
while True:
os.system('cls')
os.system('color 3E&title 欢迎使用购物车系统 V2.0')
os.system('mode con cols=75 lines=25')
print('''
【================== 欢迎使用淘宝系统 ==================】
请选择功能:
0.退出
1.登录
2.注册
3.忘记密码
''')
tag = input('请选择====>>').strip()
tag_map[tag]() if (tag in tag_map) else print('您输入有误,请重新输入!')
# ==================================== 1.登录 ====================================#
# ====================定制数据库连接 connect ====================##
def connect_sql(): # 连接数据库
global cursor, connect
try:
host = '106.75.31.89'
user = 'root'
password = '123456'
data = 'data'
port = 3306
connect = pymysql.connect(host, user, password, data, port, charset='utf8') # 数据库连接参数
cursor = connect.cursor() # 获取一个游标
print('连接成功!')
except Exception:
print('发生异常!')
exit()
# =============================获取文件=========================##
def get_file(usr): # 获取登录用户的相关文件,返回取到的data数据
global sql, cursor
sql = 'select *from info where usr ="%s"' % usr
cursor.execute(sql)
data = cursor.fetchone()
return data
# 执行sql语句
def execute_sql(sql):
global cursor, connect
cursor.execute(sql)
connect.commit()
# ========================获取用户名===========================##
def get_usr(): # get获取 usr用户名 输入用户名,判断,返回
while True:
usr = input('请输入用户名====>>').strip()
if usr == '0': start()
if not ((usr.isalpha() or usr.isnumeric()) and len(usr) >= 3):
print('用户名输入错误')
else:
break
get_info(usr)
return usr
# 获取新的info登录信息
def get_info(usr):
global info
info = get_file(usr)
return get_file(usr)
# ========================验证用户名===========================#
def check_usr(): # check 核对 usr 用户名
global info
usr = get_usr()
if not info:
input('没有此用户,请注册,按任意键返回。。。')
start() #有问题
return usr
# ==========================获取密码==========================##
def get_pwd(usr): # get 取得 pwd 密码
while True:
pwd = input('请输入密 码====>>').strip()
if not len(pwd) >= 3:
print('密码格式输入错误!')
else:
return pwd
# =====================获取锁定相差时间=======================#
# 获取当前时间
def at_time():
tim = datetime.now().strftime('%Y-%m-%d %H:%M:%S') # 当前时间
tim1 = datetime.strptime(tim, '%Y-%m-%d %H:%M:%S') # 转换统一格式
return tim1
# 获取锁定相差时间
def time_n(usr):
tim1 = at_time()
tim2 = datetime.strptime(get_file(usr)[6], '%Y-%m-%d %H:%M:%S') # 存在数据库时间格式
n = tim1 - tim2
n = 600 - n.seconds # 差额为秒
return n
# ==================判断用户是否锁定状态=====================#
# 判断用户是否锁定状态,时间到达10分钟后自定解锁
def lock_NY(usr):
n = time_n(usr)
if int(get_file(usr)[3]) < 0:
if n > 0:
input('您的账号已被锁定,还有%s秒解除,按任意键返回。。。' % n)
return False
elif n < 0:
sql = 'update info set num=2 where usr="%s"' % (usr)
execute_sql(sql)
# ===============密码错误,写入数据库次数===================#
# 密码错误,写入数据库次数
def check_pwd(usr):
global info, cursor, sql, connect
sql = 'update info set num=(num-1) where usr="%s"' % (usr) # 错误写入数据库
execute_sql(sql) # 执行sql语句
num = int(get_file(usr)[3]) # 获取还可以输入的次数
if num < 0:
tim1 = at_time()
sql = 'update info set time="%s" where usr="%s"' % (tim1, usr) # 锁定 用户,把时间插入数据库
execute_sql(sql)
input('输入密码次数已达三次,用户将锁定10分钟!')
else:
input('密码错误,用户%s还有%s次机会!' % (usr, num + 1))
# ======================== 登录判断 ========================#
# 登录判断
def login():
# 验证密码
while True:
os.system('cls')
global is_login, info
print('''
【====================== 用户登录 ======================】
按 0 退出登录界面。。。
''')
usr = check_usr() # 获取用户名
if lock_NY(usr) == False: # 判断是否是锁定状态
start()
pwd = get_pwd(usr) # 获取密码
if pwd == info[2]:
if usr == 'admin':
admin()
else:
is_login = True
shop_start()
else:
check_pwd(usr)
# ==================================== 2.注册 ====================================#
# ======================= 获取手机号 =======================#
def get_phone(phone):
global sql, cursor
sql = 'select *from info where phone ="%s"' % phone
cursor.execute(sql)
data = cursor.fetchone()
return data
# ==========================手机号=========================#
# 注册手机号,并核对手机号是否重复
def check_phone():
while True:
phone = input('请输入手机号:')
if len(phone) == 11 and phone.isnumeric():
data = get_phone(phone)
if data:
print('您输入的手机号已被" %s "注册,请重新输入!' % data[1])
else:
return phone
else:
print('您输入的格式错误!')
# =====================验证注册用户=======================#
# 验证注册用户
def check_reg_usr():
while True:
usr = get_usr()
if not info:
print('用户未被注册,合法!')
return usr
else:
print('用户已被注册,请重新输入!')
# =======================注册用户=========================#
# 注册用户
def register():
os.system('cls')
print('''
【====================== 注册用户 ======================】
按 0 退出注册界面。。。
''')
global info
usr = check_reg_usr()
pwd = get_pwd(usr)
phone = check_phone()
tim1 = at_time()
sql = 'insert into info (usr,pwd,num,money,phone,time) values ("%s","%s","2","10000","%s","%s")' % (
usr, pwd, phone, tim1)
execute_sql(sql)
input('注册成功,按任意键返回。。。')
# ================================== 3.找回密码 ==================================#
# ===================核对找回密码手机号====================#
def check_for_phone():
global info
while True:
phone = input('请输入手机号:')
if len(phone) == 11 and phone.isnumeric():
if phone == info[5]:
break
else:
print('手机号核对错误,请重新输入!')
else:
print('您输入的格式错误!')
print('手机号核对成功!')
return phone
# =======================输入新密码========================#
# 输入新密码
def now_pwd(usr):
while True:
now_pwd1 = get_pwd(usr)
now_pwd2 = get_pwd(usr)
if now_pwd1 != now_pwd2:
print('您输入两次的密码不一致,请重新输入!')
else:
return True
# ========================找回密码=========================#
# 找回密码
def forget_pwd():
os.system('cls')
print('''
【====================== 找回密码 ======================】
按 0 退出找回界面。。。
''')
usr = check_usr()
check_for_phone()
input('找回成功!') if now_pwd(usr) == True else input('找回失败!')
# ========================================华丽丽的分割线========================================#
# =========================================启动购物系统========================================#
# 启动购物车系统
def shop_start():
os.system('cls')
print('''
【================ 欢迎使用淘宝购物系统 ================】
【 客户版 】
0.退出 4.购物车
1.查询余额 5.查看已购买过的商品
2.充值 8.注销
3.查看商品 9.修改客户密码
''')
tag_map = {
'0': exit,
'1': money,
'2': top_up,
'3': look_shop,
'4': shop_cart,
'5': bill,
'8': logout,
'9': change_pwd
}
while True:
tag = input('请选择====>>')
tag_map[tag]() if tag in tag_map else print('输入错误,请重新输入!')
# 1、查询余额
def money():
global info
get_info(info[1])
moneys = int(info[4])
if moneys < 2000:
print('您的余额还有 %s 元,请及时充值,以免影响您的购物!' % moneys)
input('按任意键继续。。。')
shop_start()
else:
print('您的余额还有 %s 元 !' % moneys)
input('按任意键继续。。。')
shop_start()
# 2、充值
def top_up():
os.system('cls')
print('''
【======================= 充 值 ========================】
请选择支付方式:
0.取消支付
1.微信支付
2.支付宝支付
''')
tag_map = {
'0': shop_start,
'1': wechat_top_up,
'2': Alipay_top_up
}
while True:
tag = input('请选择====>>').strip()
if tag in tag_map:
tag_map[tag]()
else:
print('非法输入,请重新输入!')
# 2.1、微信支付
def wechat_top_up():
while True:
moneys = input('请输入您要充值的金额====>>').strip()
if moneys.isnumeric():
break
else:
print('非法输入,请重新输入!')
text = 'wxp://f2f0k4kxD8gJHD1YQNWKoRynRuntpiQ_g7ks'
img = qrcode.make(text)
img.show()
print('正在支付中。。。')
read_money(moneys)
# print('充值成功%s元。。。')%str(moneys)
money()
top_up()
# 2.2、支付宝支付
def Alipay_top_up():
while True:
moneys = input('请输入您要充值的金额====>>').strip()
if moneys.isnumeric():
break
else:
print('非法输入,请重新输入!')
text = 'https://qr.alipay.com/fkx045285gzeelj8gjxze0a'
img = qrcode.make(text)
img.show()
print('正在支付中。。。')
read_money(moneys)
# print('充值成功%s元。。。')%moneys
money()
top_up()
# 2.9、修改支付金额
def read_money(moneys):
global info
usr = info[1]
money_new = int(info[4]) + int(moneys)
sql = 'update info set money="%s" where usr="%s"' % (money_new, usr)
execute_sql(sql)
# 3、查看商品
def look_shop():
os.system('cls')
print("查看商品信息!")
print('''
【==================== 选择商品类别 ====================】
0.返回上一级 5.电脑
1.零食 6.手机
2.汽车 7.珠宝
3.水果 8.餐厨
4.百货 9.奶粉辅食
''')
# 3.1、零食 snack
# 3.2、汽车 car
# 3.3、水果 fruit
# 3.4、百货 general_goods
# 3.5、电脑 computer
# 3.6、手机 phone
# 3.7、珠宝 jewelry
# 3.8、餐厨 ware
# 3.9、奶粉辅食 milk
tag_mpa = {
'0': shop_start,
'1': '零食',
'2': '汽车',
'3': '水果',
'4': '百货',
'5': '电脑',
'6': '手机',
'7': '珠宝',
'8': '餐厨',
'9': '奶粉辅食'
}
while True:
tag = input('请选择要购买的商品类型====>>').strip()
if tag == '0':
shop_start() # 返回上级菜单函数
elif tag in tag_mpa:
sort = tag_mpa[tag]
show_shop(sort) # 调用查看商品信息函数
while True:
tag_num = input('
详情请按序号,返回上一级按【0】=====>>').strip().lower()
if tag_num == '0':
look_shop()
elif tag_num.isnumeric():
num_shop(tag_num, sort) # 调用选择商品函数
break
else:
print('您输入的有误,请重新输入!')
else:
print('您输入的有误,请重新输入!')
# 3.1、查询商品详细信息
def demand_shop(b, l, sort):
global cursor
sql = 'select *from %s where %s="%s"' % (b, l, sort)
execute_sql(sql)
data = cursor.fetchall()
return data
# 3.2、显示商品信息
def show_shop(sort):
os.system('cls')
data = demand_shop('shop', 'sort', sort)
print(''
'
【================== 您正在查看 %s 类商品 ==================】
' % sort)
print('序号| 名称 | 金额 | 库存 | 描 述 |')
for i in data:
id, sort, name, cost, QTY, units, detail, *_ = i
print('%s|【%s】|%s 元| %s%s|%s|' % (str(id).center(4), name.center(6, ' '),
cost.rjust(7), QTY.rjust(6), units, detail.ljust(9, ' ')))
# 3.3、选择商品
def num_shop(tag_num, sort):
global cursor
try:
sql = 'select pic from shop where sort="%s" and id="%s"' % (sort, tag_num)
cursor.execute(sql)
# 打开商品图片
output = open('image.png', 'wb')
output.write(cursor.fetchone()[0])
output.close()
print('正在打开详情。。。')
os.startfile('image.png')
# 选择商品
join_shop(tag_num, sort)
return True
except Exception:
print('您选择的商品序号错误,请重新选择!')
show_shop(sort)
# 3.4、加入购物车
def join_shop(tag_num, sort):
while True:
l = input('是否满意 [y / n] ====>>').lower().strip()
if l == 'y':
while True:
l = input('是否加入购物车 [y / n] ====>>').lower().strip()
if l == 'y':
while True:
sort, name, cost, QTY, units = look_QTY(tag_num, sort)
inp_num = input('输入需要加入的数量,库存还有%s%s====>>' % (QTY, units)).lower().strip()
if inp_num.isnumeric():
if int(inp_num) > int(QTY) or int(inp_num) <= 0:
print('您输入的数量大于库存,请输入%s%s以内的数量!' % (QTY, units))
else:
break
else:
print('输入不合法,请重新输入!')
print('加入成功,可以在购物车中查看。')
inp_num_1 = int(QTY) - int(inp_num)
amend_QTY(inp_num_1, sort, tag_num)
join_shop_cart(sort, name, cost, units, inp_num)
input('按任意键返回继续购物。。。').lower().strip()
look_shop()
else:
look_shop()
else:
print('滚犊子')
# 3.5、查询库存
def look_QTY(tag_num, sort):
global cursor
sql = 'select sort,name,cost,QTY,units from shop where sort = "%s" and id = "%s"' % (sort, tag_num)
execute_sql(sql)
sort, name, cost, QTY, units = cursor.fetchone()
print(QTY, units)
return sort, name, cost, QTY, units
# 3.6、修改库存
def amend_QTY(QTY, sort, tag_num):
sql = 'update shop set QTY="%s" where sort ="%s" and id = "%s" ' % (QTY, sort, tag_num)
execute_sql(sql)
# 3.7、加入购物车
def join_shop_cart(sort, name, cost, units, inp_num):
global info
info_name = info[1]
moneys = int(cost) * int(inp_num)
print(moneys)
sql = 'insert into shop_cart (sort,name,cost,QTY,units,moneys,info_name)'
' VALUES ("%s","%s","%s","%s","%s","%s","%s")' % (sort, name, cost, inp_num, units, moneys, info_name)
execute_sql(sql)
# 4、购物车
def shop_cart():
global info
get_info(info[1])
moneys = int(info[4])
add = shop_detail()
tag = input('''
0.返回
1.清空购物车
2.结账
请选择====>>''').lower().strip()
if tag == '2':
if add <= moneys:
account(info[1])
money_old = get_info(info[1])[4]
money_new = int(money_old) - add
sql = 'update info set money = "%s"' % money_new
execute_sql(sql)
print('结账成功,请亲耐心等待发货哦!')
money()
else:
input('您的余额不足,请充值!')
shop_start()
elif tag == '1':
empty()
input('已清空购物车,按任意键返回。。。')
else:
shop_start()
# 4.1、查看购物车明细
def shop_detail():
os.system('cls')
print('
'
' 【===================== 查看购物车 =====================】
')
global info
usr = info[1]
data = demand_shop('shop_cart', 'info_name', usr)
if data:
n = add = 0
print(''
'序号| 名称 | 金额 | 数量 | 小计金额 |')
for i in data:
n += 1
id, sort, name, cost, QTY, units, moneys, info_name = i
add = add + int(moneys)
print('%s|【%s】|%s 元| %s%s|%s 元|' % (str(n).center(4), name.center(6, ' '),
cost.rjust(7), QTY.rjust(6), units, moneys.rjust(8)))
print('
您购物车中的商品总计:%s件,%s元:' % (n, add))
return add
else:
input('您在购物车无商品,按任意键去购买。。。')
look_shop()
# 4.2、结账
def account(usr):
global cursor, connect
# 写入数据库buy_record表
data = demand_shop('shop_cart', 'info_name', usr)
buy_time = at_time()
for i in data:
print(i)
id, sort, name, cost, QTY, units, moneys, info_name = i
sql = 'insert into buy_record (sort, name, cost, QTY, units, moneys, info_name, buy_time) '
'VALUES ("%s","%s","%s","%s","%s","%s","%s","%s")' % (
sort, name, cost, QTY, units, moneys, info_name, buy_time)
cursor.execute(sql)
connect.commit()
sql = ''
empty(usr)
def empty(usr):
# 清除数据库shop_cart表中符信息
sql = 'delete from shop_cart where info_name = "%s"' % usr
execute_sql(sql)
# ============================== 5、查看已购买过的商品 ====================
# 5、查看已购买过的商品
def bill():
os.system('cls')
print('
'
' 【===================== 查看已够买 =====================】
')
global info
usr = info[1]
data = demand_shop('buy_record', 'info_name', usr)
if data:
n = add = 0
print('序 号| 名称 | 金额 | 数量 | 小计金额 | 购买日期 |')
for i in data:
n += 1
id, sort, name, cost, QTY, units, moneys, info_name, buy_time = i
add = add + int(moneys)
print('%s|【%s】|%s 元| %s%s|%s 元|%s|' % (str(n).center(5), name.center(6, ' '),
cost.rjust(7), QTY.rjust(6), units, moneys.rjust(8),
buy_time.rjust(20)))
input('
您消费总计:%s件,%s元。按任意键返回。。。' % (n, add))
shop_start()
return add
else:
input('您无购买记录,按任意键去购买。。。')
look_shop()
# ============================== 8、注销 ==================================
# 8、注销
def logout():
global info, is_login
is_login = False
info = ''
input('注销成功,按任意键重新登录!')
start()
# ============================== 9、修改密码 ==============================
# 9、修改密码
def change_pwd():
os.system('cls')
print('''
【======================= 修改密码 =====================】
按 0 退出修改界面。。。
''')
global info
usr = info[1]
check_for_phone()
input('修改成功!') if now_pwd(usr) == True else input('修改失败!')
# ========================================== 管理员 ================================================$
def admin():
os.system('cls')
print('''
【================ 欢迎使用淘宝购物系统 ================】
【 管理员版 】
0.退出
1.管理客户
2.管理商品
8.注销
9.修改管理员密码
''')
while True:
tag = input('请选择功能=====>>').strip()
tap_map = {
'0': exit,
'1': client_usr, # 管理客户
'2': shop_admin,
'7': logout,
'8': change_pwd,
'9': contact_way,
}
tap_map[tag]() if tag in tap_map else print('您输入有误,请重新输入!')
# 1、管理客户
def client_usr():
os.system('cls')
global cursor
print('''
【====================== 管理客户 ======================】
客户列表:
|序 号| 客户名 | 客户余额 | 手机号 |''')
# 查询客户信息
sql = 'select id,usr,money,phone from info'
cursor.execute(sql)
data = cursor.fetchall()
for i in data:
id, usr, money, phone = i
print(' | %s |%s|%s元|%s|' % (str(id).center(3), usr.center(12), money.rjust(10), phone.rjust(12)))
# 删除客户信息
usr_id = input('
请输入需要操作客户的序号 [按 0 返回]====>>').strip()
if usr_id == '0':
admin()
else:
print('''请选择:
1.修改客户名称
2.修改客户手机号
3.注销客户账户
''')
while True:
tag = input('请选择功能=====>>').strip()
tap_map = {
'1': amend_usr, # 管理客户
'2': amend_phone,
'3': del_usr
}
tap_map[tag](usr_id) if tag in tap_map else print('您输入有误,请重新输入!')
# 1.1、修改客户名称
def amend_usr(usr_id):
res = input('请输入新客户名称====>>').strip()
rest('usr', res, usr_id)
# 1.2、修改客户手机号
def amend_phone(usr_id):
res = check_phone()
rest('phone', res, usr_id)
# 1.3、注销客户账户
def del_usr(usr_id):
res = input('请确认是否[ y / n ]删除,删除无法找回!').strip().lower()
if res == 'y':
sql = 'delete from info where id = "%s"' % usr_id
execute_sql(sql)
input('修改成功按任意键返回。。。')
admin()
else:
admin()
# 1.9
def rest(field, res, usr_id):
sql = 'update info set %s = "%s" where id = "%s"' % (field, res, usr_id)
execute_sql(sql)
input('修改成功按任意键返回。。。')
admin()
# 2、管理商品
def shop_admin():
os.system('cls')
global cursor
print('''
【===================== 全部商品 =====================】
''')
print('序号| 名称 | 金额 | 库存 | 描 述 |')
sql = 'select *from shop'
execute_sql(sql)
data = cursor.fetchall()
for i in data:
id, sort, name, cost, QTY, units, detail, *_ = i
print('%s|【%s】|%s 元| %s%s|%s|' % (str(id).center(4), name.center(6, ' '),
cost.rjust(7), QTY.rjust(6), units, detail.ljust(9, ' ')))
input('如需修改库存,请到数据库修改,按任意键返回。。。')
admin()
# 9、联系我
def contact_way():
print('''
如有任何问题,请联系我!
QQ:1206709430
微信:15536582917
''')
connect_sql()
start()