|
|
@@ -0,0 +1,86 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace app\controller\api;
|
|
|
+
|
|
|
+use app\extra\basic\Base;
|
|
|
+use app\extra\dyLife\Crypt;
|
|
|
+use app\extra\tools\CodeExtend;
|
|
|
+use app\middleware\AuthMiddleware;
|
|
|
+use app\model\saas\SaasUserOpen;
|
|
|
+use DI\Attribute\Inject;
|
|
|
+use LinFly\Annotation\Attributes\Route\Controller;
|
|
|
+use LinFly\Annotation\Attributes\Route\GetMapping;
|
|
|
+use LinFly\Annotation\Attributes\Route\Middleware;
|
|
|
+use LinFly\Annotation\Attributes\Route\PostMapping;
|
|
|
+use support\Request;
|
|
|
+use Shopwwi\WebmanAuth\Facade\Auth as AuthFacade;
|
|
|
+use support\Response;
|
|
|
+
|
|
|
+
|
|
|
+#[Controller("/dy/auth"),Middleware(AuthMiddleware::class)]
|
|
|
+class Auth extends Base
|
|
|
+{
|
|
|
+
|
|
|
+ protected array $noNeedLogin = ["log2mobile"];
|
|
|
+
|
|
|
+ #[Inject]
|
|
|
+ protected SaasUserOpen $model;
|
|
|
+
|
|
|
+ #[PostMapping("mobile")]
|
|
|
+ public function log2mobile(Request $request): Response
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $param = $request->post();
|
|
|
+ $sessionKey = (new Crypt)->config($this->getDyConfig())->getSessionKey($param['login']);
|
|
|
+ if (empty($sessionKey)) return error("授权登录失败");
|
|
|
+ $mobile = [];
|
|
|
+ if (!empty($param['code'])) {
|
|
|
+ $mobileStr = (new Crypt)->config($this->getDyConfig())->token()->getMobile($param['code']);
|
|
|
+ if (!empty($mobileStr)) {
|
|
|
+ $mobile = $this->decrypt2code(sConf('wechat.min_private_key'), $mobileStr);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $userInfo = [
|
|
|
+ "openid" => $sessionKey['openid'],
|
|
|
+ "nickname" => "DY-".strtoupper(CodeExtend::random(5,3)),
|
|
|
+ "create_ip" => $request->getRealIp()
|
|
|
+ ];
|
|
|
+ $map = ["openid" => $sessionKey['openid']];
|
|
|
+ if (!empty($mobile['purePhoneNumber'])) {
|
|
|
+ $userInfo['mobile'] = $mobile['purePhoneNumber'];
|
|
|
+ }
|
|
|
+ $user = $this->model->where($map)->findOrEmpty();
|
|
|
+ if ($user->isEmpty()) {
|
|
|
+ $user->insertGetId($userInfo);
|
|
|
+ }
|
|
|
+ $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-256-CBC', $key, OPENSSL_RAW_DATA, $iv_decoded);
|
|
|
+
|
|
|
+ return $decrypted;
|
|
|
+ }
|
|
|
+}
|