<?php

namespace App\Http\Controllers\Wechat\Api;

use App\Http\Controllers\WechatController;
use App\Http\Controllers\Wechat\Qrcode\QrcodesController;
use App\Http\Controllers\Wechat\Material\MaterialsController;
use App\Http\Controllers\Wechat\Template\TemplateBasesController;
use App\Http\Requests;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use EasyWeChat\Foundation\Application;
use App\Http\Controllers\Wechat\Staff\StaffsController;

/**
 * 对外相关api接口,和微信token有关
 * @author zhoulingjie
 *
 */
class WechatOuterApisController extends WechatController
{

    public function __construct(Request $request)
    {
        // TODO 加密,检测gzh_app_id合法性
        $this->gzh_app_id = $request->get('gzh_app_id');
        parent::__construct($this->gzh_app_id);
        // 方便扩展
        $param               = array();
        $param['app']        = $this->app;
        $param['WechatApi']  = isset($this->WechatApi) ? $this->WechatApi : null;
        $param['gzh_app_id'] = $this->gzh_app_id;
        $this->Qrcode        = new QrcodesController($param);
        $this->Material      = new MaterialsController($param);
        $this->Template      = new TemplateBasesController($param);
        $this->Staff         = new StaffsController($this->param);
        $this->User          = $this->app->user;

    }

    /**
     * http://zydy/api/get_qrcode_url?gzh_app_id=wx03d107cd1ee728c2&scene_id=999&timestamp=1511503861&sign=74b21f32c094f8d343c9743c196f6a32
     * 获取带参二维码
     */
    public function get_qrcode_url(Request $request)
    {
        $result     = array('code' => 1, 'msg' => '', 'data' => '');
        $sceneId    = $request->get('scene_id');
        $gzh_app_id = $request->get('gzh_app_id');
        v($request->all());
        if (empty($sceneId) || empty($gzh_app_id)) {
            $result['code'] = 0;
            $result['msg']  = 'invalid param';
            json_echo($result);
        }

        $check_result = $this->check_sign_params($request);
        if ($check_result['code'] == 0) {
            $result['code'] = 0;
            $result['msg']  = $check_result['msg'];
            json_echo($result);
        }

        $qrcode_url = $this->Qrcode->create_qrcode('temporary', $sceneId);
        //     	v($chapters);
        if (!empty($qrcode_url)) {
            $result['data'] = $qrcode_url;
        } else {
            $result['code'] = 0;
            $result['msg']  = 'create qrcode url fail';
        }

        json_echo($result);
    }

    /**
     * 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
     * http://zydy/api/upload_gzh_img?gzh_app_id=wxdbc486f1b4f6a8c3&group_nick=zhuishuyun&timestamp=1513600581&sign=38693a228ea65fc894c15bc6d76e7287
     * 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
     * 上传公众号图片
     */
    public function upload_gzh_img(Request $request)
    {
        $result     = array('code' => 1, 'msg' => '', 'data' => array('media_id' => '', 'url' => ''));
        $group_nick = $request->get('group_nick');
        $gzh_app_id = $request->get('gzh_app_id');
        $is_default = $request->has('is_default') ? $request->get('is_default') : 1;
        $img_url    = $request->has('img_url') ? $request->get('img_url') : '';

        v($request->all());
        if (empty($group_nick) || empty($gzh_app_id)) {
            $result['code'] = 0;
            $result['msg']  = 'upload_gzh_img invalid param';
            json_echo($result);
        }
        v('upload_gzh_img:' . $gzh_app_id);

        $check_result = $this->check_sign_params($request);
        if ($check_result['code'] == 0) {
            $result['code'] = 0;
            $result['msg']  = $check_result['msg'];
            json_echo($result);
        }

        if (!$is_default) {
            $img_url = DownImage($img_url, public_path('admin/imgs/contact_customer/contact_customer_' . $gzh_app_id . '.png'));
        } else {
            $img_url = public_path('admin/imgs/common/contact/contact_customer_' . $group_nick . '.png');
        }
        $upload_result = $this->Material->upload_contact_cusomer_img($group_nick, $img_url);
        v('$upload_result');
        v($upload_result);

        if (!empty($upload_result)) {
            $result['data']['media_id'] = $upload_result->media_id;
            $result['data']['url']      = $upload_result->url;
        } else {
            $result['code'] = 0;
            $result['msg']  = 'upload img fail';
        }
        v('gzh_appid:' . $gzh_app_id . ' result:' . json_encode($result));

        json_echo($result);
    }

