Auth.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace app\controller\mini;
  3. use app\extra\basic\Base;
  4. use app\extra\douyin\Crypt;
  5. use app\extra\tools\CodeExtend;
  6. use LinFly\Annotation\Route\Controller;
  7. use LinFly\Annotation\Route\Route;
  8. use support\Request;
  9. use support\Response;
  10. #[Controller(prefix: "/api/dy/home")]
  11. class Auth extends Base
  12. {
  13. /**
  14. * @param Request $request
  15. * @return Response
  16. */
  17. #[Route(path: "login",methods: "post")]
  18. public function setLogin(Request $request): Response
  19. {
  20. try {
  21. $param = $request->post();
  22. $config = sConf("dy.");
  23. $sessionKey = (new Crypt)->config(['appid' => $config['appid'],'secret' => $config['secret']])->getSessionKey($param['code']);
  24. if (empty($sessionKey)) return error("授权登录失败");
  25. if (!isset($sessionKey['openid'])) return error("获取数据失败");
  26. $openId = $sessionKey['openid'];
  27. $userIn = [
  28. 'openid' => $openId,
  29. "create_ip" => $request->getRealIp(),
  30. "nickname" => "用户_".strtoupper(CodeExtend::random(3,5)),
  31. "avatar" => "https://img.inmei.cc/logo.png"
  32. ];
  33. print_r($sessionKey);
  34. print_r($userIn);
  35. return error("err");
  36. } catch (\Throwable $throwable) {
  37. return error($throwable->getMessage());
  38. }
  39. }
  40. protected function decrypt2code($private_key, $ciphertext_str) {
  41. // 解码 base64 密文
  42. $ciphertext = base64_decode($ciphertext_str);
  43. // 使用私钥解密
  44. openssl_private_decrypt($ciphertext, $plaintext, $private_key, OPENSSL_PKCS1_PADDING);
  45. if ($plaintext === false) {
  46. return [];
  47. }
  48. return json_decode($plaintext,true);
  49. }
  50. protected function decrypt($encrypted_data, $session_key, $iv) {
  51. $data = base64_decode($encrypted_data);
  52. $key = base64_decode($session_key);
  53. $iv_decoded = base64_decode($iv);
  54. // 使用 AES-256-CBC 模式解密
  55. $decrypted = openssl_decrypt($data, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv_decoded);
  56. return $decrypted;
  57. }
  58. }