{% import “/js-sdk/macro/total_count.md” as totalCount %}

内容库操作

获取内容库列表

{% ifanrxCodeTabs %} wx.BaaS.ContentGroup.find(options) {% endifanrxCodeTabs %}

参数说明

opions:

参数 类型 必填 默认 说明
withCount boolean 否 false 是否返回 total_count
offset number 否 0 偏移量
limit number 否 20 最大返回条数

请求示例

{% ifanrxCodeTabs %}

  1. wx.BaaS.ContentGroup.find({withCount: true, offset: 0, limit: 20}).then(res => {
  2. // success
  3. }, err => {
  4. // err
  5. })

{% endifanrxCodeTabs %}

返回示例

res.data:

  1. {
  2. meta: {
  3. limit: 20,
  4. offset: 0,
  5. total_count: 1,
  6. next: null,
  7. previous: null,
  8. },
  9. objects: [{
  10. id: 1234567890,
  11. name: '测试内容库',
  12. }],
  13. }

获取内容库详情

{% ifanrxCodeTabs %} wx.BaaS.ContentGroup.get(contentGroupID) {% endifanrxCodeTabs %}

参数说明

参数 类型 必填 说明
contentGroupID Number 是 内容库 ID

请求示例

{% ifanrxCodeTabs %}

  1. let contentGroupID = 1513076211190694
  2. wx.BaaS.ContentGroup.get(contentGroupID).then(res => {
  3. // success
  4. }, err => {
  5. // err
  6. })

{% endifanrxCodeTabs %}

返回示例

res.data:

  1. {
  2. id: 1234567890,
  3. name: '测试内容库',
  4. }

内容操作

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

{% ifanrxCodeTabs %} let MyContentGroup = new wx.BaaS.ContentGroup(contentGroupID) {% endifanrxCodeTabs %}

参数说明

参数 类型 必填 说明
contentGroupID Number 是 内容库 ID

获取内容详情

MyContentGroup.getContent(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 更新时间
visit_count Number 文章阅读数

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

visit_count 字段,只有在已经开通了“文章统计”(“阅读数支持”)功能,且该文章的阅读数大于 0 时,才会返回。

“文章阅读数统计”是一个异步的操作,统计结果略有延迟。

请求示例

{% ifanrxCodeTabs %}

  1. let contentGroupID = 1513076211190694
  2. let richTextID = 1514529306082815
  3. let MyContentGroup = new wx.BaaS.ContentGroup(contentGroupID)
  4. MyContentGroup.getContent(richTextID).then(res => {
  5. // success
  6. }, err => {
  7. // err
  8. })

{% endifanrxCodeTabs %}

返回示例

res.data:

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

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

MyContentGroup.count()

info SDK v3.0 新增

{% ifanrxCodeTabs %}

  1. let query = new wx.BaaS.Query()
  2. query.arrayContains('categories', [1513076252710475])
  3. MyContentGroup.setQuery(query).count().then(num => {
  4. // success
  5. console.log(num) // 10
  6. }, err => {
  7. // err
  8. })

{% endifanrxCodeTabs %}

查询,获取内容列表

MyContentGroup.find(options)

参数说明

options:

参数 类型 必填 默认 说明
withCount boolean 否 false 是否返回 total_count

{{totalCount.withCountTips()}}

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

info MyContentGroup.find() 接口返回的内容中,不包含 content 字段。

请求示例

{% ifanrxCodeTabs %}

  1. // 查找该内容库下的所有内容
  2. MyContentGroup.find().then()
  3. // 查找该内容库下在指定分类下的内容
  4. let query = new wx.BaaS.Query()
  5. query.arrayContains('categories', [1513076252710475])
  6. MyContentGroup.setQuery(query).find().then(res => {
  7. // success
  8. }, err => {
  9. // err
  10. })

{% endifanrxCodeTabs %}

筛选字段

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

扩展字段

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

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

请求示例 1

  1. MyContentGroup.select(['-title']).expand('pointer_test_oder').getContent(1513076305938456).then(res => {
  2. // success
  3. }, err => {
  4. // err
  5. })

请求结果 1

  1. {
  2. "statusCode": 200,
  3. "data": {
  4. "categories": [
  5. 1513076252710475
  6. ],
  7. "cover": "https://cloud-minapp-1131.cloud.ifanrusercontent.com/1donykIpnuvcRiAX.jpg",
  8. "created_at": 1513076305,
  9. "created_by": 16042162,
  10. "description": "iphoneX 发布",
  11. "group_id": 1513076211190694,
  12. "id": 1513076305938456,
  13. "updated_at": 1513076364,
  14. "pointer_test_order": {
  15. "created_at": 1538966895,
  16. "_table": "test_order",
  17. "id": "5bbac56fbd66033df7fd0aa2",
  18. "created_by": 61736923,
  19. "updated_at": 1538966895
  20. }
  21. }
  22. }

请求示例 2

  1. MyContentGroup.select(['title', 'pointer_test_oder']).expand('pointer_test_oder').find().then(res => {
  2. // success
  3. }, err => {
  4. // err
  5. })

请求结果 2

  1. {
  2. "statusCode": 200,
  3. "data": {
  4. "meta": {
  5. "next": null,
  6. "offset": 0,
  7. "total_count": 1,
  8. "limit": 20,
  9. "previous": null
  10. },
  11. "objects": [
  12. {
  13. "title": "iphone X",
  14. "pointer_test_order": {
  15. "created_at": 1538966895,
  16. "_table": "test_order",
  17. "id": "5bbac56fbd66033df7fd0aa2",
  18. "created_by": 61736923,
  19. "updated_at": 1538966895,
  20. "pointer_test_order": {
  21. "created_at": 1538966895,
  22. "_table": "test_order",
  23. "id": "5bbac56fbd66033df7fd0aa2",
  24. "created_by": 61736923,
  25. "updated_at": 1538966895
  26. }
  27. }
  28. }
  29. ]
  30. }
  31. }

获取分类详情

MyContentGroup.getCategory(categoryID)

OBJECT 参数说明

参数 类型 必填 说明
categoryID Number 是 分类 ID

返回参数

参数 类型 说明
children Array 子分类列表
have_children Boolean 是否含有子分类
id Number 分类 ID
name String 分类名称

请求示例

  1. let categoryID = 1513076252710475
  2. MyContentGroup.getCategory(categoryID).then(res => {
  3. // success
  4. }, err => {
  5. // err
  6. })

返回示例

res.data:

  1. {
  2. "have_children": true,
  3. "id": 1513076252710475,
  4. "name": "科技",
  5. "children": [
  6. {
  7. "have_children": false,
  8. "id": 1514515552050186,
  9. "name": "评测"
  10. }
  11. ]
  12. }

获取内容库分类列表

MyContentGroup.getCategoryList()

请求示例

  1. MyContentGroup.getCategoryList().then(res => {
  2. // success
  3. }, err => {
  4. // err
  5. })

分页与排序

内容查询的分页与排序操作和数据表分页与排序方法一致。

请求示例

  1. MyContentGroup.orderBy('-created_by').limit(5).offset(10).find().then()