Goods.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. $data = $this->model->where("goods_id",$param['goods'])->with(['poi' => function ($query) {
  32. $query->field("poi_id,poi_name,poi_address,longitude,latitude,service_mobile,start_at,end_at,status");
  33. },'skuSpecs'])->append(['otherImg'])->withAttr(['otherImg' => function ($qu,$resp) {
  34. $img = json_decode($resp['image_list'],true);
  35. return array_map(function ($v) {
  36. return $v['url'];
  37. }, $img);
  38. }])->findOrEmpty();
  39. if ($data->isEmpty()) return errorTrans("empty.data");
  40. if ($data['poi']['status'] <> 1) return error("该店铺已关闭,请联系管理员");
  41. if (!empty($data['content'])) {
  42. $data['content'] = filterHtmlTags($data['content'],[
  43. 'img' => [
  44. 'remove' => ['width', 'height'],
  45. 'style' => ['width' => '100%']
  46. ],
  47. 'p' => [
  48. 'style' => ['margin' => '0']
  49. ]
  50. ]);
  51. }
  52. return $this->encode("success",$data->toArray());
  53. } catch (\Throwable $th) {
  54. return error($th->getMessage());
  55. }
  56. }
  57. #[GetMapping("check")]
  58. public function checkGoodsUser (Request $request): Response
  59. {
  60. try {
  61. $param = $this->_valid([
  62. "poi.default" => ""
  63. ],$request->method());
  64. if (!is_array($param)) return error($param);
  65. $user = (new SaasUserOpen)->where(['openid' => $request->user['openid']])->findOrEmpty();
  66. if ($user->isEmpty()) return error("数据错误");
  67. $userState = 1;
  68. $isBuy = 1;
  69. if (empty($user['avatar'])) {
  70. $userState = 0;
  71. }
  72. if ($user['is_black'] == 1) {
  73. $isBuy = 0;
  74. }
  75. // 查询IP
  76. $userIp = $request->getRealIp();
  77. $ipRegion = (new IpSearch)->getIpArea($userIp);
  78. if (isset($ipRegion[2]) && !empty($param['poi'])) {
  79. $areaBlack = (new SaasOrderArea)->where(['poi_id' => $param['poi'],'name' => $ipRegion[2]])->findOrEmpty();
  80. if (!$areaBlack->isEmpty()) {
  81. $isBuy = 0;
  82. }
  83. }
  84. $msg = "当前门店无库存,请咨询客服";
  85. return success("ok",['user' => $userState,'buy' => $isBuy,'msg' => $msg]);
  86. } catch (\Throwable $throwable) {
  87. return error($throwable->getMessage());
  88. }
  89. }
  90. }