Auth.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace app\controller\v2;
  3. use app\extra\basic\Base;
  4. use app\extra\dyLife\Crypt;
  5. use app\middleware\AuthMiddleware;
  6. use app\model\saas\SaasUserOpen;
  7. use DI\Attribute\Inject;
  8. use LinFly\Annotation\Attributes\Route\Controller;
  9. use LinFly\Annotation\Attributes\Route\Middleware;
  10. use LinFly\Annotation\Attributes\Route\PostMapping;
  11. use Shopwwi\WebmanAuth\Facade\Auth as AuthFacade;
  12. use support\Request;
  13. use support\Response;
  14. #[Controller("/v2/auth"),Middleware(AuthMiddleware::class)]
  15. class Auth extends Base
  16. {
  17. protected array $noNeedLogin = ["loginMobile"];
  18. #[Inject]
  19. protected SaasUserOpen $model;
  20. /**
  21. * 手机号码
  22. * @param Request $request
  23. * @return Response
  24. */
  25. #[PostMapping("mobile")]
  26. public function loginMobile(Request $request): Response
  27. {
  28. try {
  29. $param = $this->_valid([
  30. "login.require" => '获取登录参数失败',
  31. "code.default" => '',
  32. "encryptedData.default" => '',
  33. "iv.default" => '',
  34. ],$request->method());
  35. if (!is_array($param)) return error($param);
  36. $sessionKey = (new Crypt)->config($this->getDyConfig())->getSessionKey($param['login']);
  37. if (empty($sessionKey)) return error("请重新进入再试");
  38. if (!empty($param['code'])) {
  39. $mobileStr = (new Crypt)->config($this->getDyConfig())->token()->getMobile($param['code']);
  40. } else {
  41. $data = $this->decrypt($param['encryptedData'],$sessionKey['session_key'],$param['iv']);
  42. $mobileStr = $data['purePhoneNumber'];
  43. }
  44. if (empty($mobileStr)) return error("授权信息获取失败");
  45. $map = ["openid" => $sessionKey['openid']];
  46. $user = $this->model->where($map)->findOrEmpty();
  47. if ($user->isEmpty()) {
  48. $map["create_ip"] = $request->getRealIp();
  49. $user->insertGetId($map);
  50. }
  51. $userAuth = get_object_vars(AuthFacade::guard("user")->login(['openid' => $sessionKey['openid']]));
  52. return success("ok",$userAuth);
  53. } catch (\Throwable $th) {
  54. return error($th->getMessage());
  55. }
  56. }
  57. protected function decrypt2code($private_key, $ciphertext_str) {
  58. // 解码 base64 密文
  59. $ciphertext = base64_decode($ciphertext_str);
  60. // 使用私钥解密
  61. openssl_private_decrypt($ciphertext, $plaintext, $private_key, OPENSSL_PKCS1_PADDING);
  62. if ($plaintext === false) {
  63. return [];
  64. }
  65. return json_decode($plaintext,true);
  66. }
  67. protected function decrypt($encrypted_data, $session_key, $iv) {
  68. $data = base64_decode($encrypted_data);
  69. $key = base64_decode($session_key);
  70. $iv_decoded = base64_decode($iv);
  71. // 使用 AES-256-CBC 模式解密
  72. $decrypted = openssl_decrypt($data, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv_decoded);
  73. return json_decode($decrypted,true);
  74. }
  75. }