WechatMessageHelper.php 3.77 KB
<?php

namespace common\helpers;

use common\exts\wechat\Log as WxLog;
use common\exts\wechat\PHPSDK as WxPHPSDK;

/**
 * 微信公众号消息发送助手类
 * Class WechatMessageHelper
 * @package common\helpers
 */
class WechatMessageHelper
{
    /**
     * 发送客服文本消息接口
     * @param $touser 当前关注公众号的微信用户openid
     * @param $content
     * @param WxPHPSDK $wechat
     */
    public static function sendCustomerTextMessage($touser, $content, WxPHPSDK $wechat)
    {
        WxLog::init();
        $data = array(
            'touser' => $touser,
            'msgtype' => WxPHPSDK::MSGTYPE_TEXT,
            'text' => array(
                'content'=>$content
            )
        );
        $wechat->sendCustomMessage($data);
        if($wechat->errCode ){
            WxLog::ERROR("sendTextMessage: {$wechat->errCode} {$wechat->errMsg} ".json_encode($data));
            return;
        }
        WxLog::DEBUG("sendTextMessage: ".json_encode($data));
    }

    /**
     * 发送小程序卡片客服消息
     * @param $touser 当前关注公众号的微信用户openid
     * @param $title
     * @param $pagepath
     * @param $thumb_media_id
     * @param WxPHPSDK $wechat
     * @return bool|void
     * @throws \yii\base\ErrorException
     */
    public static  function sendCustomerMiniProgrampageMessage($touser, $title, $pagepath, $thumb_media_id, WxPHPSDK $wechat)
    {
        WxLog::init();
        $minaAppId = MinaHelper::getMinaSetting();
        if (empty($minaAppId->appid)) {
            return false;
        }
        //$wechat = UserMPHelper::getWxPHPSDK();
        $data = array(
            'touser' => $touser,
            'msgtype' => WxPHPSDK::MSGTYPE_MINIPROGRAMPAGE,
            'miniprogrampage' => array(
                "title" => $title,
                "appid" => $minaAppId->appid,
                "pagepath" => $pagepath,
                "thumb_media_id" => $thumb_media_id
            )
        );
        WxLog::DEBUG("sendMiniProgrampageMessage: ".json_encode($data));
        $wechat->sendCustomMessage($data);
        if($wechat->errCode ){
            WxLog::ERROR("sendMiniProgrampageMessage: {$wechat->errCode} {$wechat->errMsg} ".json_encode($data));
            return false;
        }

        return true;
    }

    /**
     * 发送客服图文消息接口
     * @param $touser 当前关注公众号的微信用户openid
     * @param $message
     * @param WxPHPSDK $wechat
     */
    public static  function sendCustomerNewsMessage($touser, $message, WxPHPSDK $wechat)
    {
        WxLog::init();
        $data = array(
            'touser' => $touser,
            'msgtype' => WxPHPSDK::MSGTYPE_NEWS,
            'news' => array(
                'articles'=>$message
            )
        );
        $wechat->sendCustomMessage($data);
        if($wechat->errCode ){
            WxLog::ERROR("sendCustomerNewsMessage: {$wechat->errCode} {$wechat->errMsg} ".json_encode($data));
            return;
        }
        WxLog::DEBUG("sendCustomerNewsMessage: ".json_encode($data));
    }

    /**
     * 发送客服图片消息接口
     * @param $touser 当前关注公众号的微信用户openid
     * @param $media_id
     * @param WxPHPSDK $wechat
     */
    public static function sendCustomerImage($touser, $media_id, WxPHPSDK $wechat)
    {
        WxLog::init();
        $data = array(
            'touser' => $touser,
            'msgtype' => WxPHPSDK::MSGTYPE_IMAGE,
            'image' => array(
                'media_id'=>$media_id
            )
        );
        $wechat->sendCustomMessage($data);
        if($wechat->errCode ){
            WxLog::ERROR("sendCustomerImage: {$wechat->errCode} {$wechat->errMsg} ".json_encode($data));
            return;
        }
        WxLog::DEBUG("sendCustomerImage: ".json_encode($data));
    }
}