微信小程序> 微信公众号分享,微信小程序同步微信公众号文章二

微信公众号分享,微信小程序同步微信公众号文章二

浏览量:517 时间: 来源:老何
开发能否实现首先确认是否有相应的接口权限,这里主要用到获取素材相关的接口,可以看到对应接口文档,个人号还是有对应权限的。
在新增了永久素材后,开发者可以分类型获取永久素材的列表:
1、获取永久素材的列表,也包含公众号在公众平台官网素材管理模块中新建的图文消息、语音、视频等素材。
2、临时素材无法通过本接口获取。
3、调用该接口需https协议。
具体实现实现的逻辑还是比较简单的,具体分两个步骤:
1、获取公众号的access_token
获取公众号的access_token的在前文中已经实现。
2、遍历调用公众号永久素材列表接口获取数据
调用素材列表接口,获取相应的文章信息,这里主要获取公众号的图文信息(type为news),接口调用请求说明:
http请求方式:POSThttps://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN调取素材列表之后在小程序中通过视图组件scroll-view来实现,主要有标题、封面图、摘要:
scroll-viewscroll-y='true'style="height:{{height}}px"bindscrolltolower='lower'blockwx:for="{{res}}"viewclass='feed-item'id='{{item.title}}'bindtap='getDetial'viewtext{{item.title}}/text/viewviewstyle='text-align:center'imagesrc='{{item.image_url}}'tupian/image/viewviewtext{{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:
//云函数入口文件constcloud=require('wx-server-sdk')constnews=require('New')cloud.init()//云函数入口函数exports.main=async(event,context)={lettoken=null;awaitcloud.callFunction({name:'token'}).then(function(data){token=data.result;});letoffset=event.offset;letcount=event.count;letnw=newnews(token);letrst=nw.getWechatPosts(offset,count);returnrst;}然后调取文章列表信息,每次获取10条信息:
//获取文章列表getData(){varthat=this;letpgno=this.data.pageNo+1;letresult=this.data.res;wx.cloud.callFunction({name:'news',data:{offset:this.data.offset,count:this.data.count},success:function(res){varresArr=[];letbody=res.result.body;lettotal_count=body.total_count;//总共图文数量letitem_count=body.item_count;//本次调用数量letitem=body.item;letpage_total=parseInt((total_count+that.data.count-1)/that.data.count);letmud=total_count%that.data.count;constdb=wx.cloud.database();for(leti=0;iitem.length;i++){letnews_item=item[i].content.news_item;//单图文消息及多图文消息for(letj=0;jnews_item.length;j++){lettitle=news_item[j].title;//标题leturl=news_item[j].url;//详细地址letimage_url=news_item[j].thumb_url;//封面图片地址letdigest=news_item[j].digest;//摘要letauthor=news_item[j].author;//作者letcontent=news_item[j].content;resArr.push(newnw(total_count,item_count,title,url,image_url,digest,author,content));letres_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=1varpageno=this.data.pageNo;varpage=this.data.page_total;console.log("总页数:"+page+",第"+pageno+"页"+"zuohouy:"+this.data.mud)if(pagenopage){//page4wx.showToast({//如果全部加载完成了也弹一个框title:'我也是有底线的',icon:'success',duration:300});returnfalse;}else{wx.showLoading({//期间为了显示效果可以添加一个过度的弹出框提示“加载中”title:'加载中',icon:'loading',});letoffset=this.data.offset;letcount=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);}}最后调试结果

更多精彩内容,敬请关注我的微信公众号:linux白帽子

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