小程序中打开公众号文章(node版)

1.技术要求

   微信开发经验+小程序开发经验+会一点node

2.思路

    在6月份的一次更新,webview组件增加新的能力,webview 指向网页的链接。可打开关联的公众号的文章,其它网页需登录小程序管理后台配置业务域名。所以我们可以利用微信提供的"https://api.weixin.qq.com/cgi-bin/material/batchget_material“接口获取所有的素材列表,我们可以将这些文章存入数据库,方便使用,在将web-view 中的src替换为获取到的素材文章的永久链接,即可

3.源码demo

1.获取微信中的素材列表

//使用了request这个库用于服务端请求

        const token_url="https://api.weixin.qq.com/cgi-bin/token";
        const url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material";
        const appid="自己的appid";
        const appsecret="自己的APPsecret";
        const type="news";
        const offset="0";
        const count="20";
        //获取token
        let token_info=await new Promise(function(resolve,reject){
            request.get(`${token_url}?grant_type=client_credential&appid=${appid}&secret=${appsecret}`,function(error, response, body){
                if (!error && response.statusCode == 200) {
                    resolve(body);
                }
                reject(error);
            });
        })
        token_info=JSON.parse(token_info);
        const access_token=token_info.access_token;
        let data = {
            "type": type,
            "offset":offset,
            "count": count
        };
       // 获取素材列表
        const res=await new Promise(function (resolve, reject) {
            request.post({url:`${url}?access_token=${access_token}`, form:JSON.stringify(data)}, function (error, response, body) {
                console.log("body-->",body);
                if (!error && response.statusCode == 200) {
                    resolve(body);
                }
                reject(error);
            })
        });


        console.log("res-->",res);

2.小程序端

//src 中填入获取到的永久链接

<web-view src="http://mp.weixin.qq.com/s?__biz=MzI3MjM1MTUzNA==&mid=100000180&idx=1&sn=069844e709fdbe0aaa4129c97977b335&chksm=6b32a9f55c4520e3c7d9ec18b6e79a02689ea23ae01006ff7dc47677588b8b22a8c1e531661b#rd"></web-view>

3.结果

koahub node.js商城 

转载于:https://my.oschina.net/u/2433463/blog/1858662

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值