Model_logic_crm.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * 对接crm接口
  4. */
  5. class Model_logic_crm extends Lin_Model {
  6. function __construct(){
  7. parent::__construct();
  8. $this->load->_model('Model_express','express');
  9. $this->load->_model("Model_logic_ding","logic_ding");
  10. }
  11. public function panduan($order_info,$type,$express_info){
  12. if(empty($order_info)){
  13. return ;
  14. }
  15. if(!is_array($order_info)){
  16. return ;
  17. }
  18. //目前只有 4 店铺才处理
  19. if(!in_array($order_info['shop'],[4])){
  20. return ;
  21. }
  22. $express_info = $this->express->read($order_info['express']);
  23. if($type == 1){
  24. return ;
  25. return $this->orderCreated($order_info,$express_info);
  26. }
  27. if($type == 2){
  28. return $this->sendOutWarehouse($order_info,$express_info);
  29. }
  30. if($type == 3){
  31. return $this->orderPredelivery($order_info,$express_info);
  32. }
  33. if($type == 4){
  34. return $this->orderDelivery($order_info,$express_info);
  35. }
  36. }
  37. //处理转化接口需要的数据
  38. protected function transferData($order_info,$express_info){
  39. $len = strlen((string)$order_info['shop']);
  40. //var_dump($order_info['orderinfo']);
  41. $increment_id = substr($order_info['orderinfo'],$len);
  42. //var_dump($increment_id);
  43. $item_list = [
  44. // [
  45. // "title"=>"",
  46. // "sku"=>"",
  47. // "qty"=>0,
  48. // ]
  49. ];
  50. $sku_list = explode(",",$order_info['issku']);
  51. $product_list = explode(",",$order_info['product']);
  52. $qty_list = explode(";",$order_info['quantity']);
  53. $flag = false;
  54. foreach($sku_list as $key => $value){
  55. if(empty($product_list[$key])){
  56. $flag= true;
  57. }else{
  58. $item_list[] = [
  59. "name"=>$product_list[$key],
  60. "sku"=>$value,
  61. "qty"=>isset($qty_list[$key])?$qty_list[$key]:1,
  62. ];
  63. }
  64. }
  65. if($flag){
  66. return ;
  67. }
  68. // foreach($sku_list as $key => $value){
  69. // $item_list[] = [
  70. // "name"=>$product_list[$key],
  71. // "sku"=>$value,
  72. // "qty"=>isset($qty_list[$key])?$qty_list[$key]:1,
  73. // ];
  74. // }
  75. $address = "";
  76. if(!empty($order_info['street'])){
  77. $address .= $order_info['street'];
  78. }
  79. if(!empty($order_info['address'])){
  80. $address .= $order_info['address'];
  81. }
  82. if(!empty($order_info['address2'])){
  83. $address .= $order_info['address2'];
  84. }
  85. $info = [
  86. "website_id"=>(int)$order_info['shop'],
  87. "orderinfo"=>[
  88. "increment_id"=>$increment_id,
  89. "store_id"=>$order_info['shop'],
  90. "customer_id"=>0,
  91. "customer_name"=>$order_info['client'],
  92. "customer_email"=>$order_info['email'],
  93. "customer_phone"=>$order_info['phone'],
  94. "grand_total" =>$order_info['shouldmoney'],
  95. "country"=>$order_info['al'],
  96. "product_items"=>$item_list,
  97. "created_at"=>$order_info['dtime'],//建议只精确到天 不要时分秒
  98. "updated_at"=>0,//发货时间
  99. "address"=>[
  100. "street"=> $address,
  101. "city"=> $order_info['city'],
  102. "province"=> $order_info['province'],
  103. "zip"=> $order_info['zipcode'],
  104. "country"=> $order_info['al'],
  105. "phone"=> $order_info['phone'],
  106. ]
  107. ]
  108. ];
  109. return $info;
  110. }
  111. //订单创建
  112. protected function orderCreated($order_info,$express_info = []){
  113. $info = $this->transferData($order_info,$express_info);
  114. if(empty($info)){
  115. return ;
  116. }
  117. $info['updated_at'] = $order_info['time'];
  118. $info["act"] = "order_created";
  119. $info["status"]='pending';//订单进行中
  120. $this->sendOrderNotice(json_encode($info));
  121. }
  122. //向crm发送订单出货通知
  123. /**
  124. * @param $order_info 订单信息
  125. * @param $express_info 快递公司信息
  126. */
  127. protected function sendOutWarehouse($order_info,$express_info =[]){
  128. $info = $this->transferData($order_info,$express_info);
  129. if(empty($info)){
  130. return ;
  131. }
  132. $info['updated_at'] = time();
  133. $info["act"] = "order_shipped";
  134. $info["status"]='processing';//订单进行中
  135. $info['orderinfo']['tracking_number'] = $order_info['waybill'];
  136. $info['orderinfo']['carrier'] = $express_info['iscode'];
  137. $info['orderinfo']['tracking_url'] = $express_info['url'];
  138. $this->sendOrderNotice(json_encode($info));
  139. }
  140. //订单预计送达时间
  141. protected function orderPredelivery($order_info,$express_info =[]){
  142. $info = $this->transferData($order_info,$express_info);
  143. if(empty($info)){
  144. return ;
  145. }
  146. $info['updated_at'] = time();
  147. $info["act"] = "order_predelivery";
  148. $info["status"]='processing';//订单进行中
  149. $info['orderinfo']['tracking_number'] = $order_info['waybill'];
  150. $info['orderinfo']['carrier'] = $express_info['iscode'];
  151. $info['orderinfo']['tracking_url'] = $express_info['url'];
  152. $this->sendOrderNotice(json_encode($info));
  153. }
  154. //向crm 发送订单收获通知
  155. protected function orderDelivery($order_info,$express_info =[]){
  156. $info = $this->transferData($order_info,$express_info);
  157. if(empty($info)){
  158. return ;
  159. }
  160. $info['updated_at'] = time();
  161. $info["act"] = "order_delivery";
  162. $info["status"]='processing';//订单进行中
  163. $info['orderinfo']['tracking_number'] = $order_info['waybill'];
  164. $info['orderinfo']['carrier'] = $express_info['iscode'];
  165. $info['orderinfo']['tracking_url'] = $express_info['url'];
  166. $this->sendOrderNotice(json_encode($info));
  167. }
  168. //向crm发送订单通知
  169. protected function sendOrderNotice($params){
  170. $all_url = "http://crm.hnwmzp.cn/sqs";
  171. $ch = curl_init($all_url);
  172. // 设置cURL选项
  173. $options = array(
  174. CURLOPT_SSL_VERIFYPEER=>false,
  175. CURLOPT_SSL_VERIFYHOST=>false,
  176. CURLOPT_RETURNTRANSFER => true,
  177. CURLOPT_POST => true,
  178. CURLOPT_POSTFIELDS => $params,
  179. CURLOPT_HTTPHEADER => [
  180. 'lytoken'=>'lytec',
  181. 'Content-Type: application/json'
  182. ],
  183. );
  184. // 应用这些选项到cURL会话
  185. curl_setopt_array($ch, $options);
  186. // 执行cURL会话并获取响应
  187. $response = curl_exec($ch);
  188. //var_dump( $response );
  189. // 检查是否有错误发生
  190. if ($response === false) {
  191. $error = curl_error($ch);
  192. curl_close($ch);
  193. return [
  194. "code"=> -1,
  195. "msg"=> $error
  196. ];
  197. }
  198. // 关闭cURL会话
  199. curl_close($ch);
  200. // 输出响应内容
  201. return json_decode($response,true);
  202. }
  203. }