Auth.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\controller\api;
  3. use app\extra\basic\Base;
  4. use app\extra\dyLife\Crypt;
  5. use app\extra\tools\CodeExtend;
  6. use app\middleware\AuthMiddleware;
  7. use app\model\saas\SaasUserOpen;
  8. use DI\Attribute\Inject;
  9. use LinFly\Annotation\Attributes\Route\Controller;
  10. use LinFly\Annotation\Attributes\Route\GetMapping;
  11. use LinFly\Annotation\Attributes\Route\Middleware;
  12. use LinFly\Annotation\Attributes\Route\PostMapping;
  13. use support\Request;
  14. use Shopwwi\WebmanAuth\Facade\Auth as AuthFacade;
  15. use support\Response;
  16. #[Controller("/dy/auth"),Middleware(AuthMiddleware::class)]
  17. class Auth extends Base
  18. {
  19. protected array $noNeedLogin = ["log2mobile","log2nickname"];
  20. #[Inject]
  21. protected SaasUserOpen $model;
  22. #[PostMapping("nickname")]
  23. public function log2nickname(Request $request): Response
  24. {
  25. try {
  26. $param = $request->post();
  27. $sessionKey = (new Crypt)->config($this->getDyConfig())->getSessionKey($param['login']);
  28. if (empty($sessionKey)) return error("授权登录失败");
  29. $userInfo = [
  30. "openid" => $sessionKey['openid'],
  31. "nickname" => $param['nickname']??'',
  32. "avatar" => $param['avatar']??'',
  33. "create_ip" => $request->getRealIp()
  34. ];
  35. $map = ["openid" => $sessionKey['openid']];
  36. $user = $this->model->where($map)->findOrEmpty();
  37. if ($user->isEmpty()) {
  38. $user->insertGetId($userInfo);
  39. } else {
  40. $user->nickname = $param['nickname']??'';
  41. $user->avatar = $param['avatar']??'';
  42. $user->save();
  43. }
  44. $userAuth = get_object_vars(AuthFacade::guard("user")->login(['openid' => $sessionKey['openid']]));
  45. return success("ok",$userAuth);
  46. } catch (\Throwable $th) {
  47. return error($th->getMessage());
  48. }
  49. }
  50. #[PostMapping("mobile")]
  51. public function log2mobile(Request $request): Response
  52. {
  53. try {
  54. $param = $request->post();
  55. $sessionKey = (new Crypt)->config($this->getDyConfig())->getSessionKey($param['login']);
  56. if (empty($sessionKey)) return error("授权登录失败");
  57. $mobile = [];
  58. if (!empty($param['code'])) {
  59. $mobileStr = (new Crypt)->config($this->getDyConfig())->token()->getMobile($param['code']);
  60. if (!empty($mobileStr)) {
  61. $mobile = $this->decrypt2code(sConf('wechat.min_private_key'), $mobileStr);
  62. }
  63. }
  64. $userInfo = [
  65. "openid" => $sessionKey['openid'],
  66. "nickname" => "DY-".strtoupper(CodeExtend::random(5,3)),
  67. "create_ip" => $request->getRealIp()
  68. ];
  69. $map = ["openid" => $sessionKey['openid']];
  70. if (!empty($mobile['purePhoneNumber'])) {
  71. $userInfo['mobile'] = $mobile['purePhoneNumber'];
  72. }
  73. $user = $this->model->where($map)->findOrEmpty();
  74. if ($user->isEmpty()) {
  75. $user->insertGetId($userInfo);
  76. }
  77. $userAuth = get_object_vars(AuthFacade::guard("user")->login(['openid' => $sessionKey['openid']]));
  78. return success("ok",$userAuth);
  79. } catch (\Throwable $th) {
  80. return error($th->getMessage());
  81. }
  82. }
  83. protected function decrypt2code($private_key, $ciphertext_str) {
  84. // 解码 base64 密文
  85. $ciphertext = base64_decode($ciphertext_str);
  86. // 使用私钥解密
  87. openssl_private_decrypt($ciphertext, $plaintext, $private_key, OPENSSL_PKCS1_PADDING);
  88. if ($plaintext === false) {
  89. return [];
  90. }
  91. return json_decode($plaintext,true);
  92. }
  93. protected function decrypt($encrypted_data, $session_key, $iv) {
  94. $data = base64_decode($encrypted_data);
  95. $key = base64_decode($session_key);
  96. $iv_decoded = base64_decode($iv);
  97. // 使用 AES-256-CBC 模式解密
  98. $decrypted = openssl_decrypt($data, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv_decoded);
  99. return $decrypted;
  100. }
  101. }