TextsWorksController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Http\Controllers\Wechat\Staff;
  3. use App\Http\Models\OfficialAccount;
  4. use App\Http\Requests;
  5. use App\Http\Controllers\WechatController;
  6. use App\Http\Controllers\Wechat\Staff\StaffsController;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Http\Response;
  9. use EasyWeChat\Foundation\Application;
  10. use App\Http\Models\WechatUser;
  11. use App\Http\Models\WechatTemplateSendInfo;
  12. use App\Http\Models\WechatTemplateTask;
  13. use App\Http\Models\WechatTemplateOfficialInfo;
  14. use Redis;
  15. /**
  16. * 自定义消息定时发送类
  17. * @author zhoulingjie
  18. *
  19. */
  20. class TextsWorksController extends WechatController
  21. {
  22. public function __construct($template_info)
  23. {
  24. $this->template_info = $template_info;
  25. // v('$template_info');v($this->template_info);
  26. $this->gzh_app_id = isset($this->template_info['appid'])?$this->template_info['appid']:'';
  27. if(!empty($this->gzh_app_id)){
  28. parent::__construct($this->gzh_app_id);
  29. $this->Staff = new StaffsController($this->param);
  30. }else{
  31. v('template_work_appid is null');
  32. }
  33. $this->error_msg = '';
  34. }
  35. /**
  36. * 执行模板消息
  37. */
  38. public function do_work()
  39. {
  40. if(empty($this->gzh_app_id)) return false;
  41. v('do_work_start_text');
  42. // 检查表数据
  43. $openid = $this->template_info['openid'];
  44. $appid = $this->template_info['appid'];
  45. $task_id = $this->template_info['task_id'];
  46. $type = $this->template_info['type'];
  47. $content = $this->template_info['content'];
  48. if(empty($openid) || empty($task_id) || empty($content)){
  49. v('invalid_null_task_id:'.$task_id.' openid:'.$openid);
  50. return false;
  51. }
  52. $template_task = $this->WechatApi->get_template_task($task_id);
  53. $status = isset($template_task['status']) ? $template_task['status']:'';
  54. v('task_id:'.$task_id.' status:'.$status);
  55. if($status == 4){
  56. v('task_id_close:'.$task_id);
  57. return false;
  58. }
  59. $this->send_one_template($openid,$content,$task_id);
  60. if($type == 'last_task' && $status != 3){
  61. v('update_text_task_status:'.$task_id);
  62. $status = 3;
  63. $this->WechatApi->update_template_task_status($task_id, $status, $this->error_msg);
  64. }
  65. v('update_text_task_end:'.$task_id);
  66. }
  67. function send_one_template($openid,$content,$task_id=''){
  68. try{
  69. v('send_text_to_openid:'.$openid);
  70. // v('content:');v($content);
  71. $result = $this->param['app']->staff->message($content)->to($openid)->send();
  72. if($task_id && is_object($result) && isset($result->errcode) && $result->errcode == 0)
  73. {
  74. Redis::sadd('cus_ok:'.$task_id,$openid);//记录真正发送成功数
  75. }
  76. // v('send_text_result');v($result);
  77. }
  78. // 加上\ 全局抓取
  79. catch(\Exception $e){
  80. v('send_openid_ept:'.$openid.' info:'.$e->getMessage());
  81. $this->error_msg = $e->getMessage();
  82. }
  83. }
  84. }