PHP微信三方平台-代公众号发送消息模板

1、微信三方平台代公众号实现业务接口API文档地址:

https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/Official_Accounts/Official_account_interface.html

2、微信三方平台代公众号发送消息模板的模板ID是不一样的所以在公众号添加模板需要将模板ID告知三方平台开发保存数据库

3、代码实现

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/8
 * Time: 14:48
 */

namespace app\models\WeChatSendMessage;


use app\librarys\wx\wx_common\wx_tools;
use app\librarys\wx\wx_customer\wx_customer;

class WxPushMsg
{
    /**
     * pushMessage 发送自定义的模板消息
     * @param array $data 模板数据
     * $data = [
     * 'openid' => '', 用户openid
     * 'url' => '', 跳转链接
     * 'template_id' => '', 模板id
     * 'data' => [ // 消息模板数据
     * 'first'    => ['value' => urlencode('黄旭辉'),'color' => "#743A3A"],
     * 'keyword1' => ['value' => urlencode('男'),'color'=>'blue'],
     * 'keyword2' => ['value' => urlencode('1993-10-23'),'color' => 'blue'],
     * 'remark'   => ['value' => urlencode('我的模板'),'color' => '#743A3A']
     * ]
     * ];
     * @param string $topcolor 模板内容字体颜色,不填默认为黑色
     * @return array
     */


    /**
     *
     *
     * @param $wx_openid
     * @param array $data
     * @param $template_id
     * @param string $league_id
     * @return array|mixed
     */
    public static function pushMessage($wx_openid, $data = [], $template_id, $league_id = '')
    {
        $template = [
            'touser' => $wx_openid,
            'template_id' => $template_id,
            'url' => isset($data['url']) ? $data['url'] : "",
            'topcolor' => '#0000',
            'data' => $data['data']
        ];
        $json_template = json_encode($template);
        $app_id = '';//注意这是微信公众号的app_id
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . wx_customer::get_access_token($app_id);
        $result = wx_tools::postCurl($url, urldecode($json_template));
        $resultData = json_decode($result, true);
        return $resultData;
    }


    /**
     * 门店下单成功推送
     * @param $order_code
     * @param $customer_id
     * @return array
     */
    public static function leagueCreateOrderPush()
    {
        //获取用户默认登录信息
        $data = array();
        $data['url'] = 'http://weixin.qq.com/download';
        $data['data'] = array( // 模板消息内容,根据模板详情进行设置
            'first' => ['value' => urlencode("您好,您的订单提交成功。"), 'color' => "#743A3A"],
            'keyword1' => ['value' => urlencode('我是订单号测试0001')],
            'keyword2' => ['value' => urlencode(date('Y年m月d日 H:i:s', time()))],
            'keyword3' => ['value' => urlencode("门店订单")],
            'keyword4' => ['value' => urlencode('')],
            'remark' => ['value' => urlencode("点击查看详细信息。")]
        );
        $re = self::pushMessage($open_id, $data, $tempt_id, '');
        return $re;
    }
}

 

posted @ 2020-05-18 15:10  鲜花满月楼  阅读(633)  评论(0编辑  收藏  举报