Beispiel #1
0
def update_user():
    '''修改用户信息'''
    headers = request.headers
    user_id = g.user_id
    args = request.json

    try:
        mobile = args.get('mobile')
        if mobile:
            mobile_exists = User.query_user(mobile=mobile)
            if mobile_exists:
                return http_util.return_forbidden('手机号已经绑定其他用户')

        u = User.update_user_by_id(id=user_id, **args)
        if u:
            # 记录用户操作
            Action.create_action(
                user_id=user_id,
                type=BaseConfig.TYPE_ACTION_UPDATE,
                res_id=user_id,
                res_type=BaseConfig.TYPE_USER,
                ext=args
            )

            Search.sync_index_by_id(user_id, BaseConfig.TYPE_USER)

            return return_model()
        else:
            return http_util.return_internal_server_error()
    except BaseException as e:
        app.logger.error(e)
        return http_util.return_internal_server_error()
Beispiel #2
0
def resource_delete():
    """资源详情"""
    args = request.json
    key = http_util.check_params(args, 'res_id', 'res_type')
    if key:
        return http_util.return_param_not_found(key)

    login_user_id = http_util.get_login_user_id(request)

    if not login_user_id:
        return http_util.return_no_authorization()

    res_id = http_util.get_param(args, 'res_id')
    res_type = http_util.get_param_int(args, 'res_type')

    try:
        is_del = Resource.delete_resource(res_id, res_type)

        if not is_del:
            return http_util.return_internal_server_error("删除失败")

        # 记录用户行为
        Action.create_action(
            user_id=login_user_id,
            type=BaseConfig.TYPE_ACTION_DELETE,
            res_id=res_id,
            res_type=res_type
        )

        return http_util.return_model()
    except BaseException as e:
        app.logger.error(e)
        return http_util.return_internal_server_error()
Beispiel #3
0
    def get_show_resource(cls, login_user_id):
        hp = HomePage.query_daily_resource()

        if hp:
            hp_show = Resource.get_resource_detail(hp.res_id, hp.res_type)
            if hp_show:
                # 记录用户查看状态
                if login_user_id:
                    is_exists = Action.query_action(
                        user_id=login_user_id,
                        type=BaseConfig.TYPE_ACTION_VIEW_HOME_PAGE_SHOW,
                        res_id=hp_show['res_id'],
                        res_type=hp_show['res_type']
                    )
                    if is_exists:
                        return None

                    Action.create_action(
                        user_id=login_user_id,
                        type=BaseConfig.TYPE_ACTION_VIEW_HOME_PAGE_SHOW,
                        res_id=hp_show['res_id'],
                        res_type=hp_show['res_type']
                    )

                return hp_show

        return None
Beispiel #4
0
def save_image():
    user_id = g.user_id
    args = request.json
    urls = http_util.get_param(args, 'urls')

    if urls:
        for url in urls:
            image = image_db.create_image(user_id=user_id, url=url)
            # 记录用户上传图片行为
            Action.create_action(user_id=user_id,
                                 type=BaseConfig.TYPE_ACTION_UPLOAD,
                                 res_id=image.id,
                                 res_type=BaseConfig.TYPE_IMAGE)

            detail = Image.get_image_detail(image.id)
            User.update_user_by_id(id=user_id, last_upload=detail)
        return http_util.return_model()
    else:
        return http_util.return_param_not_found('urls')
Beispiel #5
0
def save_video():
    user_id = g.user_id
    args = request.json

    url = http_util.get_param(args, 'url', "")
    poster = http_util.get_param(args, 'poster', "")

    video = Video.create_video(user_id=user_id, url=url, poster=poster)

    # 记录用户上传视频
    Action.create_action(user_id=user_id,
                         type=BaseConfig.TYPE_ACTION_UPLOAD,
                         res_id=video.id,
                         res_type=BaseConfig.TYPE_VIDEO_PLAY)

    detail = Video.get_video_detail(video.id)

    User.update_user_by_id(id=user_id, last_upload=detail)

    return http_util.return_model()
Beispiel #6
0
def resource_detail():
    """资源详情"""
    args = request.args
    key = http_util.check_params(args, 'res_id', 'res_type')
    if key:
        return http_util.return_param_not_found(key)

    login_user_id = http_util.get_login_user_id(request)

    res_id = http_util.get_param(args, 'res_id')
    res_type = http_util.get_param_int(args, 'res_type')

    try:
        detail = Resource.get_resource_detail(
            res_id=res_id,
            res_type=res_type,
            source_include=['comments', 'related_items', 'view_count',
                            'like_count', 'items',
                            'comment_count'],
            login_user_id=login_user_id
        )

        if not detail:
            return http_util.return_404('res_id not found')

        # 记录用户行为
        Action.create_action(
            user_id=login_user_id,
            type=BaseConfig.TYPE_ACTION_VIEW,
            res_id=res_id,
            res_type=res_type
        )

        return http_util.return_model(
            data=detail
        )
    except BaseException as e:
        app.logger.error(e)
        return http_util.return_internal_server_error()
