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後臺批量推送小程序服務通知,搞定。

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