intraCityExpressExample.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace common\components;
  3. /**
  4. * 微信小程序同城配送使用示例
  5. *
  6. * 本文件展示了如何使用 IntraCityExpress 类的各种功能
  7. * 包括门店管理、订单管理、运力管理等
  8. */
  9. class IntraCityExpressExample
  10. {
  11. /**
  12. * 门店管理示例
  13. */
  14. public static function storeManagementExample()
  15. {
  16. // 1. 创建门店
  17. $storeData = [
  18. 'out_store_id' => 'store_001',
  19. 'store_name' => '花店总店',
  20. 'store_address' => '北京市朝阳区建国路88号',
  21. 'store_longitude' => 116.397128,
  22. 'store_latitude' => 39.916527,
  23. 'store_phone' => '010-12345678',
  24. 'service_type' => 1, // 同城配送
  25. ];
  26. $result = IntraCityExpress::createStore($storeData);
  27. if ($result['errcode'] === 0) {
  28. echo "门店创建成功\n";
  29. } else {
  30. echo "门店创建失败:" . $result['errmsg'] . "\n";
  31. }
  32. // 2. 查询门店
  33. $storeInfo = IntraCityExpress::getStore('store_001');
  34. if ($storeInfo['errcode'] === 0) {
  35. echo "门店信息:" . json_encode($storeInfo, JSON_UNESCAPED_UNICODE) . "\n";
  36. }
  37. // 3. 更新门店
  38. $updateData = [
  39. 'out_store_id' => 'store_001',
  40. 'store_name' => '花店总店(已更新)',
  41. 'store_address' => '北京市朝阳区建国路88号',
  42. 'store_longitude' => 116.397128,
  43. 'store_latitude' => 39.916527,
  44. 'store_phone' => '010-87654321',
  45. ];
  46. $updateResult = IntraCityExpress::updateStore($updateData);
  47. if ($updateResult['errcode'] === 0) {
  48. echo "门店更新成功\n";
  49. }
  50. // 4. 获取门店列表
  51. $storeList = IntraCityExpress::getStoreList(0, 10);
  52. if ($storeList['errcode'] === 0) {
  53. echo "门店列表:" . json_encode($storeList, JSON_UNESCAPED_UNICODE) . "\n";
  54. }
  55. }
  56. /**
  57. * 订单管理示例
  58. */
  59. public static function orderManagementExample()
  60. {
  61. // 1. 创建订单
  62. $orderData = [
  63. 'out_store_id' => 'store_001',
  64. 'out_order_id' => 'order_' . time(),
  65. 'delivery_service_code' => 'DADA', // 达达配送
  66. 'to_user_name' => '张三',
  67. 'to_user_phone' => '13800138000',
  68. 'to_user_address' => '北京市海淀区中关村大街1号',
  69. 'to_user_longitude' => 116.307852,
  70. 'to_user_latitude' => 39.984154,
  71. 'goods_value' => 10000, // 商品价值(分)
  72. 'goods_weight' => 1000, // 商品重量(克)
  73. 'goods_pickup_info' => '鲜花一束',
  74. 'goods_delivery_info' => '鲜花一束',
  75. 'goods_type' => IntraCityExpress::GOODS_TYPE_FLOWER, // 鲜花
  76. 'expected_pickup_time' => date('Y-m-d H:i:s', time() + 1800), // 30分钟后取货
  77. 'expected_delivery_time' => date('Y-m-d H:i:s', time() + 7200), // 2小时后送达
  78. ];
  79. $result = IntraCityExpress::createOrder($orderData);
  80. if ($result['errcode'] === 0) {
  81. echo "订单创建成功,微信订单号:" . $result['wx_order_id'] . "\n";
  82. // 保存微信订单号用于后续操作
  83. $wxOrderId = $result['wx_order_id'];
  84. // 2. 查询订单
  85. $orderInfo = IntraCityExpress::getOrder($wxOrderId);
  86. if ($orderInfo['errcode'] === 0) {
  87. echo "订单信息:" . json_encode($orderInfo, JSON_UNESCAPED_UNICODE) . "\n";
  88. }
  89. // 3. 模拟订单状态变化(测试用)
  90. $mockResult = IntraCityExpress::mockNotify(
  91. IntraCityExpress::ORDER_STATUS_ACCEPTED, // 配送员接单
  92. $wxOrderId
  93. );
  94. if ($mockResult['errcode'] === 0) {
  95. echo "模拟回调成功\n";
  96. }
  97. // 4. 取消订单(如果需要)
  98. // $cancelResult = IntraCityExpress::cancelOrder($wxOrderId);
  99. // if ($cancelResult['errcode'] === 0) {
  100. // echo "订单取消成功\n";
  101. // }
  102. } else {
  103. echo "订单创建失败:" . $result['errmsg'] . "\n";
  104. }
  105. // 5. 获取订单列表
  106. $orderList = IntraCityExpress::getOrderList(0, 10);
  107. if ($orderList['errcode'] === 0) {
  108. echo "订单列表:" . json_encode($orderList, JSON_UNESCAPED_UNICODE) . "\n";
  109. }
  110. }
  111. /**
  112. * 运力管理示例
  113. */
  114. public static function deliveryManagementExample()
  115. {
  116. // 1. 获取运力列表
  117. $deliveryList = IntraCityExpress::getDeliveryList();
  118. if ($deliveryList['errcode'] === 0) {
  119. echo "运力列表:" . json_encode($deliveryList, JSON_UNESCAPED_UNICODE) . "\n";
  120. // 2. 获取运力服务范围
  121. if (!empty($deliveryList['delivery_list'])) {
  122. $deliveryId = $deliveryList['delivery_list'][0]['delivery_id'];
  123. $serviceArea = IntraCityExpress::getDeliveryServiceArea($deliveryId);
  124. if ($serviceArea['errcode'] === 0) {
  125. echo "运力服务范围:" . json_encode($serviceArea, JSON_UNESCAPED_UNICODE) . "\n";
  126. }
  127. }
  128. }
  129. }
  130. /**
  131. * 回调处理示例
  132. */
  133. public static function callbackExample()
  134. {
  135. // 模拟接收到的回调数据
  136. $callbackData = [
  137. 'appid' => 'wx539e0b4872f19621',
  138. 'order_status' => IntraCityExpress::ORDER_STATUS_COMPLETED,
  139. 'service_trans_id' => 'DADA',
  140. 'status_change_time' => time(),
  141. 'store_order_id' => 'order_123456',
  142. 'timestamp' => time(),
  143. 'wx_order_id' => '4018734875633256960',
  144. 'wx_store_id' => '4000000000000042001',
  145. 'sign' => 'a85489d9444bdd382e0de0ddca67a8ee' // 实际签名值
  146. ];
  147. // 安全token(需要在小程序管理后台设置)
  148. $token = 'abcdefghi';
  149. // 处理回调
  150. $result = IntraCityExpress::handleOrderCallback($callbackData, $token);
  151. echo "回调处理结果:" . json_encode($result, JSON_UNESCAPED_UNICODE) . "\n";
  152. }
  153. /**
  154. * 完整业务流程示例
  155. */
  156. public static function completeWorkflowExample()
  157. {
  158. echo "=== 微信小程序同城配送完整业务流程示例 ===\n\n";
  159. // 1. 门店管理
  160. echo "1. 门店管理\n";
  161. self::storeManagementExample();
  162. echo "\n";
  163. // 2. 运力管理
  164. echo "2. 运力管理\n";
  165. self::deliveryManagementExample();
  166. echo "\n";
  167. // 3. 订单管理
  168. echo "3. 订单管理\n";
  169. self::orderManagementExample();
  170. echo "\n";
  171. // 4. 回调处理
  172. echo "4. 回调处理\n";
  173. self::callbackExample();
  174. echo "\n";
  175. echo "=== 业务流程示例完成 ===\n";
  176. }
  177. /**
  178. * 错误处理示例
  179. */
  180. public static function errorHandlingExample()
  181. {
  182. echo "=== 错误处理示例 ===\n";
  183. // 1. 处理常见错误码
  184. $errorCodes = [
  185. 934000 => '其他逻辑错误',
  186. 934001 => '请求参数有误',
  187. 934002 => '订单已存在,且订单在处理中',
  188. 934003 => '运力ID错误',
  189. 934005 => '运力预创建订单错误',
  190. 934006 => '有在途订单,暂不能退款',
  191. 934007 => '不是在途订单',
  192. 934008 => '门店ID和APPID不匹配',
  193. 934009 => '不支持该门店所在城市',
  194. 934010 => '重复创建门店',
  195. 934011 => '请求签名错误',
  196. 934012 => 'appid和access_token不匹配',
  197. 934013 => '门店余额不足无法下单',
  198. 934014 => '运力公司返回了非法金额',
  199. 934015 => '余额扣减失败',
  200. 934016 => '订单不存在',
  201. 934017 => '订单处在不能被取消的状态',
  202. 934018 => '订单已取消,请勿重复操作',
  203. 934019 => '超出运力支持的配送范围',
  204. 934020 => '商品超重',
  205. 934021 => '门店不存在',
  206. 934022 => '账号类型不可以为个人账号',
  207. 934023 => '小程序类型必须为普通小程序',
  208. 934999 => '内部系统错误',
  209. ];
  210. foreach ($errorCodes as $code => $message) {
  211. echo "错误码 {$code}: {$message}\n";
  212. }
  213. // 2. 实际错误处理示例
  214. try {
  215. // 尝试创建无效订单
  216. $invalidOrderData = [
  217. 'out_store_id' => 'invalid_store',
  218. 'out_order_id' => 'order_' . time(),
  219. 'delivery_service_code' => 'INVALID_DELIVERY',
  220. 'to_user_name' => '测试用户',
  221. 'to_user_phone' => '13800138000',
  222. 'to_user_address' => '测试地址',
  223. 'to_user_longitude' => 116.397128,
  224. 'to_user_latitude' => 39.916527,
  225. 'goods_value' => 10000,
  226. 'goods_weight' => 1000,
  227. 'goods_pickup_info' => '测试商品',
  228. 'goods_delivery_info' => '测试商品',
  229. ];
  230. $result = IntraCityExpress::createOrder($invalidOrderData);
  231. if ($result['errcode'] !== 0) {
  232. echo "订单创建失败,错误码:{$result['errcode']},错误信息:{$result['errmsg']}\n";
  233. // 根据错误码进行相应处理
  234. switch ($result['errcode']) {
  235. case 934021:
  236. echo "门店不存在,请先创建门店\n";
  237. break;
  238. case 934003:
  239. echo "运力ID错误,请检查运力配置\n";
  240. break;
  241. case 934019:
  242. echo "超出配送范围,请选择其他配送方式\n";
  243. break;
  244. default:
  245. echo "其他错误,请联系技术支持\n";
  246. break;
  247. }
  248. }
  249. } catch (\Exception $e) {
  250. echo "异常处理:" . $e->getMessage() . "\n";
  251. }
  252. }
  253. }
  254. // 使用示例(取消注释即可运行)
  255. // IntraCityExpressExample::completeWorkflowExample();
  256. // IntraCityExpressExample::errorHandlingExample();