    /**
     * 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
     * 上传公众号素材图片
     */
    public function upload_material_img(Request $request)
    {
        $result       = array('code' => 1, 'msg' => '', 'data' => array('media_id' => '', 'url' => ''));
        $group_nick   = $request->get('group_nick');
        $gzh_app_id   = $request->get('gzh_app_id');
        $image_type   = $request->has('image_type') ? $request->get('image_type') : 'common';
        $img_url      = $request->has('img_url') ? $request->get('img_url') : '';
        $real_img_url = urldecode($img_url);

        v($request->all());
        if (empty($group_nick) || empty($gzh_app_id)) {
            $result['code'] = 0;
            $result['msg']  = 'upload_gzh_img invalid param';
            json_echo($result);
        }
        v('upload_gzh_img:' . $gzh_app_id);

        $check_result = $this->check_sign_params($request);
        if ($check_result['code'] == 0) {
            $result['code'] = 0;
            $result['msg']  = $check_result['msg'];
            json_echo($result);
        }
        $path = public_path('admin/imgs/material/temp_' . $gzh_app_id . '_' . md5($img_url) . '.png');
        v('DownImage:real_img_url:' . $real_img_url . ' path:' . $path);
        DownImage($real_img_url, $path);

        if ($image_type == 'common') {
            $upload_result = $this->Material->upload_material_img($group_nick, $path);
        } else {
            $upload_result = $this->Material->upload_thumb_material_img($group_nick, $path);
        }

        if (!empty($upload_result)) {
            $result['data']['media_id'] = $upload_result->media_id;
            $result['data']['url']      = $upload_result->url;
        } else {
            $result['code'] = 0;
            $result['msg']  = $this->Material->getErrorMsg('upload_materials', 'upload img fail');
        }

        json_echo($result);
    }

    /**
     * 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
     * 上传公众号图文素材
     */
    public function upload_gzh_article(Request $request)
    {
        $result             = array('code' => 1, 'msg' => '', 'data' => array('chapter_media_id' => '', 'chapter_url' => ''));
        $group_nick         = $request->get('group_nick');
        $gzh_app_id         = $request->get('gzh_app_id');
        $title              = $request->has('title') ? $request->get('title') : '';
        $thumb_media_id     = $request->has('thumb_media_id') ? $request->get('thumb_media_id') : '';
        $show_cover_pic     = $request->has('show_cover_pic') ? $request->get('show_cover_pic') : '';
        $author             = $request->has('author') ? $request->get('author') : '';
        $digest             = $request->has('digest') ? $request->get('digest') : '';
        $content            = $request->has('content') ? $request->get('content') : '';
        $content_source_url = $request->has('content_source_url') ? $request->get('content_source_url') : '';
        v($request->all());
        if (empty($group_nick) || empty($gzh_app_id) || empty($thumb_media_id) || empty($content)) {
            $result['code'] = 0;
            $result['msg']  = 'upload_gzh_article invalid param';
            json_echo($result);
        }
        v('upload_gzh_img:' . $gzh_app_id);

        $check_result = $this->check_sign_params($request);
        if ($check_result['code'] == 0) {
            $result['code'] = 0;
            $result['msg']  = $check_result['msg'];
            json_echo($result);
        }

        $article                       = array();
        $article['thumb_media_id']     = $thumb_media_id;//'XYhErlq3w-hlmwqOB4O5SqFAgM6yWcg0u9ttENsjSIg';
        $article['title']              = $title;
        $article['show_cover_pic']     = $show_cover_pic;
        $article['author']             = $author;
        $article['digest']             = $digest;
        $article['content']            = $content;
        $article['content_source_url'] = $content_source_url;
        $upload_result                 = $this->Material->upload_article($article);

        if (!empty($upload_result)) {
            $chapter_resource = $this->Material->get_material_resource($upload_result->media_id);
            if (!empty($chapter_resource)) {
                $result['data']['chapter_url']      = isset($chapter_resource['news_item'][0]['url']) ? $chapter_resource['news_item'][0]['url'] : '';
                $result['data']['chapter_media_id'] = isset($chapter_resource['news_item'][0]['thumb_media_id']) ? $chapter_resource['news_item'][0]['thumb_media_id'] : '';
            } else {
                $result['code'] = 0;
                $result['msg']  = 'get_material_resource fail';
            }
        } else {
            $result['code'] = 0;
            $result['msg']  = 'upload img fail';
        }
        v($result);

        json_echo($result);
    }

