首页 » 微信公众平台开发:从零基础到ThinkPHP5高性能框架实践 » 微信公众平台开发:从零基础到ThinkPHP5高性能框架实践全文在线阅读

《微信公众平台开发:从零基础到ThinkPHP5高性能框架实践》10.2.3 订单查询

关灯直达底部

微信小店的后台中提供了订单查询功能,但只有商家能使用,如果用户需要查看自己的订单,则要使用微信小店的接口来开发实现。这需要用到根据订单状态/创建时间获取订单详情的接口。

根据订单状态/创建时间获取订单详情的接口如下。


https:// api.weixin.qq.com/merchant/order/getbyid?access_token=ACCESS_TOKEN  

该接口的POST数据格式如下,参数说明如表10-3所示。


{    "status": 2,     "begintime": 1397130460,     "endtime": 1397130470}  

表10-3 订单查询接口的参数说明

返回数据格式如下,参数说明如表10-4所示。


{    "errcode": 0,    "errmsg": "success",    "order_list": [        {            "order_id": "7197417460812533543",            "order_status": 6,            "order_total_price": 6,            "order_create_time": 1394635817,            "order_express_price": 5,            "buyer_openid": "oDF3iY17NsDAW4UP2qzJXPsz1S9Q",            "buyer_nick": "likeacat",            "receiver_name": "方倍",            "receiver_province": "广东省",            "receiver_city": "广州市",            "receiver_address": "华景路一号南方通信大厦5楼",            "receiver_mobile": "123456",            "receiver_phone": "123456",            "product_id": "pDF3iYx7KDQVGzB7kDg6Tge5OKFo",             "product_name": "《微信公众平台开发最佳实践》",            "product_price": 1,            "product_sku": "10000983:10000995;10001007:10001010",            "product_count": 1,            "product_img": "http:// mmbiz.qpic.cn/mmbiz/4whpV1VZl2icND8WwMThBEcehjh            Dv2icY4GrDSG5RLM3B2qd9kOicWGVJcsAhvXfibhWRNoGOvCfMC33G9z5yQr2Qw/0",            "delivery_id": "1900659372473",            "delivery_company": "059Yunda",             "trans_id": "1900000109201404103172199813"        },        {            "order_id": "7197417460812533569",            "order_status": 8,            "order_total_price": 1,            "order_create_time": 1394636235,            "order_express_price": 0,            "buyer_openid": "oDF3iY17NsDAW4UP2qzJXPsz1S9Q",            "buyer_nick": "likeacat",            "receiver_name": "张三",            "receiver_province": "广东省",            "receiver_city": "广州市",            "receiver_address": "华景路一号南方通信大厦5楼",            "receiver_mobile": "123456",            "receiver_phone": "123456",            "product_id": "pDF3iYx7KDQVGzB7kDg6Tge5OKFo",             "product_name": "《教爸爸妈妈用微信》",            "product_price": 1,            "product_sku": "1075741873:1079742377",            "product_count": 1,            "product_img": "http:// mmbiz.qpic.cn/mmbiz/4whpV1VZl2icND8WwMThBEcehjh            Dv2icY4GrDSG5RLM3B2qd9kOicWGVJcsAhvXfibhWRNoGOvCfMC33G9z5yQr2Qw/0",            "delivery_id": "1900659372473",            "delivery_company": "059Yunda",                 "trans_id": "1900000109201404103172199813"        }    ]}  

表10-4 订单查询接口返回内容的参数说明

为了在微信中实现订单查询,需要先将一个菜单设置为“订单查询”,该菜单的类型为“CLICK”,key值为“WDDD”。

菜单的实现代码如下。


 1 require_once('weixin.class.php'); 2 $weixin = new class_weixin; 3  4 $button = array('type' => "view", 5                   'name' => urlencode("微信小店"), 6                   'url'  => "http:// mp.weixin.qq.com/bizmall/mallshelf?id=&t=mall/                     list&biz=MzANDQxNDUwNQ==&shelf_id=1&showwxpaytitle=1#wechat_                     redirect", 7                  ); 8 $button = array('name' => urlencode("更多"), 9                   'sub_button' => array(array('type' => "click",10                                               'name' => urlencode("我的订单"),11                                               'key'  => urlencode("WDDD")12                                              ),13                                         )14                   );15 $menu = urldecode(json_encode(array('button' => $button)));16 var_dump($weixin->create_menu($menu));  

