Ver Fonte

0813-0219-h01

Zory há 1 mês atrás
pai
commit
36738f5467
1 ficheiros alterados com 89 adições e 0 exclusões
  1. 89 0
      app/controller/v2/Auth.php

+ 89 - 0
app/controller/v2/Auth.php

@@ -0,0 +1,89 @@
+<?php
+
+namespace app\controller\v2;
+
+use app\extra\basic\Base;
+use app\extra\dyLife\Crypt;
+use app\middleware\AuthMiddleware;
+use app\model\saas\SaasUserOpen;
+use DI\Attribute\Inject;
+use LinFly\Annotation\Attributes\Route\Controller;
+use LinFly\Annotation\Attributes\Route\Middleware;
+use LinFly\Annotation\Attributes\Route\PostMapping;
+use Shopwwi\WebmanAuth\Facade\Auth as AuthFacade;
+use support\Request;
+use support\Response;
+
+
+#[Controller("/v2/auth"),Middleware(AuthMiddleware::class)]
+class Auth extends Base
+{
+
+    protected array $noNeedLogin = ["loginMobile"];
+
+    #[Inject]
+    protected SaasUserOpen $model;
+
+
+    /**
+     * 手机号码
+     * @param Request $request
+     * @return Response
+     */
+    #[PostMapping("mobile")]
+    public function loginMobile(Request $request): Response
+    {
+        try {
+            $param = $this->_valid([
+                "login.require"     => '获取登录参数失败',
+                "code.default"      => '',
+                "encryptedData.default"      => '',
+                "iv.default"        => '',
+            ],$request->method());
+            if (!is_array($param)) return error($param);
+            $sessionKey = (new Crypt)->config($this->getDyConfig())->getSessionKey($param['login']);
+            if (empty($sessionKey)) return error("请重新进入再试");
+            if (!empty($param['code'])) {
+                $mobileStr = (new Crypt)->config($this->getDyConfig())->token()->getMobile($param['code']);
+            } else {
+                $data = $this->decrypt($param['encryptedData'],$sessionKey['session_key'],$param['iv']);
+                $mobileStr = $data['purePhoneNumber'];
+            }
+            if (empty($mobileStr)) return error("授权信息获取失败");
+            $map = ["openid" => $sessionKey['openid']];
+            $user = $this->model->where($map)->findOrEmpty();
+            if ($user->isEmpty()) {
+                $map["create_ip"] = $request->getRealIp();
+                $user->insertGetId($map);
+            }
+            $userAuth = get_object_vars(AuthFacade::guard("user")->login(['openid' => $sessionKey['openid']]));
+            return success("ok",$userAuth);
+        } catch (\Throwable $th) {
+            return error($th->getMessage());
+        }
+    }
+
+
+    protected function decrypt2code($private_key, $ciphertext_str) {
+        // 解码 base64 密文
+        $ciphertext = base64_decode($ciphertext_str);
+
+        // 使用私钥解密
+        openssl_private_decrypt($ciphertext, $plaintext, $private_key, OPENSSL_PKCS1_PADDING);
+
+        if ($plaintext === false) {
+            return [];
+        }
+        return json_decode($plaintext,true);
+    }
+    protected function decrypt($encrypted_data, $session_key, $iv) {
+        $data = base64_decode($encrypted_data);
+        $key = base64_decode($session_key);
+        $iv_decoded = base64_decode($iv);
+        // 使用 AES-256-CBC 模式解密
+        $decrypted = openssl_decrypt($data, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv_decoded);
+
+        return json_decode($decrypted,true);
+    }
+
+}