WechatNoticeService.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace app\common\services\wechatNotice;
  3. use app\Jobs\DispatchesJobs;
  4. class WechatNoticeService
  5. {
  6. private $uniacid;
  7. private $request;
  8. public static $current;
  9. public function __construct($uniacid = '')
  10. {
  11. if ($uniacid) {
  12. $this->uniacid = $uniacid;
  13. } else{
  14. $this->uniacid = \YunShop::app()->uniacid;
  15. }
  16. }
  17. public static function current($uniacid = '')
  18. {
  19. if (!isset(self::$current)) {
  20. return new static($uniacid);
  21. }
  22. return self::$current;
  23. }
  24. /**
  25. * @param $message_key //消息唯一key值
  26. * @param $param //替换参数集合
  27. * @param $openid //发送人openid,可以是数组
  28. * @param int $type //1:订阅消息,2客服消息
  29. * @param string $url //消息跳转链接,有特殊跳转链接可传
  30. * @param array $miniprogram //消息跳转小程序,有特殊跳转链接可传
  31. * @return bool
  32. */
  33. public function sendMessage($message_key,$param,$openid,$type = 1,$url = '',$miniprogram = [])
  34. {
  35. if (empty($openid) || empty($message_key)) {
  36. return true;
  37. }
  38. if (app('plugins')->isEnabled('wechat-notice')) {
  39. $this->setRequest(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS,2));
  40. if (is_array($openid)) {
  41. foreach ($openid as $item) {
  42. $job = new \Yunshop\WechatNotice\job\SendMessageJob($message_key,$param,$item,$this->uniacid,$this->request,$type,$url,$miniprogram);
  43. DispatchesJobs::dispatch($job, DispatchesJobs::LOW);
  44. }
  45. } else {
  46. $job = new \Yunshop\WechatNotice\job\SendMessageJob($message_key,$param,$openid,$this->uniacid,$this->request,$type,$url,$miniprogram);
  47. DispatchesJobs::dispatch($job, DispatchesJobs::LOW);
  48. }
  49. }
  50. return true;
  51. }
  52. protected function setRequest($request)
  53. {
  54. $this->request = 'file:'.$request[0]?$request[0]['file'].',function:'.$request[0]['function'].',line:'.$request[0]['line']:'';
  55. }
  56. }