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'] . '
'; } } $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; } }