    /**
     * 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
     * 上传公众号图文素材
     */
    public function upload_gzh_articles(Request $request)
    {
        $result     = array('code' => 1, 'msg' => '', 'data' => array('chapter_media_id' => '', 'chapter_url' => ''));
        $group_nick = $request->get('group_nick');
        $gzh_app_id = $request->get('gzh_app_id');
        $articles   = $request->has('articles') ? $request->get('articles') : '';
        v($request->all());
        if (empty($group_nick) || empty($gzh_app_id) || empty($articles)) {
            $result['code'] = 0;
            $result['msg']  = 'upload_gzh_articles invalid param';
            json_echo($result);
        }
        v('upload_gzh_articles:' . $gzh_app_id);

        $check_result = $this->check_sign_params($request);
        if ($check_result['code'] == 0) {
            $result['code'] = 0;
            $result['msg']  = $check_result['msg'];
            json_echo($result);
        }

//     	$articles = json_decode($articles);
        $upload_result = $this->Material->upload_articles($gzh_app_id, $articles);

        if (!empty($upload_result)) {
            $result['data']   = $upload_result;
            $chapter_resource = $this->Material->get_material_resource($upload_result->media_id);
        } else {
            $result['code'] = 0;
            $result['msg']  = $this->Material->getErrorMsg('upload_articles', 'upload articles fail');
        }
        v($result);

        json_echo($result);
    }


    /**
     * 维维读书 wx03d107cd1ee728c2 17
     * 纳兰阅读 wx392b782e2a082eb2 16
     * 已读攻毒 wx62c7e9290d51eeba 23
     * http://zydy/oauth/add_public_template?gzh_app_id=wxd52de911dfef6d92&common_template_id=&timestamp=1511509543&sign=98dccc7b29e3c05e4b4367f488ff1966
     * http://zsyauth.aizhuishu.com/oauth/auto_set_menu_and_template_test?authorizer_appid=wxdb365b8f2fc8aeb0&timestamp=1511509543&sign=98dccc7b29e3c05e4b4367f488ff1966
     */
    function add_public_template(Request $request)
    {
        $result             = array('code' => 1, 'msg' => '', 'data' => '');
        $gzh_app_id         = $request->get('gzh_app_id');
        $common_template_id = $request->get('common_template_id');
        v('add_public_template_start,gzh_appid:' . $gzh_app_id . ' common_template_id:' . $common_template_id);
        if (empty($gzh_app_id) || empty($common_template_id)) {
            $result['code'] = 0;
            $result['msg']  = 'invalid param';
            json_echo($result);
        }

        $check_result = $this->check_sign_params($request);
        if ($check_result['code'] == 0) {
            $result['code'] = 0;
            $result['msg']  = $check_result['msg'];
            json_echo($result);
        }
        $this->Template->add_one_tamplate($common_template_id);
        v('add_public_template_end,gzh_app_id:' . $gzh_app_id . ' common_template_id:' . $common_template_id);
    }

