OfflineShop.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\HotelCanteenRoomModel;
  5. use app\common\model\OfflineShopModel;
  6. use app\common\model\OfflineTypeModel;
  7. use app\utils\DataUtil;
  8. use think\Db;
  9. /**
  10. * 示例接口
  11. */
  12. class OfflineShop extends Api
  13. {
  14. protected $noNeedLogin = [''];
  15. protected $noNeedRight = ['*'];
  16. // 商圈分类
  17. public function type()
  18. {
  19. $params = $this->request->param();
  20. $model = new OfflineTypeModel();
  21. $model->setSelect(['id','pid','name','image']);
  22. $list = $model->getList(params: $params,orderBy: ['weigh' => 'desc','id' => 'desc']);
  23. return $this->success('success',DataUtil::recursion($list,'id','pid'));
  24. }
  25. // 商铺列表
  26. public function list()
  27. {
  28. $params = $this->request->param();
  29. $userLng = $this->auth->lng;
  30. $userLat = $this->auth->lat;
  31. $model = new OfflineShopModel();
  32. $select = ['id', 'type_id', 'type_ids', 'name', 'image', 'lng', 'lat', 'address', 'telephone', 'star', 'sales'];
  33. $orderBy = [];
  34. if (!empty($userLng) && !empty($userLat)) {
  35. $select[] = Db::raw("(st_distance(point ({$userLng}, {$userLat}),point(lng,lat))*111195) as distance");
  36. !empty($params['distance']) && $params['distance'] = $params['distance'] * 1000;
  37. }
  38. if (!empty($this->auth->city)) {
  39. $params['city'] = $this->auth->city;
  40. }
  41. if (isset($params['rank_type'])) {
  42. switch ($params['rank_type']) {
  43. case 1:
  44. $orderBy['star'] = 'desc';
  45. break;
  46. case 2:
  47. $orderBy['sales'] = 'desc';
  48. break;
  49. case 3:
  50. if (!empty($userLng) && !empty($userLat)) {
  51. $orderBy['distance'] = 'asc';
  52. }
  53. break;
  54. default:
  55. if (!empty($userLng) && !empty($userLat)) {
  56. $orderBy['distance'] = 'asc';
  57. }
  58. $orderBy['star'] = 'desc';
  59. $orderBy['sales'] = 'desc';
  60. break;
  61. }
  62. }
  63. $list = $model->getList(params: $params,orderBy: array_merge($orderBy,['weigh' => 'desc']),select: $select);
  64. foreach ($list as &$item) {
  65. [$distance,$unit] = \app\utils\Common::distanceTo($item['distance']);
  66. $item['distance'] = "{$distance}{$unit}";
  67. }
  68. return $this->success('success',$list);
  69. }
  70. // 商铺列表
  71. public function info()
  72. {
  73. $params = $this->request->param();
  74. $userLng = $this->auth->lng;
  75. $userLat = $this->auth->lat;
  76. $model = new OfflineShopModel();
  77. $select = ['*'];
  78. if (!empty($userLng) && !empty($userLat)) {
  79. $select[] = Db::raw("(st_distance(point ({$userLng}, {$userLat}),point(lng,lat))*111195) as distance");
  80. }
  81. $info = $model->getDetail(params: $params,select: $select);
  82. if (!empty($info)) {
  83. $info['images'] = explode(',',$info['image']);
  84. }
  85. return $this->success('success',$info);
  86. }
  87. // 评价
  88. public function comment()
  89. {
  90. $params = $this->request->param();
  91. if (empty($params['shop_id'])) {
  92. return $this->error('请选择评价门店');
  93. }
  94. if (empty($params['star'])){
  95. return $this->error('请选择评分');
  96. }
  97. if (empty($params['remark'])){
  98. return $this->error('请填写评价');
  99. }
  100. $data = [
  101. 'shop_id' => $params['shop_id'],
  102. 'user_id' => $this->auth->id,
  103. 'star' => $params['star'],
  104. 'remark' => $params['remark'],
  105. 'images' => $params['images'] ?? '',
  106. 'status' => 1,
  107. 'create_time' => time(),
  108. ];
  109. Db::name('offline_shop_comment')->insert($data);
  110. $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();
  111. $star = bcdiv($comment['star'] ?? 0,$comment['num'] ?? 0,2);
  112. Db::name('offline_shop')->where('id',$params['shop_id'])->update(['star'=>$star]);
  113. return $this->success('评价成功');
  114. }
  115. /**
  116. * 提交订单
  117. * @return true
  118. */
  119. public function orderSubmit()
  120. {
  121. $params = $this->request->param();
  122. if (empty($params['shop_id'])) {
  123. return $this->error('参数缺失');
  124. }
  125. if (empty($params['money'])) {
  126. return $this->error('请填写支付金额');
  127. }
  128. $user_id = $this->auth->id;
  129. $model = new OfflineShopModel();
  130. $info = $model->getDetail(params: ['id'=>$params['shop_id']]);
  131. if (!$info){
  132. return $this->error('门店信息有误');
  133. }
  134. // 开始报名
  135. $data = [
  136. 'shop_id' => $info['id'],
  137. 'user_id' => $user_id,
  138. 'order_no' => createUniqueNo('C', $user_id),
  139. 'pay_amount' => bcmul($params['money'], 1, 2),
  140. 'status' => 1,
  141. 'create_time' => time()
  142. ];
  143. if (!Db::name('offline_shop_order')->insertGetId($data)) {
  144. return $this->error('订单创建失败');
  145. }
  146. return $this->success('提交成功',[
  147. 'order_no' => $data['order_no'],
  148. 'pay_amount' => $data['pay_amount'],
  149. 'order_type' => 'offline_shop_order',
  150. ]);
  151. }
  152. }