TemplateBasesController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. namespace App\Http\Controllers\Wechat\Template;
  3. use App\Http\Requests;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\Response;
  6. use EasyWeChat\Foundation\Application;
  7. use App\Http\Models\WechatTemplateBaseInfo;
  8. use App\Http\Models\WechatTemplateOfficialInfo;
  9. /**
  10. * 模板消息基础表,用于自动创建公众号模板行业和模板消息
  11. * @author zhoulingjie
  12. *
  13. */
  14. class TemplateBasesController
  15. {
  16. public function __construct($_param)
  17. {
  18. // v('$this->param');v($_param);
  19. $this->param = $_param;
  20. $this->app = $_param['app'];
  21. $this->WechatApi = $_param['WechatApi'];
  22. }
  23. /**
  24. * 自动开通模板行业、添加对应模板消息
  25. * @param Request $request
  26. */
  27. public function auto_set_dustry_templates()
  28. {
  29. v('auto_set_dustry_templates_before');
  30. // $this->get_industry();
  31. // $this->get_private_templates();
  32. // TODO 从group取,设置行业
  33. v('industry_first_id:'.$this->param['group']['industry_first_id'].' industry_second_id:'.$this->param['group']['industry_second_id']);
  34. $this->set_template_dustry($this->param['group']['industry_first_id'],$this->param['group']['industry_second_id']);
  35. // 设置模板消息
  36. $this->set_template_notices();
  37. v('auto_set_dustry_templates_after');
  38. }
  39. /**
  40. * 设置行业(传id) 默认 2,37
  41. *
  42. IT科技 互联网/电子商务 1
  43. IT科技 IT软件与服务 2
  44. IT科技 IT硬件与设备 3
  45. IT科技 电子技术 4
  46. IT科技 通信与运营商 5
  47. IT科技 网络游戏 6
  48. 金融业 银行 7
  49. 金融业 基金|理财|信托 8
  50. 金融业 保险 9
  51. 餐饮 餐饮 10
  52. 酒店旅游 酒店 11
  53. 酒店旅游 旅游 12
  54. 运输与仓储 快递 13
  55. 运输与仓储 物流 14
  56. 运输与仓储 仓储 15
  57. 教育 培训 16
  58. 教育 院校 17
  59. 政府与公共事业 学术科研 18
  60. 政府与公共事业 交警 19
  61. 政府与公共事业 博物馆 20
  62. 政府与公共事业 公共事业|非盈利机构 21
  63. 医药护理 医药医疗 22
  64. 医药护理 护理美容 23
  65. 医药护理 保健与卫生 24
  66. 交通工具 汽车相关 25
  67. 交通工具 摩托车相关 26
  68. 交通工具 火车相关 27
  69. 交通工具 飞机相关 28
  70. 房地产 建筑 29
  71. 房地产 物业 30
  72. 消费品 消费品 31
  73. 商业服务 法律 32
  74. 商业服务 会展 33
  75. 商业服务 中介服务 34
  76. 商业服务 认证 35
  77. 商业服务 审计 36
  78. 文体娱乐 传媒 37
  79. 文体娱乐 体育 38
  80. 文体娱乐 娱乐休闲 39
  81. 印刷 印刷 40
  82. 其它 其它 41
  83. */
  84. public function set_template_dustry($industryId1=2,$industryId2=37)
  85. {
  86. if(empty($industryId1)) $industryId1 = 2;
  87. if(empty($industryId2)) $industryId2 = 37;
  88. v('set_template_dustry_start,industryId1:'.$industryId1.' industryId2:'.$industryId2);
  89. //修改账号所属行业
  90. try{
  91. $set_dustry_res = $this->app->notice->setIndustry($industryId1, $industryId2);
  92. v('set_dustry_res');v($set_dustry_res);
  93. }
  94. // 加上\ 全局抓取
  95. catch(\Exception $e){
  96. v('set_template_dustry_ept:'.$e->getMessage());
  97. }
  98. v('set_template_dustry_end');
  99. }
  100. public function set_template_dustry_res($industryId1=2,$industryId2=37)
  101. {
  102. if(empty($industryId1)) $industryId1 = 2;
  103. if(empty($industryId2)) $industryId2 = 37;
  104. v('set_template_dustry_res_start,industryId1:'.$industryId1.' industryId2:'.$industryId2);
  105. //修改账号所属行业
  106. try{
  107. $set_dustry_res = $this->app->notice->setIndustry($industryId1, $industryId2);
  108. v('set_dustry_res');v($set_dustry_res);
  109. return 1;
  110. }
  111. // 加上\ 全局抓取
  112. catch(\Exception $e){
  113. $error_msg = $e->getMessage();
  114. v('set_template_dustry_res_ept:'.$error_msg);
  115. return 2;
  116. }
  117. }
  118. /**
  119. * 得到行业列表
  120. */
  121. public function get_industry()
  122. {
  123. try{
  124. v('get_industry_start');
  125. $industrys = $this->app->notice->getIndustry();
  126. v('industrys');v($industrys);
  127. return $industrys;
  128. }
  129. // 加上\ 全局抓取
  130. catch(\Exception $e){
  131. $error_msg = $e->getMessage();
  132. v('get_industry_ept:appid:'.$this->param['gzh_app_id'].' ept:'.$error_msg);
  133. if(strpos($error_msg,'api forbidden') !== false){
  134. return 4;
  135. }
  136. return '';
  137. }
  138. }
  139. /**
  140. * 得到已添加的模板消息列表
  141. */
  142. public function get_private_templates()
  143. {
  144. try{
  145. v('get_private_templates_start');
  146. $private_templates = $this->app->notice->getPrivateTemplates();
  147. v('private_templates');v($private_templates);
  148. return $private_templates;
  149. }
  150. // 加上\ 全局抓取
  151. catch(\Exception $e){
  152. v('get_private_templates_ept:'.$e->getMessage());
  153. }
  154. }
  155. /**
  156. * 删除指定模板消息
  157. */
  158. public function delete_private_template($templateId)
  159. {
  160. v('delete_private_template:'.$templateId);
  161. $del_res = $this->app->notice->deletePrivateTemplate($templateId);
  162. v('del_res');v($del_res);
  163. }
  164. /**
  165. * 注意:不同公众号,相同模板消息的template_id不一样
  166. * 设置模板消息id
  167. * TM00202 帐户资金变动提醒
  168. TM00155 会员充值通知
  169. OPENTM217772013 任务到期通知
  170. TM405959659 会员卡升级通知
  171. */
  172. public function set_template_notices()
  173. {
  174. v('set_template_notice_start');
  175. $templates = $this->WechatApi->get_wechat_public_templates($this->param['gzh_app_id']);
  176. v('$templates');v($templates);
  177. if($templates && is_array($templates) && count($templates) > 0)
  178. {
  179. foreach($templates as $template){
  180. $this->add_one_tamplate($template['common_template_id']);
  181. }
  182. }
  183. v('set_template_notice_end');
  184. }
  185. function add_one_tamplate($common_template_id){
  186. if(!empty($common_template_id)){
  187. // 添加模板并获取模板ID
  188. try{
  189. // 同一个公众号可以添加多次模板消息,所以拿common作为标准
  190. // 数据库已存在不要再添加 FIXME:如果是号主自己后台删除了模板消息就有bug,需要从已有的模板消息做检测
  191. $origin_wechat_template_official_info = $this->WechatApi->get_template_official_info($this->param['gzh_app_id'], $common_template_id);
  192. v('origin_wechat_template_official_info');v($origin_wechat_template_official_info);
  193. if(empty($origin_wechat_template_official_info)){
  194. $add_res = $this->app->notice->addTemplate($common_template_id);
  195. v('add_res_'.$common_template_id.':');v($add_res);
  196. if($add_res->errcode==0 && $add_res->errmsg=='ok'){
  197. $insert_data = array();
  198. $insert_data['appid'] = $this->param['gzh_app_id'];
  199. $insert_data['template_id'] = $add_res->template_id;
  200. $insert_data['common_template_id'] = $common_template_id;
  201. $this->WechatApi->save_template_official_info($insert_data);
  202. }
  203. }
  204. }
  205. // 加上\ 全局抓取
  206. catch(\Exception $e){
  207. v('add_template_ept:'.$e->getMessage());
  208. }
  209. }
  210. }
  211. /**
  212. * 深度检测模板消息
  213. * @param unknown_type $common_template_id
  214. */
  215. function check_one_tamplate($common_template_id,$wx_template_ids){
  216. if(!empty($common_template_id)){
  217. // 添加模板并获取模板ID
  218. try{
  219. // 同一个公众号可以添加多次模板消息,所以拿common作为标准
  220. $origin_wechat_template_official_info = $this->WechatApi->get_template_official_info($this->param['gzh_app_id'], $common_template_id);
  221. v('origin_wechat_template_official_info');v($origin_wechat_template_official_info);
  222. if(empty($origin_wechat_template_official_info)){
  223. $add_res = $this->app->notice->addTemplate($common_template_id);
  224. v('add_res_'.$common_template_id.':'.$this->param['gzh_app_id']);v($add_res);
  225. if($add_res->errcode==0 && $add_res->errmsg=='ok'){
  226. $insert_data = array();
  227. $insert_data['appid'] = $this->param['gzh_app_id'];
  228. $insert_data['template_id'] = $add_res->template_id;
  229. $insert_data['common_template_id'] = $common_template_id;
  230. $this->WechatApi->save_template_official_info($insert_data);
  231. return true;
  232. }
  233. }
  234. // 有的话,检测下微信后台存不存在
  235. else{
  236. if(!in_array($origin_wechat_template_official_info['template_id'],$wx_template_ids)){
  237. v('template_not_exist_wx_backend:'.$common_template_id.' appid:'.$this->param['gzh_app_id']);
  238. // 不存在则执行更新操作,先添加再更新
  239. $add_res = $this->app->notice->addTemplate($common_template_id);
  240. v('update_add_res_'.$common_template_id.':appid:'.$this->param['gzh_app_id']);v($add_res);
  241. if($add_res->errcode==0 && $add_res->errmsg=='ok'){
  242. $update_data = array();
  243. $update_data['appid'] = $this->param['gzh_app_id'];
  244. $update_data['template_id'] = $add_res->template_id;
  245. $update_data['common_template_id'] = $common_template_id;
  246. $this->WechatApi->update_template_official_info($update_data);
  247. }
  248. }else{
  249. v('template_exist_wx_backend:'.$common_template_id.' origin_template_id:'.$origin_wechat_template_official_info['template_id'].' appid:'.$this->param['gzh_app_id']);
  250. }
  251. return true;
  252. }
  253. }
  254. // 加上\ 全局抓取
  255. catch(\Exception $e){
  256. v('check_template_ept:appid:'.$this->param['gzh_app_id'].' ept:'.$e->getMessage());
  257. return false;
  258. }
  259. }
  260. return true;
  261. }
  262. /**
  263. * 深度检测模板消息-返回结果
  264. * @param unknown_type $common_template_id
  265. */
  266. function check_one_tamplate_res($common_template_id,$wx_template_ids){
  267. // 添加模板并获取模板ID
  268. try{
  269. // 同一个公众号可以添加多次模板消息,所以拿common作为标准
  270. $origin_wechat_template_official_info = $this->WechatApi->get_template_official_info($this->param['gzh_app_id'], $common_template_id);
  271. v('origin_wechat_template_official_info');v($origin_wechat_template_official_info);
  272. if(empty($origin_wechat_template_official_info)){
  273. $add_res = $this->app->notice->addTemplate($common_template_id);
  274. v('check_one_tamplate_res_add_res_'.$common_template_id.':'.$this->param['gzh_app_id']);v($add_res);
  275. if($add_res->errcode==0 && $add_res->errmsg=='ok'){
  276. $insert_data = array();
  277. $insert_data['appid'] = $this->param['gzh_app_id'];
  278. $insert_data['template_id'] = $add_res->template_id;
  279. $insert_data['common_template_id'] = $common_template_id;
  280. $this->WechatApi->save_template_official_info($insert_data);
  281. }
  282. return 1;
  283. }
  284. // 有的话,检测下微信后台存不存在
  285. else{
  286. if(!in_array($origin_wechat_template_official_info['template_id'],$wx_template_ids)){
  287. v('template_not_exist_wx_backend:'.$common_template_id.' appid:'.$this->param['gzh_app_id']);
  288. // 不存在则执行更新操作,先添加再更新
  289. $add_res = $this->app->notice->addTemplate($common_template_id);
  290. v('update_add_res_'.$common_template_id.':appid:'.$this->param['gzh_app_id']);v($add_res);
  291. if($add_res->errcode==0 && $add_res->errmsg=='ok'){
  292. $update_data = array();
  293. $update_data['appid'] = $this->param['gzh_app_id'];
  294. $update_data['template_id'] = $add_res->template_id;
  295. $update_data['common_template_id'] = $common_template_id;
  296. $this->WechatApi->update_template_official_info($update_data);
  297. }
  298. return 1;
  299. }else{
  300. v('template_exist_wx_backend:'.$common_template_id.' origin_template_id:'.$origin_wechat_template_official_info['template_id'].' appid:'.$this->param['gzh_app_id']);
  301. return 2;
  302. }
  303. }
  304. }
  305. // 加上\ 全局抓取
  306. catch(\Exception $e){
  307. $error_msg = $e->getMessage();
  308. v('check_template_ept:appid:'.$this->param['gzh_app_id'].' ept:'.$error_msg);
  309. if(strpos($error_msg,'api forbidden') !== false){
  310. return 4;
  311. }elseif(strpos($error_msg,'template num exceeds limit') !== false){
  312. return 5;
  313. }elseif(strpos($error_msg,'invalid template_id') !== false){
  314. return 6;
  315. }elseif(strpos($error_msg,'disabled template') !== false){
  316. return 7;
  317. }elseif(strpos($error_msg,'user limited') !== false){
  318. return 8;
  319. }elseif(strpos($error_msg,'api unauthorized') !== false){
  320. return 9;
  321. }elseif(strpos($error_msg,'invalid industry') !== false){
  322. return 10;
  323. }elseif(strpos($error_msg,'for punishment') !== false){
  324. return 11;
  325. }
  326. else{
  327. return 3;
  328. }
  329. }
  330. }
  331. }