StaffsController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace App\Http\Controllers\Wechat\Staff;
  3. use App\Http\Requests;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\Response;
  6. use EasyWeChat\Foundation\Application;
  7. use EasyWeChat\Message\News;
  8. use EasyWeChat\Message\Text;
  9. use EasyWeChat\Message\Image;
  10. /**
  11. * 客服消息管理
  12. * @author zhoulingjie
  13. *
  14. */
  15. class StaffsController
  16. {
  17. public function __construct($_param)
  18. {
  19. $this->app = $_param['app'];
  20. }
  21. /**
  22. * 批量消息推送
  23. * 注意:text消息要被动回复出去
  24. * @param Request $request
  25. */
  26. public function batch_send_wechat_content($openid,$to_send_datas,$reply_type='staff')
  27. {
  28. v('batch_send_wechat_content_start:'.$openid);
  29. if(empty($to_send_datas) || empty($openid)) {
  30. v('batch_send_wechat_content_invalid_param:'.$openid);
  31. return '';
  32. }
  33. $result = '';
  34. v('to_send_datas:'.json_encode($to_send_datas));
  35. foreach($to_send_datas as $send_type=>$to_send_data){
  36. if($send_type == 'image'){
  37. $this->send_wechat_content_image($openid, $to_send_data);
  38. }
  39. if($send_type == 'text'){
  40. $result = $this->send_wechat_content_text($openid, $to_send_data,$reply_type);
  41. }
  42. }
  43. return $result;
  44. }
  45. /**
  46. * 图片消息推送-->image
  47. * @param Request $request
  48. */
  49. public function send_wechat_content_image($openid,$send_datas)
  50. {
  51. $medis_id = isset($send_datas['media_id'])?$send_datas['media_id']:'';
  52. v('send_wechat_content_image_start:'.$openid.' medis_id:'.$medis_id);
  53. v($send_datas);
  54. if(empty($medis_id) || empty($openid) || empty($send_datas)) {
  55. v('send_wechat_content_image_invalid_param:'.$openid);
  56. return '';
  57. }
  58. v('before_send_datas');
  59. // v($send_datas);
  60. $last_send_data = new Image(['media_id' => $medis_id]);
  61. try{
  62. v('send_to_openid:'.$openid);
  63. if(!empty($last_send_data)){
  64. $result = $this->app->staff->message($last_send_data)->to($openid)->send();
  65. v($result);
  66. }
  67. }
  68. // 加上\ 全局抓取
  69. catch(\Exception $e){
  70. v('send_wechat_content_image_ept:'.$openid.' info:'.$e->getMessage());
  71. }
  72. v('send_wechat_content_image_end:'.$openid);
  73. return '';
  74. }
  75. /**
  76. * 多图文消息推送-->text
  77. * @param Request $request
  78. */
  79. public function send_wechat_content_text($openid,$send_datas,$reply_type='staff')
  80. {
  81. // v('send_wechat_content_text_start:'.$openid);
  82. if(empty($openid) || empty($send_datas)) {
  83. v('send_wechat_content_text_invalid_param:'.$openid);
  84. return '';
  85. }
  86. // v('before_send_datas_text');
  87. // v($send_datas);
  88. $last_send_data = null;
  89. if(is_array($send_datas)){
  90. $news_tpls = array();
  91. $num = 0;
  92. foreach($send_datas as $send_data){
  93. $news = new News([
  94. 'title' => $send_data['title'],
  95. 'description' => $send_data['description'],
  96. 'url' => $send_data['url'],
  97. 'image' => $send_data['image'],
  98. // ...
  99. ]);
  100. $news_tpls[] = $news;
  101. $num++;
  102. if($num >= 8) break;
  103. }
  104. $last_send_data = $news_tpls;
  105. // v('$news_tpls');v($news_tpls);
  106. }else{
  107. // v('$text');v($send_datas);
  108. $last_send_data = $send_datas;
  109. }
  110. try{
  111. // v('send_to_openid:'.$openid);
  112. if(!empty($last_send_data)){
  113. // 直接返回
  114. if($reply_type == 'direct_return'){
  115. v('direct_return:'.$openid);
  116. return $last_send_data;
  117. }else{
  118. $result = $this->app->staff->message($last_send_data)->to($openid)->send();
  119. return $result;
  120. }
  121. //v('staff:send:result:');v($result);
  122. }
  123. }
  124. // 加上\ 全局抓取
  125. catch(\Exception $e){
  126. v('send_wechat_content_text_ept:'.$openid.' info:'.$e->getMessage());
  127. }
  128. // v('send_wechat_content_text_end:'.$openid);
  129. return '';
  130. }
  131. }