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;
- class TemplateBasesController
- {
-
- public function __construct($_param)
- {
- $this->param = $_param;
- $this->app = $_param['app'];
- $this->WechatApi = $_param['WechatApi'];
- }
-
-
- public function auto_set_dustry_templates()
- {
- v('auto_set_dustry_templates_before');
-
-
- 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');
- }
-
-
- 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);
- }
-
-
-
- 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)){
-
- try{
-
-
- $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());
- }
- }
- }
-
-
- function check_one_tamplate($common_template_id,$wx_template_ids){
- if(!empty($common_template_id)){
-
- try{
-
- $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;
- }
-
-
- function check_one_tamplate_res($common_template_id,$wx_template_ids){
-
- try{
-
- $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;
- }
- }
- }
-
- }
|