当用户点击“我的订单”时,微信接口将接收到这一点击事件通知,并且调用微信小店的订单查询接口来查询当前用户的订单信息,实现代码如下。


  1 // 接收事件消息  2 private function receiveEvent($object)  3 {  4     $content = "";  5     switch ($object->Event)  6     {  7         case "subscribe":  8             $content = "欢迎关注方倍工作室";  9             break; 10         case "CLICK": 11             switch ($object->EventKey) 12             { 13                 case "WDDD": 14                     require_once('weixin.class.php'); 15                     $weixin = new class_weixin; 16                     $openid = strval($object->FromUserName); 17                     $orderArr = $weixin->get_detail_by_filter("{}"); 18                     if ($orderArr["errcode"] == -1){ 19                         $weixin->send_custom_message($openid, "text", "系统繁忙,                            请稍后再试!"); 20                     } 21                     else if (count($orderArr["order_list"]) == 0){ 22                         $weixin->send_custom_message($openid, "text", "没有查询                            到订单记录!"); 23                     }else{ 24                         $data = array; 25                         $data = array("title"=>urlencode("我的订单"),                             "description"=>"", "picurl"=>"", "url" =>""); 26                         foreach ($orderArr["order_list"] as $index => $item){ 27                             if($item["buyer_openid"] == $openid){ 28                                 $title = "编号:".$item["order_id"]."/n时间:".                                    date("Y-m-d H:i:s",$item["order_create_time"])                                    ."/n名称:".$item["product_name"]."/n总:¥".($item                                    ["product_price"] / 100)." × ".$item["product_                                    count"]." + ¥".($item["order_express_price"] /                                     100)." = ¥".($item["order_total_price"] / 100); 29 30                                 switch ($item["order_status"]) 31                                 { 32                                     case 2: 33                                         $orderstatus = "待发货"; 34                                         break; 35                                     case 3: 36                                         $orderstatus = "已发货"; 37                                         break; 38                                     case 5: 39                                         $orderstatus = "已完成"; 40                                         break; 41                                     case 8: 42                                         $orderstatus = "维权中"; 43                                         break; 44                                     default: 45                                         $orderstatus = "未知状态码".                                            $item["order_status"]; 46                                         break; 47                                 } 48                                 $title .= "/n状态:".$orderstatus; 49                                 $url = ""; 50                                 if ($item["order_status"] == 3 && !empty($item                                       ["delivery_company"])){ 51                                     switch ($item["delivery_company"]) 52                                     { 53                                         case "Fsearch_code": 54                                             $expressName = "邮政EMS"; 55                                             break; 56                                         case "002shentong": 57                                             $expressName = "申通快递"; 58                                             break; 59                                         case "066zhongtong": 60                                             $expressName = "中通速递"; 61                                             break; 62                                         case "056yuantong": 63                                             $expressName = "圆通速递"; 64                                             break; 65                                         case "042tiantian": 66                                             $expressName = "天天快递"; 67                                             break; 68                                         case "003shunfeng": 69                                             $expressName = "顺丰速运"; 70                                             break; 71                                         case "059Yunda": 72                                             $expressName = "韵达快运"; 73                                             break; 74                                         case "064zhaijisong": 75                                             $expressName = "宅急送"; 76                                             break; 77                                         case "020huitong": 78                                             $expressName = "汇通快运"; 79                                             break; 80                                         case "zj001yixun": 81                                             $expressName = "易迅快递"; 82                                             break; 83                                         default: 84                                             $expressName = "未知物流公司,ID:".                                                $item["delivery_company"]; 85                                             break; 86                                     } 87                                     $title .= "/n物流:".$expressName." ".$item                                        ["delivery_id"]; 88                                     if(preg_match("/^/d{3}[A-Za-z]{2,10}$/",                                        $item["delivery_company"])){ 89                                         $companyEn = trim(substr($item["delivery_                                            company"],3,strlen($item["delivery_                                            company"]))); 90                                         $url = "http:// m.kuaidi100.com/result.                                            jsp?com=".strtolower($companyEn)."&nu=".                                               $item["delivery_id"]; 91                                     } 92                                 } 93                                 $data = array("title"=>urlencode($title),                                          "description"=>"", "picurl"=>"", "url" =>$url); 94                             } 95                             if (count($data) >=9){break;} 96                         } 97  98                         if (count($data) == 1){ 99                             $result = $weixin->send_custom_message($openid,                                 "text", "没有查询到你的订单记录!");100                         }else{101                             $result = $weixin->send_custom_message($openid,                                 "news", $data);102                         }103                     }104                     $content = "";105                     break;106                 default:107                     $content = "空菜单响应!";108                     break;109             }110             break;111     }112     if(is_array($content)){113         if (isset($content[0])){114             $result = $this->transmitNews($object, $content);115         }else if (isset($content['MusicUrl'])){116             $result = $this->transmitMusic($object, $content);117         }118     }else{119         $result = $this->transmitText($object, $content);120     }121     return $result;122 } 

在上述代码中与订单查询通知相关部分的简要说明如下。

第13行:判断是否收到订单查询事件通知。

第14~17行:引用微信小店SDK,获取当前的所有订单信息。

第18~23行:用于查询订单接口的异常判断。

第24~96行:遍历订单详细中的所有订单列表,将当前用户的订单内容填充到微信图文消息中。

第98~102行:使用客服接口将订单详情通过图文消息方式发送。

我的订单查询实现效果如图10-28所示。