MySQL(11) - Python+MySQL开发新闻管理系统

1.前置知识及条件

1.1.数据库脚本

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for t_news
-- ----------------------------
DROP TABLE IF EXISTS `t_news`;
CREATE TABLE `t_news` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(40) NOT NULL,
  `editor_id` int(10) unsigned NOT NULL,
  `type_id` int(10) unsigned NOT NULL,
  `content_id` char(12) NOT NULL,
  `is_top` tinyint(3) unsigned NOT NULL,
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `state` enum('草稿','待审批','已审批','隐藏') NOT NULL,
  PRIMARY KEY (`id`),
  KEY `editor_id` (`editor_id`),
  KEY `type_id` (`type_id`),
  KEY `state` (`state`),
  KEY `create_time` (`create_time`),
  KEY `is_top` (`is_top`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_news
-- ----------------------------
INSERT INTO `t_news` VALUES ('1', '新闻标题1', '2', '1', '1', '1', '2018-11-22 18:55:56', '2018-11-22 18:55:56', '待审批');

-- ----------------------------
-- Table structure for t_role
-- ----------------------------
DROP TABLE IF EXISTS `t_role`;
CREATE TABLE `t_role` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `role` varchar(20) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `role` (`role`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_role
-- ----------------------------
INSERT INTO `t_role` VALUES ('2', '新闻编辑');
INSERT INTO `t_role` VALUES ('1', '管理员');

-- ----------------------------
-- Table structure for t_type
-- ----------------------------
DROP TABLE IF EXISTS `t_type`;
CREATE TABLE `t_type` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `type` varchar(20) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `type` (`type`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_type
-- ----------------------------
INSERT INTO `t_type` VALUES ('2', '体育');
INSERT INTO `t_type` VALUES ('5', '历史');
INSERT INTO `t_type` VALUES ('4', '娱乐');
INSERT INTO `t_type` VALUES ('3', '科技');
INSERT INTO `t_type` VALUES ('1', '要闻');

-- ----------------------------
-- Table structure for t_user
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(20) NOT NULL,
  `password` varchar(500) NOT NULL,
  `email` varchar(100) NOT NULL,
  `role_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`),
  KEY `username_2` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_user
-- ----------------------------
INSERT INTO `t_user` VALUES ('1', 'admin', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', 'admin@163.com', '1');
INSERT INTO `t_user` VALUES ('2', 'scott', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', 'scott@163.com', '1');
INSERT INTO `t_user` VALUES ('3', 'test_1', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', 'test_1@163.com', '2');
INSERT INTO `t_user` VALUES ('4', 'test_2', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', 'test_2@163.com', '2');
INSERT INTO `t_user` VALUES ('5', 'test_3', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', 'test_3@163.com', '2');
INSERT INTO `t_user` VALUES ('6', 'test_4', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', 'test_4@163.com', '2');
INSERT INTO `t_user` VALUES ('7', 'test_5', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', 'test_5@163.com', '2');
INSERT INTO `t_user` VALUES ('8', 'test_6', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', 'test_6@163.com', '2');
INSERT INTO `t_user` VALUES ('9', 'test_7', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', 'test_7@163.com', '2');
INSERT INTO `t_user` VALUES ('10', 'test_8', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', 'test_8@163.com', '2');
INSERT INTO `t_user` VALUES ('11', 'test_9', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', 'test_9@163.com', '2');
INSERT INTO `t_user` VALUES ('12', 'test_10', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', 'test_10@163.com', '2');
INSERT INTO `t_user` VALUES ('13', 'test_11', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', 'test_11@163.com', '2');

1.2.python相关组件安装

1.2.1.更新pip

python -m pip install --upgrade pip

注意:如果Python环境安装在C盘,必须用管理员身份打开终端窗口才能升级pip,以及安装模块

1.2.2.安装Colorama模块

安装:

Python程序向控制台输出彩色文字,先要安装colorama模块

pip install colorama

基础使用:


1.3.项目描述

1.3.1.项目结构描述

项目采用的是DAO设计模式,封装数据层的调用,封装数据库的操作,如果后续表格发生变动,只需要修改dao对应的文件即可,不需要全部修改。

什么是DAO,说白了就是封装数据库操作,链接跳转了解:https://blog.csdn.net/zxy144/article/details/109279693

  • db:存放于数据库相关的python类,操作数据表的代码
  • service:业务类代码
  • 核心代码:app.py

1.3.2.项目功能/需求描述

 

2.Code

2.1.db目录

2.1.1.mysql_db.py

 1 import mysql.connector.pooling
 2 
 3 __config={
 4     "host":"localhost",
 5     "port":3306,
 6     "user":"root",
 7     "password":"123456",
 8     "database":"vega"
 9 }
10 
11 try:
12     pool=mysql.connector.pooling.MySQLConnectionPool(
13         **__config,
14         pool_size=10
15     )
16 except Exception as e:
17     print(e)

2.1.2.news_dao.py

  1 from db.mysql_db import pool
  2 
  3 class NewsDao:
  4     #查询待审批新闻列表
  5     def search_unreview_list(self,page):
  6         try:
  7             con=pool.get_connection()
  8             cursor=con.cursor()
  9             sql="SELECT n.id,n.title,t.type,u.username " \
 10                 "FROM t_news n JOIN t_type t ON n.type_id=t.id " \
 11                 "JOIN t_user u ON n.editor_id=u.id " \
 12                 "WHERE n.state=%s " \
 13                 "ORDER BY n.create_time DESC " \
 14                 "LIMIT %s,%s"
 15             cursor.execute(sql,("待审批",(page-1)*10,10))
 16             result=cursor.fetchall()
 17             return result
 18         except Exception as e:
 19             print(e)
 20         finally:
 21             if "con" in dir():
 22                 con.close()
 23 
 24     # 查询待审批新闻的总页数
 25     def search_unreview_count_page(self):
 26         try:
 27             con=pool.get_connection()
 28             cursor=con.cursor()
 29             sql="SELECT CEIL(COUNT(*)/10) FROM t_news WHERE state=%s"
 30             cursor.execute(sql,["待审批"])
 31             count_page=cursor.fetchone()[0]
 32             return count_page
 33         except Exception as e:
 34             print(e)
 35         finally:
 36             if "con" in dir():
 37                 con.close()
 38 
 39  #审批新闻
 40     def update_unreview_news(self,id):
 41         try:
 42             con = pool.get_connection()
 43             con.start_transaction()
 44             cursor=con.cursor()
 45             sql="UPDATE t_news SET state=%s WHERE id=%s"
 46             cursor.execute(sql,("已审批",id))
 47             con.commit()
 48         except Exception as e:
 49             if "con" in dir():
 50                 con.rollback()
 51             print(e)
 52         finally:
 53             if "con" in dir():
 54                 con.close()
 55 
 56     #查询新闻列表
 57     def search_list(self,page):
 58         try:
 59             con=pool.get_connection()
 60             cursor=con.cursor()
 61             sql="SELECT n.id,n.title,t.type,u.username " \
 62                 "FROM t_news n JOIN t_type t ON n.type_id=t.id " \
 63                 "JOIN t_user u ON n.editor_id=u.id " \
 64                 "ORDER BY n.create_time DESC " \
 65                 "LIMIT %s,%s"
 66             cursor.execute(sql,((page-1)*10,10))
 67             result=cursor.fetchall()
 68             return result
 69         except Exception as e:
 70             print(e)
 71         finally:
 72             if "con" in dir():
 73                 con.close()
 74 
 75     #查询新闻总页数
 76     def search_count_page(self):
 77         try:
 78             con=pool.get_connection()
 79             cursor=con.cursor()
 80             sql="SELECT CEIL(COUNT(*)/10) FROM t_news"
 81             cursor.execute(sql)
 82             count_page=cursor.fetchone()[0]
 83             return count_page
 84         except Exception as e:
 85             print(e)
 86         finally:
 87             if "con" in dir():
 88                 con.close()
 89 
 90     #删除新闻
 91     def delete_by_id(self,id):
 92         try:
 93             con = pool.get_connection()
 94             con.start_transaction()
 95             cursor=con.cursor()
 96             sql="DELETE FROM t_news WHERE id=%s"
 97             cursor.execute(sql,[id])
 98             con.commit()
 99         except Exception as e:
100             if "con" in dir():
101                 con.rollback()
102             print(e)
103         finally:
104             if "con" in dir():
105                 con.close()

2.1.3.role_dao.py

from db.mysql_db import pool

class RoleDao:
    #查询角色列表
    def search_list(self):
        try:
            con=pool.get_connection()
            cursor=con.cursor()
            sql="SELECT id,role FROM t_role"
            cursor.execute(sql)
            result=cursor.fetchall()
            return result
        except Exception as e:
            print(e)
        finally:
            if "con" in dir():
                con.close()

2.1.4.user_dao.py

  1 from db.mysql_db import pool
  2 
  3 class UserDao:
  4     #验证用户登录
  5     def login(self,username,password):
  6         try:
  7             con=pool.get_connection()
  8             cursor=con.cursor()
  9             sql="SELECT COUNT(*) FROM t_user WHERE username=%s AND " \
 10                 "AES_DECRYPT(UNHEX(password),'HelloWorld')=%s"
 11             cursor.execute(sql,(username,password))
 12             count=cursor.fetchone()[0]
 13             return True if count==1 else False
 14         except Exception as e:
 15             print(e)
 16         finally:
 17             if "con" in dir():
 18                 con.close()
 19 
 20     #查询用户角色
 21     def search_user_role(self,username):
 22         try:
 23             pass
 24             con=pool.get_connection()
 25             cursor=con.cursor()
 26             sql="SELECT r.role FROM t_user u JOIN t_role r ON u.role_id=r.id " \
 27                 "WHERE u.username=%s"
 28             cursor.execute(sql,[username])
 29             role=cursor.fetchone()[0]
 30             return role
 31         except Exception as e:
 32             print(e)
 33         finally:
 34             if "con" in dir():
 35                 con.close()
 36 
 37     #添加记录
 38     def insert(self,username,password,email,role_id):
 39         try:
 40             con = pool.get_connection()
 41             con.start_transaction()
 42             cursor=con.cursor()
 43             sql="INSERT INTO t_user(username,password,email,role_id) " \
 44                 "VALUES(%s,HEX(AES_ENCRYPT(%s,'HelloWorld')),%s,%s)"
 45             cursor.execute(sql,(username,password,email,role_id))
 46             con.commit()
 47         except Exception as e:
 48             if "con" in dir():
 49                 con.rollback()
 50             print(e)
 51         finally:
 52             if "con" in dir():
 53                 con.close()
 54 
 55 
 56     #查询用户总页数
 57     def search_count_page(self):
 58         try:
 59             con=pool.get_connection()
 60             cursor=con.cursor()
 61             sql="SELECT CEIL(COUNT(*)/10) FROM t_user"
 62             cursor.execute(sql)
 63             count_page=cursor.fetchone()[0]
 64             return count_page
 65         except Exception as e:
 66             print(e)
 67         finally:
 68             if "con" in dir():
 69                 con.close()
 70 
 71 #查询用户分页记录
 72     def search_list(self,page):
 73         try:
 74             con=pool.get_connection()
 75             cursor=con.cursor()
 76             sql="SELECT u.id,u.username,r.role " \
 77                 "FROM t_user u JOIN t_role r " \
 78                 "ON u.role_id=r.id " \
 79                 "ORDER BY u.id " \
 80                 "LIMIT %s,%s"
 81             cursor.execute(sql,((page-1)*10,10))
 82             result=cursor.fetchall()
 83             return result
 84         except Exception as e:
 85             print(e)
 86         finally:
 87             if "con" in dir():
 88                 con.close()
 89 
 90     #修改用户信息
 91     def update(self,id,username,password,email,role_id):
 92         try:
 93             con = pool.get_connection()
 94             con.start_transaction()
 95             cursor=con.cursor()
 96             sql="UPDATE t_user SET username=%s," \
 97                 "password=HEX(AES_ENCRYPT(%s,'HelloWorld'))," \
 98                 "email=%s,role_id=%s " \
 99                 "WHERE id=%s"
100             cursor.execute(sql,(username,password,email,role_id,id))
101             con.commit()
102         except Exception as e:
103             if "con" in dir():
104                 con.rollback()
105             print(e)
106         finally:
107             if "con" in dir():
108                 con.close()
109 
110     #删除用户
111     def delete_by_id(self,id):
112         try:
113             con = pool.get_connection()
114             con.start_transaction()
115             cursor=con.cursor()
116             sql="DELETE FROM t_user WHERE id=%s"
117             cursor.execute(sql,[id])
118             con.commit()
119         except Exception as e:
120             if "con" in dir():
121                 con.rollback()
122             print(e)
123         finally:
124             if "con" in dir():
125                 con.close()

 


2.2.service目录

2.2.1.news_service.py

 1 from db.news_dao import NewsDao
 2 
 3 class NewsService:
 4     __news_dao=NewsDao()
 5 
 6     # 查询待审批新闻列表
 7     def search_unreview_list(self,page):
 8         result=self.__news_dao.search_unreview_list(page)
 9         return result
10 
11     # 查询待审批新闻的总页数
12     def search_unreview_count_page(self):
13         count_page=self.__news_dao.search_unreview_count_page()
14         return count_page
15 
16     # 审批新闻
17     def update_unreview_news(self, id):
18         self.__news_dao.update_unreview_news(id)
19 
20     #查询新闻列表
21     def search_list(self, page):
22         result=self.__news_dao.search_list(page)
23         return result
24 
25     # 查询新闻总页数
26     def search_count_page(self):
27         count_page=self.__news_dao.search_count_page()
28         return count_page
29 
30     # 删除新闻
31     def delete_by_id(self, id):
32         self.__news_dao.delete_by_id(id)

2.2.2.role_service.py

1 from db.role_dao import RoleDao
2 
3 class RoleService:
4     __role_dao=RoleDao()
5 
6     # 查询角色列表
7     def search_list(self):
8         result=self.__role_dao.search_list()
9         return result

2.2.3.user_service.py

 1 from db.user_dao import UserDao
 2 
 3 class UserService:
 4     __user_dao=UserDao()
 5 
 6     # 验证用户登录
 7     def login(self,username,password):
 8         result=self.__user_dao.login(username,password)
 9         return result
10 
11     # 查询用户角色
12     def search_user_role(self,username):
13         role=self.__user_dao.search_user_role(username)
14         return role
15 
16     # 添加记录
17     def insert(self, username, password, email, role_id):
18         self.__user_dao.insert(username, password, email, role_id)
19 
20     # 查询用户总页数
21     def search_count_page(self):
22         count_page = self.__user_dao.search_count_page()
23         return count_page
24 
25     # 查询用户分页记录
26     def search_list(self, page):
27         result = self.__user_dao.search_list(page)
28         return result
29 
30     # 修改用户信息
31     def update(self, id, username, password, email, role_id):
32         self.__user_dao.update(id, username, password, email, role_id)
33 
34     # 删除用户
35     def delete_by_id(self, id):
36         self.__user_dao.delete_by_id(id)

 


2.3.app.py启动文件

  1 from colorama import Fore,Style,init
  2 init()
  3 from getpass import getpass
  4 from service.user_service import UserService
  5 from service.news_service import NewsService
  6 from service.role_service import RoleService
  7 import os
  8 import sys
  9 import time
 10 
 11 
 12 __user_service=UserService()
 13 __news_service=NewsService()
 14 __role_service=RoleService()
 15 
 16 while True:
 17     os.system("cls")
 18     print(Fore.LIGHTBLUE_EX,"\n\t==================")
 19     print(Fore.LIGHTBLUE_EX,"\n\t欢迎使用新闻管理系统")
 20     print(Fore.LIGHTBLUE_EX, "\n\t==================")
 21     print(Fore.LIGHTGREEN_EX,"\n\t1.登陆系统")
 22     print(Fore.LIGHTGREEN_EX,"\n\t2.退出系统")
 23     print(Style.RESET_ALL)
 24     opt=input("\n\t输入操作编号:")
 25     if opt=="1":
 26         username=input("\n\t用户名:")
 27         password=getpass("\n\t密码:")
 28         result=__user_service.login(username,password)
 29         #登陆成功
 30         if result==True:
 31             #查询角色
 32             role=__user_service.search_user_role(username)
 33             while True:
 34                 os.system("cls")
 35                 if role=="新闻编辑":
 36                     print('test')
 37                 elif role=="管理员":
 38                     print(Fore.LIGHTGREEN_EX,"\n\t1.新闻管理")
 39                     print(Fore.LIGHTGREEN_EX, "\n\t2.用户管理")
 40                     print(Fore.LIGHTRED_EX, "\n\tback.退出登陆")
 41                     print(Fore.LIGHTRED_EX, "\n\texit.退出系统")
 42                     print(Style.RESET_ALL)
 43                     opt = input("\n\t输入操作编号:")
 44                     if opt=="1":
 45                         while True:
 46                             os.system("cls")
 47                             print(Fore.LIGHTGREEN_EX, "\n\t1.审批新闻")
 48                             print(Fore.LIGHTGREEN_EX, "\n\t2.删除新闻")
 49                             print(Fore.LIGHTRED_EX, "\n\tback.返回上一层")
 50                             print(Style.RESET_ALL)
 51                             opt = input("\n\t输入操作编号:")
 52                             if opt=="1":
 53                                 page=1
 54                                 while True:
 55                                     os.system("cls")
 56                                     count_page=__news_service.search_unreview_count_page()
 57                                     result=__news_service.search_unreview_list(page)
 58                                     for index in range(len(result)):
 59                                         one=result[index]
 60                                         print(Fore.LIGHTBLUE_EX, "\n\t%d\t%s\t%s\t%s"%(index+1,one[1],one[2],one[3]))
 61                                     print(Fore.LIGHTBLUE_EX, "\n\t-------------------")
 62                                     print(Fore.LIGHTBLUE_EX,"\n\t%d/%d"%(page,count_page))
 63                                     print(Fore.LIGHTBLUE_EX, "\n\t-------------------")
 64                                     print(Fore.LIGHTRED_EX, "\n\tback.返回上一层")
 65                                     print(Fore.LIGHTRED_EX, "\n\tprev.上一页")
 66                                     print(Fore.LIGHTRED_EX, "\n\tnext.下一页")
 67                                     print(Style.RESET_ALL)
 68                                     opt = input("\n\t输入操作编号:")
 69                                     if opt=="back":
 70                                         break
 71                                     elif opt=="prev" and page>1:
 72                                         page-=1
 73                                     elif opt=="next" and page<count_page:
 74                                         page+=1
 75                                     elif int(opt)>=1 and int(opt)<=10:
 76                                         news_id=result[int(opt)-1][0]
 77                                         __news_service.update_unreview_news(news_id)
 78                             elif opt=="2":
 79                                 page=1
 80                                 while True:
 81                                     os.system("cls")
 82                                     count_page=__news_service.search_count_page()
 83                                     result=__news_service.search_list(page)
 84                                     for index in range(len(result)):
 85                                         one=result[index]
 86                                         print(Fore.LIGHTBLUE_EX, "\n\t%d\t%s\t%s\t%s"%(index+1,one[1],one[2],one[3]))
 87                                     print(Fore.LIGHTBLUE_EX, "\n\t-------------------")
 88                                     print(Fore.LIGHTBLUE_EX,"\n\t%d/%d"%(page,count_page))
 89                                     print(Fore.LIGHTBLUE_EX, "\n\t-------------------")
 90                                     print(Fore.LIGHTRED_EX, "\n\tback.返回上一层")
 91                                     print(Fore.LIGHTRED_EX, "\n\tprev.上一页")
 92                                     print(Fore.LIGHTRED_EX, "\n\tnext.下一页")
 93                                     print(Style.RESET_ALL)
 94                                     opt = input("\n\t输入操作编号:")
 95                                     if opt=="back":
 96                                         break
 97                                     elif opt=="prev" and page>1:
 98                                         page-=1
 99                                     elif opt=="next" and page<count_page:
100                                         page+=1
101                                     elif int(opt)>=1 and int(opt)<=10:
102                                         news_id=result[int(opt)-1][0]
103                                         __news_service.delete_by_id(news_id)
104                             elif opt=="back":
105                                 break
106                     elif opt=="2":
107                         while True:
108                             os.system("cls")
109                             print(Fore.LIGHTGREEN_EX, "\n\t1.添加用户")
110                             print(Fore.LIGHTGREEN_EX, "\n\t2.修改用户")
111                             print(Fore.LIGHTGREEN_EX, "\n\t3.删除用户")
112                             print(Fore.LIGHTRED_EX, "\n\tback.返回上一层")
113                             print(Style.RESET_ALL)
114                             opt = input("\n\t输入操作编号:")
115                             if opt=="back":
116                                 break
117                             elif opt=="1":
118                                 os.system("cls")
119                                 username=input("\n\t用户名:")
120                                 password = getpass("\n\t密码:")
121                                 repassword=getpass("\n\t重复密码:")
122                                 if password!=repassword:
123                                     print("\n\t两次密码不一致(3秒自动返回)")
124                                     time.sleep(3)
125                                     continue
126                                 email=input("\n\t邮箱:")
127                                 result=__role_service.search_list()
128                                 for index in range(len(result)):
129                                     one=result[index]
130                                     print(Fore.LIGHTBLUE_EX,"\n\t%d.%s"%(index+1,one[1]))
131                                 print(Style.RESET_ALL)
132                                 opt=input("\n\t角色编号:")
133                                 role_id=result[int(opt)-1][0]
134                                 __user_service.insert(username,password,email,role_id)
135                                 print("\n\t保存成功(3秒自动返回)")
136                                 time.sleep(3)
137                             elif opt=="2":
138                                 page = 1
139                                 while True:
140                                     os.system("cls")
141                                     count_page = __user_service.search_count_page()
142                                     result = __user_service.search_list(page)
143                                     for index in range(len(result)):
144                                         one = result[index]
145                                         print(Fore.LIGHTBLUE_EX,
146                                               "\n\t%d\t%s\t%s" % (index + 1, one[1], one[2]))
147                                     print(Fore.LIGHTBLUE_EX, "\n\t-------------------")
148                                     print(Fore.LIGHTBLUE_EX, "\n\t%d/%d" % (page, count_page))
149                                     print(Fore.LIGHTBLUE_EX, "\n\t-------------------")
150                                     print(Fore.LIGHTRED_EX, "\n\tback.返回上一层")
151                                     print(Fore.LIGHTRED_EX, "\n\tprev.上一页")
152                                     print(Fore.LIGHTRED_EX, "\n\tnext.下一页")
153                                     print(Style.RESET_ALL)
154                                     opt = input("\n\t输入操作编号:")
155                                     if opt == "back":
156                                         break
157                                     elif opt == "prev" and page > 1:
158                                         page -= 1
159                                     elif opt == "next" and page < count_page:
160                                         page += 1
161                                     elif int(opt) >= 1 and int(opt) <= 10:
162                                         os.system("cls")
163                                         user_id=result[int(opt)-1][0]
164                                         username = input("\n\t新用户名:")
165                                         password = getpass("\n\t新密码:")
166                                         repassword = getpass("\n\t再次输入密码:")
167                                         if password!=repassword:
168                                             print(Fore.LIGHTRED_EX,"\n\t两次密码不一致(3秒自动返回)")
169                                             print(Style.RESET_ALL)
170                                             time.sleep(3)
171                                             break
172                                         email = input("\n\t新邮箱:")
173                                         result = __role_service.search_list()
174                                         for index in range(len(result)):
175                                             one = result[index]
176                                             print(Fore.LIGHTBLUE_EX, "\n\t%d.%s" % (index + 1, one[1]))
177                                         print(Style.RESET_ALL)
178                                         opt = input("\n\t角色编号:")
179                                         role_id = result[int(opt) - 1][0]
180                                         opt=input("\n\t是否保存(Y/N)")
181                                         if opt=="Y" or opt=="y":
182                                             __user_service.update(user_id,username,password,email,role_id)
183                                             print("\n\t保存成功(3秒自动返回)")
184                                             time.sleep(3)
185                             elif opt=="3":
186                                 page = 1
187                                 while True:
188                                     os.system("cls")
189                                     count_page = __user_service.search_count_page()
190                                     result = __user_service.search_list(page)
191                                     for index in range(len(result)):
192                                         one = result[index]
193                                         print(Fore.LIGHTBLUE_EX,
194                                               "\n\t%d\t%s\t%s" % (index + 1, one[1], one[2]))
195                                     print(Fore.LIGHTBLUE_EX, "\n\t-------------------")
196                                     print(Fore.LIGHTBLUE_EX, "\n\t%d/%d" % (page, count_page))
197                                     print(Fore.LIGHTBLUE_EX, "\n\t-------------------")
198                                     print(Fore.LIGHTRED_EX, "\n\tback.返回上一层")
199                                     print(Fore.LIGHTRED_EX, "\n\tprev.上一页")
200                                     print(Fore.LIGHTRED_EX, "\n\tnext.下一页")
201                                     print(Style.RESET_ALL)
202                                     opt = input("\n\t输入操作编号:")
203                                     if opt == "back":
204                                         break
205                                     elif opt == "prev" and page > 1:
206                                         page -= 1
207                                     elif opt == "next" and page < count_page:
208                                         page += 1
209                                     elif int(opt) >= 1 and int(opt) <= 10:
210                                         os.system("cls")
211                                         user_id=result[int(opt)-1][0]
212                                         __user_service.delete_by_id(user_id)
213                                         print("\n\t删除成功(3秒自动返回)")
214                                         time.sleep(3)
215 
216                     if opt=='back':
217                         break;
218                     elif opt=='exit':
219                         sys.exit(0)
220 
221         else:
222             print("\n\t登录失败(3秒自动返回)")
223             time.sleep(3)
224 
225     elif opt=="2":
226         sys.exit(0)

 

posted @ 2022-05-25 10:50  葛老头  阅读(287)  评论(0编辑  收藏  举报