Пример #1
0
 async def login_password(self, phone, password):
     ''' 使用密码登录'''
     pwMD5 = function.MD5encrypt(password)
     res = await DbOperate().instance().select(
         'select id,userName from user where mobile=? and password=? and status=0 limit 1',
         (phone, pwMD5))
     if len(res) == 1:
         # 登录成功
         # 添加用户会话增加到redis
         uid = function.get_uuid()
         key = self.taoken_str.format(uid)
         value = {
             'id': res[0].get('id'),
             'nickname': res[0].get('userName'),
             'phone': phone,
             'login': time.time(),
             'loginby': 0
         }
         rdsave = await RedisOperate().instance().set_data(key, value)
         if rdsave != 'OK':
             return False, 3007, '保存缓存出错,请重试', None
         rdsup = await RedisOperate().instance().exprie(key, TOKEN_TIME)
         if rdsup != 1:
             return False, 3008, '更新缓存出错,请重试', None
         await CacheUserinfo(res[0].get('id')).createCache(force_update=True
                                                           )
         return True, 0, '登录成功', {'token': uid}
     else:
         return False, 2001, '用户名或密码错误', None
Пример #2
0
    def prepare(self):
        ''' 所有请求都经过这里 '''
        if API_SECURITY_CHECK_OPEN:
            '''需要验证'''
            # if len(self.request.body) == 0:
            #     return
            # if self.request.uri.endswith("timestamp"):
            #     # 时间戳接口不需要验证.
            #     return

            # 获得加密时间戳
            getstamp = self.get_body_argument('stamp')
            try:
                stamp = int(getstamp)
            except Exception as e:
                self.send_message(False, 5001, "非法参数", None)

            # 获得签名
            sign = self.get_body_argument('sign')
            server_stamp = int(time.time())
            if server_stamp - stamp > API_SECURITY_SECONDS or server_stamp - stamp < 0:
                self.send_message(False, 5002, "无效,非法访问", None)

            # 签名生成规则: md5(密钥+参数(排序)+时间戳)
            server_sign = function.MD5encrypt(API_SECURITY_SECRET + getstamp)
            print(server_sign)
            if server_sign == sign:
                return
            else:
                # 签名错误
                self.send_message(False, 5003, "错误,非法访问", None)
        else:
            # 接口不需要安全认证
            return
Пример #3
0
    async def reset_password(self, uid, password):
        ''' reset password'''
        md5pw = function.MD5encrypt(password)
        dbsave = await DbOperate().instance().execute(
            "update user set password= ? where id = ?", (md5pw, uid))
        if dbsave is None:
            return False, 3014, '密码保存失败,请重试'

        if dbsave == 0:
            log.warning("需要验证新旧密码是否一致,id:{} ,当前:{}".format(uid, md5pw))
            return True, 0, '已重置密码,请尝试登录{}'.format(dbsave)
        else:
            return True, 0, '重置密码成功'
Пример #4
0
    async def register(self, phone, password, verify_code, tag):
        ''' 用户注册 '''
        verify_key = "phone.verify.register:{}".format(phone)
        res = await DbOperate().instance().select(
            'select id from user where mobile=? limit 1', (phone))
        if len(res) > 0:
            return False, 1007, '手机号已注册', None

        rdget = await RedisOperate().instance().get_data(verify_key)
        if rdget == verify_code:
            # 验证码一致
            import random
            # nickname = 'u{}'.format(random.randint(1000000,9999999))
            nickname = '饭友_{}'.format(''.join(
                random.sample('1z2yx5w6v7u8t9srqpon3m4lkji0hgfedcba', 5)))
            insert_sql = "INSERT into user (`userName`,`mobile`,`password`) values (?,?,?)"
            md5pw = function.MD5encrypt(password)
            dbsave = await DbOperate().instance().execute(
                insert_sql, (nickname, phone, md5pw))
            if dbsave is None:
                return False, 3200, '保存数据出错,请重试', None
            # 获取新用户的ID
            userid = await DbOperate().instance().select(
                'select id from user where mobile=? limit 1', (phone))
            if len(userid) != 1:
                return False, 3004, '获取注册数据出错,请重试!', None
            # 更新token到redis
            uid = function.get_uuid()
            key = self.taoken_str.format(uid)
            value = {
                'id': userid[0].get('id'),
                'phone': phone,
                'login': time.time(),
                'loginby': 1
            }
            rdsave = await RedisOperate().instance().set_data(key, value)
            if rdsave != 'OK':
                return False, 3005, '保存注册数据缓存出错,请重试', None
            rdsup = await RedisOperate().instance().exprie(key, TOKEN_TIME)
            if rdsup != 1:
                return False, 3008, '更新缓存出错,请重试', None
            else:
                return True, 0, '注册成功', {'token': uid}
        else:
            return False, 2001, '验证码错误', None
Пример #5
0
    async def modify_phone(self, session, newphone, verify_code, password):
        ''' 修改手机号 '''
        # 新旧手机号不能一样
        db_phone_read = await DbOperate().instance().select(
            'select id from user where mobile=? limit 1', (newphone))
        if len(db_phone_read) > 0:
            return False, 2003, '手机号已注册'

        oldphone = session.get('phone', '')
        userid = session.get('id', 0)
        if oldphone == newphone:
            return False, 1014, '新旧号码不能相同'
        # 匹配手机验证码
        verify_key = "phone.verify.modifyphone:{}".format(newphone)
        rdget = await RedisOperate().instance().get_data(verify_key)
        # 匹配密码是否正确
        db_read = await DbOperate().instance().select(
            'select password from user where id=? limit 1', (userid))
        if verify_key is None:
            return False, 2004, '短信验证码未发送!'
        if len(db_read) == 0:
            return False, 2005, '用户不存在'

        if rdget == verify_code and db_read[0].get(
                'password', '') == function.MD5encrypt(password):
            db_up = await DbOperate().instance().execute(
                'update user set mobile = ? where id= ?', (newphone, userid))
            if db_up is not None:
                # 删除验证码
                rd_del = await RedisOperate().instance().del_data(verify_key)
                # 清除用户session
                return True, 0, '手机号修改成功'
            else:
                return False, 2006, '手机号保存失败!'
        else:
            return False, 1015, '验证码错误或密码错误'