def post(self): # 注册用户 TODO: 手机号验证 mobile = self.get_argument('mobile') pwd = self.get_argument('pwd') users = yield User.objects.filter(mobile=mobile).find_all() if len(users) != 0: # 手机号码已经被注册 raise HTTPError(**errors.status_21) school_id = self.get_argument('school_id') self.vaildate_id(school_id) school = yield School.objects.get(self.get_argument('school_id')) self.check_none(school) # 注册成功 user = User(mobile=mobile, password=pwd, nickname='test', school_id=ObjectId(school_id)) yield user.save() user = user.to_dict() user['token'] = token_manager.create_token(str(user['id'])) self.write_json(user)
def get(self): mobile = self.get_argument('mobile', None) pwd = self.get_argument('pwd', None) user = None if mobile is None or mobile == '' or pwd is None or pwd == '': # 没有传入手机密码,刷新 Token uid = self.is_logined() user = yield User.objects.get(uid) else: # 传入手机密码,执行登录 users = yield User.objects.filter(mobile=mobile).find_all() if len(users) == 0: # 未找到该用户 raise HTTPError(**errors.status_22) if users[0].password != pwd: # 密码有误 raise HTTPError(**errors.status_23) user = users[0] # 登陆成功 user = user.to_dict() user['token'] = token_manager.create_token(str(user['id'])) self.write_json(user)
def login(self, **args): ''' 注册 & 登录 ''' code = args['code'] wx_info = wx.exchange_code_for_session_key(code) user = self.ub.get_detail_by_openid(wx_info['openid']) if user: uid = user['id'] else: name = args['name'] sex = args['sex'] avatar = self.ub.avatarUrl2loacl(args['avatar']) country = args['country'] province = args['province'] city = args['city'] flag, uid = self.ub.reg(wx_info['openid'], wx_info['unionid'], name, sex, avatar, country, province, city) user = self.ub.get_detail(uid) token = token_manager.create_token(str(uid)) user['token'] = token user.pop('open_id') user.pop('union_id') return self.write_json(user)