當前位置:編程學習大全網 - 源碼下載 - 微信小程序同步微信公眾號文章(二)

微信小程序同步微信公眾號文章(二)

首先確認是否有相應的接口權限,這裏主要用到獲取素材相關的接口,可以看到對應接口文檔,個人號還是有對應權限的。

在新增了永久素材後,開發者可以分類型獲取永久素材的列表:

1、獲取永久素材的列表,也包含公眾號在公眾平臺官網素材管理模塊中新建的圖文消息、語音、視頻等素材 。

2、臨時素材無法通過本接口獲取。

3、調用該接口需/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}}'>tupian </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);

}

}

  • 上一篇:教育類app有哪些
  • 下一篇:linux rm 刪除文件會進回收站嗎
  • copyright 2024編程學習大全網