1. 程式人生 > >微信模板訊息---php

微信模板訊息---php

微信模板訊息備註

<?php

/**
 * Created by PhpStorm.
 * Time: 上午 9:46
 */
class topapi_sendNewOrderMsg
{
        private $appid = '*****';
        private $secret = '*******';

    //微信模板訊息
    function send($params)
    {
        //獲取token
        $access = json_decode($this->get_access_token(), true);
        $access_token = $access['access_token'];
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
        $openId=$this->getShopOpenId($params['shop_id']);
        //組裝陣列
        $array = array(
            'touser' => $openId,
            'template_id' => 'tU7LpzrkXqmJO_QRii45kduBYIhUXB0iaXaNU2cjdLc',
            'url' => $params['page'],
            'data' => array(
                'first' => array('value' => "訂單生成通知", 'color' => '#173177'),
                'keyword1' => array('value' => date('Y-m-m H:i:s'), 'color' => '#173177'),
                'keyword2' => array('value' => $params['name'], 'color' => '#173177'),
                'keyword3' => array('value' => $params['order_sn'], 'color' => '#173177'),
                'remark' => array('value' => '訂單成功。', 'color' => '#173177'),
            )
        );
        //陣列->json
        $postJson = json_encode($array);
        //呼叫函式
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postJson);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

    //獲取access_token
    public function get_access_token()
    {
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->secret}";
        return $data = $this->curl_get($url);
    }

    public function curl_get($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
        return $data;
    }

    //獲取商家openid
    public function getShopOpenId($shop_id)
    {
        //通過shop_id獲取seller_id
        $seller_id = app::get("sysshop")->model("seller")->getRow('seller_id', array('shop_id' => $shop_id, 'seller_type' => 0))['seller_id'];
        //通過seller_id獲取user_id
        $user_id = app::get("sysshop")->model("account")->getRow('user_id', array('seller_id' => $seller_id))['user_id'];
        //通過user_id獲取openid
        $openId = app::get("sysuser")->model("account")->getRow('openid', array('user_id' => $user_id))['openid'];
        return $openId;
    }

    //獲取客戶openid
    public function getCustomerOpenId($order_sn)
    {
        $user_id = app::get("systrade")->model("trade")->getRow('user_id', array('tid' => $order_sn))['user_id'];
        $unionid = app::get("sysuser")->model("account")->getRow('unionid', array('user_id' => $user_id))['unionid'];
        $openId = app::get("sysuser")->model("customer_msg")->getRow('openid', array('unionid' => $unionid))['openid'];
        return $openId;
    }

}