xieruidong il y a 2 ans
Parent
commit
4f450618eb

+ 10 - 1
application/api/controller/User.php

@@ -10,6 +10,7 @@ use app\common\model\Guest;
 use app\common\model\User as U;
 use app\common\model\UserSign;
 use app\common\service\ScoreSend;
+use app\service\byte_dance\ByteDanceCode2Session;
 use EasyWeChat\Kernel\Exceptions\DecryptException;
 use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
 use fast\Mini;
@@ -254,8 +255,16 @@ class User extends Api
     /**
      * 抖音小程序登陆
      * @ApiParams (name=code,description=code)
+     * @ApiParams (name=encryptedData,description=encryptedData)
+     * @ApiParams (name=iv,description=iv)
      */
-    public function dy_login(){
+    public function dy_login(ByteDanceCode2Session $code2Session){
+        $data=$this->_validate([
+            'code'=>['require'],
+            'encryptedData'=>['require'],
+            'iv'=>['require'],
+        ]);
+        $info=$code2Session->setCode($data['code'])->get();
 
     }
 }

+ 1 - 1
application/service/byte_dance/ByteDanceCode2Session.php

@@ -16,7 +16,7 @@ class ByteDanceCode2Session implements ByteDanceInterface {
         return ByteDance::httpPost('https://developer.toutiao.com/api/apps/v2/jscode2session',$this->params());
     }
 
-    public function params(){
+    protected function params(){
         return [
             'appid'=>ByteDance::appId(),
             'secret'=>ByteDance::appSecret(),

+ 42 - 0
application/service/byte_dance/ByteDanceDecrypt.php

@@ -0,0 +1,42 @@
+<?php
+namespace app\service\byte_dance;
+
+use think\Cache;
+
+class ByteDanceDecrypt implements ByteDanceInterface {
+    protected $session_key;
+    protected $encryptedData;
+    protected $iv;
+
+    public function setSessionKey($sessionKey)
+    {
+        $this->session_key = $sessionKey;
+        return $this;
+    }
+
+    /**
+     * @param mixed $encryptedData
+     */
+    public function setEncryptedData($encryptedData): void
+    {
+        $this->encryptedData = $encryptedData;
+    }
+
+    /**
+     * @param mixed $iv
+     */
+    public function setIv($iv): void
+    {
+        $this->iv = $iv;
+    }
+
+    public function get()
+    {
+        try {
+            $decrypted=openssl_decrypt($this->encryptedData,'AES-128-CBC',base64_decode($this->session_key),0,base64_decode($this->iv));
+            return json_decode($decrypted,true);
+        }catch (\Exception $e){
+            ByteDance::throwIf(1,"数据解密失败({$e->getMessage()})");
+        }
+    }
+}