def insertUser(self): session = user.DBSession() try: #尝试先发送邮件 EmailHandler.sendEmail(self, self.userid, self.email) new_user = user.User(userid=self.userid, username=self.username, password=self.password, email=self.email, auth=False) session.add(new_user) session.commit() self.set_status(201) result = { 'success': True, 'message': '成功创建账户,并已发送确认邮件', 'status': 201, 'token': pc.encrypt(self.userid) } self.write(json.dumps(result)) except Exception: self.set_status(403) result = { 'success': False, 'message': '创建账户失败,用户名与邮箱不能重复', 'status': 403 } self.write(json.dumps(result)) finally: session.close()
def post(self): self.set_header("Access-Control-Allow-Origin", "*") self.set_header("Access-Control-Allow-Headers", "x-requested-with") self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS') try: data = json_decode(self.request.body) except Exception: self.set_status(500) result = {'success': False, 'message': '解析错误', 'status': 500} self.write(json_encode(result)) self.password = data['password'] or None self.email = data['email'] or None self.token = data['token'] or None #采用两种方式登录 session = user.DBSession() try: if (self.token): self.token = pc.decrypt(self.token) userInfo = session.query( user.User).filter(user.User.userid == self.token).one() else: self.password = pc.encrypt(self.password) userInfo = session.query(user.User).filter( and_(user.User.email == self.email, user.User.password == self.password)).one() if (userInfo): print(userInfo.auth) if (userInfo.auth == False): self.set_status(401) result = { 'success': False, 'message': '账号未认证,请先前往邮箱进行认证', 'status': 401 } else: self.set_status(200) result = { 'success': True, 'message': '登陆成功', 'payload': { 'username': userInfo.username, 'email': userInfo.email }, 'token': pc.encrypt(userInfo.userid), 'status': 200 } self.write(json_encode(result)) except Exception: self.set_status(404) result = {'success': False, 'message': '账号不存在或密码错误', 'status': 404} self.write(json_encode(result)) finally: session.close()
#-*-coding:utf-8-*- import tornado.web import tornado.websocket import tornado.ioloop import uuid import json from model import user from controller.prpcrypt import pc session = user.DBSession() class ConfirmHandler(tornado.web.RequestHandler): def get(self, userid): #解密 decryptcode = pc.decrypt(userid) userInfo = session.query( user.User).filter(user.User.userid == decryptcode).one() if (userInfo): userInfo.auth = True try: session.commit() result = 'register successed' self.write(json.dumps(result)) except Exception: session.rollback() result = 'register failed' self.write(json.dumps(result)) finally: