def login(self): #GET请求时,opt为login时只行该方法 loginParser = self.parser.copy() loginParser.add_argument('name', required=True, help='必须提供用户名') loginParser.add_argument('password', required=True, help='必须提供密码') args = loginParser.parse_args() #验证数据 name = args.get('name') password = md5_crypt(args.get('password')) user = dao.query(User).filter( db.and_(User.name == name, User.password == password, User.is_active == True, User.is_life == True)).first() print(user) if user: user.last_login_time = datetime.datetime.now() dao.save(user) user_fields = { 'id': fields.Integer, 'name': fields.String, 'phone': fields.String, 'nickName': fields.String, 'email': fields.String } out_fields = { 'msg': fields.String, 'token': fields.String, 'data': fields.Nested(user_fields) } #如果用户登录成功,session存入{token:userid} token = helper.md5_crypt(str(uuid.uuid4())) session[token] = user.id data = {'msg': '您已登陆成功', 'token': token, 'data': user} return marshal(data, out_fields) else: return {'msg': '用户名或密码错误'}
def modifyPasswd(self): modifyParser = self.parser.copy() modifyParser.add_argument('username', required=True, help='用户登录必须提供用户名') modifyParser.add_argument('password', required=True, help='用户登录必须提供口令') modifyParser.add_argument('newpassword', required=True, help='新密码不能为空') args = modifyParser.parse_args() username = args['username'] password = args['password'] newpassword = args['newpassword'] user = dao.login(User, username, md5_crypt(password)) if user: user.password = md5_crypt(newpassword) save(user) return {'msg': '{}用户密码修改成功'.format(user.name)} return {'msg': '用户密码修改失败'}
def post(self): # 从基本的请求解析器中复制请求参数说明 registParser = self.parser.copy() # 再添加注册时使用 registParser.add_argument('password', dest='pwd', required=True, help='口令不能为空') registParser.add_argument('email', required=True, help='邮箱不能为空!') registParser.add_argument('phone', required=True, help='手机号不能为空!') registParser.add_argument('nickname', required=True, help='昵称不能为空!') # 验证请求参数是满足要求 args = registParser.parse_args() u = User() u.name = args.get('name') u.nickName = args.get('nickname') u.email = args.get('email') u.phone = args.get('phone') u.password = helper.md5_crypt(args.get('pwd')) if dao.save(u): # helper.sendEmail(u) url = request.host_url tasks.sendMail.delay(u.id, url) return {'status': 200, 'msg': '用户注册成功'} return {'status': 201, 'msg': '用户注册失败'}
def post(self): # 从基本的请求解析器中复制请求参数 registParser = self.parser.copy() # 再添加注册时使用的 registParser.add_argument('password', dest='pwd', required=True, help='密码不能为空') registParser.add_argument('nickname', required=True, help='昵称不能为空') registParser.add_argument('email', required=True, help='邮箱不能为空') registParser.add_argument('phone', required=True, help='手机号不能为空') args = registParser.parse_args() u = User() u.name = args['name'] u.nickname = args['nickname'] u.email = args['email'] u.phone = args['phone'] u.password = md5_crypt(args['pwd']) if dao.save(u): # token = md5_crypt(str(uuid4())) # App.ext.cache.set(token, u.id, timeout=60 * 10) active_url = request.host_url + 'account?opt=active&token=' # msg = Message(subject='淘票票用户激活', recipients=[u.email], sender='*****@*****.**') # msg.html = '<h1>{}注册成功</h1><h3><a href={}>点击这里验证邮件</a></h3><h2>或者复制地址到浏览器: {}'.format(u.name, active_url, # active_url) # # App.ext.mail.send(msg) tasks.sendMail.delay(u.id, active_url) return {'status': 666, 'msg': '用户注册成功'} return {'status': 660, 'msg': '用户注册失败'}
def sendMail(uId, url): with manage.app.test_request_context(): u = dao.getById(User, uId) print('查到的用户', u) token = md5_crypt(str(uuid.uuid4())) # 将token设置到redis缓存中 App.ext.cache.set(token, u.id, timeout=10 * 60) # 允许10分钟内来激活用户 print('toke', token) print('id', u.id) active_url = url + 'account/?opt=active&token=' + token print('路径', active_url) # 发送邮件 msg = Message(subject='淘票票用户激活', recipients=[u.email], sender='*****@*****.**') msg.html = "<h1>{} 注册成功!</h1><h3>请先<a href={}>激活</a>注册账号</h3> <h2>或者复制地址到浏览器: {}</h2>".format( u.name, active_url, active_url) try: print(msg.html) App.ext.mail.send(msg) print('邮件已发送') except Exception as e: print(e) print('邮件发送失败')
def post(self): #从基本的请求解析器中复制请求参数说明 registParser=self.parser.copy() #再注册时使用 registParser.add_argument('password', required=True, help='密码不能为空', dest='pwd') registParser.add_argument('email', required=True, help='邮箱不能为空' ) registParser.add_argument('phone', required=True, help='电话不能为空' ) registParser.add_argument('nickname', required=True, help='昵称不能为空' ) registParser.add_argument('photo_1', type=FileStorage, location='files', required=True, help='必须提供一个photo' ) # 验证请求参数是满足要求 args=registParser.parse_args() u=User() u.name=args.get('name') u.nickname=args.get('nickname') u.phone=args.get('phone') u.email=args.get('email') u.password=helper.md5_crypt(args.get('pwd')) uFile: FileStorage = args.get('photo_1') print('上传的文件名:', uFile.filename) newFileName = str(uuid.uuid4()).replace('-', '') newFileName += '.' + uFile.filename.split('.')[-1] uFile.save(os.path.join(settings.MEDIA_DIR, newFileName)) if dao.save(u): helper.sendEmail(u) return {'status':200, 'msg':'上传成功,用户注册成功', 'path': '/static/uploads/{}'.format(newFileName)} return {'status':201, 'msg':'用户注册失败'}
def login(self): # GET请求时,opt为login时 loginParser = self.parser.copy() loginParser.add_argument('name', required=True, help='需提供name') loginParser.add_argument('passwd', required=True, help='需提供口令') # 验证登录参数 args = loginParser.parse_args() username = args.get('name') password = args.get('passwd') # 查询用户(额外添加一条件:用户已激活) print(username, password) qs = dao.query(User).filter( User.name.__eq__(username), User.password.__eq__(helper.md5_crypt(password)), User.is_active == True, User.is_life == True) if not qs.count(): return {'status': 600, 'msg': '用户登录失败,用户名或口令不正确!'} u: User = qs.first() u.last_login_time = datetime.today() dao.save(u) # 更新用户登录的时间 token = helper.getToken() print('skdjfksdfksdkfh', token) session[token] = u.id # 将token存放session中 print('++++++++++', session.get(token)) out_user_fields = { 'name': fields.String, 'email': fields.String, 'phone': fields.String, 'photo': fields.String(attribute='photo_1') } out_fields = { 'msg': fields.String, 'data': fields.Nested(out_user_fields), 'access_token': fields.String } data = {'msg': '登录成功!', 'data': u, 'access_token': token} # 通过marshal 将返回的data数据按输出字段转成json字符 return marshal(data, out_fields)
def post(self): # 从基本的请求解析器中复制请求参数说明 registParser = self.parser.copy() # 再添加注册时使用 registParser.add_argument('password', dest='pwd', required=True, help='口令不能为空') registParser.add_argument('email', required=True, help='邮箱不能为空!') registParser.add_argument('phone', required=True, help='手机号不能为空!') registParser.add_argument('nickname', required=True, help='昵称不能为空!') # 验证请求参数是满足要求 args = registParser.parse_args() u = User() u.name = args.get('name') u.nickName = args.get('nickname') u.email = args.get('email') u.phone = args.get('phone') u.password = helper.md5_crypt(args.get('pwd')) if dao.save(u): # token = helper.md5_crypt(str(uuid.uuid4())) # # # 将token设置到redis缓存中 # App.ext.cache.set(token, u.id, timeout=10 * 60) # 允许10分钟内来激活用户 # # active_url = request.host_url + 'account/?opt=active&token=' + token # # 发送邮件 # msg = Message(subject='淘票票用户激活', # recipients=[u.email], # sender='*****@*****.**') # msg.html = "<h1>{} 注册成功!</h1><h3>请先<a href={}>激活</a>注册账号</h3> <h2>或者复制地址到浏览器: {}</h2>".format(u.name, active_url, active_url) # # App.ext.mail.send(msg)e url = request.host_url tasks.sendMail.delay(u.id,url) return {'status': 200, 'msg': '用户注册成功'} return {'status': 201, 'msg': '用户注册失败'}
def login(self): loginParser = self.parser.copy() loginParser.add_argument('username', required=True, help='用户登录必须提供用户名') loginParser.add_argument('password', required=True, help='用户登录必须提供口令') args = loginParser.parse_args() username = args.get('username') password = args.get('password') user = dao.login(User, username, md5_crypt(password)) print(user) if user: token = getToken() user.last_login_time = datetime.today() dao.save(user) session[token] = user.id out_user_fields = { 'name': fields.String, 'email': fields.String, 'phone': fields.String, 'photo1': fields.String(attribute='photo_1') } out_fields = { 'msg': fields.String, 'data': fields.Nested(out_user_fields), 'access_token': fields.String } data = {'msg': '登录成功', 'data': user, 'access_token': token} return marshal(data, out_fields) return {'msg': '用户登录失败'}