内容

内容操作

以下操作都需指明操作的内容库,方法如下:

let MyContent = new BaaS.Content(contentGroupID)

参数说明

参数类型必填说明

contentGroupID

Number

内容库 ID

创建内容

创建步骤

创建内容的步骤与创建数据记录的方法相同,这里只做简单说明。

1.本地创建一条空记录

let content = MyContent.create()

2.为上面创建的空记录赋值

content.set(data)

3.将创建的记录保存到服务器

content.save()

参数说明

参数类型必填说明

title

String

内容标题

content

String

详细容

cover

File

封面图

description

String

内容摘要

categories

Integer Array

内容所属分类

返回参数

参数类型说明

categories

Number Array

内容分类

content

String

内容详情

cover

String

封面图 url

created_at

Number

创建时间

created_by

Number

user ID

description

String

摘要

group_id

Number

内容库 ID

id

Number

内容 ID

title

String

内容标题

update_at

Number

更新时间

info 如果有自定义字段,则一并返回

暂时不支持批量创建内容

请求示例

let MyContent = new BaaS.Content(groupID)
let content = MyContent.create()
let data = {
  title: 'Test Title',
  content: 'test content'  
}
content.set(data).save().then(res => {
  // success
}, err => {
  // err
})

返回示例

res.data:

{
  "categories": [],
  "content": "test content",
  "created_at": 1536818888,
  "group_id": 1534478179506295,
  "id": 1536818888733676,
  "title": "Test Title",
  "updated_at": 1536818888
}

获取内容详情

MyContent.get(richTextID)

参数说明

参数名类型必填说明

richTextID

Number

内容 ID

返回参数

参数类型说明

categories

Number Array

内容分类

content

String

内容详情

cover

String

封面图 url

created_at

Number

创建时间

created_by

Number

user ID

description

String

摘要

group_id

Number

内容库 ID

id

Number

内容 ID

title

String

内容标题

update_at

Number

更新时间

info 如果有自定义字段,则一并返回

请求示例

let richTextID = 1514529306082815
MyContent.get(richTextID).then(res => {
  // success
}, err => {
  // err
})

返回示例

res.data:

{
  "categories": [1513076252710475],
  "content": "<p>\b 该片讲述了伊娅不满父亲的恶作剧</p>",
  "cover": "https://cloud-minapp-1131.cloud.ifanrusercontent.com/1donykIpnuvcRiAX.jpg",
  "created_at": 1513076305,
  "created_by": 16042162,
  "description": "iphoneX 发布",
  "group_id": 1513076211190694,
  "id": 1513076305938456,
  "title": "iphone X",
  "updated_at": 1513076364
}

获取符合筛选条件的内容总数

BaaS.Content#count()

let query = new BaaS.Query()
query.arrayContains('categories', [1513076252710475])
let num = await MyContent.setQuery(query).count()
console.log(num)  // 10
let query = new BaaS.Query()
query.arrayContains('categories', [1513076252710475])
MyContent.setQuery(query).count().then(num => {
  // success
  console.log(num)  // 10
  callback(null, res)
}, err => {
  // err
  callback(err)
})

查询,获取内容列表

BaaS.Content#find(options)

参数说明

options:

参数类型必填默认说明

withCount

boolean

true

是否返回 total_count

{{totalCount.withCountTips()}}

内容查询与数据表查询方法一致

请求示例

// 查找该内容库下的所有内容
MyContent.find().then()

// 查找该内容库下在指定分类下的内容
let query = new BaaS.Query()
query.arrayContains('categories', [1513076252710475])
MyContent.setQuery(query).find().then(res => {
  // success
}, err => {
  // err
})

筛选字段

select 使用方法可以参考数据表 - 字段过滤小节

扩展字段

expand 使用方法可以参考数据表 - 字段扩展小节

假设 _richtextcontent 表中有一个类型为 pointer 的字段,名称为 pointer_test_oder, 指向了 test_order 表

请求示例

let richTextID = 1514529306082815
MyContent.select(['content', 'pointer_test_order']).expand('pointer_test_order').get(richTextID).then(res => {
  // success
}, err => {
  // err
})

返回示例

res.data:

{
  "content": "<p>\b 该片讲述了伊娅不满父亲的恶作剧</p>",
  "pointer_test_order": {
    "created_at": 1538966895,
    "_table": "test_order",
    "id": "5bbac56fbd66033df7fd0aa2",
    "created_by": 61736923,
    "updated_at": 1538966895
  }    
}

请求示例

let richTextID = 1514529306082815
MyContent.select(['content', 'pointer_test_order']).expand('pointer_test_order').find().then(res => {
  // success
}, err => {
  // err
})

返回示例

res.data:

{
    "meta": {
      "next": null,
      "offset": 0,
      "total_count": 1,
      "limit": 20,
      "previous": null
    },
    "objects": [
      {
        "content": "<p>\b 该片讲述了伊娅不满父亲的恶作剧</p>",
        "pointer_test_order": {
          "created_at": 1538966895,
          "_table": "test_order",
          "id": "5bbac56fbd66033df7fd0aa2",
          "created_by": 61736923,
          "updated_at": 1538966895
        }
      }
    ]
  }

更新内容

更新内容的步骤与更新数据记录的方法相同。

请求示例

const content = MyContent.getWithoutData(richTextID)
content.set({
  title: 'update title'
})
content.update().then(res => {
  // success
}, err => {
  // err
})

返回示例

res.data:

{
  "categories": [],
  "content": "test content new 0001",
  "created_at": 1536826933,
  "group_id": 1534478179506295,
  "id": 1536826933728326,
  "title": "update title",
  "updated_at": 1536828592
}

info 如果有自定义字段,则一并返回

暂时不支持批量更新内容

删除内容

MyContent.delete()

创建步骤

更新内容的步骤与删除数据记录的方法相同。

请求示例

MyContent.delete(richTextID).then(res => {
  // success
}, err => {
  // err
})

返回示例

res.data:

""

info 暂时不支持批量删除内容

Last updated