讀古今文學網 > 微信公眾平台開發:從零基礎到ThinkPHP5高性能框架實踐 > 16.8.2 創建會議門票 >

16.8.2 創建會議門票

會議門票是一種很常用的票據。使用微信卡券的會議門票功能,可以為參會者發放電子門票,設置座次,掃碼簽到及統計到訪率等。


  1 class class_wxcard
  2 {
  3     var $appid = APPID;
  4     var $appsecret = APPSECRET;
  5 
  6     // 構造函數,獲取Access Token
  7     public function __construct($appid = NULL, $appsecret = NULL)
  8     {
  9         if($appid && $appsecret){
 10             $this->appid = $appid;
 11             $this->appsecret = $appsecret;
 12         }
 13         $res = file_get_contents('token.json');
 14         $result = json_decode($res, true);
 15         $this->expires_time = $result["expires_time"];
 16         $this->access_token = $result["access_token"];
 17         if (time > ($this->expires_time + 3600)){
 18             $url = "https:// api.weixin.qq.com/cgi-bin/token?grant_type=client_
                credential&appid=".$this->appid."&secret=".$this->appsecret;
 19             $res = $this->http_request($url);
 20             $result = json_decode($res, true);
 21             $this->access_token = $result["access_token"];
 22             $this->expires_time = time;
 23             file_put_contents('token.json', '{"access_token": "'.$this->access_
                token.'", "expires_time": '.$this->expires_time.'}');
 24         }
 25     }
 26 
 27     // 上傳圖片
 28     public function upload_image($file)
 29     {
 30         if (PHP_OS == "Linux"){        // Linux
 31             $data = array("buffer"  => "@".dirname(__FILE__).'/'.$file);
 32         }else{                        // WINNT
 33             $data = array("buffer"  => "@".dirname(__FILE__).'\\'.$file);
 34         }
 35         $url = "https:// api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=".$this-
            >access_token;
 36         $res = $this->http_request($url, $data);
 37         return json_decode($res, true);
 38     }
 39 
 40     // 創建卡券
 41     public function create_card($data)
 42     {
 43         $url = "https:// api.weixin.qq.com/card/create?access_token=".$this->access_token;
 44         $res = $this->http_request($url, $this->array_to_json(array('card'=>$data)));
 45         return json_decode($res, true);
 46     }
 47 
 48     // 創建二維碼接口
 49     public function create_card_qrcode($data)
 50     {
 51         $url = "https:// api.weixin.qq.com/card/qrcode/create?access_token=".$this-
            >access_token;
 52         $res = $this->http_request($url, json_encode($data));
 53         return json_decode($res, true);
 54     }
 55     
 56     // 特殊票類
 57     // 更新會議門票
 58     public function update_card_meetingticket($data)
 59     {
 60         $url = "https:// api.weixin.qq.com/card/meetingticket/updateuser?access_
            token=".$this->access_token;
 61         $res = $this->http_request($url, $this->array_to_json($data));
 62         return json_decode($res, true);
 63     }
 64     
 65     // 多級數組轉JSON(兼容中文、數字、英文、布爾型)
 66     protected function array_to_json($array)
 67     {
 68         foreach ($array as $k => &$v) {
 69             if (is_array($v)){
 70                 foreach ($v as $k1 => &$v1) {
 71                     if (is_array($v1)){
 72                         foreach ($v1 as $k2 => &$v2) {
 73                             if (is_array($v2)){
 74                                 foreach ($v2 as $k3 => &$v3) {
 75                                     if (is_array($v3)){
 76                                         foreach ($v3 as $k4 => &$v4) {
 77                                             $v3[$k4] = (is_string($v4))?urlencode
                                               ($v4):$v4;
 78                                         }
 79                                     }else{
 80                                         $v2[$k3] = (is_string($v3))?urlencode
                                           ($v3):$v3;
 81                                     }
 82                                 }
 83                             }else{
 84                                 $v1[$k2] = (is_string($v2))?urlencode($v2):$v2;
 85                             }
 86                         }
 87                     }else{
 88                         $v[$k1] = (is_string($v1))?urlencode($v1):$v1;
 89                     }
 90                 }
 91                 // $this->array_to_json($v);
 92             }else{
 93                 $array[$k] = (is_string($v))?urlencode($v):$v;
 94             }
 95         }
 96         return urldecode(json_encode($array));
 97     }
 98 
 99     // HTTP請求(支持HTTP/HTTPS,支持GET/POST)
100     protected function http_request($url, $data = null)
101     {
102         $curl = curl_init;
103         curl_setopt($curl, CURLOPT_URL, $url);
104         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
105         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
106         if (!empty($data)){
107             curl_setopt($curl, CURLOPT_POST, 1);
108             curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
109         }
110         curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
111         $output = curl_exec($curl);
112         curl_close($curl);
113         return $output;
114     }
115 }
 

創建會議門票的代碼如下。


 1 // 創建會議/演出門票
 2 $data = array('card_type' => "MEETING_TICKET",
 3               'meeting_ticket' => array('base_info' => array('logo_url' => "http:// 
                mmbiz.qpic.cn/mmbiz/K4LBh6RUO0qAa2sbY1EyJGDZ0eCetML4quMDRsiczNX9
                 8UPE6ryGM7ynjWCX1kibM5iaOLV5ibXHRhs8kdNoAthSjw/0",
 4                                   'code_type' => "CODE_TYPE_BARCODE",
 5                                   'brand_name' => "方倍票務公司",
 6                                   'title' => "方倍會議門票",
 7                                   'color' => "Color020",
 8                                   'notice' => "使用時向檢票員出示此券",
 9                                   'description' => "請務必準時入場",
10                                   'sku' => array('quantity' => 10000),
11                                   'date_info' => array('type' => 1,
12                                            'begin_timestamp' => time,
13                                            'end_timestamp' => time + 2 * 86400),
14
15                             ),
16                 'meeting_detail' => "地點:水立方",
17                 'map_url' => "http:// www.baidu.com/"
18             ),
19     );
20 $result = $weixin->create_card($data);
  

生成卡券ID以後,使用該ID生成二維碼進行投放,其代碼如下。


 1 // 創建二維碼進行投放
 2 $data = array('action_name' => "QR_CARD",
 3               // 'expire_seconds' => 1800,
 4               'action_info' => array('card' => array('card_id' => "piPuduNg0bM6
                 Q5hB8rrcK3OXavSE",
 5                              // 'code' => "198374613512",
 6                              // 'openid' => "oFS7Fjl0WsZ9AMZqrI80nbIq8xrA",
 7                              'is_unique_code' => false,
 8                              'outer_id' => 100),
 9
10                         ),
11               );
12 $result = $weixin->create_card_qrcode($data);
  

當用戶領取門票以後,為其更新座次信息,代碼如下。


1 // 更新會議門票
2 $data = array(
3               'code' => "019538767119",
4               'card_id' => "piPuduNg0bM6Q5hB8rrcK3OXavSE",
5               'zone' => "C區",
6               'entrance' => "東北門",
7               'seat_number' => "2排15號",
8              );
9 $result = $weixin->update_card_meetingticket($data);
  

最終用戶領取到的門票及更新後的門票信息如圖16-11所示。

圖16-11 微信門票