Auth.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. $sessionKey = (new Crypt)->config($this->getDyConfig())->getSessionKey($param['auth']);
  40. if (!isset($sessionKey['openid'])) return error("获取数据失败");
  41. $openId = $sessionKey['openid'];
  42. $userIn = [
  43. 'open_id' => $openId,
  44. "create_ip" => $request->getRealIp(),
  45. // "nickname" => "用户_".strtoupper(CodeExtend::random(3,5)),
  46. "avatar" => "https://img.inmei.cc/logo.png"
  47. ];
  48. $user = (new SaasMember)->where("open_id",$openId)->findOrEmpty();
  49. $userIn['id'] = $user['id'];
  50. if ($user->isEmpty()) {
  51. $userIn['id'] = $user->insertGetId($userIn);
  52. }
  53. // 同步已经下单的订单
  54. if (!empty($param['account_id'])) {
  55. $this->asyncDyOrder($param['account_id'],$openId);
  56. }
  57. return successTrans("success.data",[
  58. "user" => get_object_vars((new AuthMode)->guard("member")->login($userIn)),
  59. "coupon" => (new SaasOrderLife)->with(['goods' => function($query){
  60. $query->field("out_id,product_img");
  61. },'store' => function($query){
  62. $query->field("store_id,service_mobile");
  63. }])->append(['expire_at'])->withAttr(['expire_at' => function($data,$resp){
  64. return date("Y-m-d",strtotime($resp['expire_at']));
  65. }])->where("open_id",$openId)->where("status",1)->select()
  66. ]);
  67. } catch (\Throwable $throwable) {
  68. echo getDateFull()."==授权登陆===".$throwable->getMessage()."\n";
  69. echo getDateFull()."==授权登陆===".$throwable->getFile()."\n";
  70. echo getDateFull()."==授权登陆===".$throwable->getLine()."\n";
  71. return error($throwable->getMessage());
  72. }
  73. }
  74. protected function decrypt2code($private_key, $ciphertext_str) {
  75. // 解码 base64 密文
  76. $ciphertext = base64_decode($ciphertext_str);
  77. // 使用私钥解密
  78. openssl_private_decrypt($ciphertext, $plaintext, $private_key, OPENSSL_PKCS1_PADDING);
  79. if ($plaintext === false) {
  80. return [];
  81. }
  82. return json_decode($plaintext,true);
  83. }
  84. protected function decrypt($encrypted_data, $session_key, $iv) {
  85. $data = base64_decode($encrypted_data);
  86. $key = base64_decode($session_key);
  87. $iv_decoded = base64_decode($iv);
  88. // 使用 AES-256-CBC 模式解密
  89. return openssl_decrypt($data, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv_decoded);
  90. }
  91. }