    /**
     * 嘉言小说 wxdbc486f1b4f6a8c3
     * http://zydy/api/get_full_official_account_users?gzh_app_id=wxdbc486f1b4f6a8c3&next_openid=timestamp=1511509543&sign=98dccc7b29e3c05e4b4367f488ff1966
     */
    function get_full_official_account_users(Request $request)
    {
        $result      = array('code' => 1, 'msg' => '', 'data' => array('total' => '', 'count' => '', 'data' => '', 'next_openid' => ''));
        $gzh_app_id  = $request->get('gzh_app_id');
        $next_openid = $request->get('next_openid');
        v('get_full_official_account_users_start,gzh_appid:' . $gzh_app_id . ' next_openid:' . $next_openid);
        if (empty($gzh_app_id)) {
            $result['code'] = 0;
            $result['msg']  = 'invalid param';
            json_echo($result);
        }

        $check_result = $this->check_sign_params($request);
        if ($check_result['code'] == 0) {
            $result['code'] = 0;
            $result['msg']  = $check_result['msg'];
            json_echo($result);
        }
        $users = $this->User->lists($next_openid);
        v($users);
        $result['data']['total']       = isset($users->total) ? $users->total : '';
        $result['data']['count']       = isset($users->count) ? $users->count : '';
        $result['data']['data']        = isset($users->data) ? $users->data : '';
        $result['data']['next_openid'] = isset($users->next_openid) ? $users->next_openid : '';
        v('get_full_official_account_users_end,gzh_app_id:' . $gzh_app_id . ' next_openid:' . $next_openid);
        json_echo($result);
    }

    /**
     * 维护公众号模板消息(被封,删除等异常情况)
     * http://zydy/api/check_official_account_templates?gzh_app_id=wxdbc486f1b4f6a8c3&timestamp=1523440738&sign=1d9640c40ef0bbb19b5352f60372c300
     */
    function check_official_account_templates(Request $request)
    {
        $result     = array('code' => 1, 'msg' => '', 'data' => '');
        $gzh_app_id = $request->get('gzh_app_id');
        v('check_official_account_templates_start,gzh_appid:' . $gzh_app_id);
        if (empty($gzh_app_id)) {
            $result['code'] = 0;
            $result['msg']  = 'invalid param';
            json_echo($result);
        }

        $check_result = $this->check_sign_params($request);
        if ($check_result['code'] == 0) {
            $result['code'] = 0;
            $result['msg']  = $check_result['msg'];
            json_echo($result);
        }
        // 微信后台的模板
        $wx_backend_templates = $this->Template->get_private_templates();
        $wx_template_ids      = array();
        if (isset($wx_backend_templates->template_list) && !empty($wx_backend_templates->template_list)) {
            foreach ($wx_backend_templates->template_list as $one_template) {
                $wx_template_ids[] = $one_template['template_id'];
            }
        }
        v('wx_template_ids:' . json_encode($wx_template_ids));

        // 我们目前的基础模板
        $base_templates = $this->WechatApi->get_wechat_public_templates($gzh_app_id);
//     	v('base_templates');v($base_templates);
        if ($base_templates) {
            foreach ($base_templates as $template) {
                $this->Template->check_one_tamplate($template['common_template_id'], $wx_template_ids);
            }
        }

        v('maintain_official_account_templates_end,gzh_app_id:' . $gzh_app_id);
    }

    /**
     * 删除菜单
     * http://zydy/api/del_menu?gzh_app_id=wxdbc486f1b4f6a8c3&timestamp=1523615257&sign=056ea8390b3d5a1a6d2444288c1a0251
     */
    function del_menu(Request $request)
    {
        $result     = array('code' => 1, 'msg' => '', 'data' => '');
        $gzh_app_id = $request->get('gzh_app_id');
        v('del_menu_start,gzh_appid:' . $gzh_app_id);
        if (empty($gzh_app_id)) {
            $result['code'] = 0;
            $result['msg']  = 'invalid param';
            json_echo($result);
        }

        $check_result = $this->check_sign_params($request);
        if ($check_result['code'] == 0) {
            $result['code'] = 0;
            $result['msg']  = $check_result['msg'];
            json_echo($result);
        }
        $menu         = $this->app->menu;
        $current_menu = $menu->current();
        v('$current_menu');
        v($current_menu);

        $del_menu = $menu->destroy();
        v('$del_menu');
        v($del_menu);

        $current_menu = $menu->current();
        v('$after_current_menu');

        v('del_menu_end,gzh_app_id:' . $gzh_app_id);
    }

