Apidsyskxcx.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. defined('BASEPATH') or exit('No direct script access allowed');
  3. /**
  4. * 此类是为了解决erp没有队列的问题,为了解决这个问题,先写一下这个类,避免后期写那么多定时任务类 这里只能解决每间隔多少时间执行一会的
  5. */
  6. class Apidsyskxcx extends Start_Controller
  7. {
  8. private $serect_str = "yskxcx";
  9. private $user_agent = 'Xcly251618/Yskxcx (Erp)';
  10. private $api = "Yskxcx25.424";
  11. private $ip = ['127.0.0.1', '47.105.156.18'];
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. $this->load->_model('Model_fullorder', 'fullorder');
  16. $this->load->_model("Model_logic_ding", "logic_ding");
  17. $this->load->_model("Model_cne", "cne");
  18. $this->load->_model("Model_zzjobs", "zzjobs");
  19. $this->load->_model('Model_notice','notice');
  20. $this->load->_model('Model_shop','shop');
  21. $this->load->_model('Model_express','express');
  22. $this->load->_model('Model_emaildata','emaildata');
  23. }
  24. public function _remap($arg, $arg_array)
  25. {
  26. $ip = $_SERVER['REMOTE_ADDR'];
  27. // if (!in_array($ip, $this->ip)) {
  28. // exit("Unauthorized access");
  29. // }
  30. $user_agent = empty($_SERVER['HTTP_USER_AGENT']) ? "" : $_SERVER['HTTP_USER_AGENT'];
  31. $token = empty($_SERVER['HTTP_X_AUTH_TOKEN']) ? "" : $_SERVER['HTTP_X_AUTH_TOKEN'];
  32. $time = $this->input->get('time', true);
  33. $api = $this->input->get('api', true);
  34. // $this->checkAuth($user_agent, $token, $time, $api);
  35. if ($arg == 'jobs') //调出单
  36. {
  37. $this->_doRenwu();
  38. } else {
  39. $this->_index();
  40. }
  41. }
  42. /**
  43. * 校验是否有权限可以执行
  44. * $user_agent 自定义的请求客户端名称
  45. * $token 自定义的清华客户端的token
  46. * $time 请求的时间
  47. *
  48. */
  49. private function checkAuth($user_agent, $token, $time, $api)
  50. {
  51. if ($api != $this->api) {
  52. exit("Access denied due to insufficient permissions");
  53. }
  54. $check_str = date("Ymd") . "¥_" . $this->serect_str . "_¥" . $time;
  55. if (empty($user_agent)) {
  56. exit('No direct script access allowed');
  57. }
  58. if ($user_agent != $this->user_agent) {
  59. exit('Illegal request');
  60. }
  61. if (empty($token)) {
  62. exit('No access permission');
  63. }
  64. $sercet_str = md5($check_str);
  65. if ($sercet_str != $token) {
  66. exit("Request permission is illegal");
  67. }
  68. }
  69. private function _index() {}
  70. private function _doRenwu()
  71. {
  72. echo "<pre>";
  73. $list = $this->zzjobs->find_all("status = 0 and quque = 'ysgjcx'", "*",null, 0, 1);
  74. $ids = array_column($list, 'id');
  75. $this->db->query("update crowd_zzjobs set status = 10 where id in (" . implode(",", $ids) . ")");
  76. foreach ($list as $v) {
  77. $this->doAction($v);
  78. sleep(2);
  79. }
  80. }
  81. private function doAction($v)
  82. {
  83. $param = json_decode($v['payload'], true);
  84. $info = $param['v'];
  85. $notice_id = $param['id'];
  86. $notice_info = $this->notice->read($notice_id);
  87. $fs = $this->cne->get_logistics_yskx(
  88. [
  89. [
  90. 'TrackNumber' => $info['waybill']
  91. ]
  92. ]
  93. );
  94. var_dump($fs);
  95. $wlfs = [];
  96. if (!isset($fs['f'])) {
  97. $this->fullorder->save(array('wlerror' => $fs['content']), $info['id']);
  98. } else {
  99. if ($fs['f'] == '1') {
  100. if ($fs['exstate'] > $info['expressstate'] || isset($fs['webhookregister'])) {
  101. $this->fullorder->save(array('expressstate' => $fs['exstate'], 'excontent' => $fs['data'], 'webhookregister' => 0, 'wlerror' => ''), $info['id']);
  102. }
  103. if ($fs['exstate'] == $notice_info['logisticstatus'] && $info['wlyc'] == '0' && $info['wltype'] != '1' && $info['source'] != '1') //获取快递状态等于模板状态、订单状态等于模板状态、出库时间大于-、未加入物流异常、允许发送邮件状态、非线下单
  104. {
  105. if ($info['shop'] < '7') // && $v['shouldmoney'] > 100)
  106. {
  107. $wlfs[] = array('v' => $info, 'id' => $notice_id );
  108. //$go = $this->_sdfs($v,$var['id']);//更新顺带发送
  109. }
  110. }
  111. } else {
  112. $this->fullorder->save(array('wlerror' => $fs['content']), $v['id']);
  113. }
  114. }
  115. $this->zzjobs->save(['status'=>30,'last_time'=>time(),'result'=>json_encode($fs,JSON_UNESCAPED_UNICODE)],$v['id']);
  116. // if(!empty($wlfs)){
  117. // $this->_sdfs($v['v'],$v['id']);
  118. // }
  119. // @$this->allocation->insert(array('title'=>$shopid.'-'.json_encode($csck)));
  120. // if($wlfs)
  121. // {
  122. // foreach ($wlfs as $k=>$v)
  123. // {
  124. // $t1 = microtime(true);
  125. // $this->_sdfs($v['v'],$v['id']);
  126. // $t2 = microtime(true);
  127. // $t3 = (40-($t2-$t1) < 1)?1:round(40-($t2-$t1));
  128. // sleep($t3);
  129. // }
  130. // }
  131. }
  132. public function _sdfs($fullorder,$id)//手动发送信息
  133. {
  134. $time = time();
  135. $notice = $this->notice->read($id);//无条件同模板匹配
  136. $notice['email'] = $this->emaildata->read($notice['email']);
  137. $shop = $this->shop->read($fullorder['shop']);
  138. $express = $this->express->read($fullorder['express']);
  139. if($notice['type'] == '1')
  140. {
  141. $fslx = 'tab';
  142. $content = '自动发送';
  143. $stripos = '1-';
  144. }
  145. else if ($notice['type'] == '2')
  146. {
  147. $fslx = 'logisticstatus';
  148. $content = $notice['title'];
  149. $stripos = $notice['type'].'-'.$notice[$fslx].'|';
  150. }
  151. $this->logic_ding->sendToDing("云尚轨迹获取快递信息暂时中断");
  152. return ;
  153. if(stripos($fullorder['zdstate'],$stripos) === false && $fullorder['wltype'] == '0')//如果没发送过并且允许发送
  154. {
  155. $go = $this->notice->get_god($fullorder,$shop,$express,$notice);
  156. $this->logic_ding->sendToDing("云尚轨迹获取快递信息".json_encode($go,JSON_UNESCAPED_UNICODE));
  157. if($go == 1)
  158. {
  159. if($notice['type'] == '1')
  160. {
  161. $this->fullorder->save(array('content'=>$fullorder['content'].$content.'-成功;','zdstate'=>$fullorder['zdstate'].$notice['type'].'-'.$notice[$fslx].'|','exstateerror'=>'','wlerror'=>''),$fullorder['id']);
  162. }
  163. else if ($notice['type'] == '2')
  164. {
  165. $this->fullorder->save(array('wlcontent'=>$fullorder['wlcontent'].$content.'-成功;','wlfstime'=>time(),'zdstate'=>$fullorder['zdstate'].$notice['type'].'-'.$notice[$fslx].'|','exstateerror'=>'','wlerror'=>''),$fullorder['id']);
  166. }
  167. return 1;
  168. }
  169. else
  170. {
  171. if($notice['type'] == '1')
  172. {
  173. $this->fullorder->save(array('exstateerror'=>$content.'发送失败1:'.date('Y-m-d H',time()).'-'.$go.'('.$notice['type'].'-'.$notice[$fslx].')'),$fullorder['id']);
  174. }
  175. else if ($notice['type'] == '2')
  176. {
  177. $this->fullorder->save(array('wlerror'=>$content.'发送失败2:'.date('Y-m-d H',time()).'-'.$go.'('.$notice['type'].'-'.$notice[$fslx].')'),$fullorder['id']);
  178. }
  179. }
  180. }
  181. }
  182. }