
在新增了永久素材后,开发者可以分类型获取永久素材的列表:
1、获取永久素材的列表,也包含公众号在公众平台官网素材管理模块中新建的图文消息、语音、视频等素材 。
2、临时素材无法通过本接口获取。
3、调用该接口需https协议。
实现的逻辑还是比较简单的,具体分两个步骤:
1、获取公众号的access_token
获取公众号的access_token的在前文中已经实现。
基于微信小程序云函数的方式获取微信公众号access_token -
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}}'>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 = 0i <item.lengthi++) {
let news_item = item[i].content.news_item
//单图文消息及多图文消息
for (let j = 0j <news_item.lengthj++) {
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({ //如果全部加载完成了也d一个框
title: '我也是有底线的',
icon: 'success',
duration: 300
})
return false
} else {
wx.showLoading({ //期间为了显示效果可以添加一个过度的d出框提示“加载中”
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)
}
}
多次拒绝登录和允许登录微信小程序的登录获取用户信息,是通过微信d出窗口,用户可点击允许 和拒绝两个按钮,点击允许,则获取用户信息登录成功,若点击拒绝,则获取失败,可通过二次请求调用d起获取用户信息窗口。
具体代码如下:
//app.js
App({
onLaunch: function () {
//调用API从本地缓存中获取数据
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
console.log('logs',logs)
},
getUserInfo:function(cb){
var that = this
//调用登录接口
function getOpenid(backMsg){
that.globalData.userInfo = backMsg.userInfo
that.globalData.encryptedData = backMsg.encryptedData
that.globalData.iv = backMsg.iv
that.globalData.login = true
console.log(that.globalData.code)
wx.request({
url:that.localUrl+'Login/sendCodeLogin',
data:{
code:that.globalData.code,
encryptedData: backMsg.encryptedData,
iv:backMsg.iv
},
success:function(openData){
console.log('返回openid',openData,openData.data)
if(openData.data.code==1001){
that.globalData.openid = openData.data.data.openid
wx.setStorageSync('openid',that.globalData.openid)
wx.setStorageSync('userInfo',that.globalData.userInfo)
wx.setStorageSync('isManager', openData.data.data.isManager)
wx.showToast({
title: '登录成功',
icon: 'success',
duration: 500
})
typeof cb == "function" &&cb(that.globalData.userInfo)
}else{
wx.showLoading({
title: '登录失败'
})
setTimeout(function () {
wx.hideLoading()
}, 500)
}
}
})
}
wx.login({
success: function (msg) {
console.log('code',msg)
if(msg.code){
that.globalData.code = msg.code
if(that.globalData.login==false){
wx.openSetting({
success: function (data) {
if(data) {
if (data.authSetting["scope.userInfo"] == true) {
//loginStatus = true
wx.getUserInfo({
withCredentials: false,
success: function (res) {
console.log('第二次成功',res)
getOpenid(res)
},
fail: function (res) {
that.globalData.login = false
console.log('二次失败',res)
}
})
}else{
that.globalData.login = false
console.log('二次失败02')
}
}
},
fail: function () {
console.info("设置失败返回数据")
}
})
}else{
wx.getUserInfo({
success: function (res) {
console.log('第一次成功',res)
getOpenid(res)
},fail:function(msg){
that.globalData.login = false
console.log('第一次失败',msg)
}
})
}
}
},
fail:function(res){
console.log(res)
}
})
},
globalData:{
userInfo:null,
encryptedData:null,
iv:null,
openid:null,
code:null
}
})
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)