微信模板消息---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;
    }

}

猜你喜欢

转载自blog.csdn.net/ahaotata/article/details/83896760
今日推荐