xg il y a 2 ans
Parent
commit
6c72e381f3

+ 3 - 7
application/index/controller/Index.php

@@ -5,6 +5,7 @@ namespace app\index\controller;
 use app\common\controller\Frontend;
 use app\common\model\Mobile;
 use app\common\service\MobileComputer;
+use app\service\byte_dance\ByteDance;
 use app\service\id\IdVerify;
 use PhpOffice\PhpSpreadsheet\IOFactory;
 use think\helper\Str;
@@ -30,12 +31,7 @@ class Index extends Frontend
         }
         return file_get_contents($file);
     }
-    public function test(IdVerify $idVerify){
-
-        $newErrorExcel=IOFactory::createReader('Xlsx');
-        $newErrorExcelLoaded=$newErrorExcel->load('/www/wwwroot/lh.hdlkeji.com/beauti-no//public//uploads/20220630/a70e2feeef87e59f50d4e803c3c8c4f2_err.xlsx');
-        //sleep(1800);
-        //$idVerify->check('吴芷萱','350725201406294026');
-        //dd(MobileComputer::setMobile('13900086666')->filter());
+    public function test(){
+        dd(ByteDance::accessToken());
     }
 }

+ 45 - 0
application/service/byte_dance/ByteDance.php

@@ -0,0 +1,45 @@
+<?php
+namespace app\service\byte_dance;
+use GuzzleHttp\Client;
+
+class ByteDance{
+    public static function accessToken(){
+        return self::single(ByteDanceAccessToken::class)
+            ->setAppId(self::appId())
+            ->setAppSecret(self::appSecret())
+            ->get();
+    }
+
+    private static function single($class){
+        static $container=[];
+        if (!isset($container[$class])){
+            $container[$class]=new $class;
+        }
+        return $container[$class];
+    }
+
+    public static function appId(){
+        return config('site.byte_dance_mapp_appId');
+    }
+    public static function appSecret(){
+        return config('site.byte_dance_mapp_appSecret');
+    }
+    public static function httpGet($url){
+        $request=(new Client)
+            ->get($url);
+        return json_decode($request->getBody()->getContents(),true);
+    }
+    public static function httpPost($url,$params,$header=[]){
+        $request=(new Client)
+            ->request('post',$url,[
+                'json'=>$params,
+                'headers'=>$header
+            ]);
+        return json_decode($request->getBody()->getContents(),true);
+    }
+    public static function throwIf($condition,$msg){
+        if($condition){
+            throw new ByteDanceException($msg);
+        }
+    }
+}

+ 43 - 0
application/service/byte_dance/ByteDanceAccessToken.php

@@ -0,0 +1,43 @@
+<?php
+namespace app\service\byte_dance;
+
+use think\Cache;
+
+class ByteDanceAccessToken implements ByteDanceInterface {
+    protected $appId;
+    protected $appSecret;
+    protected $cacheName='cache_bytedance_access_token';
+    public function get()
+    {
+        $token=Cache::get($this->cacheName);
+        if(!$token){
+            $request=ByteDance::httpPost('https://developer.toutiao.com/api/apps/v2/token',[
+                'appid'=>$this->appId,
+                'secret'=>$this->appSecret,
+                'grant_type'=>'client_credential',
+            ]);
+            ByteDance::throwIf(!empty($request['err_no']),$request['err_tips']);
+            $token=$request['data']['access_token'];
+            Cache::set($this->cacheName,$token,$request['data']['expires_in']-10);
+        }
+        return $token;
+    }
+
+    /**
+     * @param mixed $appId
+     */
+    public function setAppId($appId)
+    {
+        $this->appId = $appId;
+        return $this;
+    }
+
+    /**
+     * @param mixed $appSecret
+     */
+    public function setAppSecret($appSecret)
+    {
+        $this->appSecret = $appSecret;
+        return $this;
+    }
+}

+ 10 - 0
application/service/byte_dance/ByteDanceException.php

@@ -0,0 +1,10 @@
+<?php
+namespace app\service\byte_dance;
+
+use GuzzleHttp\Client;
+use think\Cache;
+use think\Exception;
+
+class ByteDanceException extends Exception {
+
+}

+ 6 - 0
application/service/byte_dance/ByteDanceInterface.php

@@ -0,0 +1,6 @@
+<?php
+namespace app\service\byte_dance;
+
+interface ByteDanceInterface{
+    public function get();
+}