Wechat.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace addons\qingdongams\library;
  3. use addons\qingdongams\model\AdminConfig;
  4. use EasyWeChat\Factory;
  5. use think\Model;
  6. use fast\Http;
  7. /**
  8. *
  9. */
  10. class Wechat extends Model {
  11. public $config;
  12. protected $app;
  13. public function __construct($platform) {
  14. switch ($platform) {
  15. case 'wxOfficialAccount':
  16. $this->config = [
  17. 'app_id' => AdminConfig::getConfigValue('appid', 'wechat'),
  18. 'secret' => AdminConfig::getConfigValue('secret', 'wechat')
  19. ];
  20. $this->app = Factory::officialAccount($this->config);
  21. break;
  22. case 'wxMiniProgram':
  23. $this->config = [
  24. 'app_id' => AdminConfig::getConfigValue('mini_appid', 'wechat'),
  25. 'secret' => AdminConfig::getConfigValue('mini_secret', 'wechat')
  26. ];
  27. $this->app = Factory::miniProgram($this->config);
  28. break;
  29. }
  30. return parent::__construct();
  31. }
  32. // 返回实例
  33. public function getApp() {
  34. return $this->app;
  35. }
  36. //小程序:获取openid&session_key
  37. public function code($code) {
  38. return $this->app->auth->session($code);
  39. }
  40. /**
  41. * 获取授权token网页授权
  42. *
  43. * @param string $code
  44. * @return mixed|string
  45. */
  46. public function getOfficialAccessToken($code = '')
  47. {
  48. $params = [
  49. 'appid' => $this->config['app_id'],
  50. 'secret' => $this->config['secret'],
  51. 'code' => $code,
  52. 'grant_type' => 'authorization_code'
  53. ];
  54. $ret = Http::sendRequest('https://api.weixin.qq.com/sns/oauth2/access_token', $params, 'GET');
  55. if ($ret['ret']) {
  56. $ar = json_decode($ret['msg'], true);
  57. return $ar;
  58. }
  59. return [];
  60. }
  61. public function oauth() {
  62. $oauth = $this->app->oauth;
  63. return $oauth;
  64. }
  65. //解密信息
  66. public function decryptData($session, $iv, $encryptData) {
  67. $data = $this->app->encryptor->decryptData($session, $iv, $encryptData);
  68. return $data;
  69. }
  70. public function unify($orderBody) {
  71. $result = $this->app->order->unify($orderBody);
  72. return $result;
  73. }
  74. public function bridgeConfig($prepayId) {
  75. $jssdk = $this->app->jssdk;
  76. $config = $jssdk->bridgeConfig($prepayId, false);
  77. return $config;
  78. }
  79. public function notify() {
  80. $result = $this->app;
  81. return $result;
  82. }
  83. //获取accessToken
  84. public function getAccessToken() {
  85. $accessToken = $this->app->access_token;
  86. $token = $accessToken->getToken(); // token 数组 token['access_token'] 字符串
  87. //$token = $accessToken->getToken(true); // 强制重新从微信服务器获取 token.
  88. return $token;
  89. }
  90. public function sendTemplateMessage($attributes) {
  91. extract($attributes);
  92. $this->app->template_message->send([
  93. 'touser' => $openId,
  94. 'template_id' => $templateId,
  95. 'page' => $page,
  96. 'form_id' => $formId,
  97. 'data' => $data,
  98. 'emphasis_keyword' => $emphasis_keyword
  99. ]);
  100. }
  101. // 同步小程序直播
  102. public function live(Array $params = []) {
  103. $default = [
  104. 'start' => 0,
  105. 'limit' => 10
  106. ];
  107. $params = array_merge($default, $params);
  108. $default = json_encode($params);
  109. $access_token = $this->app->access_token->getToken();
  110. $getRoomsListUrl = "https://api.weixin.qq.com/wxa/business/getliveinfo?access_token={$access_token['access_token']}";
  111. $headers = ['Content-type: application/json'];
  112. $options = [
  113. CURLOPT_HTTPHEADER => $headers
  114. ];
  115. $result = Http::sendRequest($getRoomsListUrl, $default, 'POST', $options);
  116. if (isset($result['ret']) && $result['ret']) {
  117. $msg = json_decode($result['msg'], true);
  118. $result = $msg;
  119. }
  120. $rooms = [];
  121. if ($result && $result['errcode'] == 0 && $result['errmsg'] === 'ok') {
  122. $rooms = $result['room_info'];
  123. }
  124. return $rooms;
  125. }
  126. // 小程序直播回放
  127. public function liveReplay(array $params = []) {
  128. $default = [
  129. 'room_id' => 0,
  130. 'start' => 0,
  131. 'limit' => 20
  132. ];
  133. $params = array_merge($default, $params);
  134. $default = json_encode($params);
  135. $access_token = $this->app->access_token->getToken();
  136. $getPlayBackListUrl = "https://api.weixin.qq.com/wxa/business/getliveinfo?access_token={$access_token['access_token']}";
  137. $headers = ['Content-type: application/json'];
  138. $options = [
  139. CURLOPT_HTTPHEADER => $headers
  140. ];
  141. $result = Http::sendRequest($getPlayBackListUrl, $default, 'POST', $options);
  142. if (isset($result['ret']) && $result['ret']) {
  143. $msg = json_decode($result['msg'], true);
  144. $result = $msg;
  145. }
  146. $liveReplay = [];
  147. if ($result && $result['errcode'] == 0 && $result['errmsg'] === 'ok') {
  148. $liveReplay = $result['live_replay'];
  149. }
  150. return $liveReplay;
  151. }
  152. public function menu($act = 'create', $buttons = '') {
  153. $result = $this->app->menu->$act($buttons);
  154. return $result;
  155. }
  156. //通过openid获取已经关注的用户信息
  157. public function getSubscribeUserInfoByOpenId(array $openIdsArray) {
  158. $result = $this->app->user->select($openIdsArray);
  159. return $result;
  160. }
  161. }