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(); } } }