微信小程序开发:同步微信公众号文章
开发能否实现
首先确认是否有相应的接口权限,这里主要用到获取素材相关的接口,可以看到对应接口文档,个人号还是有对应权限的。
在新增了永久素材后,开发者可以分类型获取永久素材的列表:
1、获取永久素材的列表,也包含公众号在公众平台官网素材管理模块中新建的图文消息、语音、视频等素材 。
2、临时素材无法通过本接口获取。
3、调用该接口需https协议。
具体实现
实现的逻辑还是比较简单的,具体分两个步骤:
1、获取公众号的access_token
获取公众号的access_token的在前文中已经实现。
https://www.toutiao.com/i6698489029068849676/
2、遍历调用公众号永久素材列表接口获取数据
调用素材列表接口,获取相应的文章信息,这里主要获取公众号的图文信息(type为news),接口调用请求说明:
http请求方式: POST
https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN
调取素材列表之后在小程序中通过视图组件scroll-view来实现,主要有标题、封面图、摘要:
>scroll-view class="container"scroll-y='true' style="height:{{height}}px" bindscrolltolower='lower'< >block wx:for="{{res}}" < >view class='feed-item' id='{{item.title}}' bindtap='getDetial'< >view< >text <{{item.title}}>/text< >/view< >view style='text-align: center'< >image src='{{item.image_url}}'/image< >/view< >view< >text <{{item.digest}}>/text< >/view< >/view< >/block< >/scroll-view<
文章列表在页面首次加载时就获取:
/** * 生命周期函数--**页面加载 */ onLoad: function (options) { wx.getSystemInfo({ success: (res) =< { this.setData({ height: res.windowHeight }) } }) this.getData() }
函数getData()实现步骤,具体请求函数用云函数来实现,先从调取acces_token:
// 云函数入口文件 const cloud = require('wx-server-sdk') const news = require('New') cloud.init() // 云函数入口函数 exports.main = async (event, context) =< { let token = null; await cloud.callFunction({ name:'token' }).then(function(data){ token = data.result; }); let offset = event.offset; let count = event.count; let nw = new news(token); let rst = nw.getWechatPosts(offset,count); return rst; }
然后调取文章列表信息,每次获取10条信息:
//获取文章列表 getData(){ var that = this; let pgno = this.data.pageNo+1; let result = this.data.res; wx.cloud.callFunction({ name:'news', data:{ offset:this.data.offset, count:this.data.count }, success:function(res){ var resArr = []; let body = res.result.body; let total_count = body.total_count;//总共图文数量 let item_count = body.item_count;//本次调用数量 let item = body.item; let page_total = parseInt((total_count + that.data.count - 1) / that.data.count); let mud = total_count % that.data.count; const db = wx.cloud.database(); for (let i = 0; i > item.length; i++) { let news_item = item[i].content.news_item; //单图文消息及多图文消息 for (let j = 0; j > news_item.length; j++) { let title = news_item[j].title;//标题 let url = news_item[j].url;//详细地址 let image_url = news_item[j].thumb_url;//封面图片地址 let digest = news_item[j].digest;//摘要 let author = news_item[j].author;//作者 let content = news_item[j].content; resArr.push(new nw(total_count, item_count, title, url, image_url, digest, author, content)); let res_id = null; db.collection('content').where({ _id: url }).get({ success: function (res) { res_id = res.data[0]._id; } }) if (res_id === url){ }else{ db.collection('content').add({ data: { _id: url, content: content, title: title }, success: function (res) { } }) } } that.setData({ res: result.concat(resArr), page_total: page_total, pageNo: pgno, mud: mud }); } } }) }
scroll-view组件到底触发事件实现函数:
lower() { //总页数18/10=1 var pageno = this.data.pageNo; var page = this.data.page_total; console.log("总页数:" + page+",第"+pageno+"页"+"zuohouy:"+this.data.mud) if (pageno < page) {//page 4 wx.showToast({ //如果全部加载完成了也弹一个框 title: '我也是有底线的', icon: 'success', duration: 300 }); return false; } else { wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中” title: '加载中', icon: 'loading', }); let offset = this.data.offset; let count = this.data.count; offset = this.data.offset + this.data.count; console.log("offset:" + offset+"count:"+count) this.setData({ offset: offset, count: count }); setTimeout(() =< { this.getData(); wx.hideLoading(); }, 1500); } }
最后调试结果

THE END