Auth.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace app\controller\mini;
  3. use app\extra\basic\Base;
  4. use app\extra\douyin\Client;
  5. use app\extra\douyin\Crypt;
  6. use app\extra\tools\CodeExtend;
  7. use app\model\saas\SaasMember;
  8. use app\model\saas\SaasOrderLife;
  9. use app\model\saas\SaasOrderLog;
  10. use app\model\saas\SaasStore;
  11. use LinFly\Annotation\Route\Controller;
  12. use LinFly\Annotation\Route\Route;
  13. use support\Request;
  14. use support\Response;
  15. use Shopwwi\WebmanAuth\Auth as AuthMode;
  16. #[Controller(prefix: "/api/dy/home")]
  17. class Auth extends Base
  18. {
  19. #[Route(path: "license",methods: "post")]
  20. public function getLicense(): Response
  21. {
  22. try {
  23. return success("ok",[
  24. "img" => "https://hx-mini.jsshuita.com.cn/license.jpg"
  25. ]);
  26. } catch (\Throwable $throwable) {
  27. return error($throwable->getMessage());
  28. }
  29. }
  30. /**
  31. * @param Request $request
  32. * @return Response
  33. */
  34. #[Route(path: "login",methods: "post")]
  35. public function setLogin(Request $request): Response
  36. {
  37. try {
  38. $param = $request->post();
  39. print_r($param);
  40. $sessionKey = (new Crypt)->config($this->getDyConfig())->getSessionKey($param['auth']);
  41. if (!isset($sessionKey['openid'])) return error("获取数据失败");
  42. $openId = $sessionKey['openid'];
  43. $userIn = [
  44. 'open_id' => $openId,
  45. "create_ip" => $request->getRealIp(),
  46. "nickname" => "用户_".strtoupper(CodeExtend::random(3,5)),
  47. "avatar" => "https://img.inmei.cc/logo.png"
  48. ];
  49. $user = (new SaasMember)->where("open_id",$openId)->findOrEmpty();
  50. $userIn['id'] = $user['id'];
  51. if ($user->isEmpty()) {
  52. $userIn['id'] = $user->insertGetId($userIn);
  53. }
  54. // 同步已经下单的订单
  55. if (!empty($param['account'])) {
  56. $this->asyncDyOrder($param['account'],$openId);
  57. }
  58. return successTrans("success.data",[
  59. "user" => get_object_vars((new AuthMode)->guard("member")->login($userIn)),
  60. "coupon" => (new SaasOrderLife)->with(['goods' => function($query){
  61. $query->field("out_id,product_img");
  62. },'store' => function($query){
  63. $query->field("store_id,service_mobile");
  64. }])->append(['expire_at'])->withAttr(['expire_at' => function($data,$resp){
  65. return date("Y-m-d",strtotime($resp['expire_at']));
  66. }])->where("open_id",$openId)->where("status",1)->select()
  67. ]);
  68. } catch (\Throwable $throwable) {
  69. echo getDateFull()."==授权登陆===".$throwable->getMessage()."\n";
  70. echo getDateFull()."==授权登陆===".$throwable->getFile()."\n";
  71. echo getDateFull()."==授权登陆===".$throwable->getLine()."\n";
  72. return error($throwable->getMessage());
  73. }
  74. }
  75. protected function decrypt2code($private_key, $ciphertext_str) {
  76. // 解码 base64 密文
  77. $ciphertext = base64_decode($ciphertext_str);
  78. // 使用私钥解密
  79. openssl_private_decrypt($ciphertext, $plaintext, $private_key, OPENSSL_PKCS1_PADDING);
  80. if ($plaintext === false) {
  81. return [];
  82. }
  83. return json_decode($plaintext,true);
  84. }
  85. protected function decrypt($encrypted_data, $session_key, $iv) {
  86. $data = base64_decode($encrypted_data);
  87. $key = base64_decode($session_key);
  88. $iv_decoded = base64_decode($iv);
  89. // 使用 AES-256-CBC 模式解密
  90. return openssl_decrypt($data, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv_decoded);
  91. }
  92. }