Home.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace app\controller\api;
  3. use app\extra\basic\Base;
  4. use app\extra\dyLife\Crypt;
  5. use app\extra\dyLife\data\BaseData;
  6. use app\extra\tools\UploadExtend;
  7. use app\middleware\AuthMiddleware;
  8. use app\model\saas\SaasGoods;
  9. use app\model\saas\SaasStore;
  10. use LinFly\Annotation\Attributes\Route\Controller;
  11. use LinFly\Annotation\Attributes\Route\GetMapping;
  12. use LinFly\Annotation\Attributes\Route\Middleware;
  13. use LinFly\Annotation\Attributes\Route\PostMapping;
  14. use support\Request;
  15. use support\Response;
  16. use Webman\RedisQueue\Redis;
  17. #[Controller("/dy/home"),Middleware(AuthMiddleware::class)]
  18. class Home extends Base
  19. {
  20. /**
  21. * @var array|string[]
  22. */
  23. protected array $noNeedLogin = ["getHomeData","getLicense","payBtnMobile"];
  24. #[GetMapping("data")]
  25. public function getHomeData(Request $request): Response
  26. {
  27. try {
  28. $param = $this->_valid([
  29. "store.require" => trans("empty.require")
  30. ],$request->method());
  31. if (!is_array($param)) return error($param);
  32. $banner = [
  33. [
  34. "cover" => "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/dc7fd761c2e2af9c340f20677f8f115d942f0bde.png"
  35. ]
  36. ];
  37. $store = (new SaasStore)->where("poi_id",$param['store'])->field("poi_name,start_at,end_at,longitude,latitude,service_mobile,poi_id")->findOrEmpty();
  38. $goods = (new SaasGoods)->where("poi_id",$param['store'])->where("status",1)->field("product_id,product_name,image_list,category,price,line_price,sale_stock,id")->limit(10)->select()->toArray();
  39. return success("ok",compact('banner','store','goods'));
  40. } catch (\Throwable $th) {
  41. return error($th->getMessage());
  42. }
  43. }
  44. #[GetMapping("license")]
  45. public function getLicense(): Response
  46. {
  47. try {
  48. return success("ok",['img' => sConf("service.license")]);
  49. } catch (\Throwable $throwable) {
  50. return error($throwable->getMessage());
  51. }
  52. }
  53. /**
  54. * 三级城市信息
  55. * @param Request $request
  56. * @return Response
  57. */
  58. #[GetMapping("city")]
  59. public function getCityJson(Request $request): Response
  60. {
  61. try {
  62. $data = json_decode(file_get_contents(base_path()."/city.json"),true);
  63. return successTrans("success.data",$data);
  64. } catch (\Throwable $throwable) {
  65. return error($throwable->getMessage());
  66. }
  67. }
  68. /**
  69. * 生服组件解密手机号码
  70. * @param Request $request
  71. * @return Response
  72. */
  73. #[PostMapping("mobile")]
  74. public function payBtnMobile(Request $request): Response
  75. {
  76. try {
  77. $param = $request->all();
  78. $mobile = [];
  79. if (!empty($param['code'])) {
  80. $sessionKey = (new Crypt)->config($this->getDyConfig())->token()->getSessionKey($param['code']);
  81. if (!empty($sessionKey)) {
  82. $mobileStr = $this->decrypt2code($param['encryptedData'], $sessionKey['session_key'],$param['iv']);
  83. if (!empty($mobileStr)) $mobile = json_decode($mobileStr,true);
  84. }
  85. }
  86. if (empty($mobile)) return error("获取失败");
  87. return successTrans("success.data",['mobile' => $mobile['purePhoneNumber']]);
  88. } catch (\Throwable $throwable) {
  89. return error($throwable->getMessage());
  90. }
  91. }
  92. /**
  93. * 解密微信用户手机号等加密数据
  94. *
  95. * @param string $encrypted_data Base64 编码的密文
  96. * @param string $session_key Base64 编码的会话密钥
  97. * @param string $iv Base64 编码的初始向量
  98. * @return string 解密后的原始字符串(通常为 JSON)
  99. * @throws Exception
  100. */
  101. protected function decrypt2code($encrypted_data, $session_key, $iv)
  102. {
  103. // 1. Base64 解码
  104. $data = base64_decode($encrypted_data, true);
  105. $key = base64_decode($session_key, true);
  106. $iv = base64_decode($iv, true);
  107. if ($data === false || $key === false || $iv === false) {
  108. throw new \Exception('Base64 解码失败');
  109. }
  110. // 2. 根据密钥长度选择 AES 算法
  111. $key_len = strlen($key);
  112. switch ($key_len) {
  113. case 16:
  114. $method = 'AES-128-CBC';
  115. break;
  116. case 24:
  117. $method = 'AES-192-CBC';
  118. break;
  119. case 32:
  120. $method = 'AES-256-CBC';
  121. break;
  122. default:
  123. throw new \Exception("无效的密钥长度: {$key_len} 字节");
  124. }
  125. // 3. 解密(OPENSSL_RAW_DATA 表示返回二进制数据,不进行 Base64 编码)
  126. $decrypted = openssl_decrypt($data, $method, $key, OPENSSL_RAW_DATA, $iv);
  127. if ($decrypted === false) {
  128. throw new \Exception('解密失败: ' . openssl_error_string());
  129. }
  130. return $decrypted;
  131. }
  132. }