123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\common\model\HotelCanteenRoomModel;
- use app\common\model\OfflineShopModel;
- use app\common\model\OfflineTypeModel;
- use app\utils\DataUtil;
- use think\Db;
- /**
- * 示例接口
- */
- class OfflineShop extends Api
- {
- protected $noNeedLogin = [''];
- protected $noNeedRight = ['*'];
- // 商圈分类
- public function type()
- {
- $params = $this->request->param();
- $model = new OfflineTypeModel();
- $model->setSelect(['id','pid','name','image']);
- $list = $model->getList(params: $params,orderBy: ['weigh' => 'desc','id' => 'desc']);
- return $this->success('success',DataUtil::recursion($list,'id','pid'));
- }
- // 商铺列表
- public function list()
- {
- $params = $this->request->param();
- $userLng = $this->auth->lng;
- $userLat = $this->auth->lat;
- $model = new OfflineShopModel();
- $select = ['id', 'type_id', 'type_ids', 'name', 'image', 'lng', 'lat', 'address', 'telephone', 'star', 'sales'];
- $orderBy = [];
- if (!empty($userLng) && !empty($userLat)) {
- $select[] = Db::raw("(st_distance(point ({$userLng}, {$userLat}),point(lng,lat))*111195) as distance");
- !empty($params['distance']) && $params['distance'] = $params['distance'] * 1000;
- }
- if (!empty($this->auth->city)) {
- $params['city'] = $this->auth->city;
- }
- if (isset($params['rank_type'])) {
- switch ($params['rank_type']) {
- case 1:
- $orderBy['star'] = 'desc';
- break;
- case 2:
- $orderBy['sales'] = 'desc';
- break;
- case 3:
- if (!empty($userLng) && !empty($userLat)) {
- $orderBy['distance'] = 'asc';
- }
- break;
- default:
- if (!empty($userLng) && !empty($userLat)) {
- $orderBy['distance'] = 'asc';
- }
- $orderBy['star'] = 'desc';
- $orderBy['sales'] = 'desc';
- break;
- }
- }
- $list = $model->getList(params: $params,orderBy: array_merge($orderBy,['weigh' => 'desc']),select: $select);
- foreach ($list as &$item) {
- [$distance,$unit] = \app\utils\Common::distanceTo($item['distance']);
- $item['distance'] = "{$distance}{$unit}";
- }
- return $this->success('success',$list);
- }
- // 商铺列表
- public function info()
- {
- $params = $this->request->param();
- $userLng = $this->auth->lng;
- $userLat = $this->auth->lat;
- $model = new OfflineShopModel();
- $select = ['*'];
- if (!empty($userLng) && !empty($userLat)) {
- $select[] = Db::raw("(st_distance(point ({$userLng}, {$userLat}),point(lng,lat))*111195) as distance");
- }
- $info = $model->getDetail(params: $params,select: $select);
- if (!empty($info)) {
- $info['images'] = explode(',',$info['image']);
- }
- return $this->success('success',$info);
- }
- // 评价
- public function comment()
- {
- $params = $this->request->param();
- if (empty($params['shop_id'])) {
- return $this->error('请选择评价门店');
- }
- if (empty($params['star'])){
- return $this->error('请选择评分');
- }
- if (empty($params['remark'])){
- return $this->error('请填写评价');
- }
- $data = [
- 'shop_id' => $params['shop_id'],
- 'user_id' => $this->auth->id,
- 'star' => $params['star'],
- 'remark' => $params['remark'],
- 'images' => $params['images'] ?? '',
- 'status' => 1,
- 'create_time' => time(),
- ];
- Db::name('offline_shop_comment')->insert($data);
- $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();
- $star = bcdiv($comment['star'] ?? 0,$comment['num'] ?? 0,2);
- Db::name('offline_shop')->where('id',$params['shop_id'])->update(['star'=>$star]);
- return $this->success('评价成功');
- }
- /**
- * 提交订单
- * @return true
- */
- public function orderSubmit()
- {
- $params = $this->request->param();
- if (empty($params['shop_id'])) {
- return $this->error('参数缺失');
- }
- if (empty($params['money'])) {
- return $this->error('请填写支付金额');
- }
- $user_id = $this->auth->id;
- $model = new OfflineShopModel();
- $info = $model->getDetail(params: ['id'=>$params['shop_id']]);
- if (!$info){
- return $this->error('门店信息有误');
- }
- // 开始报名
- $data = [
- 'shop_id' => $info['id'],
- 'user_id' => $user_id,
- 'order_no' => createUniqueNo('C', $user_id),
- 'pay_amount' => bcmul($params['money'], 1, 2),
- 'status' => 1,
- 'create_time' => time()
- ];
- if (!Db::name('offline_shop_order')->insertGetId($data)) {
- return $this->error('订单创建失败');
- }
- return $this->success('提交成功',[
- 'order_no' => $data['order_no'],
- 'pay_amount' => $data['pay_amount'],
- 'order_type' => 'offline_shop_order',
- ]);
- }
- }
|