Auth.php 3.6 KB

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