完善用户管理机制建立与什么相挂钩的用户等级评价体系_统一用户管理平台

(37) 2024-08-31 08:01:01

user_dao.py

# !/user/bin/env python # -*- coding: utf-8 -*- from db.mysql_db import pool class UserDao(object): # 验证用户登录 def login(self, username, password): try: con = pool.get_connection() cursor = con.cursor() sql = "SELECT COUNT(*) FROM t_user WHERE username=%s " \ "AND password=HEX(AES_ENCRYPT(%s,'HelloWorld'));" cursor.execute(sql, (username, password)) count = cursor.fetchone()[0] return True if count == 1 else False except Exception as e: print(e) finally: if "con" in dir(): con.close() # 将连接放回连接池 # 根据用户名查询用户角色 def search_user_role(self, username): try: con = pool.get_connection() cursor = con.cursor() sql = "SELECT r.role FROM t_user u JOIN t_role r ON u.role_id=r.id " \ "WHERE u.username=%s" cursor.execute(sql, [username]) role = cursor.fetchone()[0] return role except Exception as e: print(e) finally: if "con" in dir(): con.close() # 将连接放回连接池 # 添加记录 def insert(self,username,password,email,role_id): try: con = pool.get_connection() con.start_transaction() cursor = con.cursor() sql="INSERT INTO t_user(username,password,email,role_id) " \ "VALUES(%s,HEX(AES_ENCRYPT(%s,'HelloWorld')),%s,%s)" cursor.execute(sql, (username,password,email,role_id)) con.commit() except Exception as e: if "con" in dir(): con.rollback() print(e) finally: if "con" in dir(): con.close() # 将连接放回连接池 # 查询用户总页数 def search_count_page(self): try: con = pool.get_connection() cursor = con.cursor() sql = "SELECT CEIL(COUNT(*)/10) FROM t_user" cursor.execute(sql) count_page = cursor.fetchone()[0] return count_page except Exception as e: print(e) finally: if "con" in dir(): con.close() # 将连接放回连接池 # 查询用户分页记录 def search_list(self,page): try: con = pool.get_connection() cursor = con.cursor() sql = "SELECT u.id,u.username,r.role FROM t_user u JOIN t_role r " \ "ON u.role_id=r.id " \ "ORDER BY u.id DESC " \ "LIMIT %s,%s" cursor.execute(sql,((page-1)*10,10)) result = cursor.fetchall() return result except Exception as e: print(e) finally: if "con" in dir(): con.close() # 将连接放回连接池 # 修改用户信息 def update(self,id,username,password,email,role_id): try: con = pool.get_connection() con.start_transaction() cursor = con.cursor() sql="UPDATE t_user SET username=%s," \ "password=HEX(AES_ENCRYPT(%s,'HelloWorld'))," \ "email=%s,role_id=%s " \ "WHERE id=%s" cursor.execute(sql, (username,password,email,role_id,id)) con.commit() except Exception as e: if "con" in dir(): con.rollback() print(e) finally: if "con" 
THE END

发表回复