Home.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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","getMoreGoods"];
  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,poi_city,poi_address,poi_logo")->findOrEmpty();
  38. if (!$store->isEmpty()) {
  39. $store['poi_logo'] = empty($store['poi_logo']) ? sConf("service.logo") : $store['poi_logo'];
  40. }
  41. $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)->order('id','desc')->select()->toArray();
  42. return success("ok",compact('banner','store','goods'));
  43. } catch (\Throwable $th) {
  44. return error($th->getMessage());
  45. }
  46. }
  47. #[GetMapping("goods")]
  48. public function getMoreGoods(Request $request): Response
  49. {
  50. try {
  51. $param = $this->_valid([
  52. "store.require" => trans("empty.require"),
  53. "page.default" => 1,
  54. "size.default" => 10
  55. ],$request->method());
  56. if (!is_array($param)) return error($param);
  57. $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)->order('id','desc')->paginate([
  58. "list_rows" => $param['size'],
  59. "page" => $param['page']
  60. ]);
  61. return successTrans(100010,pageFormat($goods),200);
  62. } catch (\Throwable $throwable) {
  63. return error($throwable->getMessage());
  64. }
  65. }
  66. #[GetMapping("license")]
  67. public function getLicense(): Response
  68. {
  69. try {
  70. return success("ok",['img' => sConf("service.license")]);
  71. } catch (\Throwable $throwable) {
  72. return error($throwable->getMessage());
  73. }
  74. }
  75. /**
  76. * 三级城市信息
  77. * @param Request $request
  78. * @return Response
  79. */
  80. #[GetMapping("city")]
  81. public function getCityJson(Request $request): Response
  82. {
  83. try {
  84. $data = json_decode(file_get_contents(base_path()."/city.json"),true);
  85. return successTrans("success.data",$data);
  86. } catch (\Throwable $throwable) {
  87. return error($throwable->getMessage());
  88. }
  89. }
  90. /**
  91. * 生服组件解密手机号码
  92. * @param Request $request
  93. * @return Response
  94. */
  95. #[PostMapping("mobile")]
  96. public function payBtnMobile(Request $request): Response
  97. {
  98. try {
  99. $param = $request->all();
  100. $mobile = [];
  101. if (!empty($param['code'])) {
  102. $sessionKey = (new Crypt)->config($this->getDyConfig())->token()->getSessionKey($param['code']);
  103. if (!empty($sessionKey)) {
  104. $mobileStr = $this->decrypt2code($param['encryptedData'], $sessionKey['session_key'],$param['iv']);
  105. if (!empty($mobileStr)) $mobile = json_decode($mobileStr,true);
  106. }
  107. }
  108. if (empty($mobile)) return error("获取失败");
  109. return successTrans("success.data",['mobile' => $mobile['purePhoneNumber']]);
  110. } catch (\Throwable $throwable) {
  111. return error($throwable->getMessage());
  112. }
  113. }
  114. /**
  115. * 解密微信用户手机号等加密数据
  116. *
  117. * @param string $encrypted_data Base64 编码的密文
  118. * @param string $session_key Base64 编码的会话密钥
  119. * @param string $iv Base64 编码的初始向量
  120. * @return string 解密后的原始字符串(通常为 JSON)
  121. * @throws Exception
  122. */
  123. protected function decrypt2code($encrypted_data, $session_key, $iv)
  124. {
  125. // 1. Base64 解码
  126. $data = base64_decode($encrypted_data, true);
  127. $key = base64_decode($session_key, true);
  128. $iv = base64_decode($iv, true);
  129. if ($data === false || $key === false || $iv === false) {
  130. throw new \Exception('Base64 解码失败');
  131. }
  132. // 2. 根据密钥长度选择 AES 算法
  133. $key_len = strlen($key);
  134. switch ($key_len) {
  135. case 16:
  136. $method = 'AES-128-CBC';
  137. break;
  138. case 24:
  139. $method = 'AES-192-CBC';
  140. break;
  141. case 32:
  142. $method = 'AES-256-CBC';
  143. break;
  144. default:
  145. throw new \Exception("无效的密钥长度: {$key_len} 字节");
  146. }
  147. // 3. 解密(OPENSSL_RAW_DATA 表示返回二进制数据,不进行 Base64 编码)
  148. $decrypted = openssl_decrypt($data, $method, $key, OPENSSL_RAW_DATA, $iv);
  149. if ($decrypted === false) {
  150. throw new \Exception('解密失败: ' . openssl_error_string());
  151. }
  152. return $decrypted;
  153. }
  154. }