diff --git a/handler/MysqlDB.py b/handler/MysqlDB.py index 62bee3f..a0738ec 100644 --- a/handler/MysqlDB.py +++ b/handler/MysqlDB.py @@ -7,6 +7,7 @@ database_passwd = "guest1234" database_name = "GUESTDB" table_name = "user_info" +DEBUG = False # 打开数据库连接 db = pymysql.connect(host=database_ip, user=database_user, password=database_passwd, database=database_name ) @@ -24,9 +25,27 @@ def DB_Check_byName(user_name): passwd = results[0][2] return passwd except: - print("There is no the user's info !") + if DEBUG: + print("There is no the user's info !") return False + +def DB_Check_byEmail(email): + sql = "SELECT * FROM %s WHERE user_email='%s' " % (table_name,email) + try: + # 执行SQL语句 + cursor.execute(sql) + # 获取所有记录列表 + results = cursor.fetchall() + passwd = results[0][2] + return passwd + except: + if DEBUG: + print("There is no the user's info !") + return False + + + def DB_Insert_User(user_info): user_name = user_info["username"] user_passwd = user_info["password"] @@ -38,12 +57,14 @@ def DB_Insert_User(user_info): cursor.execute(sql) # 执行sql语句 db.commit() - print("Insert user_info successfully!") + if DEBUG: + print("Insert user_info successfully!") return True except: # 发生错误时回滚 db.rollback() - print("Insert user_info error!") + if DEBUG: + print("Insert user_info error!") return False diff --git a/handler/__pycache__/MysqlDB.cpython-36.pyc b/handler/__pycache__/MysqlDB.cpython-36.pyc index 6b19add..6a0ba71 100644 Binary files a/handler/__pycache__/MysqlDB.cpython-36.pyc and b/handler/__pycache__/MysqlDB.cpython-36.pyc differ diff --git a/handler/__pycache__/account.cpython-36.pyc b/handler/__pycache__/account.cpython-36.pyc index fd74217..7e4241b 100644 Binary files a/handler/__pycache__/account.cpython-36.pyc and b/handler/__pycache__/account.cpython-36.pyc differ diff --git a/handler/__pycache__/auth.cpython-36.pyc b/handler/__pycache__/auth.cpython-36.pyc index b7ae86d..0b2a5ed 100644 Binary files a/handler/__pycache__/auth.cpython-36.pyc and b/handler/__pycache__/auth.cpython-36.pyc differ diff --git a/handler/__pycache__/main.cpython-36.pyc b/handler/__pycache__/main.cpython-36.pyc index 7f64ed6..96819af 100644 Binary files a/handler/__pycache__/main.cpython-36.pyc and b/handler/__pycache__/main.cpython-36.pyc differ diff --git a/handler/__pycache__/simulation.cpython-36.pyc b/handler/__pycache__/simulation.cpython-36.pyc index 00d4994..d37dd6a 100644 Binary files a/handler/__pycache__/simulation.cpython-36.pyc and b/handler/__pycache__/simulation.cpython-36.pyc differ diff --git a/handler/__pycache__/spice.cpython-36.pyc b/handler/__pycache__/spice.cpython-36.pyc index 5aeae31..73ca7e7 100644 Binary files a/handler/__pycache__/spice.cpython-36.pyc and b/handler/__pycache__/spice.cpython-36.pyc differ diff --git a/handler/account.py b/handler/account.py index 0ca8957..2f4bc9a 100644 --- a/handler/account.py +++ b/handler/account.py @@ -15,6 +15,27 @@ def add_user(username,password,email=''): result = DB_Insert_User({'username':username,'password':password,'email':email,'date':create_time}) return result +def check_register(username,password,email): + check_name = DB_Check_byName(username) + check_email = DB_Check_byEmail(email) + check_pw = check_password(password) + + if check_name != False: + return 1 + if check_email != False: + return 2 + if check_pw == False: + return 3 + + return 0 + + +def check_password(password): + if(len(password)<4): + return False + + return True + # USER_DATA = { # 'name':'user1', # 'password':'1234' diff --git a/handler/auth.py b/handler/auth.py index a7d2877..59c0863 100644 --- a/handler/auth.py +++ b/handler/auth.py @@ -1,5 +1,5 @@ from .main import AuthBaseHandler -from .account import authenticate, add_user +from .account import authenticate, add_user, check_register from .MongoDB import * DEBUG = False @@ -62,13 +62,22 @@ class RegisterHandler(AuthBaseHandler): password2 = self.get_argument('password2','') if username and password1 and (password1 == password2): - success = add_user(username,password1,email) - if success: - Mongo.connect(DataBase='example',Collection=username) - Mongo.update(behavior='register',tags='auth') - self.redirect('/login') - else: - self.write({'msg':'register fail'}) + flag_checck = check_register(username,password1,email) + if flag_checck == 0: + success = add_user(username,password1,email) + if success: + Mongo.connect(DataBase='example',Collection=username) + Mongo.update(behavior='register',tags='auth') + self.redirect('/login') + else: + self.write({'msg':'register fail'}) + elif flag_checck == 1: + self.write({'msg':'the username is not avaliable!'}) + elif flag_checck == 2: + self.write({'msg':'the email is not avaliable!'}) + elif flag_checck == 3: + self.write({'msg':'the password is too shor!'}) + else: if DEBUG: print('register again') diff --git a/handler/main.py b/handler/main.py index 2d6e685..ea1f37f 100644 --- a/handler/main.py +++ b/handler/main.py @@ -8,7 +8,7 @@ class AuthBaseHandler(tornado.web.RequestHandler,SessionMixin): return self.session.get('username',None) #session是一种会话状态,跟数据库的session可能不一样 #添加装饰器,装饰需要验证的请求 -class IndexHandler(AuthBaseHandler): +class IndexHandler(AuthBaseHandler): @tornado.web.authenticated #@tornado.web.authenticated装饰器包裹get方法时,表示这个方法只有在用户合法时才会调用,authenticated装饰器会调用get_current_user()方法获取current_user的值,若值为False,则重定向到登录url装饰器判断有没有登录,如果没有则跳转到配置的路由下去,但是要在app.py里面设置login_url def get(self,*args,**kwargs): # self.render('main/index_Zh.html') @@ -43,13 +43,21 @@ class Index_En_Handler(AuthBaseHandler): self.render('main/index_En.html') #添加装饰器,装饰需要验证的请求 -class Open_Index_Zh_Handler(tornado.web.RequestHandler): +class Open_Index_Zh_Handler(AuthBaseHandler): def get(self,*args,**kwargs): - self.render('main/open_index_Zh.html') + username = self.get_current_user() + if username == None: + self.render('main/open_index_Zh.html') + else: + self.render('main/index_Zh.html') #添加装饰器,装饰需要验证的请求 -class Open_Index_En_Handler(tornado.web.RequestHandler): +class Open_Index_En_Handler(AuthBaseHandler): def get(self,*args,**kwargs): - self.render('main/open_index_En.html') \ No newline at end of file + username = self.get_current_user() + if username == None: + self.render('main/open_index_En.html') + else: + self.render('main/index_En.html') \ No newline at end of file