StaffsController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. foreach($to_send_datas as $send_type=>$to_send_data){
  35. if($send_type == 'image'){
  36. $this->send_wechat_content_image($openid, $to_send_data);
  37. }
  38. if($send_type == 'text'){
  39. $result = $this->send_wechat_content_text($openid, $to_send_data,$reply_type);
  40. }
  41. }
  42. return $result;
  43. }
  44. /**
  45. * 图片消息推送-->image
  46. * @param Request $request
  47. */
  48. public function send_wechat_content_image($openid,$send_datas)
  49. {
  50. $medis_id = isset($send_datas['media_id'])?$send_datas['media_id']:'';
  51. v('send_wechat_content_image_start:'.$openid.' medis_id:'.$medis_id);
  52. v($send_datas);
  53. if(empty($medis_id) || empty($openid) || empty($send_datas)) {
  54. v('send_wechat_content_image_invalid_param:'.$openid);
  55. return '';
  56. }
  57. v('before_send_datas');
  58. // v($send_datas);
  59. $last_send_data = new Image(['media_id' => $medis_id]);
  60. try{
  61. v('send_to_openid:'.$openid);
  62. if(!empty($last_send_data)){
  63. $result = $this->app->staff->message($last_send_data)->to($openid)->send();
  64. v($result);
  65. }
  66. }
  67. // 加上\ 全局抓取
  68. catch(\Exception $e){
  69. v('send_wechat_content_image_ept:'.$openid.' info:'.$e->getMessage());
  70. }
  71. v('send_wechat_content_image_end:'.$openid);
  72. return '';
  73. }
  74. /**
  75. * 多图文消息推送-->text
  76. * @param Request $request
  77. */
  78. public function send_wechat_content_text($openid,$send_datas,$reply_type='staff')
  79. {
  80. // v('send_wechat_content_text_start:'.$openid);
  81. if(empty($openid) || empty($send_datas)) {
  82. v('send_wechat_content_text_invalid_param:'.$openid);
  83. return '';
  84. }
  85. // v('before_send_datas_text');
  86. // v($send_datas);
  87. $last_send_data = null;
  88. if(is_array($send_datas)){
  89. $news_tpls = array();
  90. $num = 0;
  91. foreach($send_datas as $send_data){
  92. $news = new News([
  93. 'title' => $send_data['title'],
  94. 'description' => $send_data['description'],
  95. 'url' => $send_data['url'],
  96. 'image' => $send_data['image'],
  97. // ...
  98. ]);
  99. $news_tpls[] = $news;
  100. $num++;
  101. if($num >= 8) break;
  102. }
  103. $last_send_data = $news_tpls;
  104. // v('$news_tpls');v($news_tpls);
  105. }else{
  106. // v('$text');v($send_datas);
  107. $last_send_data = $send_datas;
  108. }
  109. try{
  110. // v('send_to_openid:'.$openid);
  111. if(!empty($last_send_data)){
  112. // 直接返回
  113. if($reply_type == 'direct_return'){
  114. v('direct_return:'.$openid);
  115. return $last_send_data;
  116. }else{
  117. $result = $this->app->staff->message($last_send_data)->to($openid)->send();
  118. return $result;
  119. }
  120. //v('staff:send:result:');v($result);
  121. }
  122. }
  123. // 加上\ 全局抓取
  124. catch(\Exception $e){
  125. v('send_wechat_content_text_ept:'.$openid.' info:'.$e->getMessage());
  126. }
  127. // v('send_wechat_content_text_end:'.$openid);
  128. return '';
  129. }
  130. }