Ver código fonte

feat(直播列表接口):获取直播列表

geek 4 anos atrás
pai
commit
b31795202e

+ 23 - 21
application/expand/controller/Live.php

@@ -1,18 +1,27 @@
 <?php
 
+namespace app\expand\controller;
+
 use app\common\service\HelperService;
 use app\expand\controller\BaseAuth;
+use think\Cache;
 use think\Validate;
 
 /**
  * Author: luzheng.liu
  * Time: 2020/10/13 14:03
  */
-
 class Live extends BaseAuth {
 
+    public function __construct() {
+        parent::__construct();
 
 
+    }
+
+    /**
+     * 获取直播列表
+     */
     public function getLiveList() {
         $params = $this->_params;
         $rule = [
@@ -24,32 +33,25 @@ class Live extends BaseAuth {
         if (!$validate->check($params)) {
             HelperService::returnJson(['code' => '400', 'msg' => $validate->getError(), 'data' => []]);
         }
-        $appId = 'wx68df76aec541062d';
-        $appSecret = '415549b10c1ea7aaa62991ed1b26acc0';
-        /**
-         * $auth = {"access_token":"ACCESS_TOKEN","expires_in":7200}
-         */
-        $auth = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={{$appId}}&secret={{$appSecret}}
-        
-");
-        $auth = json_decode($auth, true);
-        if (empty($auth['access_token'])) {
-            HelperService::returnJson(['code' => 4000, 'data' => $auth, 'msg' => $auth['errmsg']]);
+        Cache::rm('access_token_' . $this->_apiCode);
+        $accessToken = Cache::get('access_token_' . $this->_apiCode);
+        if (empty($accessToken)) {
+            $wx = new WechatJs();
+            $accessToken = $wx->getWxToken();
+            Cache::set('access_token_' . $this->_apiCode, $accessToken, 300);
         }
-        $accessToken = $auth['access_token'];
         $data = [
-            "start" => 0,
-            'limit' => 10,
+            "start" => $params['start'] ?: 0,
+            'limit' => $params['limit'] ?: 10,
         ];
-        $rommList = HelperService::httpPost('https://api.weixin.qq.com/wxa/business/getliveinfo?access_token=
-
-' . $accessToken, json_encode($data), false, ['Content-Type: application/json; charset=utf-8']);
-        $roomList = json_decode($rommList, true);
-        if ($rommList['errcode'] === 1) {
+        $roomList = HelperService::httpPost('https://api.weixin.qq.com/wxa/business/getliveinfo?access_token=' . $accessToken, json_encode($data), false);
+        $roomList = json_decode($roomList, true);
+        if ($roomList['errcode'] === 1) {
             HelperService::returnJson(['code' => '200', 'msg' => '未创建直播间', 'data' => []]);
         }
         $room = [
-            'roomList' => $rommList['room_info'],
+            'roomList' => $roomList['room_info'],
+            'total'    => $roomList['total']
         ];
         HelperService::returnJson(['code' => '200', 'msg' => 'success', 'data' => $room]);
     }

+ 6 - 6
application/expand/controller/WechatJs.php

@@ -37,7 +37,7 @@ class WechatJs extends BaseAuth
             HelperService::returnJson(['code'=>200,'msg'=>'cache','data'=>"$cacheTicket"]);
         }
         
-        $token = $this->_getWxToken();
+        $token = $this->getWxToken();
         
         $requestTicketUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$token&type=jsapi";
         $ticketJson = HelperService::httpPost($requestTicketUrl,'',true);
@@ -64,7 +64,7 @@ class WechatJs extends BaseAuth
         if(!empty($cacheToken) && $this->_apiCode!="BAIXIONG"){//白熊
             HelperService::returnJson(['code'=>200,'msg'=>'cache','data'=>"$cacheToken"]);
         }
-        $token = $this->_getWxToken();
+        $token = $this->getWxToken();
         Cache::set('access_token_'.$this->_apiCode,$token,7200);
 
         HelperService::returnJson(['code'=>200,'msg'=>'curl','data'=>"$token"]);
@@ -72,15 +72,15 @@ class WechatJs extends BaseAuth
     
     /**
      * 获取微信的token
-     * @return type
+     * @return string
      */
-    private function _getWxToken(){
+    public function getWxToken(){
         
         $requestTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".Config::get('WECHAT_APPID')."&secret=".Config::get('WECHAT_APPSECRET');
         $tokenJson = HelperService::httpPost($requestTokenUrl,'',true);
 
         $tokenArr = @json_decode($tokenJson,true);
-        
+
         if(!isset($tokenArr['access_token'])){
             HelperService::returnJson(['code'=>'400','msg'=>'access_token error','data'=>$tokenJson]);
         }
@@ -105,7 +105,7 @@ class WechatJs extends BaseAuth
             HelperService::returnJson(['code'=>400,'msg'=>$validate->getError(),'data'=>[]]);
         }
         
-        $token = $this->_getWxToken();
+        $token = $this->getWxToken();
         $url = "https://api.weixin.qq.com/card/user/getcardlist?access_token=$token";
         $data = [
             "openid"=>$params['openid'],

+ 3 - 0
application/route.php

@@ -138,4 +138,7 @@ return [
     //会员信息缓存
     'v1/insertCache'=>'expand/Fcoupons/insertCache',
     'v1/getMemberCache'=>'expand/Fcoupons/getMemberCache',
+
+    //直播
+    'v1/live/liveList'=>'expand/Live/getLiveList',
 ];

+ 20 - 0
public/COMPANY_LIST/VKLIVE/auth.php

@@ -0,0 +1,20 @@
+<?php
+
+return [
+
+    'Xcx_pay_appId'=>'',//公众号appid
+    'Xcx_pay_sub_appId'=>'wx68df76aec541062d',//小程序appid
+    'Wechat_js_appId' => 'wx68df76aec541062d',
+    'Xcx_mchId'=>'',//主账户
+    'Xcx_sub_mchId'=>'',//子账户
+    'Xcx_pay_key'=>'',
+    'Xcx_secret'=>'415549b10c1ea7aaa62991ed1b26acc0',
+    'Wechat_js_appsecret' => '415549b10c1ea7aaa62991ed1b26acc0',
+    'Xcx_tip'=>'VKLIVE',
+    'Xcx_pay_notify_url'=>[
+        ''
+    ],
+
+    //验证参数
+    'signKey'=>'43F9279CE14DBF34257C08612CE51GF9',
+];