123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605 |
- <?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;
- class WechatOuterApisController extends WechatController
- {
- public function __construct(Request $request)
- {
-
- $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;
- }
-
- 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);
-
- if (!empty($qrcode_url)) {
- $result['data'] = $qrcode_url;
- } else {
- $result['code'] = 0;
- $result['msg'] = 'create qrcode url fail';
- }
- json_echo($result);
- }
-
- 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);
- }
-
- 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);
- }
-
- 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;
- $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);
- }
-
- 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);
- }
- $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);
- }
-
- 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);
- }
-
- 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);
- }
-
- 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);
- 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);
- }
-
- 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);
- }
-
- 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);
- }
-
- 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);
- }
-
- 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);
-
- $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);
- $result['data']['user_summary'] = $this->app->stats->userSummary($from_date, $to_date);
- 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;
- }
- }
|