Jobs.php 4.9 KB

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