def findps(): response_object = {'status': 'success'} if request.method == 'POST': #获取前端传来的数据 data = request.get_json() username = data['username'] newpassword = data['password'] code = data['code'] #验证新密码是否合乎规范 vPs = validate.ValidatePassword(newpassword) #验证邮箱是否已经注册 #未注册 if statement.Isregister(username) != True: response_object['message'] = "该邮箱账号尚未注册,请检查并重试!" #已注册 else: current_code = statement.find_item('CODE', 'register', username)[0] if code == current_code: if vPs == True: sql = "UPDATE register SET PASSWORD = \'%s\' WHERE USERNAME = \'%s\' " % ( newpassword, username) msg, res = statement.update_mysql(sql) if res == True: response_object['message'] = "密码重置成功!" response_object['resCode'] = 101 else: response_object['message'] = "密码重置失败,请重试!" else: response_object['message'] = "无效密码,请重新输入!" else: response_object['message'] = "验证码不正确!" else: response_object['message'] = "Unallowed request method." return jsonify(response_object)
def register(): response_object = {'status': 'success'} if request.method == 'POST': #获取前端传来的数据 data = request.get_json() username = data['username'] xm = data['xm'] password = data['password'] code = data['code'] #验证数据是否合法 vEmail = validate.ValidateEmail(username) vXm = validate.ValidateXm(xm) vPs = validate.ValidatePassword(password) vCode = validate.ValidateCode(code) #判断邮箱是否获取过验证码 idcode = '' if statement.Isgetsms(username) == True: idcode = statement.find_item('CODE', 'getsms', username)[0] else: idcode = email.get_code() #随机获取一个字符串 #邮箱尚未注册 if statement.Isregister(username) != True: #如果邮箱有效 if vEmail == True: #如果昵称尚未被注册 if statement.IsXmUsed(xm) != True: #如果昵称有效 if vXm == True: #如果密码有效 if vPs == True: #如果验证码有效 and idcode == code if vCode == True and code == idcode: sql = "insert into register values (null,\'%s\',\'%s\',\'%s\',\'%s\')" % ( username, xm, password, idcode) msg, res = statement.insert_mysql(sql) if res == True: response_object['message'] = "注册成功!" response_object['resCode'] = 101 else: response_object['message'] = "注册失败,请重新尝试!" else: response_object['message'] = "验证码不正确!" #如果密码无效 else: response_object['message'] = vPs #如果昵称无效 else: response_object['message'] = vXm #如果昵称已经被注册 else: response_object['message'] = "该用户名已存在,请重新尝试!" #如果邮箱无效 else: response_object['message'] = vEmail #邮箱已注册 else: response_object['message'] = "该邮箱账号已注册,请前往登录!" else: response_object['message'] = "Unallowed request method." return jsonify(response_object)
def login(): response_object = {'status': 'success'} if request.method == 'POST': #获取前端传来的数据 data = request.get_json() username = data['username'] password = data['password'] #验证邮箱是否已经注册 #未注册 if statement.Isregister(username) != True: response_object['message'] = "该邮箱账号尚未注册,请检查并重试!" #已经注册 else: #验证密码/验证码是否与register的匹配 Ismatch_ps = False Ismatch_ps = statement.Ismatch('PASSWORD', 'register', username, password) #判断密码 if Ismatch_ps == True: xm = statement.find_item('XM', 'register', username)[0] response_object['message'] = "登录成功!" response_object['xm'] = xm response_object['resCode'] = 101 else: response_object['message'] = "密码不正确,请重新尝试!" else: response_object['message'] = "Unallowed request method." return jsonify(response_object)
def GetSms(): T = '验证码已成功发送!' F = '获取验证码失败!' response_object = {'status': 'success'} if request.method == 'POST': data = request.get_json() username = data['username'] #验证邮箱是否合法 vEmail = validate.ValidateEmail(username) #如果该邮箱合法 if vEmail == True: #验证该邮箱是否已注册 #未注册账号获取验证码:注册___________________ 获取getsms table的验证码 if statement.Isregister(username) != True: insert_v = False update_v = False #随机获取验证码 new_code = '' new_code = email.get_code() #如果生成新密码、更新数据库、发送邮件成功 if validate.ValidateCode(new_code) == True: #如果该邮箱从未获取过验证码:插入insert数据库 if statement.Isgetsms(username) != True: #getsms插入记录 sql = "insert into getsms values (null,\'%s\',\'%s\')" % ( username, new_code) msg, insert_v = statement.insert_mysql(sql) #如果该邮箱曾获取过验证码:更新update数据库 else: #getsms更新记录 sql = "UPDATE getsms SET CODE = \'%s\' WHERE USERNAME = \'%s\' " % ( new_code, username) msg, update_v = statement.update_mysql(sql) #如果插入or更新成功 if update_v == True or insert_v == True: #发送验证码 sed = email.send_mail(username, new_code) if sed == True: response_object['message'] = T response_object['resCode'] = 101 else: response_object['message'] = '无效邮箱,请重新尝试!' else: response_object['message'] = F #如果生成新密码、更新数据库、发送邮件失败 else: response_object['message'] = F #已注册账号获取验证码:登录______________获取register table的验证码 else: origional_code = statement.find_item( 'CODE', 'register', username)[0] #获取register表格里该用户名的验证码 print("以前:{}".format(origional_code)) #获取新密码 code = '' code = email.get_code() print("现在:{}".format(code)) #如果生成新密码、更新数据库、发送邮件成功 if validate.ValidateCode(code) == True: #更新数据库 sql = "UPDATE register SET CODE = \'%s\' WHERE USERNAME = \'%s\' " % ( code, username) msg, res = statement.update_mysql(sql) #发送邮件 sed = email.send_mail(username, code) if res == True and sed == True: response_object['message'] = T response_object['resCode'] = 101 else: response_object['message'] = F #如果生成新密码、更新数据库、发送邮件失败 else: response_object['message'] = F #如果该邮箱不合法 else: response_object['message'] = vEmail else: response_object['message'] = "Unallowed request method." return jsonify(response_object)