1. 程式人生 > >小程式模板訊息推送

小程式模板訊息推送

$push_state = $this->senMsg($openid,$templateid,$form_id,$data_arr,$page);
 //推送成功拼團模板訊息
    public function sendOrderMsg($collage_order_id,$state = 1)
    {
        //取得課程詳情
        $sql = "select b.course_id,b.course_title,b.pay_price,b.collage_scale from collage_order a left join collage_course b on a.collage_id = b.id where a.id = $collage_order_id";
        $search_info = M()->query($sql)[0];
        if($search_info){
            $course_name = $search_info['course_title'];
            $price = $search_info['pay_price'];
            $people_count = $search_info['collage_scale'];

            if($state == 1){
                $data_arr = array(
                    'keyword1' => array( "value" => $course_name, "color" => ''),
                    'keyword2' => array( "value" => $price, "color" => ''),
                    'keyword3' => array( "value" => $people_count, "color" => '')
                );
                $templateid = 'sPrON_KE2bLohU54isxi44vO0OMZFgjQwANrq2x2yzo';
            }else{
                $data_arr = array(
                    'keyword1' => array( "value" => $course_name, "color" => ''),
                    'keyword2' => array( "value" => "人數不足,拼團失敗", "color" => ''),
                    'keyword3' => array( "value" => $people_count, "color" => '')
                );
                $templateid = 'mJUpfIf2uc82LradebdXTZ1Z6CJxv0GuZMBi0VUqLM8';
            }
            $course_id = $search_info['course_id'];
            //所有發起拼團的人
            $sql = "select id,collage_id,openid,from_id from collage_info where order_id = $collage_order_id and push_msg = 0";
            $search = M()->query($sql);
            foreach($search  as $key=>$value){
                $collage_id = $value['collage_id'];
                $openid = $value['openid'];
                $form_id = $value['from_id'];
                $create_time = $value['createTime'];
                $arr_create_time = array('keyword4'=>array( "value" => $create_time, "color" => ''));
                $data_arr = array_merge($data_arr,$arr_create_time);
                $page = "pages/collagedetails/collagedetails?collage_id=$collage_id&course_id=$course_id&order_id=$collage_order_id";
                $push_state = $this->senMsg($openid,$templateid,$form_id,$data_arr,$page);
                if($push_state){
                    $id = $value['id'];
                    $sql = "update collage_info set push_msg = 1 where id = $id,page='$page'";
                    M()->execute($sql);
                }
            }
        }
    }


    //小程式推送訊息
    public function senMsg($openids,$templateid,$formid,$data_arr,$page)
    {
        // 這裡的返回值是一個 JSON,可通過 json_decode() 解碼成陣列
        $data = $this->getPostData($openids,$templateid,$formid,$data_arr,$page);
        $smallwx_token = M('smallwx_token')->find();
        $access_token    =$smallwx_token['access_token'];
        $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$access_token;
        $return = $this->send_post( $url, $data);
        $state = json_decode($return,true);
        if($state['errmsg'] == 'ok'){
            return true;
        }else{
            return false;
        }

    }

    public function getPostData($openids,$templateid,$formid,$data_arr,$page='')
    {
        $post_data = array (
            // 使用者的 openID,可用過 wx.getUserInfo 獲取
            "touser"           => $openids,
            // 小程式後臺申請到的模板編號
            "template_id"      => $templateid,
            // 點選模板訊息後跳轉到的頁面,可以傳遞引數
            "page"             => $page,//"/pages/check/result?orderID=".$orderID,
            // 第一步裡獲取到的 formID
            "form_id"          => $formid,
            // 資料
            "data"             => $data_arr,
            // 需要強調的關鍵字,會加大居中顯示
            "emphasis_keyword" => "keyword2.DATA"
        );
        // 將陣列編碼為 JSON
        $data = json_encode($post_data, true);
        return $data;
    }

    function send_post( $url, $post_data ) {
        $options = array(
            'http' => array(
                'method'  => 'POST',
                // header 需要設定為 JSON
                'header'  => 'Content-type:application/json',
                'content' => $post_data,
                // 超時時間
                'timeout' => 60
            )
        );

        $context = stream_context_create( $options );
        $result = file_get_contents( $url, false, $context );

        return $result;
    }