OfflineShop.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\OfflineShopModel;
  5. use app\common\model\OfflineTypeModel;
  6. use app\utils\DataUtil;
  7. use think\Db;
  8. /**
  9. * 示例接口
  10. */
  11. class OfflineShop extends Api
  12. {
  13. protected $noNeedLogin = [''];
  14. protected $noNeedRight = ['*'];
  15. // 商圈分类
  16. public function type()
  17. {
  18. $params = $this->request->param();
  19. $model = new OfflineTypeModel();
  20. $model->setSelect(['id','pid','name','image']);
  21. $list = $model->getList(params: $params,orderBy: ['weigh' => 'desc','id' => 'desc']);
  22. return $this->success('success',DataUtil::recursion($list,'id','pid'));
  23. }
  24. // 商铺列表
  25. public function list()
  26. {
  27. $params = $this->request->param();
  28. $userLng = $this->auth->lng;
  29. $userLat = $this->auth->lat;
  30. $model = new OfflineShopModel();
  31. $select = ['id', 'type_id', 'type_ids', 'name', 'image', 'lng', 'lat', 'address', 'telephone', 'star', 'sales'];
  32. $orderBy = [];
  33. if (!empty($userLng) && !empty($userLat)) {
  34. $select[] = Db::raw("(st_distance(point ({$userLng}, {$userLat}),point(lng,lat))*111195) as distance");
  35. !empty($params['distance']) && $params['distance'] = $params['distance'] * 1000;
  36. }
  37. if (!empty($this->auth->city)) {
  38. $params['city'] = $this->auth->city;
  39. }
  40. if (isset($params['rank_type'])) {
  41. switch ($params['rank_type']) {
  42. case 1:
  43. $orderBy['star'] = 'desc';
  44. break;
  45. case 2:
  46. $orderBy['sales'] = 'desc';
  47. break;
  48. case 3:
  49. if (!empty($userLng) && !empty($userLat)) {
  50. $orderBy['distance'] = 'asc';
  51. }
  52. break;
  53. default:
  54. if (!empty($userLng) && !empty($userLat)) {
  55. $orderBy['distance'] = 'asc';
  56. }
  57. $orderBy['star'] = 'desc';
  58. $orderBy['sales'] = 'desc';
  59. break;
  60. }
  61. }
  62. $list = $model->getList(params: $params,orderBy: array_merge($orderBy,['weigh' => 'desc']),select: $select);
  63. foreach ($list as &$item) {
  64. [$distance,$unit] = \app\utils\Common::distanceTo($item['distance']);
  65. $item['distance'] = "{$distance}{$unit}";
  66. }
  67. return $this->success('success',$list);
  68. }
  69. // 商铺列表
  70. public function info()
  71. {
  72. $params = $this->request->param();
  73. $userLng = $this->auth->lng;
  74. $userLat = $this->auth->lat;
  75. $model = new OfflineShopModel();
  76. $select = ['*'];
  77. if (!empty($userLng) && !empty($userLat)) {
  78. $select[] = Db::raw("(st_distance(point ({$userLng}, {$userLat}),point(lng,lat))*111195) as distance");
  79. }
  80. $info = $model->getDetail(params: $params,select: $select);
  81. if (!empty($info)) {
  82. $info['images'] = explode(',',$info['image']);
  83. }
  84. return $this->success('success',$info);
  85. }
  86. // 评价
  87. public function comment()
  88. {
  89. $params = $this->request->param();
  90. if (empty($params['shop_id'])) {
  91. return $this->error('请选择评价门店');
  92. }
  93. if (empty($params['star'])){
  94. return $this->error('请选择评分');
  95. }
  96. if (empty($params['remark'])){
  97. return $this->error('请填写评价');
  98. }
  99. $data = [
  100. 'shop_id' => $params['shop_id'],
  101. 'user_id' => $this->auth->id,
  102. 'star' => $params['star'],
  103. 'remark' => $params['remark'],
  104. 'images' => $params['images'] ?? '',
  105. 'status' => 1,
  106. 'create_time' => time(),
  107. ];
  108. Db::name('offline_shop_comment')->insert($data);
  109. $comment = Db::name('offline_shop_comment')->where('shop_id',$params['shop_id'])->field([Db::raw('sum(star) as star'),Db::raw('count(*) as num')])->find();
  110. $star = bcdiv($comment['star'] ?? 0,$comment['num'] ?? 0,2);
  111. Db::name('offline_shop')->where('id',$params['shop_id'])->update(['star'=>$star]);
  112. return $this->success('评价成功');
  113. }
  114. }