def login(self, userId, password, token): ''' 登陆 :param userId: 电话号码 :param password: 密码 :param token: token :return: ''' self.collection = self.db['user'] cnt = self.collection.find({'userId': userId}).count() if cnt == 0: return False, 'userId num: %s need to register then login' % userId else: if mongo_utils.MongoUtil().verification_token(userId, token): ret = self.collection.find({'userId': userId}) ret = ret.next() if util.hash(userId + password) == ret['password']: deadtime = ret['token']['deadline'] now = str(datetime.datetime.now()) if deadtime < now: token = util.produce_token() token_model = user.Token() token_model.token = token logger.info('login success') self.collection.update_one({'userId': userId}, {'$set': {'token': token_model.todict()}}, True) return True, token else: return True, ret['token']['token'] else: return False, 'password is incorrect' else: return False, 'token is over-date'
def register(self, userId, password): ''' 注册 :param userId: 用户编号 :param password: 密码 :return: True or False & reason ''' self.collection = self.db['user'] cnt = self.collection.find({'userId': userId}).count() if cnt == 0: user_model = user.User() token_model = user.Token() token_model.token = util.produce_token() user_model.phone = userId user_model.userId = userId user_model.password = util.hash(userId + password) user_model.registerTime = str(datetime.datetime.now()) user_model.token = token_model.todict() self.collection.insert_one(user_model.todict()) return True, token_model.token else: return False, ' userId num: %s have been already registered' % userId
def change_password(self, userId, new_password, token): ''' 修改密码 :param userId: 电话号码 :param new_password: 新密码 :param token: token :return: ''' self.collection = self.db['user'] cnt = self.collection.find({'userId': userId}).count() if cnt == 0: return False, u'用户: %s 需注册' % userId else: if mongo_utils.MongoUtil().verification_token(userId, token): self.collection.update_one({'userId': userId}, {'$set': {'password': util.hash(userId + new_password)}}, True) token = util.produce_token() token_model = user.Token() token_model.token = token self.collection.update_one({'userId': userId}, {'$set': {'token': token_model.todict()}}, True) return True, token else: return False, u'用户信息校验不正确'