123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace App\Http\Controllers\Wechat\Staff;
- use App\Http\Models\OfficialAccount;
- use App\Http\Requests;
- use App\Http\Controllers\WechatController;
- use App\Http\Controllers\Wechat\Staff\StaffsController;
- use Illuminate\Http\Request;
- use Illuminate\Http\Response;
- use EasyWeChat\Foundation\Application;
- use App\Http\Models\WechatUser;
- use App\Http\Models\WechatTemplateSendInfo;
- use App\Http\Models\WechatTemplateTask;
- use App\Http\Models\WechatTemplateOfficialInfo;
- use Redis;
- /**
- * 图文模板消息定时发送类
- * @author zhoulingjie
- * cat /var/log/supervisord-logs/send_news_list* | grep '{"send_time":"2018-04-08 18' | wc -l
- * cat /var/log/supervisord-logs/send* | grep '"receive_time":"2018-02-01 09' | wc -l
- * cat /var/log/supervisord-logs/send* | grep '"receive_time":"2018-02-01 09:01' | wc -l
- *
- */
- class NewsWorksController extends WechatController
- {
-
- public function __construct($template_info)
- {
- $this->template_info = $template_info;
- // v('$template_info');v($this->template_info);
- $this->gzh_app_id = isset($this->template_info['appid'])?$this->template_info['appid']:'';
- if(!empty($this->gzh_app_id)){
- parent::__construct($this->gzh_app_id);
- $this->Staff = new StaffsController($this->param);
- }else{
- v('template_work_appid is null');
- }
- $this->error_msg = '';
- }
-
- /**
- * 执行模板消息
- */
- public function do_work()
- {
- if(empty($this->gzh_app_id)) return false;
-
- // 检查表数据
- $openid = $this->template_info['openid'];
- v('do_work_start_news:'.$openid);
-
- $appid = $this->template_info['appid'];
- $task_id = $this->template_info['task_id'];
- $type = $this->template_info['type'];
- $news_content = $this->template_info['news_content'];
- if(empty($openid) || empty($task_id) || empty($news_content)){
- v('invalid_null_task_id:'.$task_id.' openid:'.$openid);
- return false;
- }
-
- $template_task = $this->WechatApi->get_template_task($task_id);
- $status = isset($template_task['status']) ? $template_task['status']:'';
- if($status == 4){
- v('task_id_close:'.$task_id);
- return false;
- }
-
- $this->send_one_template($openid,$news_content,$task_id);
-
- if($type == 'last_task' && $status != 3){
- //v('update_news_task_status:'.$task_id);
- $status = 3;
- $this->WechatApi->update_template_task_status($task_id, $status, $this->error_msg);
- }
- }
-
- function send_one_template($openid,$datas,$task_id=''){
- try{
- $datas = objectToArray(json_decode($datas));
- $send_data = array();
- foreach($datas as $no=> $data){
- foreach($data as $_data){
- foreach($_data as $key=>$one_data){
- $send_data[$no][$key] = $one_data;
- }
- }
- }
- $to_send_data = array();
- $to_send_data['text'] = $send_data;
- $result = '';
- $result = $this->Staff->batch_send_wechat_content($openid, $to_send_data);
- if($task_id && is_object($result) && isset($result->errmsg) && $result->errmsg == 'ok')
- {
- Redis::sadd('cus_ok:'.$task_id,$openid);//记录真正发送成功数
- }
- }
- // 加上\ 全局抓取
- catch(\Exception $e){
- v('send_openid_ept:oKtP5w5SeNDNPmN6qN-4llpVUPbw'.$openid.' info:'.$e->getMessage());
- $this->error_msg = $e->getMessage();
- }
- }
-
-
- }
|