Auth.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 app\model\saas\SaasMember;
  7. use app\model\saas\SaasOrderLife;
  8. use LinFly\Annotation\Route\Controller;
  9. use LinFly\Annotation\Route\Route;
  10. use support\Request;
  11. use support\Response;
  12. use Shopwwi\WebmanAuth\Auth as AuthMode;
  13. #[Controller(prefix: "/api/dy/home")]
  14. class Auth extends Base
  15. {
  16. #[Route(path: "license",methods: "post")]
  17. public function getLicense(): Response
  18. {
  19. try {
  20. return success("ok",[
  21. "img" => "https://hx-mini.jsshuita.com.cn/license.jpg"
  22. ]);
  23. } catch (\Throwable $throwable) {
  24. return error($throwable->getMessage());
  25. }
  26. }
  27. /**
  28. * @param Request $request
  29. * @return Response
  30. */
  31. #[Route(path: "login",methods: "post")]
  32. public function setLogin(Request $request): Response
  33. {
  34. try {
  35. $param = $request->post();
  36. $sessionKey = (new Crypt)->config($this->getDyConfig())->getSessionKey($param['code']);
  37. if (!isset($sessionKey['openid'])) return error("获取数据失败");
  38. $openId = $sessionKey['openid'];
  39. $userIn = [
  40. 'open_id' => $openId,
  41. "create_ip" => $request->getRealIp(),
  42. "nickname" => "用户_".strtoupper(CodeExtend::random(3,5)),
  43. "avatar" => "https://img.inmei.cc/logo.png"
  44. ];
  45. $user = (new SaasMember)->where("open_id",$openId)->findOrEmpty();
  46. $userIn['id'] = $user['id'];
  47. if ($user->isEmpty()) {
  48. $userIn['id'] = $user->insertGetId($userIn);
  49. }
  50. return successTrans("success.data",[
  51. "user" => get_object_vars((new AuthMode)->guard("member")->login($userIn)),
  52. "coupon" => (new SaasOrderLife)->where("open_id",$openId)->where("status",1)->select()
  53. ]);
  54. } catch (\Throwable $throwable) {
  55. echo getDateFull()."==授权登陆===".$throwable->getMessage()."\n";
  56. echo getDateFull()."==授权登陆===".$throwable->getFile()."\n";
  57. echo getDateFull()."==授权登陆===".$throwable->getLine()."\n";
  58. return error($throwable->getMessage());
  59. }
  60. }
  61. protected function decrypt2code($private_key, $ciphertext_str) {
  62. // 解码 base64 密文
  63. $ciphertext = base64_decode($ciphertext_str);
  64. // 使用私钥解密
  65. openssl_private_decrypt($ciphertext, $plaintext, $private_key, OPENSSL_PKCS1_PADDING);
  66. if ($plaintext === false) {
  67. return [];
  68. }
  69. return json_decode($plaintext,true);
  70. }
  71. protected function decrypt($encrypted_data, $session_key, $iv) {
  72. $data = base64_decode($encrypted_data);
  73. $key = base64_decode($session_key);
  74. $iv_decoded = base64_decode($iv);
  75. // 使用 AES-256-CBC 模式解密
  76. return openssl_decrypt($data, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv_decoded);
  77. }
  78. }