Apidsyskxcx.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. $list = $this->zzjobs->find_all("status = 0 and quque = 'ysgjcx'", "*",null, 0, 40);
  73. if(empty($list)){
  74. exit("There is no executable data available");
  75. }
  76. $ids = array_column($list, 'id');
  77. $this->db->query("update crowd_zzjobs set status = 10 where id in (" . implode(",", $ids) . ")");
  78. foreach ($list as $v) {
  79. $this->doAction($v);
  80. sleep(1);
  81. }
  82. }
  83. private function doAction($v)
  84. {
  85. $param = json_decode($v['payload'], true);
  86. $info = $param['v'];
  87. $notice_id = $param['id'];
  88. $notice_info = $this->notice->read($notice_id);
  89. $rr = $this->cne->get_logistics_yskx(
  90. [
  91. [
  92. 'TrackNumber' => $info['waybill']
  93. ]
  94. ]
  95. );
  96. if($rr['code'] != 1){
  97. return $this->zzjobs->save(['status'=>20,'last_time'=>time(),'result'=>json_encode($rr,JSON_UNESCAPED_UNICODE)],$v['id']);;
  98. }
  99. $fs = $rr['data'][0];
  100. $wlfs = [];
  101. if (!isset($fs['f'])) {
  102. $this->fullorder->save(array('wlerror' => $fs['content']), $info['id']);
  103. } else {
  104. if ($fs['f'] == '1') {
  105. if ($fs['exstate'] > $info['expressstate'] || isset($fs['webhookregister'])) {
  106. $this->fullorder->save(array('expressstate' => $fs['exstate'], 'excontent' => $fs['data'], 'webhookregister' => 0, 'wlerror' => ''), $info['id']);
  107. }else{
  108. $this->fullorder->save(array('excontent' => $fs['data'], 'webhookregister' => 0, 'wlerror' => ''), $info['id']);
  109. }
  110. if ($fs['exstate'] == $notice_info['logisticstatus'] && $info['wlyc'] == '0' && $info['wltype'] != '1' && $info['source'] != '1') //获取快递状态等于模板状态、订单状态等于模板状态、出库时间大于-、未加入物流异常、允许发送邮件状态、非线下单
  111. {
  112. if ($info['shop'] < '7') // && $v['shouldmoney'] > 100)
  113. {
  114. // $wlfs[] = array('v' => $info, 'id' => $notice_id );
  115. $this->_sdfs($info,$notice_id);
  116. //$go = $this->_sdfs($v,$var['id']);//更新顺带发送
  117. }
  118. }
  119. } else {
  120. $this->fullorder->save(array('wlerror' => $fs['content']), $v['id']);
  121. }
  122. }
  123. $this->zzjobs->save(['status'=>30,'last_time'=>time(),'result'=>json_encode($fs,JSON_UNESCAPED_UNICODE)],$v['id']);
  124. // if(!empty($wlfs)){
  125. // }
  126. // @$this->allocation->insert(array('title'=>$shopid.'-'.json_encode($csck)));
  127. // if($wlfs)
  128. // {
  129. // foreach ($wlfs as $k=>$v)
  130. // {
  131. // $t1 = microtime(true);
  132. // $this->_sdfs($v['v'],$v['id']);
  133. // $t2 = microtime(true);
  134. // $t3 = (40-($t2-$t1) < 1)?1:round(40-($t2-$t1));
  135. // sleep($t3);
  136. // }
  137. // }
  138. }
  139. public function _sdfs($fullorder,$id)//手动发送信息
  140. {
  141. $time = time();
  142. $notice = $this->notice->read($id);//无条件同模板匹配
  143. $notice['email'] = $this->emaildata->read($notice['email']);
  144. $shop = $this->shop->read($fullorder['shop']);
  145. $express = $this->express->read($fullorder['express']);
  146. if($notice['type'] == '1')
  147. {
  148. $fslx = 'tab';
  149. $content = '自动发送';
  150. $stripos = '1-';
  151. }
  152. else if ($notice['type'] == '2')
  153. {
  154. $fslx = 'logisticstatus';
  155. $content = $notice['title'];
  156. $stripos = $notice['type'].'-'.$notice[$fslx].'|';
  157. }
  158. $this->logic_ding->sendToDing("云尚轨迹获取快递信息暂时中断"."【".json_encode($fullorder)."】{".json_encode($notice)."}");
  159. return ;
  160. if(stripos($fullorder['zdstate'],$stripos) === false && $fullorder['wltype'] == '0')//如果没发送过并且允许发送
  161. {
  162. $go = $this->notice->get_god($fullorder,$shop,$express,$notice);
  163. $this->logic_ding->sendToDing("云尚轨迹获取快递信息".json_encode($go,JSON_UNESCAPED_UNICODE));
  164. if($go == 1)
  165. {
  166. if($notice['type'] == '1')
  167. {
  168. $this->fullorder->save(array('content'=>$fullorder['content'].$content.'-成功;','zdstate'=>$fullorder['zdstate'].$notice['type'].'-'.$notice[$fslx].'|','exstateerror'=>'','wlerror'=>''),$fullorder['id']);
  169. }
  170. else if ($notice['type'] == '2')
  171. {
  172. $this->fullorder->save(array('wlcontent'=>$fullorder['wlcontent'].$content.'-成功;','wlfstime'=>time(),'zdstate'=>$fullorder['zdstate'].$notice['type'].'-'.$notice[$fslx].'|','exstateerror'=>'','wlerror'=>''),$fullorder['id']);
  173. }
  174. return 1;
  175. }
  176. else
  177. {
  178. if($notice['type'] == '1')
  179. {
  180. $this->fullorder->save(array('exstateerror'=>$content.'发送失败1:'.date('Y-m-d H',time()).'-'.$go.'('.$notice['type'].'-'.$notice[$fslx].')'),$fullorder['id']);
  181. }
  182. else if ($notice['type'] == '2')
  183. {
  184. $this->fullorder->save(array('wlerror'=>$content.'发送失败2:'.date('Y-m-d H',time()).'-'.$go.'('.$notice['type'].'-'.$notice[$fslx].')'),$fullorder['id']);
  185. }
  186. }
  187. }
  188. }
  189. }