TP5微信小程序开发批量推送服务通知那点事

开发环境:PhpStorm+Xampp(pthread多线程插件)
pthread插件下载地址:http://windows.php.net/downloads/pecl/releases/pthreads/
pthread扩展安装方法:http://www.cnblogs.com/renzhicai/p/7862963.html

写在前面的话

对于批量推送服务通知,在这里先大致说一下整体思路是怎么样的。
第一,前台小程序方面,需要获取足够多的formid,需要注意的就是,小程序没有那么多表单提交的时候,需要自己创造表单来提交。说白了,也就是自定义微信小程序button的样式,让用户在毫不知情的情况下进行一次表单提交,并获取它的formid并存入后台的数据库中,如下:

<form bindsubmit='saveformid' report-submit='true'>
  <button  form-type='submit' bindtap="navbarTap"  style='background:#fff;padding:0rpx;border:none;border-radius: 0rpx;margin:0rpx;width:150rpx;height:100%;position: relative;'>
</button>
</form>

第二,后台方面,涉及的批量推送的话,就需要用到php多线程扩展了,这个项目中用的则是pthread,下载地址与安装方法在文章最前面。

重点来了

多线程安装成功的标志
在后台代码中继承Thread类,重写它的run()方法,并在控制器中调用成功。如下:

<?php
/**
 * Created by PhpStorm.
 * User: zw
 * Date: 2018/8/16
 * Time: 10:59
 */

namespace app\api\service;


use Thread;

class WxPushService extends Thread
{
    protected $openid = "";//微信用户openid
    protected $formid = "";//对应表单id
    protected $name = "";//服务通知字段
    protected $columnid = 0;

    public function __construct($openid, $formid, $name, $columnid)
    {
        $this->openid = $openid;
        $this->formid = $formid;
        $this->name = $name;
        $this->columnid=$columnid;
    }

    public function run()
    {
        $data = array(
            'touser' => $this->openid,
            'template_id' => 'xxxxxxxxxxxxxxx',
            'page' => 'xxxxxxxxxxxxxxxx',
            "form_id" => $this->formid,
            'topcolor' => '#FF0000',
            'data' => array(
                'keyword1' => array(
                    'value' => $this->name
                ),
                'keyword2' => array(
                    'value' => $this->name
                ),
                'keyword3' => array(
                    'value' => $this->name
                )
            )
        );//服务通知发送必要字段
        $params = json_encode($data, JSON_UNESCAPED_UNICODE);
        $result = curl_post_raw("https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=" . $accessToken, $params);//其中$accessToken为发送服务通知的安全令牌
    }
}

微信小程序发送服务通知具体细节请看https://developers.weixin.qq.com/miniprogram/dev/api/notice.html#%E6%A8%A1%E7%89%88%E6%B6%88%E6%81%AF%E7%AE%A1%E7%90%86
最后在控制器中通过遍历之前存的formid通过for循环执行多个子线程。

TP5后台批量推送小程序服务通知,搞定。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章