123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- <?php
- namespace App\Http\Controllers\Wechat\Template;
- use App\Http\Requests;
- use Illuminate\Http\Request;
- use Illuminate\Http\Response;
- use EasyWeChat\Foundation\Application;
- use App\Http\Models\WechatTemplateBaseInfo;
- use App\Http\Models\WechatTemplateOfficialInfo;
- /**
- * 模板消息基础表,用于自动创建公众号模板行业和模板消息
- * @author zhoulingjie
- *
- */
- class TemplateBasesController
- {
-
- public function __construct($_param)
- {
- // v('$this->param');v($_param);
- $this->param = $_param;
- $this->app = $_param['app'];
- $this->WechatApi = $_param['WechatApi'];
- }
-
- /**
- * 自动开通模板行业、添加对应模板消息
- * @param Request $request
- */
- public function auto_set_dustry_templates()
- {
- v('auto_set_dustry_templates_before');
- // $this->get_industry();
- // $this->get_private_templates();
-
- // TODO 从group取,设置行业
- v('industry_first_id:'.$this->param['group']['industry_first_id'].' industry_second_id:'.$this->param['group']['industry_second_id']);
- $this->set_template_dustry($this->param['group']['industry_first_id'],$this->param['group']['industry_second_id']);
- // 设置模板消息
- $this->set_template_notices();
-
- v('auto_set_dustry_templates_after');
- }
-
- /**
- * 设置行业(传id) 默认 2,37
- *
- IT科技 互联网/电子商务 1
- IT科技 IT软件与服务 2
- IT科技 IT硬件与设备 3
- IT科技 电子技术 4
- IT科技 通信与运营商 5
- IT科技 网络游戏 6
- 金融业 银行 7
- 金融业 基金|理财|信托 8
- 金融业 保险 9
- 餐饮 餐饮 10
- 酒店旅游 酒店 11
- 酒店旅游 旅游 12
- 运输与仓储 快递 13
- 运输与仓储 物流 14
- 运输与仓储 仓储 15
- 教育 培训 16
- 教育 院校 17
- 政府与公共事业 学术科研 18
- 政府与公共事业 交警 19
- 政府与公共事业 博物馆 20
- 政府与公共事业 公共事业|非盈利机构 21
- 医药护理 医药医疗 22
- 医药护理 护理美容 23
- 医药护理 保健与卫生 24
- 交通工具 汽车相关 25
- 交通工具 摩托车相关 26
- 交通工具 火车相关 27
- 交通工具 飞机相关 28
- 房地产 建筑 29
- 房地产 物业 30
- 消费品 消费品 31
- 商业服务 法律 32
- 商业服务 会展 33
- 商业服务 中介服务 34
- 商业服务 认证 35
- 商业服务 审计 36
- 文体娱乐 传媒 37
- 文体娱乐 体育 38
- 文体娱乐 娱乐休闲 39
- 印刷 印刷 40
- 其它 其它 41
- */
- public function set_template_dustry($industryId1=2,$industryId2=37)
- {
- if(empty($industryId1)) $industryId1 = 2;
- if(empty($industryId2)) $industryId2 = 37;
- v('set_template_dustry_start,industryId1:'.$industryId1.' industryId2:'.$industryId2);
-
- //修改账号所属行业
- try{
- $set_dustry_res = $this->app->notice->setIndustry($industryId1, $industryId2);
- v('set_dustry_res');v($set_dustry_res);
- }
- // 加上\ 全局抓取
- catch(\Exception $e){
- v('set_template_dustry_ept:'.$e->getMessage());
- }
-
- v('set_template_dustry_end');
- }
-
- public function set_template_dustry_res($industryId1=2,$industryId2=37)
- {
- if(empty($industryId1)) $industryId1 = 2;
- if(empty($industryId2)) $industryId2 = 37;
- v('set_template_dustry_res_start,industryId1:'.$industryId1.' industryId2:'.$industryId2);
-
- //修改账号所属行业
- try{
- $set_dustry_res = $this->app->notice->setIndustry($industryId1, $industryId2);
- v('set_dustry_res');v($set_dustry_res);
- return 1;
- }
- // 加上\ 全局抓取
- catch(\Exception $e){
- $error_msg = $e->getMessage();
- v('set_template_dustry_res_ept:'.$error_msg);
- return 2;
- }
-
- }
-
- /**
- * 得到行业列表
- */
- public function get_industry()
- {
- try{
- v('get_industry_start');
- $industrys = $this->app->notice->getIndustry();
- v('industrys');v($industrys);
- return $industrys;
- }
- // 加上\ 全局抓取
- catch(\Exception $e){
- $error_msg = $e->getMessage();
- v('get_industry_ept:appid:'.$this->param['gzh_app_id'].' ept:'.$error_msg);
- if(strpos($error_msg,'api forbidden') !== false){
- return 4;
- }
- return '';
- }
- }
-
- /**
- * 得到已添加的模板消息列表
- */
- public function get_private_templates()
- {
- try{
- v('get_private_templates_start');
- $private_templates = $this->app->notice->getPrivateTemplates();
- v('private_templates');v($private_templates);
- return $private_templates;
- }
- // 加上\ 全局抓取
- catch(\Exception $e){
- v('get_private_templates_ept:'.$e->getMessage());
- }
- }
-
- /**
- * 删除指定模板消息
- */
- public function delete_private_template($templateId)
- {
- v('delete_private_template:'.$templateId);
- $del_res = $this->app->notice->deletePrivateTemplate($templateId);
- v('del_res');v($del_res);
- }
-
-
- /**
- * 注意:不同公众号,相同模板消息的template_id不一样
- * 设置模板消息id
- * TM00202 帐户资金变动提醒
- TM00155 会员充值通知
- OPENTM217772013 任务到期通知
- TM405959659 会员卡升级通知
- */
- public function set_template_notices()
- {
- v('set_template_notice_start');
- $templates = $this->WechatApi->get_wechat_public_templates($this->param['gzh_app_id']);
- v('$templates');v($templates);
- if($templates && is_array($templates) && count($templates) > 0)
- {
- foreach($templates as $template){
- $this->add_one_tamplate($template['common_template_id']);
- }
- }
- v('set_template_notice_end');
- }
-
- function add_one_tamplate($common_template_id){
- if(!empty($common_template_id)){
- // 添加模板并获取模板ID
- try{
- // 同一个公众号可以添加多次模板消息,所以拿common作为标准
- // 数据库已存在不要再添加 FIXME:如果是号主自己后台删除了模板消息就有bug,需要从已有的模板消息做检测
- $origin_wechat_template_official_info = $this->WechatApi->get_template_official_info($this->param['gzh_app_id'], $common_template_id);
- v('origin_wechat_template_official_info');v($origin_wechat_template_official_info);
- if(empty($origin_wechat_template_official_info)){
- $add_res = $this->app->notice->addTemplate($common_template_id);
- v('add_res_'.$common_template_id.':');v($add_res);
- if($add_res->errcode==0 && $add_res->errmsg=='ok'){
- $insert_data = array();
- $insert_data['appid'] = $this->param['gzh_app_id'];
- $insert_data['template_id'] = $add_res->template_id;
- $insert_data['common_template_id'] = $common_template_id;
- $this->WechatApi->save_template_official_info($insert_data);
- }
- }
- }
- // 加上\ 全局抓取
- catch(\Exception $e){
- v('add_template_ept:'.$e->getMessage());
- }
- }
- }
-
- /**
- * 深度检测模板消息
- * @param unknown_type $common_template_id
- */
- function check_one_tamplate($common_template_id,$wx_template_ids){
- if(!empty($common_template_id)){
- // 添加模板并获取模板ID
- try{
- // 同一个公众号可以添加多次模板消息,所以拿common作为标准
- $origin_wechat_template_official_info = $this->WechatApi->get_template_official_info($this->param['gzh_app_id'], $common_template_id);
- v('origin_wechat_template_official_info');v($origin_wechat_template_official_info);
- if(empty($origin_wechat_template_official_info)){
- $add_res = $this->app->notice->addTemplate($common_template_id);
- v('add_res_'.$common_template_id.':'.$this->param['gzh_app_id']);v($add_res);
- if($add_res->errcode==0 && $add_res->errmsg=='ok'){
- $insert_data = array();
- $insert_data['appid'] = $this->param['gzh_app_id'];
- $insert_data['template_id'] = $add_res->template_id;
- $insert_data['common_template_id'] = $common_template_id;
- $this->WechatApi->save_template_official_info($insert_data);
- return true;
- }
- }
- // 有的话,检测下微信后台存不存在
- else{
- if(!in_array($origin_wechat_template_official_info['template_id'],$wx_template_ids)){
- v('template_not_exist_wx_backend:'.$common_template_id.' appid:'.$this->param['gzh_app_id']);
- // 不存在则执行更新操作,先添加再更新
- $add_res = $this->app->notice->addTemplate($common_template_id);
- v('update_add_res_'.$common_template_id.':appid:'.$this->param['gzh_app_id']);v($add_res);
- if($add_res->errcode==0 && $add_res->errmsg=='ok'){
- $update_data = array();
- $update_data['appid'] = $this->param['gzh_app_id'];
- $update_data['template_id'] = $add_res->template_id;
- $update_data['common_template_id'] = $common_template_id;
- $this->WechatApi->update_template_official_info($update_data);
- }
- }else{
- v('template_exist_wx_backend:'.$common_template_id.' origin_template_id:'.$origin_wechat_template_official_info['template_id'].' appid:'.$this->param['gzh_app_id']);
- }
- return true;
- }
- }
- // 加上\ 全局抓取
- catch(\Exception $e){
- v('check_template_ept:appid:'.$this->param['gzh_app_id'].' ept:'.$e->getMessage());
- return false;
- }
- }
-
- return true;
- }
-
- /**
- * 深度检测模板消息-返回结果
- * @param unknown_type $common_template_id
- */
- function check_one_tamplate_res($common_template_id,$wx_template_ids){
- // 添加模板并获取模板ID
- try{
- // 同一个公众号可以添加多次模板消息,所以拿common作为标准
- $origin_wechat_template_official_info = $this->WechatApi->get_template_official_info($this->param['gzh_app_id'], $common_template_id);
- v('origin_wechat_template_official_info');v($origin_wechat_template_official_info);
- if(empty($origin_wechat_template_official_info)){
- $add_res = $this->app->notice->addTemplate($common_template_id);
- v('check_one_tamplate_res_add_res_'.$common_template_id.':'.$this->param['gzh_app_id']);v($add_res);
- if($add_res->errcode==0 && $add_res->errmsg=='ok'){
- $insert_data = array();
- $insert_data['appid'] = $this->param['gzh_app_id'];
- $insert_data['template_id'] = $add_res->template_id;
- $insert_data['common_template_id'] = $common_template_id;
- $this->WechatApi->save_template_official_info($insert_data);
- }
- return 1;
- }
- // 有的话,检测下微信后台存不存在
- else{
- if(!in_array($origin_wechat_template_official_info['template_id'],$wx_template_ids)){
- v('template_not_exist_wx_backend:'.$common_template_id.' appid:'.$this->param['gzh_app_id']);
- // 不存在则执行更新操作,先添加再更新
- $add_res = $this->app->notice->addTemplate($common_template_id);
- v('update_add_res_'.$common_template_id.':appid:'.$this->param['gzh_app_id']);v($add_res);
- if($add_res->errcode==0 && $add_res->errmsg=='ok'){
- $update_data = array();
- $update_data['appid'] = $this->param['gzh_app_id'];
- $update_data['template_id'] = $add_res->template_id;
- $update_data['common_template_id'] = $common_template_id;
- $this->WechatApi->update_template_official_info($update_data);
- }
- return 1;
- }else{
- v('template_exist_wx_backend:'.$common_template_id.' origin_template_id:'.$origin_wechat_template_official_info['template_id'].' appid:'.$this->param['gzh_app_id']);
- return 2;
- }
- }
- }
- // 加上\ 全局抓取
- catch(\Exception $e){
- $error_msg = $e->getMessage();
- v('check_template_ept:appid:'.$this->param['gzh_app_id'].' ept:'.$error_msg);
- if(strpos($error_msg,'api forbidden') !== false){
- return 4;
- }elseif(strpos($error_msg,'template num exceeds limit') !== false){
- return 5;
- }elseif(strpos($error_msg,'invalid template_id') !== false){
- return 6;
- }elseif(strpos($error_msg,'disabled template') !== false){
- return 7;
- }elseif(strpos($error_msg,'user limited') !== false){
- return 8;
- }elseif(strpos($error_msg,'api unauthorized') !== false){
- return 9;
- }elseif(strpos($error_msg,'invalid industry') !== false){
- return 10;
- }elseif(strpos($error_msg,'for punishment') !== false){
- return 11;
- }
-
- else{
- return 3;
- }
- }
- }
-
- }
|