    /**
     * 得到用户信息
     * http://zydy/api/get_userinfo?gzh_app_id=wxdbc486f1b4f6a8c3&openid=oAcqg1LRHNKN2jaEkJ5v56HOwPEQ&timestamp=1524130007&sign=e7ceefdf35ae7eee15e00404fe89c66c
     */
    function get_userinfo(Request $request)
    {
        $result     = array('code' => 1, 'msg' => '', 'data' => '');
        $gzh_app_id = $request->get('gzh_app_id');
        $openid     = $request->get('openid');
        v('get_userinfo,gzh_appid:' . $gzh_app_id . ' openid:' . $openid);
        if (empty($gzh_app_id) || empty($openid)) {
            $result['code'] = 0;
            $result['msg']  = 'invalid param';
            json_echo($result);
        }

        $check_result = $this->check_sign_params($request);
        if ($check_result['code'] == 0) {
            $result['code'] = 0;
            $result['msg']  = $check_result['msg'];
            json_echo($result);
        }
        $user     = $this->app->user;
        $userinfo = $user->get($openid);
        v('userinfo');
        v($userinfo);

        $result['data'] = $userinfo;
        json_echo($result);
    }

    /**
     * 得到用户信息
     * http://zydy/api/get_short_url?gzh_app_id=wxdbc486f1b4f6a8c3&url=www.baidu.com&timestamp=1524130007&sign=e7ceefdf35ae7eee15e00404fe89c66c
     */
    function get_short_url(Request $request)
    {
        $result     = array('code' => 1, 'msg' => '', 'data' => '');
        $gzh_app_id = $request->get('gzh_app_id');
        $url        = $request->get('url');
        $url        = urldecode($url);
        v('get_short_url,gzh_appid:' . $gzh_app_id . ' $url:' . $url);
        if (empty($gzh_app_id) || empty($url)) {
            $result['code'] = 0;
            $result['msg']  = 'invalid param';
            json_echo($result);
        }

        $check_result = $this->check_sign_params($request);
        if ($check_result['code'] == 0) {
            $result['code'] = 0;
            $result['msg']  = $check_result['msg'];
            json_echo($result);
        }

        try {
            v('get_short_url_start:' . $gzh_app_id);
            $app_short_url = $this->app->url;
            $short_result  = $app_short_url->shorten($url);
            v('short_result:' . $gzh_app_id);
            v($short_result);
            if (isset($short_result->errcode) && $short_result->errcode == '0'
                && isset($short_result->errmsg) && $short_result->errmsg == 'ok') {
                $result['data'] = $short_result->short_url;
            } else {
                $result['code'] = 0;
                $result['msg']  = $short_result->errmsg;
            }
        } catch (\Exception $e) {
            $error_msg = $e->getMessage();
            v('get_short_url_ept:' . $error_msg);
            if (strpos($error_msg, 'api forbidden') !== false) {
                $result['code'] = 0;
                $result['msg']  = '公众号微信接口被封,请等待解封!';
            } elseif (strpos($error_msg, 'user limited') !== false) {
                $result['code'] = 0;
                $result['msg']  = '公众号微信接口被限制,请等待解除!';
            }
        }

        json_echo($result);
    }

