本文是对百度小程序获取织梦文章内容的实现的方法。
接口标识:show
接口url:https://你的网站/api/index.php?action=show[&id=10]
接口参数:
小程序代码
- swan.request({
- url: app.globalData.api + "action=show",
- data: {
- typeid: that.data.typeid,
- id: that.data.id,
- relation: 1,//开启调用相关推荐
- aid: app.globalData.aid
- },
- method: 'POST',
- header: {
- 'content-type': 'application/x-www-form-urlencoded',
- 'x-appsecret': app.globalData.appsecret
- },
- success: function (res) {
- var content = res.data.data;
- ......
- }
- })
说明:
(1)该页面引入了require('../../utils/util.js'),里边封装的有对时间的处理,因为api返回的是时间戳,需要处理
- content.relations[i].pubdate = util.formatTime(content.relations[i].pubdate, 'Y-M-D');
(2)由于小程序不支持直接的html标签,所以引入bdparse插件,具体参考demo。
(3)relation默认关闭,需要修改代码来开启,参考demo
(4)字段中如果有数组,需要进行特殊处理,代码实例:
- //判断是否有组图自动判断
- if (content.pictureurls){
- content.pictureurls = JSON.parse(content.pictureurls)
- }
(5)相关内容推荐,swan页面做了判断,如果没有内容默认不显示,而相关评价却没加,自己根据需要修改。
(6)接口返回的数据包含文章的上一篇和下一篇,调用代码:
- that.setData({
- pre: content.preNext.pre,
- next: content.preNext.next,
- })
前端代码:
- <view class="nextpage mt10" bindtap="nextpage" data-id="{{pre.id}}">上一篇:{{pre.title}}</view>
- <view class="nextpage" bindtap="nextpage" data-id="{{next.id}}" >下一篇:{{next.title}}</view>
收藏