|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- /**
- *
- * ============================================================================
- * * 版权所有 运游通 * *
- * 网站地址: http://www.zhizhuchuxing.com
- * ----------------------------------------------------------------------------
- * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
- * 使用;不允许对程序代码以任何形式任何目的的再发布。
- * ============================================================================
- * Author By: 倪宗锋
- * PhpStorm WxInterfaceController.php
- * Create By 2017/1/4 13:49 $
- */
-
-
- namespace zzcx\controllers\interfaces;
-
-
-
-
- use common\service\WeChatService;
- use common\util\Util;
- use yii\web\Controller;
-
- class WxInterfaceController extends Controller
- {
- private $service;
-
- /**
- * @return WeChatService
- */
- public function getService()
- {
- if ($this->service == null) {
- $this->service = new WeChatService();
- }
- return $this->service;
- }
-
- /**
- * Des:开放给微信调用的接口
- * Name: indexAction
- * @return bool|string
- * @author 倪宗锋
- */
- public function actionIndex()
- {
- if (isset($_GET['echostr'])) {//首次校验
- $check = $this->getService()->valid();
- return $check;
- }
- return $this->getService()->exec();
- }
-
- /**
- * Des:设置菜单
- * Name: setMenuAction
- * @author 倪宗锋
- */
- public function actionSetMenu()
- {
- $access_token = $this->getService()->getAccessToken();
- $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $access_token;
- $wechatConfig = Util::getWeChatConfig();
- $data = require ROOT_PATH . '/common/config/wechatConfig/wxInterface/'.$wechatConfig['mch_id'].'/wxMenu.config.php';
- $result = $this->getService()->_requestPost($url, $data);
- print_r($result);
- }
-
- /**
- * Des:获取素材ID
- * Name: getMediaAction
- * @author 倪宗锋
- */
- public function actionGetMedia()
- {
- $type = $this->_get('type', 'news');//素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news)
- $list = $this->getMediaList($type);
- include ROOT_PATH.'/common/config/wechatConfig/getMedia.phtml';
- return '';
- }
-
- /**
- * @param $type
- * @return array
- */
- public function getMediaList($type)
- {
- $clear = $this->_get('clear', '');
- $cache = \Yii::$app->getCache();
- $wxConfig = Util::getWeChatConfig();
- $keys = $wxConfig['appid'] . 'wxMediaList';
- if ($clear == 'clear') {
- $cache->delete($keys);
- }
- $wxMediaList = $cache->get($keys);
- $access_token = $this->getService()->getAccessToken();
- $url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" . $access_token;
- $data = '{"type":"' . $type . '","offset":0,"count":20}';
- $result = $this->getService()->httpRequest($url, $data);
- $return = json_decode($result, true);
- $list = array();
- if (isset($return['item'])) {
- $list = $return['item'];
- }
- foreach ($list as $key => $val) {
- if ($type == 'news') {
- $list[$key] = $this->setNewsShow($val);
- }
- }
- $wxMediaList[$type] = $list;
- $cache->set($keys, $wxMediaList, 60 * 10);//缓存10分钟
- return $list;
- }
-
- public function actionClear()
- {
- $cache = \Yii::$app->getCache();
- $wxConfig = Util::getWeChatConfig();
- $keys = $wxConfig['appid'] . 'wxMediaList';
- $cache->set($keys, array(), 60 * 10);
- }
-
- /**
- * Des: 设置图文展示
- * Name: setNewsShow
- * @param $list
- * @return array
- * @author 倪宗锋
- */
- public function setNewsShow($list)
- {
- $listArr = $list['content']['news_item'];
- $title = '';
- foreach ($listArr as $val) {
- if ($val['title']) {
- $title .= $val['title'] . '</br>';
- }
- }
- $return = array(
- 'media_id' => $list['media_id'],
- 'name' => $title
- );
- return $return;
- }
- }
|