    /**
     * 检查公众号模板消息(被封,删除等异常情况)
     * http://zydy/api/check_template_status?gzh_app_id=wxdbc486f1b4f6a8c3&timestamp=1523440738&sign=1d9640c40ef0bbb19b5352f60372c300
     */
    function check_template_status(Request $request)
    {
        $result             = array('code' => 1, 'msg' => '', 'data' => '');
        $gzh_app_id         = $request->get('gzh_app_id');
        $common_template_id = $request->get('common_template_id');
        v('check_template_status_start,gzh_appid:' . $gzh_app_id . ' common_template_id:' . $common_template_id);
        if (empty($gzh_app_id) || empty($common_template_id)) {
            $result['code'] = 0;
            $result['msg']  = 'invalid param';
            json_echo($result);
        }

        $check_result = $this->check_sign_params($request);
        if ($check_result['code'] == 0) {
            $result['code'] = 0;
            $result['msg']  = $check_result['msg'];
            json_echo($result);
        }

        $error_msgs = [
            '1' => '模板修复成功',
            '2' => '模板正常',
            '3' => '微信模板修复失败!',
            '4' => '微信模板接口被封,请等待解封!',
            '5' => '微信后台模板数量达到上限,请删除部分模板后再操作',
            '6' => '微信模板被封,请等待解封!',
            '7' => '模板被微信官方取消失效了!',
            '8' => '号被限制了权限',
            '9' => '接口未授权',
            '10' => '分类不正确!',
            '11' => '号的模板功能被处罚限制中!',
        ];

        // 一、先检查分类
        $industry = $this->Template->get_industry();
        if ($industry == '4') {
            $result['msg'] = $error_msgs[$industry];
            json_echo($result);
        }
        $primary_industry_first_class    = $industry->primary_industry['first_class'];
        $primary_industry_second_class   = $industry->primary_industry['second_class'];
        $secondary_industry_first_class  = $industry->secondary_industry['first_class'];
        $secondary_industry_second_class = $industry->secondary_industry['second_class'];
        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);
        
        $extra_error_msg = '';
        // 存在部分分类异常情况,所以分类错误允许往下走,还能抢救一部分模板消息
        if (!($primary_industry_first_class == 'IT科技' && $primary_industry_second_class == 'IT软件与服务'
            && $secondary_industry_first_class == '文体娱乐' && $secondary_industry_second_class == '文化|传媒')) {
            v('check_template_status_set_template_dustry:' . $gzh_app_id);
            $set_res = $this->Template->set_template_dustry_res(2, 37);
            if ($set_res == 2) {
                v('modify_dustry_ept:' . $gzh_app_id);
//                 $result['msg'] = '模板分类错误,请修改分类至(IT科技->IT软件与服务、文体娱乐->文化|传媒),如不能修改请等待,分类1个月才能修改一次';
//                 json_echo($result);
                
                $extra_error_msg = '请注意:模板分类错误,请修改分类至(IT科技->IT软件与服务、文体娱乐->文化|传媒),如不能修改请等待,分类1个月才能修改一次';
                
            } else {
                v('modify_dustry_ok:' . $gzh_app_id);
            }
        }

        // 二、分类正确,检查某一个模板,微信后台是否存在,我们系统后台是否存在

        // 微信后台的模板
        $wx_backend_templates = $this->Template->get_private_templates();
        $wx_template_ids      = array();
        if (isset($wx_backend_templates->template_list) && !empty($wx_backend_templates->template_list)) {
            foreach ($wx_backend_templates->template_list as $one_template) {
                $wx_template_ids[] = $one_template['template_id'];
            }
        }
        v('wx_template_ids:' . json_encode($wx_template_ids));

        $check_res     = $this->Template->check_one_tamplate_res($common_template_id, $wx_template_ids);
        $result['msg'] = $error_msgs[$check_res].' '.$extra_error_msg;

        v('check_template_status_end,gzh_app_id:' . $gzh_app_id);
        json_echo($result);
    }

    public function get_gzh_statistics(Request $request)
    {
        $from_date  = $request->input('from_date', date('Y-m-d', strtotime('-7 day')));
        $to_date    = $request->input('to_date', date('Y-m-d', strtotime('-1 day')));
        $gzh_app_id = $this->gzh_app_id;
        if (empty($gzh_app_id)) {
            return ['code' => 304, 'msg' => 'appid empty'];
        }
        $result = array('code' => 1, 'msg' => '', 'data' => array());
        try {
            $result['data']['user_cumulate'] = $this->app->stats->userCumulate($from_date, $to_date);//获取累计用户数据, 最大时间跨度:7;
            $result['data']['user_summary']  = $this->app->stats->userSummary($from_date, $to_date);//获取用户增减数据, 最大时间跨度:7;
            v($result);
        } // 加上\ 全局抓取
        catch (\Exception $e) {
            $result['code'] = 0;
            $result['msg']  = $e->getMessage();
            v('get_gzh_statistics_ept:' . $gzh_app_id . ' info:' . $e->getMessage());
        }

        return $result;
    }
}