QQ小程序通知消息的推送

原创 麻木  2020-05-07 17:45:15  阅读 2738 次 评论 0 条
摘要:

QQ小程序通知消息的推送


需要用到

  • 登陆QQ小程序开发者后台

  • 需要有服务器用于存放php文件

  • 支持HTTPS,有ssl证书

  • QQ小程序开发者工具

登陆QQ小程序开发者后台选择消息模板

image.png

选择你需要显示的行内容

image.png


设置合法域名,只支持https域名

image.png

添加按钮

<form bindsubmit="form_submit" report-submit="true">

<button  formType="submit">这是按钮</button>

</form>

image.png

添加js

form_submit(e) {

    console.log(e.detail.formId)

    var that = this

    wx.showToast({

      title: '正在发送模板消息请求',

      duration: 5000,

      icon: 'loading',

      mask: true

    })

    //推送消息

    wx.login({

      success: function (res) {

        console.log("获得的code");

        console.log(res)

        var code = res.code;//发送给服务器的code

        console.log("获得用户信息成功");

            if (code) {

              wx.request({

                url: 'https://xxxx/tokentest.php',//服务器的地址,现在微信小程序只支持https请求,所以调试的时候请勾选不校监安全域名

                data: {

                  code: code,

                  formID: e.detail.formId,

                },

                header: {

                  'content-type': 'application/json'

                },

                success: function (res) {

                  console.log(res.data);

                  wx.setStorageSync('useropenid', res.data)

                  wx.showToast({

                    title: '发送模板消息成功!',

                  })

                }

              })

            }

            else {

              console.log("获取用户登录态失败!");

            }

      },

      fail: function (error) {

        console.log('login failed ' + error);

      }

    })

  },

image.png

模板ID在我的模板中复制到下方php中更改

image.png

appid与appsecret在设置-开发设置中

image.png

服务器添加php文件及代码

下方标绿色是需要根据上面的内容改为你的信息

<?php

//require_once('getAccessToken.php');

$appid="111111111";//你的小程序id

$appsecret="1111111111";//你的小程序密钥

function curl_get_https($url)

{

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);

curl_setopt($curl, CURLOPT_HEADER, 0);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  // 跳过检查

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  // 跳过检查

$tmpInfo = curl_exec($curl); 

curl_close($curl);

return $tmpInfo;   //返回json对象

}

function getAccessToken ($appid, $appsecret) {                  

  $url='https://api.q.qq.com/api/getToken?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret;

  //$url='https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code';

  /*

  $arrContextOptions=array(

      "ssl"=>array(

            "verify_peer"=>false,

            "verify_peer_name"=>false,

        ),

    );  

$response = file_get_contents($url, false, stream_context_create($arrContextOptions));

*/

    $info = curl_get_https($url);

    $json = json_decode($info);//对json数据解码

    $arr = get_object_vars($json);

  $access_token = $arr['access_token'];

  return $access_token;

}


  //获得openid

  $code = $_GET['code'];//小程序传来的code值

    //$nick = $_GET['nick'];//小程序传来的用户昵称

    //$imgUrl = $_GET['avaurl'];//小程序传来的用户头像地址

    //$*** = $_GET['***'];//小程序传来的用户性别

   // $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$appsecret.'&js_code=' . $code . '&grant_type=authorization_code';

   $url='https://api.q.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$appsecret.'&js_code=' . $code . '&grant_type=authorization_code';

    //yourAppid为开发者appid.appSecret为开发者的appsecret,都可以从微信公众平台获取;

    //$info = file_get_contents($url);//发送HTTPs请求并获取返回的数据,推荐使用curl

$info = curl_get_https($url);

    $json = json_decode($info);//对json数据解码

    $arr = get_object_vars($json);

    $openid = $arr['openid'];

echo "openid:";

echo $openid;

    $session_key = $arr['session_key'];

$formid = $_GET['formID'];//小程序传来的用户

echo "formid:";

echo $formid;

// 根据你的模板对应的关键字建立数组

// color 属性是可选项目,用来改变对应字段的颜色

date_default_timezone_set("Asia/Shanghai");

$nowtime=date("Y.m.d");

$color="black";


$data_arr = array(

  'keyword1' => array( "value" => "麻木博客首页", "color" => $color ) ,                //这里模板行数几行就添加几行

  'keyword2' => array( "value" => $nowtime, "color" => $color ),

  'keyword3' => array( "value" => "点我前往查看", "color" => $color ) ,

  'keyword4' => array( "value" => "实用技术教程", "color" => $color ) ,

);

$templateid="11111111111111111111111";//这里填自己的模板id


$post_data = array (

  // 用户的 openID,可用过 wx.getUserInfo 获取

  "touser"           => $openid,

  // 小程序后台申请到的模板编号

  "template_id"      => $templateid,

  // 点击模板消息后跳转到的页面,可以传递参数

  "page"             => "/pages/home/index",

  // 第一步里获取到的 formID

  "form_id"          => $formid,

  // 数据

  "data"             => $data_arr,

  // 需要强调的关键字,会加大居中显示

 // "emphasis_keyword" => "keyword2.DATA"


);

        

// 发送 POST 请求的函数

// 你也可以用 cUrl 或者其他网络库,简单的请求这个函数就够用了         

function send_post( $url, $post_data ) {

  $options = array(

    'http' => array(

      'method'  => 'POST',

      // header 需要设置为 JSON

      'header'  => 'Content-type:application/json',

      'content' => $post_data,

      // 超时时间

      'timeout' => 60

    ),

"ssl"=>array(

            "verify_peer"=>false,

            "verify_peer_name"=>false,

        )

  );


  $context = stream_context_create( $options );

  $result = file_get_contents( $url, false, $context );


  return $result;

}


// 这里替换为你的 appID 和 appSecret

$url = "https://api.q.qq.com/api/json/template/send?access_token=".getAccessToken ($appid, $appsecret);  

// 将数组编码为 JSON

$data = json_encode($post_data, true);   


// 这里的返回值是一个 JSON,可通过 json_decode() 解码成数组

$return = send_post( $url, $data);

var_dump($return);


?>

保存预览测试

2232.gif

扫描查看或者QQ小程序搜索:麻木博客查看我的小程序

QQ小程序通知消息的推送第9张-麻木站

打赏 支付宝打赏 微信打赏
 您阅读本篇文章共花了: 

本文地址:https://mamublog.cn/post/131.html
版权声明:本文为原创文章,版权归 mamublog 所有,欢迎分享本文,转载请保留出处!

推荐文章

发表评论


表情

还没有留言,还不快点抢沙发?