0
0

Jobs.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace app\queue;
  3. use app\common\model\Config;
  4. use app\common\model\Orders;
  5. use app\common\model\SmsLog;
  6. use app\common\util\AppMsg;
  7. use think\Db;
  8. use think\queue\Job;
  9. use tools\Qxsms;
  10. class Jobs
  11. {
  12. /**
  13. * 发送极光推送
  14. * @param Job $job
  15. * @param $data 例:{"users":[1,2,3],"type":1,"msg":"有新订单需要你的处理","extra"=>[]}
  16. */
  17. public function jpush(Job $job, $data){
  18. $ndata = json_decode($data,true);
  19. $ret = $this->sendJpush($ndata['users'],$ndata['type'],$ndata['msg'],$ndata['extra']);
  20. if($ret){
  21. $job->delete();
  22. }else{
  23. if ($job->attempts() > 2) { //重试2次任务失败后记录失败日志
  24. $this->jobFail('jpush',$data);
  25. $job->delete(); // 删除任务
  26. }else{
  27. $job->release(3); // 3秒后重新执行
  28. }
  29. }
  30. // 增加信号量分发方法 暂时隐藏
  31. // pcntl_signal_dispatch();
  32. }
  33. /**
  34. * 短信发送
  35. * @param Job $job
  36. * @param $data {"orgId":1,"mobiles":["13838379498","13838379497"],"msg":"有新订单需要你的处理","fromId":0,"type":1}
  37. */
  38. public function qxsms(Job $job, $data){
  39. $ndata = json_decode($data,true);
  40. $ret = $this->sendSms($ndata['mobiles'],$ndata['msg'],$ndata['orgId'],$ndata['type']);
  41. if($ret){
  42. $job->delete();
  43. }else{
  44. if ($job->attempts() > 2) { //重试2次任务失败后记录失败日志
  45. $this->jobFail('qxsms',$data);
  46. $job->delete(); // 删除任务
  47. }else{
  48. $job->release(3); // 3秒后重新执行
  49. }
  50. }
  51. // 增加信号量分发方法
  52. //pcntl_signal_dispatch();
  53. }
  54. // 任务失败记录
  55. private function jobFail($cate,$data){
  56. Db::name('jobs_fail')->insert([
  57. 'cate' => $cate,
  58. 'payload' => $data,
  59. 'create_time' => date('Y-m-d H:i:s')
  60. ]);
  61. }
  62. /**
  63. * 发送短信
  64. * @param array $mobiles 手机号
  65. * @param string $msg 短信内容
  66. * @param int $orgId
  67. * @return bool
  68. */
  69. private function sendSms($mobiles=[],$msg='',$orgId=0,$fromId=0,$type=1){
  70. if(!$mobiles){
  71. return true;
  72. }
  73. try{
  74. $config = config('app.sms_config');
  75. if(empty($config['qx_corpid']) || empty($config['qx_pwd']) || empty($config['name'])){
  76. exception('短信配置缺少');
  77. }
  78. $msg = '【'.$config['name'].'】'.$msg;
  79. $sms = new Qxsms($config);
  80. $smsonoff = (new Config())->getConfig('web_send_sms');
  81. $smsorgonoff = (new Config())->getConfig('org_send_sms',$orgId);
  82. if($smsonoff && $smsorgonoff){
  83. $ret = $sms->send(implode(',',$mobiles),$msg);
  84. if(!$ret){
  85. exception($sms->getError());
  86. }
  87. // 短信日志
  88. (new SmsLog())->addLog(implode(',',$mobiles),$msg,$orgId,$fromId,$type);
  89. }
  90. return true;
  91. }catch (\Exception $e){
  92. trace('短信发送失败:'.$e->getMessage());
  93. trace('短信发送号码:'.implode(',',$mobiles));
  94. trace('短信发送内容:['.$orgId.']'.$msg);
  95. return false;
  96. }
  97. }
  98. /**
  99. * 极光推送
  100. * @param array $userids
  101. * @param string $msg
  102. * @param array $extras
  103. */
  104. private function sendJpush($userids,$type,$msg='',$extra=array()){
  105. if(!$userids){
  106. return true;
  107. }
  108. try { //不管推送成功与失败,不能影响正常业务流程
  109. $jpushconfig = config('app.jpush');
  110. $client = new \JPush\Client($jpushconfig['appkey'], $jpushconfig['mastersecret'],null);
  111. $push = $client->push();
  112. $push->setOptions(null,null,null, true);
  113. $push->setPlatform(array('ios', 'android'));
  114. foreach ($userids as $k=>$v){
  115. $userids[$k] = (string)$v;
  116. }
  117. $push->addAlias($userids);
  118. $extras = array('type'=>$type);
  119. if($extra){
  120. foreach ($extra as $k=>$v){
  121. if($k != 'type'){
  122. $extras[$k] = $v;
  123. }
  124. }
  125. }
  126. $msg = AppMsg::getPushMsg($type,$msg);
  127. $push->androidNotification($msg, array(
  128. 'extras' => $extras
  129. ));
  130. $sound = 'default';
  131. if($type == 2 || $type == 7){
  132. $sound = 'type'.$type.'.mp3';
  133. }
  134. $push->iosNotification($msg, array(
  135. 'sound' => $sound,
  136. 'badge' => '+1',
  137. 'extras' => $extras
  138. ));
  139. $ret = $push->send();
  140. trace($ret);
  141. return true;
  142. } catch (\Exception $e) {
  143. trace('push-error:'.$e->getMessage());
  144. return false;
  145. }
  146. }
  147. /**
  148. * 自动派单轮转
  149. * @param Job $job
  150. */
  151. public function autoSend(Job $job, $data){
  152. $ndata = json_decode($data,true);
  153. $ret = $this->toAutoSend($ndata);
  154. if($ret){
  155. $job->delete();
  156. }else{
  157. if ($job->attempts() > 2) { //重试2次任务失败后记录失败日志
  158. $this->jobFail('autoSend',$data);
  159. $job->delete(); // 删除任务
  160. }else{
  161. $job->release(3); // 3秒后重新执行
  162. }
  163. }
  164. }
  165. public function toAutoSend($ndata){
  166. $min= (new Config())
  167. ->getConfig('org_auto_send_time');
  168. $time = intval($min);
  169. if($time <5){
  170. return true;
  171. }
  172. $orgId = $ndata['org_id'];
  173. $orgGrabOrder = (new Config())
  174. ->getConfig('org_grab_order1',$orgId);
  175. if(!$orgGrabOrder){
  176. return true;
  177. }
  178. $send_off= (new Config())
  179. ->getConfig('org_auto_send',$orgId);
  180. if(!$send_off){
  181. return true;
  182. }
  183. $date = date('Y-m-d H:i:s',time()-(60*$time));
  184. $orders = Db::name('orders')
  185. ->alias('a')
  186. ->join('order_repair b','a.id=b.order_id')
  187. ->where('a.order_mode',1)
  188. ->where('a.work_type_mode',1)
  189. ->where('a.org_id',$orgId)
  190. ->where('a.del',0)
  191. ->where('a.create_time','<=',$date)
  192. ->where('b.type_id','>',0)
  193. ->field('a.id')
  194. ->select();
  195. foreach ($orders as $k=>$v){
  196. (new Orders())->autoSend($v['id']);
  197. }
  198. return true;
  199. }
  200. }