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.
 
 
 
 
 
 

147 line
4.2 KiB

  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 zzcx\controllers\interfaces;
  16. use common\service\WeChatService;
  17. use common\util\Util;
  18. use yii\web\Controller;
  19. class WxInterfaceController extends Controller
  20. {
  21. private $service;
  22. /**
  23. * @return WeChatService
  24. */
  25. public function getService()
  26. {
  27. if ($this->service == null) {
  28. $this->service = new WeChatService();
  29. }
  30. return $this->service;
  31. }
  32. /**
  33. * Des:开放给微信调用的接口
  34. * Name: indexAction
  35. * @return bool|string
  36. * @author 倪宗锋
  37. */
  38. public function actionIndex()
  39. {
  40. if (isset($_GET['echostr'])) {//首次校验
  41. $check = $this->getService()->valid();
  42. return $check;
  43. }
  44. return $this->getService()->exec();
  45. }
  46. /**
  47. * Des:设置菜单
  48. * Name: setMenuAction
  49. * @author 倪宗锋
  50. */
  51. public function actionSetMenu()
  52. {
  53. $access_token = $this->getService()->getAccessToken();
  54. $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $access_token;
  55. $wechatConfig = Util::getWeChatConfig();
  56. $data = require ROOT_PATH . '/common/config/wechatConfig/wxInterface/'.$wechatConfig['mch_id'].'/wxMenu.config.php';
  57. $result = $this->getService()->_requestPost($url, $data);
  58. print_r($result);
  59. }
  60. /**
  61. * Des:获取素材ID
  62. * Name: getMediaAction
  63. * @author 倪宗锋
  64. */
  65. public function actionGetMedia()
  66. {
  67. $type = $this->_get('type', 'news');//素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news)
  68. $list = $this->getMediaList($type);
  69. include ROOT_PATH.'/common/config/wechatConfig/getMedia.phtml';
  70. return '';
  71. }
  72. /**
  73. * @param $type
  74. * @return array
  75. */
  76. public function getMediaList($type)
  77. {
  78. $clear = $this->_get('clear', '');
  79. $cache = \Yii::$app->getCache();
  80. $wxConfig = Util::getWeChatConfig();
  81. $keys = $wxConfig['appid'] . 'wxMediaList';
  82. if ($clear == 'clear') {
  83. $cache->delete($keys);
  84. }
  85. $wxMediaList = $cache->get($keys);
  86. $access_token = $this->getService()->getAccessToken();
  87. $url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" . $access_token;
  88. $data = '{"type":"' . $type . '","offset":0,"count":20}';
  89. $result = $this->getService()->httpRequest($url, $data);
  90. $return = json_decode($result, true);
  91. $list = array();
  92. if (isset($return['item'])) {
  93. $list = $return['item'];
  94. }
  95. foreach ($list as $key => $val) {
  96. if ($type == 'news') {
  97. $list[$key] = $this->setNewsShow($val);
  98. }
  99. }
  100. $wxMediaList[$type] = $list;
  101. $cache->set($keys, $wxMediaList, 60 * 10);//缓存10分钟
  102. return $list;
  103. }
  104. public function actionClear()
  105. {
  106. $cache = \Yii::$app->getCache();
  107. $wxConfig = Util::getWeChatConfig();
  108. $keys = $wxConfig['appid'] . 'wxMediaList';
  109. $cache->set($keys, array(), 60 * 10);
  110. }
  111. /**
  112. * Des: 设置图文展示
  113. * Name: setNewsShow
  114. * @param $list
  115. * @return array
  116. * @author 倪宗锋
  117. */
  118. public function setNewsShow($list)
  119. {
  120. $listArr = $list['content']['news_item'];
  121. $title = '';
  122. foreach ($listArr as $val) {
  123. if ($val['title']) {
  124. $title .= $val['title'] . '</br>';
  125. }
  126. }
  127. $return = array(
  128. 'media_id' => $list['media_id'],
  129. 'name' => $title
  130. );
  131. return $return;
  132. }
  133. }