Beispiel #7
0
def detail():
    login_user_id = g.user_id
    args = request.args
    user_id = args.get('user_id', None)

    detail_id = None
    if user_id:
        detail_id = user_id
    else:
        detail_id = login_user_id

    if not detail_id:
        return http_util.return_404('not found user')

    # 记录用户观看其他用户的记录
    Action.create_action(
        user_id=login_user_id,
        type=BaseConfig.TYPE_ACTION_VIEW,
        res_id=detail_id,
        res_type=BaseConfig.TYPE_USER
    )

    user = get_user_detail(
        id=detail_id,
        login_user_id=login_user_id,
        source_include=['opens', 'view_count', 'attention_count', 'banner']
    )

    if not user:
        return http_util.return_404('user not found')

    if not user_id:
        user['ext']['rongcloud_token'] = rc_user.getToken(
            userId=login_user_id,
            name=user['name'],
            portraitUri=user['portrait']
        ).result['token']

    return return_model(user)
Beispiel #8
0
def signin():
    args = request.json

    key = check_params(args, 'mobile', 'code')

    if key:
        return return_not_found(key)
    mobile = args['mobile']
    code = args['code']
    is_exist = sms.verify_code(
        mobile=mobile,
        code=code
    )

    if mobile.startswith('110') and code == '0000':
        is_exist = True

    if mobile == '15890687745' and code == '0000':
        is_exist = True

    if not is_exist:
        return return_model(message='code {} is not found '.format(code),
                            status=404)

    try:
        user = User.query_user(mobile=mobile)

        # 如果用户不存在则创建
        if not user:
            # 获取token携带用户
            login_user_id = get_login_user_id(request)
            login_user = User.query_user(id=login_user_id)
            # 如果登陆用户不存在,则用手机创建用户
            if not login_user or login_user.mobile:
                user = User.create_user(
                    mobile=mobile,
                    status=BaseConfig.TYPE_USER_NORMAL
                )
            else:
                user = user_db.update_user_by_id(
                    user_id=login_user_id,
                    mobile=mobile,
                    status=BaseConfig.TYPE_USER_NORMAL
                )

        authorization = generate_authorization(user.id)

        # 记录用户登录操作
        Action.create_action(
            user_id=user.id,
            type=BaseConfig.TYPE_ACTION_LOGIN,
            res_id=user.id,
            res_type=BaseConfig.TYPE_USER,
            ext=args
        )

        return return_model(
            header={"authorization": authorization}
        )
    except BaseException as e:
        app.logger.error(e)
        return http_util.return_internal_server_error()
Beispiel #9
0
def signin_open():
    '''第三方登录'''
    args = request.json

    need_keys = ['open_id', 'source', 'name', 'portrait']

    key = check_params(args, *need_keys)

    if key:
        return http_util.return_param_not_found(key)

    login_user_id = get_login_user_id(request)

    try:
        open_id = args['open_id']

        if login_user_id:
            luo = UserOpen.query_open_user(
                id=open_id,
                user_id=login_user_id
            )
            if luo:
                return http_util.return_forbidden('当前用户已登录,不能重复登录')

            login_user = User.query_user(
                id=login_user_id,
                status=BaseConfig.TYPE_USER_NORMAL
            )

            if login_user:
                return http_util.return_forbidden('当前用户已登录,不能重复登录')

        open_user = UserOpen.query_open_user(id=open_id)

        if open_user:

            # 如果第三方用户没有绑定用户,则生成用户并绑定
            if not open_user.user_id:
                binding_user_id = ""
                if login_user_id:
                    binding_user_id = login_user_id
                    User.update_user_by_id(
                        id=login_user_id,
                        name=open_user.name,
                        portrait=open_user.portrait,
                        status=BaseConfig.TYPE_USER_NORMAL
                    )
                else:
                    user = User.create_user(
                        name=args.get('name'),
                        portrait=args.get('portrait')
                    )
                    binding_user_id = user.id

                open_user = UserOpen.update_open_user_by_id(
                    id=open_id,
                    user_id=binding_user_id
                )
        else:
            # 创建第三方用户
            if login_user_id:
                args['user_id'] = login_user_id
                open_user = UserOpen.create_open_user(**args)
                User.update_user_by_id(
                    id=login_user_id,
                    name=open_user.name,
                    portrait=open_user.portrait,
                    status=BaseConfig.TYPE_USER_NORMAL
                )
            else:
                open_user = UserOpen.create_open_user_and_user(**args)

        user_id = open_user.user_id

        authorization = generate_authorization(user_id)

        # 记录用户登录操作
        Action.create_action(
            user_id=user_id,
            type=BaseConfig.TYPE_ACTION_LOGIN,
            res_id=user_id,
            res_type=BaseConfig.TYPE_USER,
            ext=args
        )

        return return_model(
            header={"authorization": authorization}
        )
    except BaseException as e:
        app.logger.error(e)
        return http_util.return_internal_server_error()