TemplateWorksController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Http\Controllers\Wechat\Template;
  3. use App\Http\Models\OfficialAccount;
  4. use App\Http\Requests;
  5. use App\Http\Controllers\WechatController;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Http\Response;
  8. use EasyWeChat\Foundation\Application;
  9. use App\Http\Models\WechatUser;
  10. use App\Http\Models\WechatTemplateSendInfo;
  11. use App\Http\Models\WechatTemplateTask;
  12. use App\Http\Models\WechatTemplateOfficialInfo;
  13. /**
  14. * 模板消息定时发送类
  15. * @author zhoulingjie
  16. *
  17. */
  18. class TemplateWorksController extends WechatController
  19. {
  20. public function __construct($template_info)
  21. {
  22. $this->template_info = $template_info;
  23. // v('$template_info');v($this->template_info);
  24. $this->gzh_app_id = isset($this->template_info['appid'])?$this->template_info['appid']:'';
  25. if(!empty($this->gzh_app_id)){
  26. parent::__construct($this->gzh_app_id);
  27. }else{
  28. v('template_work_appid is null');
  29. }
  30. $this->error_msg = '';
  31. }
  32. /**
  33. * 执行模板消息
  34. */
  35. public function do_work()
  36. {
  37. if(empty($this->gzh_app_id)) return false;
  38. v('do_work_start2');
  39. // 检查表数据
  40. $openid = $this->template_info['openid'];
  41. $appid = $this->template_info['appid'];
  42. $url = $this->template_info['url'];
  43. $task_id = $this->template_info['task_id'];
  44. $type = $this->template_info['type'];
  45. $template_id = $this->template_info['template_id'];
  46. $template_content = $this->template_info['template_content'];
  47. if(empty($openid) || empty($task_id) || empty($template_id)){
  48. v('invalid_null_task_id:'.$task_id.' openid:'.$openid);
  49. return false;
  50. }
  51. v('template_start:openid:'.$openid);
  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,$template_id,$url,$template_content);
  60. if($type == 'last_task' && $status != 3){
  61. v('update_tamplate_task_status:'.$task_id);
  62. $status = 3;
  63. $this->WechatApi->update_template_task_status($task_id, $status, $this->error_msg);
  64. }
  65. // TODO 休眠200ms,外面的调度相当于for循环,直接不停止cpu会飙高,sleep的方式有待考证
  66. usleep(200000);
  67. v('update_tamplate_task_end:'.$task_id);
  68. }
  69. function send_one_template($openid,$templateId,$url,$data){
  70. try{
  71. v('send_to_openid:'.$openid.' templateId:'.$templateId.' url:'.$url);
  72. $data = objectToArray(json_decode($data));
  73. $send_data = array();
  74. foreach($data as $_data){
  75. foreach($_data as $key=>$one_data){
  76. $send_data[$key] = $one_data;
  77. }
  78. }
  79. // v($send_data);
  80. $result = $this->app->notice->uses($templateId)->withUrl($url)->andData($send_data)->andReceiver($openid)->send();
  81. // v('send_one_template_res:'.$openid.' res:'.json_encode($result));
  82. }
  83. // 加上\ 全局抓取
  84. catch(\Exception $e){
  85. v('send_openid_ept:'.$openid.' info:'.$e->getMessage());
  86. $this->error_msg = $e->getMessage();
  87. }
  88. }
  89. }