MiniMessageNoticeJob.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace app\Jobs;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Queue\SerializesModels;
  5. use Illuminate\Queue\InteractsWithQueue;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. class MiniMessageNoticeJob implements ShouldQueue
  8. {
  9. use InteractsWithQueue, Queueable, SerializesModels;
  10. /**
  11. * The number of times the job may be attempted.
  12. *
  13. * @var int
  14. */
  15. public $tries = 5;
  16. /**
  17. * The number of seconds the job can run before timing out.
  18. *
  19. * @var int
  20. */
  21. public $timeout = 120;
  22. protected $noticeModel;
  23. protected $templateId;
  24. protected $noticeData;
  25. protected $openId;
  26. protected $url;
  27. protected $formId;
  28. protected $app_id;
  29. protected $app_secret;
  30. protected $get_token_url;
  31. protected $miniprogram_state;
  32. protected $lang;
  33. /**
  34. * Create a new job instance.
  35. *
  36. *
  37. */
  38. public function __construct($options, $templateId, $noticeData, $openId, $url)
  39. {
  40. $this->app_id = $options['app_id'];
  41. $this->app_secret = $options['secret'];
  42. $this->templateId = $templateId;
  43. $this->noticeData = $noticeData;
  44. $this->openId = $openId;
  45. $this->url = $url?:'pages/index/index';
  46. //$this->formId = $formId;
  47. $this->miniprogram_state = 'formal'; //developer为开发版;trial为体验版;formal为正式版;默认为正式版
  48. $this->lang = 'zh_CN'; //支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN
  49. $this->get_token_url = 'https://api.weixin.qq.com/cgi-bin/token?'
  50. .'grant_type=client_credential&appid=%s&secret=%s';
  51. // "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=$code&grant_type=authorization_code"
  52. }
  53. /**
  54. * Execute the job.
  55. *
  56. * @return bool
  57. */
  58. public function handle()
  59. {
  60. if ($this->attempts() > 2) {
  61. \Log::info('消息通知测试,执行大于两次终止');
  62. return true;
  63. }
  64. $this->sendTemplate();
  65. //$this->noticeModel->uses($this->templateId)->andData($this->noticeData)->andReceiver($this->openId)->andUrl($this->url)->send();
  66. return true;
  67. }
  68. public function sendTemplate($method_msg = 'sendTemplate'){
  69. $opUrl = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=%s";
  70. //$opUrl = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=%s";
  71. $rawPost = [
  72. 'touser' => $this->openId ,
  73. 'template_id' => $this->templateId,
  74. 'page' => $this->url,
  75. 'data' => $this->noticeData,
  76. 'miniprogram_state' => $this->miniprogram_state,
  77. 'lang' => $this->lang
  78. ];
  79. \Log::debug('=================111111参数1111111================');
  80. \Log::debug($rawPost);
  81. $this->opTemplateData($opUrl,$rawPost,$method_msg);
  82. }
  83. /**
  84. * 提取公共方法 获取模板数据
  85. * @param string $opUrl
  86. * @param array $rawPost
  87. * @param string $method
  88. */
  89. public function opTemplateData($opUrl = '',$rawPost = [],$method = ''){
  90. $access_token = self::opGetAccessToken();
  91. \Log::debug('=================22222 access_token 2222================');
  92. \Log::debug($access_token);
  93. if(!$access_token){
  94. \Log::debug('获取 access_token 时异常,微信内部错误');
  95. return;
  96. }else{
  97. $templateUrl = sprintf($opUrl,$access_token);
  98. $listRes = self::curl_post($templateUrl,$rawPost);
  99. \Log::debug($templateUrl);
  100. \Log::debug($rawPost);
  101. \Log::debug('=================33333333发送返回值333333333================');
  102. \Log::debug($listRes);
  103. $wxResult = json_decode($listRes,true);
  104. if($wxResult['errcode']){
  105. return ($method.' - Failed!:'.$wxResult);
  106. }else{
  107. return $wxResult;
  108. }
  109. }
  110. }
  111. /**
  112. * 提取公共方法 - 获取 AccessToken
  113. * @return bool
  114. */
  115. public function opGetAccessToken(){
  116. $get_token_url = sprintf($this->get_token_url, $this->app_id,$this->app_secret);
  117. $result = self::curl_get($get_token_url);
  118. $wxResult = json_decode($result,true);
  119. if(empty($wxResult)){
  120. return false;
  121. }else{
  122. $access_token = $wxResult['access_token'];
  123. return $access_token;
  124. }
  125. }
  126. /**
  127. * @param string $url get请求地址
  128. * @param int $httpCode 返回状态码
  129. * @return mixed
  130. */
  131. protected function curl_get($url,&$httpCode = 0){
  132. $ch = curl_init();
  133. curl_setopt($ch,CURLOPT_URL,$url);
  134. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  135. //不做证书校验,部署在linux环境下请改位true
  136. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,true);
  137. curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);
  138. $file_contents = curl_exec($ch);
  139. $httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
  140. curl_close($ch);
  141. return $file_contents;
  142. }
  143. /**
  144. * PHP 处理 post数据请求
  145. * @param $url 请求地址
  146. * @param array $params 参数数组
  147. * @return mixed
  148. */
  149. protected function curl_post($url,array $params = array()){
  150. //TODO 转化为 json 数据
  151. $data_string = json_encode($params);
  152. $ch = curl_init();
  153. curl_setopt($ch,CURLOPT_URL,$url);
  154. curl_setopt($ch,CURLOPT_HEADER,0);
  155. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  156. curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);
  157. curl_setopt($ch,CURLOPT_POST,1);
  158. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
  159. curl_setopt($ch,CURLOPT_POSTFIELDS,$data_string);
  160. curl_setopt($ch,CURLOPT_HTTPHEADER,
  161. array(
  162. 'Content-Type: application/json'
  163. )
  164. );
  165. $data = curl_exec($ch);
  166. curl_close($ch);
  167. return ($data);
  168. }
  169. }