123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- namespace addons\qingdongams\library;
- use addons\qingdongams\model\AdminConfig;
- use EasyWeChat\Factory;
- use think\Model;
- use fast\Http;
- /**
- *
- */
- class Wechat extends Model {
- public $config;
- protected $app;
- public function __construct($platform) {
- switch ($platform) {
- case 'wxOfficialAccount':
- $this->config = [
- 'app_id' => AdminConfig::getConfigValue('appid', 'wechat'),
- 'secret' => AdminConfig::getConfigValue('secret', 'wechat')
- ];
- $this->app = Factory::officialAccount($this->config);
- break;
- case 'wxMiniProgram':
- $this->config = [
- 'app_id' => AdminConfig::getConfigValue('mini_appid', 'wechat'),
- 'secret' => AdminConfig::getConfigValue('mini_secret', 'wechat')
- ];
- $this->app = Factory::miniProgram($this->config);
- break;
- }
- return parent::__construct();
- }
- // 返回实例
- public function getApp() {
- return $this->app;
- }
- //小程序:获取openid&session_key
- public function code($code) {
- return $this->app->auth->session($code);
- }
- /**
- * 获取授权token网页授权
- *
- * @param string $code
- * @return mixed|string
- */
- public function getOfficialAccessToken($code = '')
- {
- $params = [
- 'appid' => $this->config['app_id'],
- 'secret' => $this->config['secret'],
- 'code' => $code,
- 'grant_type' => 'authorization_code'
- ];
- $ret = Http::sendRequest('https://api.weixin.qq.com/sns/oauth2/access_token', $params, 'GET');
- if ($ret['ret']) {
- $ar = json_decode($ret['msg'], true);
- return $ar;
- }
- return [];
- }
- public function oauth() {
- $oauth = $this->app->oauth;
- return $oauth;
- }
- //解密信息
- public function decryptData($session, $iv, $encryptData) {
- $data = $this->app->encryptor->decryptData($session, $iv, $encryptData);
- return $data;
- }
- public function unify($orderBody) {
- $result = $this->app->order->unify($orderBody);
- return $result;
- }
- public function bridgeConfig($prepayId) {
- $jssdk = $this->app->jssdk;
- $config = $jssdk->bridgeConfig($prepayId, false);
- return $config;
- }
- public function notify() {
- $result = $this->app;
- return $result;
- }
- //获取accessToken
- public function getAccessToken() {
- $accessToken = $this->app->access_token;
- $token = $accessToken->getToken(); // token 数组 token['access_token'] 字符串
- //$token = $accessToken->getToken(true); // 强制重新从微信服务器获取 token.
- return $token;
- }
- public function sendTemplateMessage($attributes) {
- extract($attributes);
- $this->app->template_message->send([
- 'touser' => $openId,
- 'template_id' => $templateId,
- 'page' => $page,
- 'form_id' => $formId,
- 'data' => $data,
- 'emphasis_keyword' => $emphasis_keyword
- ]);
- }
- // 同步小程序直播
- public function live(Array $params = []) {
- $default = [
- 'start' => 0,
- 'limit' => 10
- ];
- $params = array_merge($default, $params);
- $default = json_encode($params);
- $access_token = $this->app->access_token->getToken();
- $getRoomsListUrl = "https://api.weixin.qq.com/wxa/business/getliveinfo?access_token={$access_token['access_token']}";
- $headers = ['Content-type: application/json'];
- $options = [
- CURLOPT_HTTPHEADER => $headers
- ];
- $result = Http::sendRequest($getRoomsListUrl, $default, 'POST', $options);
- if (isset($result['ret']) && $result['ret']) {
- $msg = json_decode($result['msg'], true);
- $result = $msg;
- }
- $rooms = [];
- if ($result && $result['errcode'] == 0 && $result['errmsg'] === 'ok') {
- $rooms = $result['room_info'];
- }
- return $rooms;
- }
- // 小程序直播回放
- public function liveReplay(array $params = []) {
- $default = [
- 'room_id' => 0,
- 'start' => 0,
- 'limit' => 20
- ];
- $params = array_merge($default, $params);
- $default = json_encode($params);
- $access_token = $this->app->access_token->getToken();
- $getPlayBackListUrl = "https://api.weixin.qq.com/wxa/business/getliveinfo?access_token={$access_token['access_token']}";
- $headers = ['Content-type: application/json'];
- $options = [
- CURLOPT_HTTPHEADER => $headers
- ];
- $result = Http::sendRequest($getPlayBackListUrl, $default, 'POST', $options);
- if (isset($result['ret']) && $result['ret']) {
- $msg = json_decode($result['msg'], true);
- $result = $msg;
- }
- $liveReplay = [];
- if ($result && $result['errcode'] == 0 && $result['errmsg'] === 'ok') {
- $liveReplay = $result['live_replay'];
- }
- return $liveReplay;
- }
- public function menu($act = 'create', $buttons = '') {
- $result = $this->app->menu->$act($buttons);
- return $result;
- }
- //通过openid获取已经关注的用户信息
- public function getSubscribeUserInfoByOpenId(array $openIdsArray) {
- $result = $this->app->user->select($openIdsArray);
- return $result;
- }
- }
|