|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- <?php
- /**
- *
- * ============================================================================
- * * 版权所有 运游通 * *
- * 网站地址: http://www.zhizhuchuxing.com
- * ----------------------------------------------------------------------------
- * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
- * 使用;不允许对程序代码以任何形式任何目的的再发布。
- * ============================================================================
- * Author By: 倪宗锋
- * PhpStorm WxInterfaceController.php
- * Create By 2017/1/4 13:49 $
- */
-
-
- namespace WxInterface\Controller;
-
-
- use Base\Tool\LoginTool;
- use Util\Controller\MvcController;
- use Util\Util\CurlInterface;
- use Util\Util\Util;
- use WxInterface\Service\WxInterface;
-
- class WxInterfaceController extends MvcController
- {
- private $service;
-
- public function getService()
- {
- if ($this->service == null) {
- $this->service = new WxInterface();
- }
- return $this->service;
- }
-
- /**
- * Des:页面跳转
- * Name: setLoginAction
- * @return string
- * @author 倪宗锋
- */
- public function setLoginAction()
- {
- $code = $this->_get('code', '');
- $wxLogin = LoginTool::wxLoginByCode($code);
- if (empty($wxLogin['url']) == false) {
- header('location:' . $wxLogin['url']);
- return $wxLogin['url'];
- }
- $url = $this->_get('redirect', '');
- $siteConfig = Util::getSiteConfig();
- $url = empty($url) ? $siteConfig['host_name'] : $url;
- $array = parse_url($url);
- if (!isset($array['query'])) {
- $url = $url . '?ma=' . rand(1000, 9999);
- } else {
- $url = $url . '&ma=' . rand(1000, 9999);
- }
- header('location:' . $url);
- return $url;
- }
-
- /*
- * 第三方授权路径
- */
- public function authLoginAction()
- {
- $code = $this->_get('code', '');
- if (empty($code)) {
- $code = $this->_get('auth_code', '');
- }
- $url = $this->_get('redirect', '');
- // $url = $url;
- $url .= '&code=' . $code;
- header('location:' . $url);
- return $url;
- }
-
- /**
- * Des:开放给微信调用的接口
- * Name: indexAction
- * @return bool|string
- * @author 倪宗锋
- */
- public function indexAction()
- {
- if (isset($_GET['echostr'])) {//首次校验
- $check = $this->getService()->valid();
- return $check;
- }
- return $this->getService()->exec();
- }
-
- /**
- * Des:设置菜单
- * Name: setMenuAction
- * @author 倪宗锋
- */
- public function setMenuAction()
- {
- $access_token = $this->getService()->getAccessToken();
- $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $access_token;
- $data = require ROOT_PATH . '/data/wxConfig/wxMenu.config.php';
- $result = $this->getService()->_requestPost($url, $data);
- print_r($result);
- }
-
- /**
- * Des:获取素材ID
- * Name: getMediaAction
- * @author 倪宗锋
- */
- public function getMediaAction()
- {
- $type = $this->_get('type', 'news');//素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news)
- $list = $this->getMediaList($type);
- $this->viewModel->setParam('type', $type);
- $this->viewModel->setParam('list', $list);
- return $this->viewModel('getMedia');
- }
-
- public function getMediaList($type)
- {
- $clear = $this->_get('clear', '');
- $cache = \BaseMemcached::link();
- $wxConfig = Util::getWxPayConfig();
- $keys = $wxConfig['appid'] . 'wxMediaList';
- if ($clear == 'clear') {
- $cache->delete($keys);
- }
- $wxMediaList = $cache->get($keys);
- // if (isset($wxMediaList[$type])) {
- // return $wxMediaList[$type];
- // }
- $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 clearAction()
- {
- $cache = \BaseMemcached::link();
- $wxConfig = Util::getWxPayConfig();
- $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;
- }
-
- public function showConfigAction()
- {
- $textConfig = require ROOT_PATH . '/data/wxConfig/Wx.text.config.php';
- $cache = \BaseMemcached::link();
- $wxConfig = Util::getWxPayConfig();
- $key = $wxConfig['appid'] . 'wxMediaList';
- $wxMediaList = $cache->get($key);
- $config = array();
- foreach ($textConfig as $key => $val) {
- $arr = array();
- if (is_array($val)) {
- if ($val['type'] == 'img') {
- $val['type'] = 'image';
- }
- $arr['name'] = $key;
- $arr['type'] = $val['type'];
- $arr['id'] = $val['id'];
- $arr['title'] = '';
- foreach ($wxMediaList[$val['type']] as $listVal) {
- if ($val['id'] == $listVal['media_id']) {
- $arr['title'] = $listVal['name'];
- }
- }
- } else {
- $arr['name'] = $key;
- $arr['type'] = '文本';
- $arr['id'] = $val;
- $arr['title'] = '';
- }
- $config[] = $arr;
- }
- $this->viewModel->setParam('config', $config);
- return $this->viewModel('showConfig');
-
- }
-
- public function addQRcodeAction()
- {
- $code = $this->_get('id', '');
- if (empty($code)) {
- return $this->viewError('id不能为空!');
- }
- $token = $this->getService()->getAccessToken();
- $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" . $token;
- $data = array(
- 'action_name' => 'QR_LIMIT_STR_SCENE',
- 'action_info' => array(
- 'scene' => array(
- 'scene_str' => $code
- )
- )
- );
- $inface = new CurlInterface($data);
- $inface->setBaseUrl($url);
- $return = $inface->execute('', 'POST');
- $return = print_r($return, 1);
- return $return;
- }
-
- public function testAction()
- {
- $memcache = new \Memcache(); //创建一个memcache对象
- $memcache->connect('101.37.36.30', 11211) or die ("Could not connect"); //连接Memcached服务器
- $memcache->set('key', 'test'); //设置一个变量到内存中,名称是key 值是test
- $get_value = $memcache->get('key'); //从内存中取出key的值
- echo $get_value;
- }
-
- }
|