随身笔记
随身笔记

推荐:node.js微信H5页面jssdk签名配置

公众号,小程序里面使用H5页面基本都会有分享H5的功能,这时候使用JS-SDK签名

https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html

 

html:

axios.get('https://gzh.jskoa.com/getjsapi_ticket?url='+window.location.href).then((res)=>{ //**注意这里的url不能带有#的hash,具体看文档

  wx.config({
    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
    appId: 'wx1d062c6797dd4774', // 必填,公众号的唯一标识
    timestamp: res.data.timestamp, // 必填,生成签名的时间戳
    nonceStr: res.data.noncestr, // 必填,生成签名的随机串
    signature: res.data.signature, // 必填,签名
    jsApiList: [
      'checkJsApi',
      'onMenuShareTimeline',
      'onMenuShareAppMessage',
      'updateTimelineShareData',
      'updateAppMessageShareData',
      'onMenuShareQQ',
      'onMenuShareWeibo',
      'hideMenuItems',
      'chooseImage',
      'updateAppMessageShareData',
      'scanQRCode'
    ] // 必填,需要使用的JS接口列表
  });


  //分享功能
  wx.ready(function () { //需在用户可能点击分享按钮前就先调用
    var shareData = {
      title: '分享标题', // 分享标题
      desc: '分享描述', // 分享描述
      link: 'https://gzh.jskoa.com', // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
      imgUrl: 'https://gzh.jskoa.com/images/1.png', // 分享图标
      success: function (res) {
        // 设置成功
      },
      error:function(res){
      }
    };
    //自定义“分享给朋友”及“分享到QQ”按钮的分享内容(1.4.0)
    wx.updateAppMessageShareData(shareData);
    //自定义“分享到朋友圈”及“分享到QQ空间”按钮的分享内容(1.4.0)
    wx.updateTimelineShareData(shareData);
  });



  // wx.error(function(res){
  //   alert(JSON.stringify(res))
  //   console.log(JSON.stringify(res))
  // })


})

 

 

node.js后台

//signature签名算法

sign

*需要把access_token和ticket存在服务器上,它们2个小时后才过期,不能每次请求一次页面都要重新获取,有次数限制的

var sign = require('./sign.js');

router.get('/getjsapi_ticket', async (ctx, next) => {


    let tokenInfo = fs.existsSync('token_info.json')
        ? JSON.parse(fs.readFileSync('token_info.json', 'utf-8'))
        : null
    let expires_time = tokenInfo ? tokenInfo.expires_time : ''
    let cache_access_token =
        tokenInfo && tokenInfo.access_token ? tokenInfo.access_token : ''

    let cache_ticketjson = fs.existsSync('ticket.json')
        ? JSON.parse(fs.readFileSync('ticket.json', 'utf-8'))
        : null

    if (
        parseInt(Date.now() / 1000) > expires_time + 3600 ||  //设置1个小时就更新
        tokenInfo == null ||
        cache_access_token == ''
    ) {


      //拿token
      var gettoken =  await new Promise(function (resolve, reject) {


                axios.get(`https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${appsecret}` ).then(function (response2) {

                    fs.writeFileSync(
                        'token_info.json',
                        JSON.stringify({
                            access_token: response2.data.access_token,
                            expires_time: parseInt(Date.now() / 1000),
                        })
                    )

                    resolve(response2.data.access_token)
                })
                    .catch(function (error) {
                        console.log(error);
                    });

        });


        //拿ticket
       var ticketjson = await new Promise((a,b)=>{

            axios.get(`https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=${gettoken}&type=jsapi` ).then(function (response2) {
                // {
                //     errcode: 0,
                //     errmsg: 'ok',
                //     ticket: 'O3SMpm8bG7kJnF36aXbe8_JQGXJ4DukP3a02ntc7x9lrWoqujnAjetfpaZzA1XGF6G9SVgw8M-W1Cpn7sfA0ZA',
                //     expires_in: 7200
                // }

                fs.writeFileSync(
                    'ticket.json',
                    JSON.stringify(sign(response2.data.ticket, ctx.request.query.url))
                )
                a(sign(response2.data.ticket, ctx.request.query.url)) //ticket换取signature签名


            })
                .catch(function (error) {
                    console.log(error);
                });
        })


        await new Promise((a,b)=>{
            ctx.body = ticketjson //wx.config配置返回给前端
            a(1)
        })
    } else {
        ctx.body =  sign(cache_ticketjson.jsapi_ticket, ctx.request.query.url) //有缓存就拿缓存数据 wx.config配置返回给前端
    }

})

 

展示一个分享案例:

https://sdeno.com/wp-content/uploads/2021/07/clipboard111.png

一定要在微信上操作,或者开发者工具

 

微信公众号页面授权获取用户基本信息

 

微信端分享、手机QQ好友分享以及小程序和H5数据交互

 

微信jssdk授权签名以及config配置

 

微信内置浏览器打开h5页面上传图片

《解决vue hash模式微信分享#号问题》

 

没有标签
首页      前端资源      推荐:node.js微信H5页面jssdk签名配置

随身笔记

推荐:node.js微信H5页面jssdk签名配置
公众号,小程序里面使用H5页面基本都会有分享H5的功能,这时候使用JS-SDK签名 https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html   html: axios…
扫描二维码继续阅读
2021-07-05