You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WxInterfaceController.php 7.8 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. *
  4. * ============================================================================
  5. * * 版权所有 运游通 * *
  6. * 网站地址: http://www.zhizhuchuxing.com
  7. * ----------------------------------------------------------------------------
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
  9. * 使用;不允许对程序代码以任何形式任何目的的再发布。
  10. * ============================================================================
  11. * Author By: 倪宗锋
  12. * PhpStorm WxInterfaceController.php
  13. * Create By 2017/1/4 13:49 $
  14. */
  15. namespace WxInterface\Controller;
  16. use Base\Tool\LoginTool;
  17. use Util\Controller\MvcController;
  18. use Util\Util\CurlInterface;
  19. use Util\Util\Util;
  20. use WxInterface\Service\WxInterface;
  21. class WxInterfaceController extends MvcController
  22. {
  23. private $service;
  24. public function getService()
  25. {
  26. if ($this->service == null) {
  27. $this->service = new WxInterface();
  28. }
  29. return $this->service;
  30. }
  31. /**
  32. * Des:页面跳转
  33. * Name: setLoginAction
  34. * @return string
  35. * @author 倪宗锋
  36. */
  37. public function setLoginAction()
  38. {
  39. $code = $this->_get('code', '');
  40. $wxLogin = LoginTool::wxLoginByCode($code);
  41. if (empty($wxLogin['url']) == false) {
  42. header('location:' . $wxLogin['url']);
  43. return $wxLogin['url'];
  44. }
  45. $url = $this->_get('redirect', '');
  46. $siteConfig = Util::getSiteConfig();
  47. $url = empty($url) ? $siteConfig['host_name'] : $url;
  48. $array = parse_url($url);
  49. if (!isset($array['query'])) {
  50. $url = $url . '?ma=' . rand(1000, 9999);
  51. } else {
  52. $url = $url . '&ma=' . rand(1000, 9999);
  53. }
  54. header('location:' . $url);
  55. return $url;
  56. }
  57. /*
  58. * 第三方授权路径
  59. */
  60. public function authLoginAction()
  61. {
  62. $code = $this->_get('code', '');
  63. if (empty($code)) {
  64. $code = $this->_get('auth_code', '');
  65. }
  66. $url = $this->_get('redirect', '');
  67. // $url = $url;
  68. $url .= '&code=' . $code;
  69. header('location:' . $url);
  70. return $url;
  71. }
  72. /**
  73. * Des:开放给微信调用的接口
  74. * Name: indexAction
  75. * @return bool|string
  76. * @author 倪宗锋
  77. */
  78. public function indexAction()
  79. {
  80. if (isset($_GET['echostr'])) {//首次校验
  81. $check = $this->getService()->valid();
  82. return $check;
  83. }
  84. return $this->getService()->exec();
  85. }
  86. /**
  87. * Des:设置菜单
  88. * Name: setMenuAction
  89. * @author 倪宗锋
  90. */
  91. public function setMenuAction()
  92. {
  93. $access_token = $this->getService()->getAccessToken();
  94. $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $access_token;
  95. $data = require ROOT_PATH . '/data/wxConfig/wxMenu.config.php';
  96. $result = $this->getService()->_requestPost($url, $data);
  97. print_r($result);
  98. }
  99. /**
  100. * Des:获取素材ID
  101. * Name: getMediaAction
  102. * @author 倪宗锋
  103. */
  104. public function getMediaAction()
  105. {
  106. $type = $this->_get('type', 'news');//素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news)
  107. $list = $this->getMediaList($type);
  108. $this->viewModel->setParam('type', $type);
  109. $this->viewModel->setParam('list', $list);
  110. return $this->viewModel('getMedia');
  111. }
  112. public function getMediaList($type)
  113. {
  114. $clear = $this->_get('clear', '');
  115. $cache = \BaseMemcached::link();
  116. $wxConfig = Util::getWxPayConfig();
  117. $keys = $wxConfig['appid'] . 'wxMediaList';
  118. if ($clear == 'clear') {
  119. $cache->delete($keys);
  120. }
  121. $wxMediaList = $cache->get($keys);
  122. // if (isset($wxMediaList[$type])) {
  123. // return $wxMediaList[$type];
  124. // }
  125. $access_token = $this->getService()->getAccessToken();
  126. $url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" . $access_token;
  127. $data = '{"type":"' . $type . '","offset":0,"count":20}';
  128. $result = $this->getService()->httpRequest($url, $data);
  129. $return = json_decode($result, true);
  130. $list = array();
  131. if (isset($return['item'])) {
  132. $list = $return['item'];
  133. }
  134. foreach ($list as $key => $val) {
  135. if ($type == 'news') {
  136. $list[$key] = $this->setNewsShow($val);
  137. }
  138. }
  139. $wxMediaList[$type] = $list;
  140. $cache->set($keys, $wxMediaList, 60 * 10);//缓存10分钟
  141. return $list;
  142. }
  143. public function clearAction()
  144. {
  145. $cache = \BaseMemcached::link();
  146. $wxConfig = Util::getWxPayConfig();
  147. $keys = $wxConfig['appid'] . 'wxMediaList';
  148. $cache->set($keys, array(), 60 * 10);
  149. }
  150. /**
  151. * Des: 设置图文展示
  152. * Name: setNewsShow
  153. * @param $list
  154. * @return array
  155. * @author 倪宗锋
  156. */
  157. public function setNewsShow($list)
  158. {
  159. $listArr = $list['content']['news_item'];
  160. $title = '';
  161. foreach ($listArr as $val) {
  162. if ($val['title']) {
  163. $title .= $val['title'] . '</br>';
  164. }
  165. }
  166. $return = array(
  167. 'media_id' => $list['media_id'],
  168. 'name' => $title
  169. );
  170. return $return;
  171. }
  172. public function showConfigAction()
  173. {
  174. $textConfig = require ROOT_PATH . '/data/wxConfig/Wx.text.config.php';
  175. $cache = \BaseMemcached::link();
  176. $wxConfig = Util::getWxPayConfig();
  177. $key = $wxConfig['appid'] . 'wxMediaList';
  178. $wxMediaList = $cache->get($key);
  179. $config = array();
  180. foreach ($textConfig as $key => $val) {
  181. $arr = array();
  182. if (is_array($val)) {
  183. if ($val['type'] == 'img') {
  184. $val['type'] = 'image';
  185. }
  186. $arr['name'] = $key;
  187. $arr['type'] = $val['type'];
  188. $arr['id'] = $val['id'];
  189. $arr['title'] = '';
  190. foreach ($wxMediaList[$val['type']] as $listVal) {
  191. if ($val['id'] == $listVal['media_id']) {
  192. $arr['title'] = $listVal['name'];
  193. }
  194. }
  195. } else {
  196. $arr['name'] = $key;
  197. $arr['type'] = '文本';
  198. $arr['id'] = $val;
  199. $arr['title'] = '';
  200. }
  201. $config[] = $arr;
  202. }
  203. $this->viewModel->setParam('config', $config);
  204. return $this->viewModel('showConfig');
  205. }
  206. public function addQRcodeAction()
  207. {
  208. $code = $this->_get('id', '');
  209. if (empty($code)) {
  210. return $this->viewError('id不能为空!');
  211. }
  212. $token = $this->getService()->getAccessToken();
  213. $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" . $token;
  214. $data = array(
  215. 'action_name' => 'QR_LIMIT_STR_SCENE',
  216. 'action_info' => array(
  217. 'scene' => array(
  218. 'scene_str' => $code
  219. )
  220. )
  221. );
  222. $inface = new CurlInterface($data);
  223. $inface->setBaseUrl($url);
  224. $return = $inface->execute('', 'POST');
  225. $return = print_r($return, 1);
  226. return $return;
  227. }
  228. public function testAction()
  229. {
  230. $memcache = new \Memcache(); //创建一个memcache对象
  231. $memcache->connect('101.37.36.30', 11211) or die ("Could not connect"); //连接Memcached服务器
  232. $memcache->set('key', 'test'); //设置一个变量到内存中,名称是key 值是test
  233. $get_value = $memcache->get('key'); //从内存中取出key的值
  234. echo $get_value;
  235. }
  236. }