Webhook.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. class Webhook extends Start_Controller {
  3. public function __construct(){
  4. parent::__construct();
  5. $this->load->library('session');
  6. $this->load->_model('Model_fullorder','fullorder');
  7. $this->load->_model('Model_fullordertt','fullordertt');
  8. $this->load->_model('Model_fullordersmt','fullordersmt');
  9. $this->load->_model('Model_als','als');
  10. $this->load->_model('Model_notice','notice');
  11. $this->load->_model('Model_express','express');
  12. $this->load->_model('Model_shop','shop');
  13. }
  14. public function _remap($arg,$arg_array)
  15. {
  16. if($arg == '17track')
  17. {
  18. $this->_17track();
  19. }
  20. else
  21. {
  22. $this->_index();
  23. }
  24. }
  25. public function _17track()
  26. {
  27. $xq = array();
  28. $j = file_get_contents('PHP://input');
  29. $j = json_decode($j, true);
  30. if(isset($j['data']) && isset($j['sign']))
  31. {
  32. $sh = hash("sha256",$j['event'].'/'.json_encode($j['data']).'/0811AC1711EAABB7D764D04B824F8C2D');
  33. if($j['data']['number'] != '')
  34. {
  35. $f = 'fullorder';
  36. $d = $this->fullorder->get_waybill($j['data']['number']);
  37. if(!$d)
  38. {
  39. $f = 'fullordersmt';
  40. $d = $this->fullordersmt->get_waybill($j['data']['number']);
  41. }
  42. if(!$d)
  43. {
  44. exit;
  45. }
  46. if($j['data']['track']['e'] == 0)//查询不到
  47. {
  48. $exstate = 0;//无信息
  49. }
  50. else if($j['data']['track']['e'] == 10)//运输途中
  51. {
  52. $exstate = 3;//在途中
  53. }
  54. else if($j['data']['track']['e'] == 20)//运输过久
  55. {
  56. $exstate = 3;//在途中
  57. }
  58. else if($j['data']['track']['e'] == 30)//到达待取
  59. {
  60. $exstate = 5;//派送中
  61. }
  62. else if($j['data']['track']['e'] == 35)//投递失败
  63. {
  64. $exstate = 1;//派送异常
  65. }
  66. else if($j['data']['track']['e'] == 40)//成功签收
  67. {
  68. $exstate = 6;//签收
  69. }
  70. else if($j['data']['track']['e'] == 50)//可能异常
  71. {
  72. $exstate = 1;//派送异常
  73. }
  74. if(isset($j['data']['track']['z1'][0]))
  75. {
  76. foreach ($j['data']['track']['z1'] as $v)
  77. {
  78. $xq .= $v['a'].' ['.$v['c'].'] '.$v['z'].'<br />';// a日期 c地址 z状态
  79. }
  80. }
  81. $this->$f->save(array('expressstate'=>$exstate,'excontent'=>$xq),$d['id']);
  82. $time = time();
  83. $notice = $this->notice->find_all("message = '1' and state = '1' and type = '2' and ktime < '$time' and jtime > '$time' and shop = '".$d['shop']."' and logisticstatus = '".$exstate."'");
  84. if($notice && $d['wlyc'] == '0' && $d['wltype'] != '1' && $d['source'] != '1' && stripos($d['wlcontent'],$notice[0]['title']) === false)
  85. {
  86. $this->_sdfs($d,$notice[0]['id']);
  87. }
  88. }
  89. }
  90. }
  91. public function _sdfs($fullorder,$id)//手动发送信息
  92. {
  93. $time = time();
  94. $notice = $this->notice->read($id);//无条件同模板匹配
  95. $shop = $this->shop->read($fullorder['shop']);
  96. $express = $this->express->read($fullorder['express']);
  97. if($notice['type'] == '1')
  98. {
  99. $fslx = 'tab';
  100. $content = '自动发送';
  101. $stripos = '1-';
  102. }
  103. else if ($notice['type'] == '2')
  104. {
  105. $fslx = 'logisticstatus';
  106. $content = $notice['title'];
  107. $stripos = $notice['type'].'-'.$notice[$fslx].'|';
  108. }
  109. if(stripos($fullorder['zdstate'],$stripos) === false && $fullorder['wltype'] == '0')//如果没发送过并且允许发送
  110. {
  111. $go = $this->notice->get_god($fullorder,$shop,$express,$notice['content'],$notice['message'],$notice['emailtitle']);
  112. if($go == 1)
  113. {
  114. if($notice['type'] == '1')
  115. {
  116. $this->fullorder->save(array('content'=>$fullorder['content'].$content.'-成功;','zdstate'=>$fullorder['zdstate'].$notice['type'].'-'.$notice[$fslx].'|','exstateerror'=>''),$fullorder['id']);
  117. }
  118. else if ($notice['type'] == '2')
  119. {
  120. $this->fullorder->save(array('wlcontent'=>$fullorder['wlcontent'].$content.'-成功;','wlfstime'=>time(),'zdstate'=>$fullorder['zdstate'].$notice['type'].'-'.$notice[$fslx].'|','exstateerror'=>''),$fullorder['id']);
  121. }
  122. return 1;
  123. }
  124. else
  125. {
  126. if($notice['type'] == '1')
  127. {
  128. $this->fullorder->save(array('exstateerror'=>$content.'发送失败:'.date('Y-m-d H',time()).'-'.$go.'('.$notice['type'].'-'.$notice[$fslx].')'),$fullorder['id']);
  129. }
  130. else if ($notice['type'] == '2')
  131. {
  132. $this->fullorder->save(array('wlerror'=>$content.'发送失败:'.date('Y-m-d H',time()).'-'.$go.'('.$notice['type'].'-'.$notice[$fslx].')'),$fullorder['id']);
  133. }
  134. }
  135. }
  136. }
  137. }