ThirthdeliveryController.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * lionfish 商城系统
  4. *
  5. * 第三方配送回调地址
  6. * @author cy 2020.08.06
  7. *
  8. */
  9. namespace Home\Controller;
  10. class ThirthdeliveryController extends CommonController {
  11. protected function _initialize()
  12. {
  13. parent::_initialize();
  14. }
  15. public function notify(){
  16. $return_json_data = file_get_contents("php://input");
  17. $json_data = json_decode($return_json_data,true);
  18. //达达平台回调接口
  19. if(isset($json_data['client_id']) && isset($json_data['order_id']) && isset($json_data['order_status']) && isset($json_data['signature'])){
  20. //error_log($return_json_data.'///',3,'logs/error_imdada_'.date('Ymd').'.log');
  21. $other_data = array();
  22. $other_data['data_type'] = 'imdada';
  23. $other_data['order_status'] = $json_data['order_status'];
  24. if($json_data['order_status'] == 2){//待取货
  25. $other_data['dm_id'] = $json_data['dm_id'];
  26. $other_data['dm_name'] = $json_data['dm_name'];
  27. $other_data['dm_mobile'] = $json_data['dm_mobile'];
  28. $other_data['third_id'] = $json_data['client_id'];
  29. D('Seller/Order')->do_localtown_thirth_delivery_return($json_data['order_id'],0,$other_data);
  30. }else if($json_data['order_status'] == 3){//配送中
  31. $other_data['dm_id'] = $json_data['dm_id'];
  32. $other_data['dm_name'] = $json_data['dm_name'];
  33. $other_data['dm_mobile'] = $json_data['dm_mobile'];
  34. $other_data['third_id'] = $json_data['client_id'];
  35. D('Seller/Order')->do_localtown_thirth_delivery_return($json_data['order_id'],0,$other_data);
  36. }else if($json_data['order_status'] == 4){//已完成
  37. D('Seller/Order')->do_localtown_thirth_delivery_return($json_data['order_id'],6,$other_data);
  38. }else if($json_data['order_status'] == 5){//已取消
  39. //订单取消原因
  40. $other_data['cancel_reason'] = $json_data['cancel_reason'];
  41. //订单取消原因来源
  42. $other_data['cancel_from'] = $json_data['cancel_from'];
  43. D('Seller/Order')->do_localtown_thirth_delivery_return($json_data['order_id'],0,$other_data);
  44. }else if($json_data['order_status'] == 9){//妥投异常之物品返回中
  45. //订单取消原因
  46. $other_data['cancel_reason'] = "妥投异常-物品返回中";
  47. //订单取消原因来源
  48. $other_data['cancel_from'] = $json_data['cancel_from'];
  49. D('Seller/Order')->do_localtown_thirth_delivery_return($json_data['order_id'],0,$other_data);
  50. }else if($json_data['order_status'] == 10){//妥投异常之物品返回完成
  51. //订单取消原因
  52. $other_data['cancel_reason'] = "妥投异常-物品返回完成";
  53. //订单取消原因来源
  54. $other_data['cancel_from'] = $json_data['cancel_from'];
  55. D('Seller/Order')->do_localtown_thirth_delivery_return($json_data['order_id'],0,$other_data);
  56. }else if($json_data['order_status'] == 1000){//创建达达运单失败
  57. //订单取消原因
  58. $other_data['cancel_reason'] = $json_data['cancel_reason'];
  59. //订单取消原因来源
  60. $other_data['cancel_from'] = $json_data['cancel_from'];
  61. D('Seller/Order')->do_localtown_thirth_delivery_return($json_data['order_id'],0,$other_data);
  62. }
  63. echo 'notify';
  64. }else if(isset($_GET['sign']) && isset($json_data['sf_order_id']) && isset($json_data['shop_order_id']) && isset($json_data['url_index']) && isset($json_data['push_time'])){
  65. //顺丰同城回调接口
  66. $sign = $_GET['sign'];
  67. //error_log($sign.'==='.$return_json_data.'///',3,'logs/error_sf_'.date('Ymd').'.log');
  68. $dev_id = D('Home/Front')->get_config_by_name('localtown_sf_dev_id');
  69. $dev_key = D('Home/Front')->get_config_by_name('localtown_sf_dev_key');
  70. if ($sign && $sign == base64_encode(MD5("{$return_json_data}&{$dev_id}&{$dev_key}"))) {
  71. $other_data = array();
  72. $url_index = $json_data['url_index'];
  73. $other_data['data_type'] = 'sf';
  74. if($url_index == 'sf_cancel'){//取消订单
  75. //订单取消原因
  76. $other_data['cancel_reason'] = $json_data['cancel_reason'];
  77. //状态描述
  78. $other_data['status_desc'] = $json_data['status_desc'];
  79. //操作人
  80. $other_data['operator_name'] = $json_data['operator_name'];
  81. //操作状态 2:订单取消
  82. $other_data['order_status'] = $json_data['order_status'];
  83. D('Seller/Order')->do_localtown_thirth_delivery_return($json_data['shop_order_id'],0,$other_data);
  84. }else if($url_index == 'rider_status'){//配送状态更改回调
  85. //配送员姓名
  86. $other_data['operator_name'] = $json_data['operator_name'];
  87. //配送员电话
  88. $other_data['operator_phone'] = $json_data['operator_phone'];
  89. //操作状态 10-配送员确认;12:配送员到店;15:配送员配送中
  90. $other_data['order_status'] = $json_data['order_status'];
  91. //备注信息
  92. $other_data['status_desc'] = $json_data['status_desc'];
  93. D('Seller/Order')->do_localtown_thirth_delivery_return($json_data['shop_order_id'],0,$other_data);
  94. }else if($url_index == 'rider_exception'){//订单异常回调
  95. //异常ID
  96. $other_data['ex_id'] = $json_data['ex_id'];
  97. //异常详情
  98. $other_data['ex_content'] = $json_data['ex_content'];
  99. //异常状态 0:订单异常
  100. $other_data['order_status'] = $json_data['order_status'];
  101. D('Seller/Order')->do_localtown_thirth_delivery_return($json_data['shop_order_id'],0,$other_data);
  102. }else if($url_index == 'order_complete'){//订单完成
  103. //操作状态 17-配送员点击完成
  104. $other_data['order_status'] = $json_data['order_status'];
  105. D('Seller/Order')->do_localtown_thirth_delivery_return($json_data['shop_order_id'],6,$other_data);
  106. }
  107. $result_array = array();
  108. $result_array['error_code'] = 0;
  109. $result_array['error_msg'] = "success";
  110. echo json_encode($result_array);
  111. }
  112. }else if(isset($json_data['messageType']) && isset($json_data['messageBody'])){
  113. //达达消息通知:骑士取消订单通知
  114. $messageBody = json_decode($json_data['messageBody'],true);
  115. $other_data = array();
  116. $other_data['data_type'] = 'imdada';
  117. $other_data['order_status'] = 5;
  118. //订单取消原因
  119. $other_data['cancel_reason'] = $messageBody['cancelReason'];
  120. //订单取消原因来源 11:达达骑手取消订单
  121. $other_data['cancel_from'] = 11;
  122. D('Seller/Order')->do_localtown_thirth_delivery_return($messageBody['orderId'],0,$other_data);
  123. $result_array = array();
  124. $result_array['status'] = "ok";
  125. echo json_encode($result_array);
  126. }else if(isset($json_data['app_id']) && isset($json_data['data']) && isset($json_data['salt']) && isset($json_data['signature'])){
  127. //蜂鸟即配 订单状态变更回调
  128. $app_id = D('Home/Front')->get_config_by_name('localtown_ele_app_id');
  129. $secret_key = D('Home/Front')->get_config_by_name('localtown_ele_secret_key');
  130. // 获取签名
  131. $sig = $this->eleGenerateSign($app_id, $json_data['salt'], $secret_key);
  132. if($sig == $json_data['signature']){
  133. $data = urldecode($json_data['data']);
  134. //商户自己的订单号
  135. $partner_order_code = $data['partner_order_code'];
  136. //状态码
  137. $order_status = $data['order_status'];
  138. $order_sn = $this->getOrderSnByThirdOrderId($partner_order_code);
  139. if(!empty($order_sn)){
  140. $other_data = [];
  141. $other_data['data_type'] = 'ele';
  142. if($order_status == 1){//系统已接单
  143. $other_data['desc'] = '已接单';
  144. $other_data['order_status'] = 2;
  145. D('Seller/Order')->do_localtown_thirth_delivery_return($order_sn,0,$other_data);
  146. }else if($order_status == 20){//已分配骑手
  147. //配送员姓名
  148. $other_data['operator_name'] = $data['carrier_driver_name'];
  149. //配送员电话
  150. $other_data['operator_phone'] = $data['carrier_driver_phone'];
  151. $other_data['desc'] = "已分配骑手:".$other_data['operator_name'];
  152. $other_data['order_status'] = 3;
  153. D('Seller/Order')->do_localtown_thirth_delivery_return($order_sn,0,$other_data);
  154. }else if($order_status == 80){//骑手已到店
  155. //配送员姓名
  156. $other_data['operator_name'] = $data['carrier_driver_name'];
  157. //配送员电话
  158. $other_data['operator_phone'] = $data['carrier_driver_phone'];
  159. $other_data['desc'] = "骑手".$other_data['operator_name']."已到店";
  160. $other_data['order_status'] = 3;
  161. D('Seller/Order')->do_localtown_thirth_delivery_return($order_sn,0,$other_data);
  162. }else if($order_status == 2){//配送中
  163. //配送员姓名
  164. $other_data['operator_name'] = $data['carrier_driver_name'];
  165. //配送员电话
  166. $other_data['operator_phone'] = $data['carrier_driver_phone'];
  167. $other_data['desc'] = "骑手".$other_data['operator_name']."配送中";
  168. $other_data['order_status'] = 3;
  169. D('Seller/Order')->do_localtown_thirth_delivery_return($order_sn,0,$other_data);
  170. }else if($order_status == 3){//已送达
  171. //配送员姓名
  172. $other_data['operator_name'] = $data['carrier_driver_name'];
  173. //配送员电话
  174. $other_data['operator_phone'] = $data['carrier_driver_phone'];
  175. $other_data['desc'] = "骑手".$data['carrier_driver_name']."已送达";
  176. $other_data['order_status'] = 4;
  177. D('Seller/Order')->do_localtown_thirth_delivery_return($order_sn,6,$other_data);
  178. }else if($order_status == 5){//异常
  179. $other_data['error_code'] = $data['error_code'];
  180. $other_data['desc'] = $data['detail_description'];
  181. $other_data['order_status'] = 100;
  182. D('Seller/Order')->do_localtown_thirth_delivery_return($order_sn,0,$other_data);
  183. }
  184. }
  185. }
  186. }
  187. }
  188. /**
  189. * @author cy 2021-02-03
  190. * @desc 通过第三方商户订单号获取狮子鱼订单号
  191. * @param $third_order_id
  192. * @return string
  193. */
  194. public function getOrderSnByThirdOrderId($third_order_id){
  195. $orders = M('lionfish_comshop_orderdistribution_order')->field('order_id')->where( array('third_order_id' => $third_order_id) )->find();
  196. if(!empty($orders)){
  197. $order_info = M('lionfish_comshop_order')->field('order_num_alias')->where( array('order_id' => $orders['order_id']) )->find();
  198. if(!empty($order_info)){
  199. return $order_info['order_num_alias'];
  200. }else{
  201. return '';
  202. }
  203. }else{
  204. return '';
  205. }
  206. }
  207. /**
  208. * @author cy 2021-02-03
  209. * @desc 蜂鸟即配签名
  210. * @param $appId
  211. * @param $salt
  212. * @param $secretKey
  213. * @return string
  214. */
  215. public function eleGenerateSign($appId, $salt, $secretKey) {
  216. $seed = 'app_id=' . $appId . '&salt=' . $salt . '&secret_key=' . $secretKey;
  217. return md5(urlencode($seed));
  218. }
  219. }