def login(self): # 1、从user_auth表里拉取数据 # 1.1、失败,返回并提示报错信息 # 1.2、没有拉取到符合条件的用户、,返回并提示报错信息 # 1.3、拉取正常,进入2 # 2、查看该账号是否处于激活状态 # 2.1、未激活,返回错误提示信息,并提示用户激活(走激活url) # 2.2、已激活,进入3 # 3、此时说明账号密码正确,生成token,将用户信息存到token里,返回token给用户 email = self.email # 密码加密 tool = Md5Tool() pw = tool.get_md5(self.password) with MySQLTool(host=mysql_config['host'], user=mysql_config['user'], password=mysql_config['pw'], port=mysql_config['port'], database=mysql_config['database']) as mtool: select_result = mtool.run_sql([ [ 'SELECT * FROM user_auth WHERE email = %s and pw = %s', [ email, pw ] ] ]) if select_result is False: return get_res(code=0, msg='服务器错误') if len(select_result) is 0: return get_res(code=0, msg='用户名/密码错误或用户不存在') user_auth = { 'id': select_result[0][0], 'email': select_result[0][1], 'permission': select_result[0][4], 'status': select_result[0][5], } if user_auth['permission'] is 0: return get_res(code=1, msg='邮箱未激活') # 获取当前时间 nowtime = get_date_time() # 更新最后登录时间 mtool.update_row( 'UPDATE user_auth SET lastlogin_time = %s WHERE id = %', [ nowtime, user_auth['id'] ] ) # 生成token,返回给用户 # user_auth['token'] = self.make_token() return get_res(code=200, data=user_auth, msg='登录成功', token=self.make_token())
def login(request): # 获取当前时间 nowtime = get_date_time() print(nowtime) if request.method != 'POST': return get_res_json(code=0, msg="请通过POST请求来进行登陆") data = json.loads(request.body) uf = UserForm(data) # 验证不通过,返回错误信息 if not uf.is_valid(): msg = uf.get_form_error_msg() return get_res_json(code=0, msg=msg) username = data.get('username', '') password = data.get('password', '') print(username, password) tool = Md5Tool() md5pw = tool.get_md5(password) print(md5pw) # 连接数据库 with MySQLTool(host=mysql_config['host'], user=mysql_config['user'], password=mysql_config['pw'], database=mysql_config['database']) as mtool: # 执行sql并获得返回结果 result = mtool.run_sql( [['select * from developer_info where name = %s', [username]]]) # 打印结果e print(result) # 判定密码是否相等 if len(result) > 0: if md5pw == result[0][2]: # 再判定该用户状态是否正常 if result[0][4] != 0: return get_res_json(code=0, msg="该用户禁止登陆") sm = SessionManage(request.session) sm.set_login(result[0]) # 更新登陆时间 mtool.update_row( 'UPDATE developer_info SET lastlogin_time = %s WHERE name = %s', [nowtime, username]) return get_res_json( data={ 'redirecturl': '/home' if not IS_ON_WEBPACK_DEVELOPMENT else '/home.html', 'msg': '登陆成功' }) else: return get_res_json(code=0, msg="密码错误") else: return get_res_json(code=0, msg="不存在该用户")
def verify_email(self): email = self.email vcode = self.vcode # 然后去数据库找符合的数据 # 连接数据库 with MySQLTool(host=mysql_config['host'], user=mysql_config['user'], password=mysql_config['pw'], port=mysql_config['port'], database=mysql_config['database']) as mtool: result = mtool.run_sql([ [ 'SELECT * FROM verify_email WHERE email = %s and verify_key = %s and is_pass = 0', [ email, vcode ] ] ]) # 如果查找失败,或者查不到符合的信息 if result is False: return get_res(code=0, msg='激活因未知原因失败,请重试或者联系管理员。QQ:20004604,微信:qq20004604') if result is False or len(result) <= 0: return get_res(code=0, msg='验证信息不存在,请重试或者联系管理员。QQ:20004604,微信:qq20004604') # 查到符合的信息,则更新邮箱验证表,设置该行通过 # 获取当前时间 nowtime = get_date_time() affect_verify_email_rows = mtool.update_row( 'UPDATE verify_email SET is_pass = 1, is_invalid = 1, last_vtime = %s WHERE email = %s and verify_key = %s', [ nowtime, email, vcode ] ) if affect_verify_email_rows is False or affect_verify_email_rows is 0: mtool.set_uncommit() return get_res(code=0, msg='激活失败(0),请重试或者联系管理员。QQ:20004604,微信:qq20004604') # 再修改用户表,设置账号状态为启用 affect_user_auth_rows = mtool.update_row( 'UPDATE user_auth SET permission = 1 WHERE email = %s', [ email ] ) if affect_user_auth_rows is False or affect_user_auth_rows is 0: mtool.set_uncommit() return get_res(code=0, msg='激活失败(1),请重试或者联系管理员。QQ:20004604,微信:qq20004604') return get_res(code=200, msg='激活成功')
def _load_from_mysql(self): if self.mysql_config is None: return None # 连接数据库 with MySQLTool(host=self.mysql_config['host'], user=self.mysql_config['user'], password=self.mysql_config['pw'], port=self.mysql_config['port'], database=self.mysql_config['database']) as mtool: result = mtool.run_sql([['SELECT * FROM info']]) print(result) return result
def get_userinfo(self, id): d = uid.get_mysql_select_sql(id) with MySQLTool(host=mysql_config['host'], user=mysql_config['user'], password=mysql_config['pw'], port=mysql_config['port'], database=mysql_config['database']) as mtool: print(d) select_result = mtool.run_sql([[d['sql'], d['val_list']]]) if select_result is False: return get_res_json(code=0, msg='查询用户信息失败') userinfo = uid.get_mysql_select_data(select_result) return get_res_json(code=200, data=userinfo)
def update(self, data): d = uid.get_mysql_update_sql(data) print(d) with MySQLTool(host=mysql_config['host'], user=mysql_config['user'], password=mysql_config['pw'], port=mysql_config['port'], database=mysql_config['database']) as mtool: u_result = mtool.update_row(d['sql'], d['val_list']) if u_result is not False: return get_res_json(code=200, msg='修改成功') else: return get_res_json(code=0, msg='修改用户信息失败')
def verify_vcode(self, email, vcode): # 流程: # 1、检查邮箱、验证码是否合法; # 2、检查该条数据是否存在; # 3、检查该条数据是否在期限范围之内; with MySQLTool(host=mysql_config['host'], user=mysql_config['user'], password=mysql_config['pw'], port=mysql_config['port'], database=mysql_config['database']) as mtool: verify_result = self._is_vcode_correct(mtool, email, vcode) if verify_result['is_pass'] is True: return get_res(code=200, msg='success') else: return get_res(code=0, msg=verify_result['res'])
def _save_into_mysql(self, email, md5pw, phone): vcode = None # 连接数据库 with MySQLTool(host=mysql_config['host'], user=mysql_config['user'], password=mysql_config['pw'], port=mysql_config['port'], database=mysql_config['database']) as mtool: # 查看有没有同名的用户 result = mtool.run_sql( [['select (email) from user_info where email = %s', [email]]]) # 打印结果e print(result) # 判定密码是否相等 if len(result) > 0: return get_res_json(code=0, msg="该邮箱已注册,请更换邮箱") # 获取当前时间 nowtime = get_date_time() # 插入 row_id = mtool.insert_row( 'INSERT user_info' '(id, email, pw, phone, permission, status, create_time, lastlogin_time) VALUES' '(%s, %s, %s, %s, 0, 0, %s, %s)', [None, email, md5pw, phone, nowtime, nowtime]) if row_id is False: return get_res_json(code=0, msg='注册失败') vcode = self._get_verify_code() self._insert_info_into_verify(mtool, email, vcode) # 发送激活邮件给用户 send_result = self.send_verify_email(email, vcode) # 发送失败——》返回错误信息 if send_result.code is not 200: return get_res_json(code=200, data={'msg': send_result.msg}) # 此时跳转到邮件发送提示页面,提示用户点击邮箱里的链接进行验证 return get_res_json(code=200, data={'msg': '用户注册成功,已发送激活邮件,请访问邮箱打开激活邮件以激活账号'})
def send_mail(self, email): # 3、验证邮箱是否存在(不存在则返回,并返回提示信息) # 4、验证上一次发送重置密码邮件的时间(每次时间间隔不少于180秒)(低于这个时间,返回提示信息) # 5、生成重置密码的验证码,将验证码插入生成的链接,将链接插入生成的重置密码的邮件文本中 # 6、发送验证邮件,并插入一条重置密码的数据,然后返回用户提示信息 with MySQLTool(host=mysql_config['host'], user=mysql_config['user'], password=mysql_config['pw'], port=mysql_config['port'], database=mysql_config['database']) as mtool: # 【3】【4】 check_result = self.is_can_send(email, mtool) # 检查不通过,返回 if check_result['is_pass'] is False: # 回滚数据库操作 mtool.set_uncommit() return check_result['res'] # 【5】【6】 return self.send(mtool, email)
def reset_pw(self, email, vcode, pw): print(email, vcode, pw) # 流程梳理 # 1、检查能否重置 # 2、重置密码 # 3、发送重置密码成功的通知邮件 with MySQLTool(host=mysql_config['host'], user=mysql_config['user'], password=mysql_config['pw'], port=mysql_config['port'], database=mysql_config['database']) as mtool: # 【1】 verify_result = self._is_vcode_correct(mtool, email, vcode) if verify_result['is_pass'] is False: return get_res(code=0, msg=verify_result['res']) # 【2】 reset_result = self._pw_reset(mtool, email, pw) # 重置密码失败 if reset_result['is_pass'] is False: return reset_result['res'] # 【3】 self._send_success_mail(email) # 无论发送邮件成功或失败,都提示用户密码重置成功 return get_res_json(code=200, data={'msg': '密码重置成功'})
def register(request): # 获取当前时间 nowtime = get_date_time() print(nowtime) if request.method != 'POST': return get_res_json(code=0, msg="请通过POST请求来进行登陆") data = json.loads(request.body) uf = UserForm(data) # 验证不通过,返回错误信息 if not uf.is_valid(): msg = uf.get_form_error_msg() return get_res_json(code=0, msg=msg) username = data.get('username') password = data.get('password') email = data.get('email', '') print(username, password, email) tool = Md5Tool() md5pw = tool.get_md5(password) print(md5pw) # 连接数据库 with MySQLTool(host=mysql_config['host'], user=mysql_config['user'], password=mysql_config['pw'], database=mysql_config['database']) as mtool: # 查看有没有同名的用户 result = mtool.run_sql( [['select (name) from developer_info where name = %s', [username]]]) # 打印结果e print(result) # 判定密码是否相等 if len(result) > 0: return get_res_json(code=0, msg="该用户名已注册,请更换用户名") # 插入 row_id = mtool.insert_row( 'INSERT developer_info' '(id, name, pw, permission, status, create_time, lastlogin_time, email) VALUES' '(%s, %s, %s, 3, 0, %s, %s, %s)', [None, username, md5pw, nowtime, nowtime, email]) if row_id is False: return get_res_json(code=0, msg='注册失败') sm = SessionManage(request.session) sm.set_login([row_id, username, '', 3, 0, nowtime, nowtime, email]) return get_res_json( code=200, data={ 'msg': '用户注册成功,正在跳转中...', 'redirecturl': '/home' if not IS_ON_WEBPACK_DEVELOPMENT else '/home.html', })
def _init_mysql(self): return MySQLTool(host=mysql_config['host'], user=mysql_config['user'], password=mysql_config['pw'], port=mysql_config['port'], database=mysql_config['database'])