Apidsyskxcx.php 7.4 KB

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