WechatOuterApisController.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. <?php
  2. namespace App\Http\Controllers\Wechat\Api;
  3. use App\Http\Controllers\WechatController;
  4. use App\Http\Controllers\Wechat\Qrcode\QrcodesController;
  5. use App\Http\Controllers\Wechat\Material\MaterialsController;
  6. use App\Http\Controllers\Wechat\Template\TemplateBasesController;
  7. use App\Http\Requests;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Http\Response;
  10. use EasyWeChat\Foundation\Application;
  11. use App\Http\Controllers\Wechat\Staff\StaffsController;
  12. /**
  13. * 对外相关api接口,和微信token有关
  14. * @author zhoulingjie
  15. *
  16. */
  17. class WechatOuterApisController extends WechatController
  18. {
  19. public function __construct(Request $request)
  20. {
  21. // TODO 加密,检测gzh_app_id合法性
  22. $this->gzh_app_id = $request->get('gzh_app_id');
  23. parent::__construct($this->gzh_app_id);
  24. // 方便扩展
  25. $param = array();
  26. $param['app'] = $this->app;
  27. $param['WechatApi'] = isset($this->WechatApi) ? $this->WechatApi : null;
  28. $param['gzh_app_id'] = $this->gzh_app_id;
  29. $this->Qrcode = new QrcodesController($param);
  30. $this->Material = new MaterialsController($param);
  31. $this->Template = new TemplateBasesController($param);
  32. $this->Staff = new StaffsController($this->param);
  33. $this->User = $this->app->user;
  34. }
  35. /**
  36. * http://zydy/api/get_qrcode_url?gzh_app_id=wx03d107cd1ee728c2&scene_id=999&timestamp=1511503861&sign=74b21f32c094f8d343c9743c196f6a32
  37. * 获取带参二维码
  38. */
  39. public function get_qrcode_url(Request $request)
  40. {
  41. $result = array('code' => 1, 'msg' => '', 'data' => '');
  42. $sceneId = $request->get('scene_id');
  43. $gzh_app_id = $request->get('gzh_app_id');
  44. v($request->all());
  45. if (empty($sceneId) || empty($gzh_app_id)) {
  46. $result['code'] = 0;
  47. $result['msg'] = 'invalid param';
  48. json_echo($result);
  49. }
  50. $check_result = $this->check_sign_params($request);
  51. if ($check_result['code'] == 0) {
  52. $result['code'] = 0;
  53. $result['msg'] = $check_result['msg'];
  54. json_echo($result);
  55. }
  56. $qrcode_url = $this->Qrcode->create_qrcode('temporary', $sceneId);
  57. // v($chapters);
  58. if (!empty($qrcode_url)) {
  59. $result['data'] = $qrcode_url;
  60. } else {
  61. $result['code'] = 0;
  62. $result['msg'] = 'create qrcode url fail';
  63. }
  64. json_echo($result);
  65. }
  66. /**
  67. * http://zydy/api/upload_gzh_img?gzh_app_id=wxdbc486f1b4f6a8c3&img_path=http%3A%2F%2Fauth.aizhuishu.com%2Fadmin%2Fimgs%2Fcommon%2Fcontact%2Fcontact_customer.png&timestamp=1511503861&sign=74b21f32c094f8d343c9743c196f6a32
  68. * http://zydy/api/upload_gzh_img?gzh_app_id=wxdbc486f1b4f6a8c3&group_nick=zhuishuyun&timestamp=1513600581&sign=38693a228ea65fc894c15bc6d76e7287
  69. * http://zydy/api/upload_gzh_img?gzh_app_id=wxdbc486f1b4f6a8c3&is_default=0&img_url=https%3A%2F%2Fimages.csdn.net%2F20171218%2FVCG41103923818.jpg&group_nick=zhuishuyun&timestamp=1513600581&sign=38693a228ea65fc894c15bc6d76e7287
  70. * 上传公众号图片
  71. */
  72. public function upload_gzh_img(Request $request)
  73. {
  74. $result = array('code' => 1, 'msg' => '', 'data' => array('media_id' => '', 'url' => ''));
  75. $group_nick = $request->get('group_nick');
  76. $gzh_app_id = $request->get('gzh_app_id');
  77. $is_default = $request->has('is_default') ? $request->get('is_default') : 1;
  78. $img_url = $request->has('img_url') ? $request->get('img_url') : '';
  79. v($request->all());
  80. if (empty($group_nick) || empty($gzh_app_id)) {
  81. $result['code'] = 0;
  82. $result['msg'] = 'upload_gzh_img invalid param';
  83. json_echo($result);
  84. }
  85. v('upload_gzh_img:' . $gzh_app_id);
  86. $check_result = $this->check_sign_params($request);
  87. if ($check_result['code'] == 0) {
  88. $result['code'] = 0;
  89. $result['msg'] = $check_result['msg'];
  90. json_echo($result);
  91. }
  92. if (!$is_default) {
  93. $img_url = DownImage($img_url, public_path('admin/imgs/contact_customer/contact_customer_' . $gzh_app_id . '.png'));
  94. } else {
  95. $img_url = public_path('admin/imgs/common/contact/contact_customer_' . $group_nick . '.png');
  96. }
  97. $upload_result = $this->Material->upload_contact_cusomer_img($group_nick, $img_url);
  98. v('$upload_result');
  99. v($upload_result);
  100. if (!empty($upload_result)) {
  101. $result['data']['media_id'] = $upload_result->media_id;
  102. $result['data']['url'] = $upload_result->url;
  103. } else {
  104. $result['code'] = 0;
  105. $result['msg'] = 'upload img fail';
  106. }
  107. v('gzh_appid:' . $gzh_app_id . ' result:' . json_encode($result));
  108. json_echo($result);
  109. }
  110. /**
  111. * http://zydy/api/upload_material_img?gzh_app_id=wxdbc486f1b4f6a8c3&img_url=https%3A%2F%2Fimages.csdn.net%2F20171218%2FVCG41103923818.jpg&group_nick=zhuishuyun&timestamp=1514453795&sign=35d5a601b9d45d57b648f8574b946274
  112. * 上传公众号素材图片
  113. */
  114. public function upload_material_img(Request $request)
  115. {
  116. $result = array('code' => 1, 'msg' => '', 'data' => array('media_id' => '', 'url' => ''));
  117. $group_nick = $request->get('group_nick');
  118. $gzh_app_id = $request->get('gzh_app_id');
  119. $image_type = $request->has('image_type') ? $request->get('image_type') : 'common';
  120. $img_url = $request->has('img_url') ? $request->get('img_url') : '';
  121. $real_img_url = urldecode($img_url);
  122. v($request->all());
  123. if (empty($group_nick) || empty($gzh_app_id)) {
  124. $result['code'] = 0;
  125. $result['msg'] = 'upload_gzh_img invalid param';
  126. json_echo($result);
  127. }
  128. v('upload_gzh_img:' . $gzh_app_id);
  129. $check_result = $this->check_sign_params($request);
  130. if ($check_result['code'] == 0) {
  131. $result['code'] = 0;
  132. $result['msg'] = $check_result['msg'];
  133. json_echo($result);
  134. }
  135. $path = public_path('admin/imgs/material/temp_' . $gzh_app_id . '_' . md5($img_url) . '.png');
  136. v('DownImage:real_img_url:' . $real_img_url . ' path:' . $path);
  137. DownImage($real_img_url, $path);
  138. if ($image_type == 'common') {
  139. $upload_result = $this->Material->upload_material_img($group_nick, $path);
  140. } else {
  141. $upload_result = $this->Material->upload_thumb_material_img($group_nick, $path);
  142. }
  143. if (!empty($upload_result)) {
  144. $result['data']['media_id'] = $upload_result->media_id;
  145. $result['data']['url'] = $upload_result->url;
  146. } else {
  147. $result['code'] = 0;
  148. $result['msg'] = $this->Material->getErrorMsg('upload_materials', 'upload img fail');
  149. }
  150. json_echo($result);
  151. }
  152. /**
  153. * http://zydy/api/upload_gzh_article?show_cover_pic=0&author=%E8%B6%85%E5%93%A5&digest=%E5%8F%AA%E6%98%AF%E4%B8%AA%E6%91%98%E8%A6%81&content=%E5%8F%AA%E6%98%AF%E4%B8%AA%E5%86%85%E5%AE%B9%E8%80%8C%E5%B7%B2&title=testtest&thumb_media_id=XYhErlq3w-hlmwqOB4O5SlIHHHRE3jetv2k1y1iwemE&gzh_app_id=wxdbc486f1b4f6a8c3&group_nick=zhuishuyun&timestamp=1514454260&sign=9d708b61806fe5dacb7f632803463da1
  154. * 上传公众号图文素材
  155. */
  156. public function upload_gzh_article(Request $request)
  157. {
  158. $result = array('code' => 1, 'msg' => '', 'data' => array('chapter_media_id' => '', 'chapter_url' => ''));
  159. $group_nick = $request->get('group_nick');
  160. $gzh_app_id = $request->get('gzh_app_id');
  161. $title = $request->has('title') ? $request->get('title') : '';
  162. $thumb_media_id = $request->has('thumb_media_id') ? $request->get('thumb_media_id') : '';
  163. $show_cover_pic = $request->has('show_cover_pic') ? $request->get('show_cover_pic') : '';
  164. $author = $request->has('author') ? $request->get('author') : '';
  165. $digest = $request->has('digest') ? $request->get('digest') : '';
  166. $content = $request->has('content') ? $request->get('content') : '';
  167. $content_source_url = $request->has('content_source_url') ? $request->get('content_source_url') : '';
  168. v($request->all());
  169. if (empty($group_nick) || empty($gzh_app_id) || empty($thumb_media_id) || empty($content)) {
  170. $result['code'] = 0;
  171. $result['msg'] = 'upload_gzh_article invalid param';
  172. json_echo($result);
  173. }
  174. v('upload_gzh_img:' . $gzh_app_id);
  175. $check_result = $this->check_sign_params($request);
  176. if ($check_result['code'] == 0) {
  177. $result['code'] = 0;
  178. $result['msg'] = $check_result['msg'];
  179. json_echo($result);
  180. }
  181. $article = array();
  182. $article['thumb_media_id'] = $thumb_media_id;//'XYhErlq3w-hlmwqOB4O5SqFAgM6yWcg0u9ttENsjSIg';
  183. $article['title'] = $title;
  184. $article['show_cover_pic'] = $show_cover_pic;
  185. $article['author'] = $author;
  186. $article['digest'] = $digest;
  187. $article['content'] = $content;
  188. $article['content_source_url'] = $content_source_url;
  189. $upload_result = $this->Material->upload_article($article);
  190. if (!empty($upload_result)) {
  191. $chapter_resource = $this->Material->get_material_resource($upload_result->media_id);
  192. if (!empty($chapter_resource)) {
  193. $result['data']['chapter_url'] = isset($chapter_resource['news_item'][0]['url']) ? $chapter_resource['news_item'][0]['url'] : '';
  194. $result['data']['chapter_media_id'] = isset($chapter_resource['news_item'][0]['thumb_media_id']) ? $chapter_resource['news_item'][0]['thumb_media_id'] : '';
  195. } else {
  196. $result['code'] = 0;
  197. $result['msg'] = 'get_material_resource fail';
  198. }
  199. } else {
  200. $result['code'] = 0;
  201. $result['msg'] = 'upload img fail';
  202. }
  203. v($result);
  204. json_echo($result);
  205. }
  206. /**
  207. * http://zydy/api/upload_gzh_articles?show_cover_pic=0&author=%E8%B6%85%E5%93%A5&digest=%E5%8F%AA%E6%98%AF%E4%B8%AA%E6%91%98%E8%A6%81&content=%E5%8F%AA%E6%98%AF%E4%B8%AA%E5%86%85%E5%AE%B9%E8%80%8C%E5%B7%B2&title=testtest&thumb_media_id=XYhErlq3w-hlmwqOB4O5SlIHHHRE3jetv2k1y1iwemE&gzh_app_id=wxdbc486f1b4f6a8c3&group_nick=zhuishuyun&timestamp=1514454260&sign=9d708b61806fe5dacb7f632803463da1
  208. * 上传公众号图文素材
  209. */
  210. public function upload_gzh_articles(Request $request)
  211. {
  212. $result = array('code' => 1, 'msg' => '', 'data' => array('chapter_media_id' => '', 'chapter_url' => ''));
  213. $group_nick = $request->get('group_nick');
  214. $gzh_app_id = $request->get('gzh_app_id');
  215. $articles = $request->has('articles') ? $request->get('articles') : '';
  216. v($request->all());
  217. if (empty($group_nick) || empty($gzh_app_id) || empty($articles)) {
  218. $result['code'] = 0;
  219. $result['msg'] = 'upload_gzh_articles invalid param';
  220. json_echo($result);
  221. }
  222. v('upload_gzh_articles:' . $gzh_app_id);
  223. $check_result = $this->check_sign_params($request);
  224. if ($check_result['code'] == 0) {
  225. $result['code'] = 0;
  226. $result['msg'] = $check_result['msg'];
  227. json_echo($result);
  228. }
  229. // $articles = json_decode($articles);
  230. $upload_result = $this->Material->upload_articles($gzh_app_id, $articles);
  231. if (!empty($upload_result)) {
  232. $result['data'] = $upload_result;
  233. $chapter_resource = $this->Material->get_material_resource($upload_result->media_id);
  234. } else {
  235. $result['code'] = 0;
  236. $result['msg'] = $this->Material->getErrorMsg('upload_articles', 'upload articles fail');
  237. }
  238. v($result);
  239. json_echo($result);
  240. }
  241. /**
  242. * 维维读书 wx03d107cd1ee728c2 17
  243. * 纳兰阅读 wx392b782e2a082eb2 16
  244. * 已读攻毒 wx62c7e9290d51eeba 23
  245. * http://zydy/oauth/add_public_template?gzh_app_id=wxd52de911dfef6d92&common_template_id=&timestamp=1511509543&sign=98dccc7b29e3c05e4b4367f488ff1966
  246. * http://zsyauth.aizhuishu.com/oauth/auto_set_menu_and_template_test?authorizer_appid=wxdb365b8f2fc8aeb0&timestamp=1511509543&sign=98dccc7b29e3c05e4b4367f488ff1966
  247. */
  248. function add_public_template(Request $request)
  249. {
  250. $result = array('code' => 1, 'msg' => '', 'data' => '');
  251. $gzh_app_id = $request->get('gzh_app_id');
  252. $common_template_id = $request->get('common_template_id');
  253. v('add_public_template_start,gzh_appid:' . $gzh_app_id . ' common_template_id:' . $common_template_id);
  254. if (empty($gzh_app_id) || empty($common_template_id)) {
  255. $result['code'] = 0;
  256. $result['msg'] = 'invalid param';
  257. json_echo($result);
  258. }
  259. $check_result = $this->check_sign_params($request);
  260. if ($check_result['code'] == 0) {
  261. $result['code'] = 0;
  262. $result['msg'] = $check_result['msg'];
  263. json_echo($result);
  264. }
  265. $this->Template->add_one_tamplate($common_template_id);
  266. v('add_public_template_end,gzh_app_id:' . $gzh_app_id . ' common_template_id:' . $common_template_id);
  267. }
  268. /**
  269. * 嘉言小说 wxdbc486f1b4f6a8c3
  270. * http://zydy/api/get_full_official_account_users?gzh_app_id=wxdbc486f1b4f6a8c3&next_openid=timestamp=1511509543&sign=98dccc7b29e3c05e4b4367f488ff1966
  271. */
  272. function get_full_official_account_users(Request $request)
  273. {
  274. $result = array('code' => 1, 'msg' => '', 'data' => array('total' => '', 'count' => '', 'data' => '', 'next_openid' => ''));
  275. $gzh_app_id = $request->get('gzh_app_id');
  276. $next_openid = $request->get('next_openid');
  277. v('get_full_official_account_users_start,gzh_appid:' . $gzh_app_id . ' next_openid:' . $next_openid);
  278. if (empty($gzh_app_id)) {
  279. $result['code'] = 0;
  280. $result['msg'] = 'invalid param';
  281. json_echo($result);
  282. }
  283. $check_result = $this->check_sign_params($request);
  284. if ($check_result['code'] == 0) {
  285. $result['code'] = 0;
  286. $result['msg'] = $check_result['msg'];
  287. json_echo($result);
  288. }
  289. $users = $this->User->lists($next_openid);
  290. v($users);
  291. $result['data']['total'] = isset($users->total) ? $users->total : '';
  292. $result['data']['count'] = isset($users->count) ? $users->count : '';
  293. $result['data']['data'] = isset($users->data) ? $users->data : '';
  294. $result['data']['next_openid'] = isset($users->next_openid) ? $users->next_openid : '';
  295. v('get_full_official_account_users_end,gzh_app_id:' . $gzh_app_id . ' next_openid:' . $next_openid);
  296. json_echo($result);
  297. }
  298. /**
  299. * 维护公众号模板消息(被封,删除等异常情况)
  300. * http://zydy/api/check_official_account_templates?gzh_app_id=wxdbc486f1b4f6a8c3&timestamp=1523440738&sign=1d9640c40ef0bbb19b5352f60372c300
  301. */
  302. function check_official_account_templates(Request $request)
  303. {
  304. $result = array('code' => 1, 'msg' => '', 'data' => '');
  305. $gzh_app_id = $request->get('gzh_app_id');
  306. v('check_official_account_templates_start,gzh_appid:' . $gzh_app_id);
  307. if (empty($gzh_app_id)) {
  308. $result['code'] = 0;
  309. $result['msg'] = 'invalid param';
  310. json_echo($result);
  311. }
  312. $check_result = $this->check_sign_params($request);
  313. if ($check_result['code'] == 0) {
  314. $result['code'] = 0;
  315. $result['msg'] = $check_result['msg'];
  316. json_echo($result);
  317. }
  318. // 微信后台的模板
  319. $wx_backend_templates = $this->Template->get_private_templates();
  320. $wx_template_ids = array();
  321. if (isset($wx_backend_templates->template_list) && !empty($wx_backend_templates->template_list)) {
  322. foreach ($wx_backend_templates->template_list as $one_template) {
  323. $wx_template_ids[] = $one_template['template_id'];
  324. }
  325. }
  326. v('wx_template_ids:' . json_encode($wx_template_ids));
  327. // 我们目前的基础模板
  328. $base_templates = $this->WechatApi->get_wechat_public_templates($gzh_app_id);
  329. // v('base_templates');v($base_templates);
  330. if ($base_templates) {
  331. foreach ($base_templates as $template) {
  332. $this->Template->check_one_tamplate($template['common_template_id'], $wx_template_ids);
  333. }
  334. }
  335. v('maintain_official_account_templates_end,gzh_app_id:' . $gzh_app_id);
  336. }
  337. /**
  338. * 删除菜单
  339. * http://zydy/api/del_menu?gzh_app_id=wxdbc486f1b4f6a8c3&timestamp=1523615257&sign=056ea8390b3d5a1a6d2444288c1a0251
  340. */
  341. function del_menu(Request $request)
  342. {
  343. $result = array('code' => 1, 'msg' => '', 'data' => '');
  344. $gzh_app_id = $request->get('gzh_app_id');
  345. v('del_menu_start,gzh_appid:' . $gzh_app_id);
  346. if (empty($gzh_app_id)) {
  347. $result['code'] = 0;
  348. $result['msg'] = 'invalid param';
  349. json_echo($result);
  350. }
  351. $check_result = $this->check_sign_params($request);
  352. if ($check_result['code'] == 0) {
  353. $result['code'] = 0;
  354. $result['msg'] = $check_result['msg'];
  355. json_echo($result);
  356. }
  357. $menu = $this->app->menu;
  358. $current_menu = $menu->current();
  359. v('$current_menu');
  360. v($current_menu);
  361. $del_menu = $menu->destroy();
  362. v('$del_menu');
  363. v($del_menu);
  364. $current_menu = $menu->current();
  365. v('$after_current_menu');
  366. v('del_menu_end,gzh_app_id:' . $gzh_app_id);
  367. }
  368. /**
  369. * 得到用户信息
  370. * http://zydy/api/get_userinfo?gzh_app_id=wxdbc486f1b4f6a8c3&openid=oAcqg1LRHNKN2jaEkJ5v56HOwPEQ&timestamp=1524130007&sign=e7ceefdf35ae7eee15e00404fe89c66c
  371. */
  372. function get_userinfo(Request $request)
  373. {
  374. $result = array('code' => 1, 'msg' => '', 'data' => '');
  375. $gzh_app_id = $request->get('gzh_app_id');
  376. $openid = $request->get('openid');
  377. v('get_userinfo,gzh_appid:' . $gzh_app_id . ' openid:' . $openid);
  378. if (empty($gzh_app_id) || empty($openid)) {
  379. $result['code'] = 0;
  380. $result['msg'] = 'invalid param';
  381. json_echo($result);
  382. }
  383. $check_result = $this->check_sign_params($request);
  384. if ($check_result['code'] == 0) {
  385. $result['code'] = 0;
  386. $result['msg'] = $check_result['msg'];
  387. json_echo($result);
  388. }
  389. $user = $this->app->user;
  390. $userinfo = $user->get($openid);
  391. v('userinfo');
  392. v($userinfo);
  393. $result['data'] = $userinfo;
  394. json_echo($result);
  395. }
  396. /**
  397. * 得到用户信息
  398. * http://zydy/api/get_short_url?gzh_app_id=wxdbc486f1b4f6a8c3&url=www.baidu.com&timestamp=1524130007&sign=e7ceefdf35ae7eee15e00404fe89c66c
  399. */
  400. function get_short_url(Request $request)
  401. {
  402. $result = array('code' => 1, 'msg' => '', 'data' => '');
  403. $gzh_app_id = $request->get('gzh_app_id');
  404. $url = $request->get('url');
  405. $url = urldecode($url);
  406. v('get_short_url,gzh_appid:' . $gzh_app_id . ' $url:' . $url);
  407. if (empty($gzh_app_id) || empty($url)) {
  408. $result['code'] = 0;
  409. $result['msg'] = 'invalid param';
  410. json_echo($result);
  411. }
  412. $check_result = $this->check_sign_params($request);
  413. if ($check_result['code'] == 0) {
  414. $result['code'] = 0;
  415. $result['msg'] = $check_result['msg'];
  416. json_echo($result);
  417. }
  418. try {
  419. v('get_short_url_start:' . $gzh_app_id);
  420. $app_short_url = $this->app->url;
  421. $short_result = $app_short_url->shorten($url);
  422. v('short_result:' . $gzh_app_id);
  423. v($short_result);
  424. if (isset($short_result->errcode) && $short_result->errcode == '0'
  425. && isset($short_result->errmsg) && $short_result->errmsg == 'ok') {
  426. $result['data'] = $short_result->short_url;
  427. } else {
  428. $result['code'] = 0;
  429. $result['msg'] = $short_result->errmsg;
  430. }
  431. } catch (\Exception $e) {
  432. $error_msg = $e->getMessage();
  433. v('get_short_url_ept:' . $error_msg);
  434. if (strpos($error_msg, 'api forbidden') !== false) {
  435. $result['code'] = 0;
  436. $result['msg'] = '公众号微信接口被封,请等待解封!';
  437. } elseif (strpos($error_msg, 'user limited') !== false) {
  438. $result['code'] = 0;
  439. $result['msg'] = '公众号微信接口被限制,请等待解除!';
  440. }
  441. }
  442. json_echo($result);
  443. }
  444. /**
  445. * 检查公众号模板消息(被封,删除等异常情况)
  446. * http://zydy/api/check_template_status?gzh_app_id=wxdbc486f1b4f6a8c3&timestamp=1523440738&sign=1d9640c40ef0bbb19b5352f60372c300
  447. */
  448. function check_template_status(Request $request)
  449. {
  450. $result = array('code' => 1, 'msg' => '', 'data' => '');
  451. $gzh_app_id = $request->get('gzh_app_id');
  452. $common_template_id = $request->get('common_template_id');
  453. v('check_template_status_start,gzh_appid:' . $gzh_app_id . ' common_template_id:' . $common_template_id);
  454. if (empty($gzh_app_id) || empty($common_template_id)) {
  455. $result['code'] = 0;
  456. $result['msg'] = 'invalid param';
  457. json_echo($result);
  458. }
  459. $check_result = $this->check_sign_params($request);
  460. if ($check_result['code'] == 0) {
  461. $result['code'] = 0;
  462. $result['msg'] = $check_result['msg'];
  463. json_echo($result);
  464. }
  465. $error_msgs = [
  466. '1' => '模板修复成功',
  467. '2' => '模板正常',
  468. '3' => '微信模板修复失败!',
  469. '4' => '微信模板接口被封,请等待解封!',
  470. '5' => '微信后台模板数量达到上限,请删除部分模板后再操作',
  471. '6' => '微信模板被封,请等待解封!',
  472. '7' => '模板被微信官方取消失效了!',
  473. '8' => '号被限制了权限',
  474. '9' => '接口未授权',
  475. '10' => '分类不正确!',
  476. '11' => '号的模板功能被处罚限制中!',
  477. ];
  478. // 一、先检查分类
  479. $industry = $this->Template->get_industry();
  480. if ($industry == '4') {
  481. $result['msg'] = $error_msgs[$industry];
  482. json_echo($result);
  483. }
  484. $primary_industry_first_class = $industry->primary_industry['first_class'];
  485. $primary_industry_second_class = $industry->primary_industry['second_class'];
  486. $secondary_industry_first_class = $industry->secondary_industry['first_class'];
  487. $secondary_industry_second_class = $industry->secondary_industry['second_class'];
  488. v('$primary_industry_first_class:' . $primary_industry_first_class . ' $primary_industry_second_class:' . $primary_industry_second_class . ' $secondary_industry_first_class:' . $secondary_industry_first_class . ' $secondary_industry_second_class:' . $secondary_industry_second_class);
  489. $extra_error_msg = '';
  490. // 存在部分分类异常情况,所以分类错误允许往下走,还能抢救一部分模板消息
  491. if (!($primary_industry_first_class == 'IT科技' && $primary_industry_second_class == 'IT软件与服务'
  492. && $secondary_industry_first_class == '文体娱乐' && $secondary_industry_second_class == '文化|传媒')) {
  493. v('check_template_status_set_template_dustry:' . $gzh_app_id);
  494. $set_res = $this->Template->set_template_dustry_res(2, 37);
  495. if ($set_res == 2) {
  496. v('modify_dustry_ept:' . $gzh_app_id);
  497. // $result['msg'] = '模板分类错误,请修改分类至(IT科技->IT软件与服务、文体娱乐->文化|传媒),如不能修改请等待,分类1个月才能修改一次';
  498. // json_echo($result);
  499. $extra_error_msg = '请注意:模板分类错误,请修改分类至(IT科技->IT软件与服务、文体娱乐->文化|传媒),如不能修改请等待,分类1个月才能修改一次';
  500. } else {
  501. v('modify_dustry_ok:' . $gzh_app_id);
  502. }
  503. }
  504. // 二、分类正确,检查某一个模板,微信后台是否存在,我们系统后台是否存在
  505. // 微信后台的模板
  506. $wx_backend_templates = $this->Template->get_private_templates();
  507. $wx_template_ids = array();
  508. if (isset($wx_backend_templates->template_list) && !empty($wx_backend_templates->template_list)) {
  509. foreach ($wx_backend_templates->template_list as $one_template) {
  510. $wx_template_ids[] = $one_template['template_id'];
  511. }
  512. }
  513. v('wx_template_ids:' . json_encode($wx_template_ids));
  514. $check_res = $this->Template->check_one_tamplate_res($common_template_id, $wx_template_ids);
  515. $result['msg'] = $error_msgs[$check_res].' '.$extra_error_msg;
  516. v('check_template_status_end,gzh_app_id:' . $gzh_app_id);
  517. json_echo($result);
  518. }
  519. public function get_gzh_statistics(Request $request)
  520. {
  521. $from_date = $request->input('from_date', date('Y-m-d', strtotime('-7 day')));
  522. $to_date = $request->input('to_date', date('Y-m-d', strtotime('-1 day')));
  523. $gzh_app_id = $this->gzh_app_id;
  524. if (empty($gzh_app_id)) {
  525. return ['code' => 304, 'msg' => 'appid empty'];
  526. }
  527. $result = array('code' => 1, 'msg' => '', 'data' => array());
  528. try {
  529. $result['data']['user_cumulate'] = $this->app->stats->userCumulate($from_date, $to_date);//获取累计用户数据, 最大时间跨度:7;
  530. $result['data']['user_summary'] = $this->app->stats->userSummary($from_date, $to_date);//获取用户增减数据, 最大时间跨度:7;
  531. v($result);
  532. } // 加上\ 全局抓取
  533. catch (\Exception $e) {
  534. $result['code'] = 0;
  535. $result['msg'] = $e->getMessage();
  536. v('get_gzh_statistics_ept:' . $gzh_app_id . ' info:' . $e->getMessage());
  537. }
  538. return $result;
  539. }
  540. }