Goods.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace app\controller\v2;
  3. use app\extra\basic\Base;
  4. use app\extra\ip2region\IpSearch;
  5. use app\middleware\AuthMiddleware;
  6. use app\model\saas\SaasGoods;
  7. use app\model\saas\SaasOrderArea;
  8. use app\model\saas\SaasUserOpen;
  9. use DI\Attribute\Inject;
  10. use LinFly\Annotation\Attributes\Route\Controller;
  11. use LinFly\Annotation\Attributes\Route\GetMapping;
  12. use LinFly\Annotation\Attributes\Route\Middleware;
  13. use support\Request;
  14. use support\Response;
  15. #[Controller("/v2/goods"),Middleware(AuthMiddleware::class)]
  16. class Goods extends Base
  17. {
  18. #[Inject]
  19. protected SaasGoods $model;
  20. protected array $noNeedLogin = ["getGoodsData"];
  21. #[GetMapping("data")]
  22. public function getGoodsData(Request $request): Response
  23. {
  24. try {
  25. $param = $this->_valid([
  26. "goods.require" => trans("empty.require"),
  27. "spm.default" => "",
  28. "option.default" => ""
  29. ],$request->method());
  30. if (!is_array($param)) return error($param);
  31. $where = [];
  32. if (is_numeric($param['goods'])) {
  33. $where['goods_id'] = $param['goods'];
  34. } else {
  35. $where['product_id'] = $param['goods'];
  36. }
  37. $data = $this->model->where($where)->with(['poi' => function ($query) {
  38. $query->field("poi_id,poi_name,poi_address,longitude,latitude,service_mobile,start_at,end_at,status");
  39. },'skuSpecs'])->append(['otherImg'])->withAttr(['otherImg' => function ($qu,$resp) {
  40. $img = json_decode($resp['image_list'],true);
  41. return array_map(function ($v) {
  42. return $v['url'];
  43. }, $img);
  44. }])->findOrEmpty();
  45. if ($data->isEmpty()) return errorTrans("empty.data");
  46. if ($data['poi']['status'] <> 1) return error("该店铺已关闭,请联系管理员");
  47. if (!empty($data['content'])) {
  48. $data['content'] = filterHtmlTags($data['content'],[
  49. 'img' => [
  50. 'remove' => ['width', 'height'],
  51. 'style' => ['width' => '100%']
  52. ],
  53. 'p' => [
  54. 'style' => ['margin' => '0']
  55. ]
  56. ]);
  57. }
  58. return $this->encode("success",$data->toArray());
  59. } catch (\Throwable $th) {
  60. return error($th->getMessage());
  61. }
  62. }
  63. #[GetMapping("check")]
  64. public function checkGoodsUser (Request $request): Response
  65. {
  66. try {
  67. $param = $this->_valid([
  68. "poi.default" => ""
  69. ],$request->method());
  70. if (!is_array($param)) return error($param);
  71. $user = (new SaasUserOpen)->where(['openid' => $request->user['openid']])->findOrEmpty();
  72. if ($user->isEmpty()) return error("数据错误");
  73. $userState = 1;
  74. $isBuy = 1;
  75. if (empty($user['avatar'])) {
  76. $userState = 0;
  77. }
  78. if ($user['is_black'] == 1) {
  79. $isBuy = 0;
  80. }
  81. // 查询IP
  82. $userIp = $request->getRealIp();
  83. $ipRegion = (new IpSearch)->getIpArea($userIp);
  84. if (isset($ipRegion[2]) && !empty($param['poi'])) {
  85. $areaBlack = (new SaasOrderArea)->where(['poi_id' => $param['poi'],'name' => $ipRegion[2]])->findOrEmpty();
  86. if (!$areaBlack->isEmpty()) {
  87. $isBuy = 0;
  88. }
  89. }
  90. $msg = "当前门店无库存,请咨询客服";
  91. return success("ok",['user' => $userState,'buy' => $isBuy,'msg' => $msg]);
  92. } catch (\Throwable $throwable) {
  93. return error($throwable->getMessage());
  94. }
  95. }
  96. }