<strike id="cy2gs"><menu id="cy2gs"></menu></strike>
  • <del id="cy2gs"><dfn id="cy2gs"></dfn></del>
  • 微信小程序發送訂閱消息(之前是模板消息)

    2020-8-20    seo達人

    之前的模板消息已經廢棄,現在改為訂閱消息,訂閱消息發布前,需要用戶確認后才能接收訂閱消息。


    image


    小程序端

    index.wxml


    <button bindtap="send">發送訂閱消息</button>

    index.js


    const app = getApp()

    Page({ data: {

     }, send:function(){

       wx.requestSubscribeMessage({ tmplIds: ['WZiCliW1zVtHXqX7dGnFNmFvxhW-wd9S_W4WfrwNvss'],


    success:(res)=> { // 在登錄的時候,獲取到的openid進行緩存,現在直接把openid提取出來即可 wx.getStorage({ key: 'openid',


             success (res) { console.log(res.data)

               wx.request({ url: 'https://www.xxx.com/send.php?openid='+res.data, data: {},


    header: { 'content-type': 'application/json' },


                 success (res) { // 推送 if(res.data.errcode == '43101'){ console.log("拒絕訂閱消息")

                   }else if(res.data.errcode == '0'){ console.log("發送訂閱消息")

                   }else{ console.log("未知錯誤")

                   }

                 }

               })

             },

             fail (res) { console.log("沒有openid,無法發送")

             }

           })

         }

       })

     }

    })

    后端

    <?php //設置 header  header("Content-type:application/json"); //接收參數 $openid = $_GET["openid"];


    //初始化 CURL $ch = curl_init(); // 獲取access_token // include ''; require_once("access_token.php");


    //目標服務器地址  curl_setopt($ch, CURLOPT_URL,


    'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$access_token);


    //設置要POST的數據 curl_setopt($ch, CURLOPT_POST, true);


    $data = '{

     "touser": "'.$openid.'",

     "template_id": "模板ID",

     "page": "pages/index/index",// 要跳轉的頁面

     "lang":"zh_CN",

     "data": {

         "thing4": {

             "value": "歡迎使用專插本最前線小程序"

         },

         "thing5": {

             "value": "小程序由公眾號:廣東專插本最前線開發"

         }

     }

    }';

    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); // 對認證證書來源的檢查 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 從證書中檢查SSL加密算法是否存在 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); //獲取的信息以文件流的形式返回,而不是直接輸出 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //發起請求 $result = curl_exec($ch); echo $result; //關閉請求 curl_close($ch); ?>

    access_token.php


    <?php // 聲明頁面header header("Content-type:charset=utf-8"); // APPID、APPSECRET $appid = "你的小程序APPID";

    $appsecret = "你的小程序APPSECRET"; // 獲取access_token和jsapi_ticket function getToken(){

       $file = file_get_contents("access_token.json",true);//讀取access_token.json里面的數據 $result = json_decode($file,true); //判斷access_token是否在有效期內,如果在有效期則獲取緩存的access_token //如果過期了則請求接口生成新的access_token并且緩存access_token.json if (time() > $result['expires']){

           $data = array();

           $data['access_token'] = getNewToken();

           $data['expires'] = time()+7000;

           $jsonStr =  json_encode($data);

           $fp = fopen("access_token.json", "w");

           fwrite($fp, $jsonStr);

           fclose($fp); return $data['access_token'];

       }else{ return $result['access_token'];

       }

    } //獲取新的access_token function getNewToken($appid,$appsecret){ global $appid; global $appsecret;

       $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret."";

       $access_token_Arr =  file_get_contents($url);

       $token_jsonarr = json_decode($access_token_Arr, true); return $token_jsonarr["access_token"];

    }


    $access_token = getToken(); ?>

    邏輯

    1、通過button控件出發send函數

    2、send函數調用wx.requestSubscribeMessageAPI,微信允許接收訂閱消息

    3、 wx.request向send.php后端請求

    4、后端獲取access_token后,調用訂閱消息接口POST一段json數據即可發送訂閱消息


    官方文檔

    1、https://developers.weixin.qq.com/miniprogram/dev/api/open-api/subscribe-message/wx.requestSubscribeMessage.html


    2、https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.addTemplate.html


    Author:TANKING

    Date:2020-08-24

    Web:http://www.likeyun.cn/

    WeChat:face6009

    藍藍設計www.skdbbs.com )是一家專注而深入的界面設計公司,為期望卓越的國內外企業提供卓越的UI界面設計、BS界面設計 、 cs界面設計 、 ipad界面設計 、 包裝設計 、 圖標定制 、 用戶體驗 、交互設計、 網站建設 平面設計服務

    日歷

    鏈接

    個人資料

    藍藍設計的小編 http://www.skdbbs.com

    存檔

    主站蜘蛛池模板: 国99精品无码一区二区三区| 99精品热这里只有精品| 亚洲AV乱码久久精品蜜桃| 一级香蕉精品视频在线播放| 无码精品人妻一区二区三区AV| 欧美亚洲国产精品第一页| 无码国产69精品久久久久网站| 精品无码国产自产拍在线观看蜜| 国内精品91最新在线观看| 久久丫精品国产亚洲av| 日本免费精品一区二区三区| 色花堂国产精品第一页| 精品深夜AV无码一区二区| 无码人妻精品一区二区三区夜夜嗨| 男女男精品网站免费观看 | 欧美日韩精品久久久免费观看| 91嫩草亚洲精品| 中文字幕一区二区精品区| 国产亚洲欧洲精品| 99精品在线观看| 久久99精品久久久久久久不卡 | 国产成人精品天堂| 99久久精品费精品国产一区二区| 精品一区二区三区免费毛片爱| 野狼精品社区| 亚洲欧洲成人精品香蕉网| 一区二区三区精品高清视频免费在线播放 | 500av导航大全精品| 久久久久久亚洲精品成人| 青青草原精品99久久精品66| 亚洲国产精品va在线播放 | 国产欧美久久久精品| 国产精品大白天新婚身材| 国产高清在线精品一区| 精品四虎免费观看国产高清午夜| 欧美视频精品一区二区三区 | 日本精品自产拍在线观看中文| 久久久久九九精品影院| 精品精品国产欧美在线小说区| 国产久爱免费精品视频| 国产精品高清免费网站|