فهرست منبع

Merge branch 'master' of http://git.huxiukeji.com/lizhen/xiaoshan

lizhen_gitee 3 ماه پیش
والد
کامیت
63061088cd

+ 207 - 0
application/api/controller/Hotel.php

@@ -0,0 +1,207 @@
+<?php
+
+namespace app\api\controller;
+
+use addons\epay\library\Service;
+use app\common\controller\Api;
+use app\common\model\HotelModel;
+use app\common\model\HotelOrderModel;
+use app\common\model\HotelRoomModel;
+use app\common\model\PayOrderModel;
+use app\common\model\UniversityEventModel;
+use app\common\model\Wallet;
+use app\utils\CurlUtil;
+use app\utils\Service\Tencent\TencentIm;
+use think\Db;
+
+/**
+ * 老年大学 活动板块
+ */
+class Hotel extends Api
+{
+    protected $noNeedLogin = [''];
+    protected $noNeedRight = ['*'];
+    protected $status = [
+        0 => '全部',
+        1 => '待入住',
+        2 => '已入住',
+        3 => '已取消',
+    ];
+
+    // 活动列表
+    public function list()
+    {
+        $params = $this->request->param();
+        $params['sort_type'] = !empty($params['sort_type']) ? $params['sort_type'] : 1;
+        $field = ['id','name','image','price','original_price','tags','lat','lng'];
+        if (!empty($params['lng']) && !empty($params['lng'])){
+            $field[] = "(st_distance(point ({$params['lng']}, {$params['lat']}),point(lng,lat))*111195) as distance";
+        }
+        $query    = HotelModel::field($field);
+
+        if (!empty($params['lng']) && !empty($params['lng']) && $params['sort_type'] == 1){
+            $query->order('distance asc');
+        }
+
+        if ($params['sort_type'] == 2){
+            $query->order('price asc');
+        }
+
+        $list = $query->where('status', 1)
+            ->order('weigh desc')
+            ->order('id desc')
+            ->autopage()
+            ->select();
+        foreach ($list as $k => $v) {
+            // 计算距离
+            $list[$k]['distance'] = distance_ext($v['distance'] ?? -1);
+        }
+        return $this->success('success', $list);
+    }
+
+    // 详情
+    public function info()
+    {
+        $params = $this->request->param();
+        $field = ['id','name','image','images','price','original_price','tags','lat','lng','address'];
+        if (!empty($params['lng']) && !empty($params['lng'])){
+            $field[] = "(st_distance(point ({$params['lng']}, {$params['lat']}),point(lng,lat))*111195) as distance";
+        }
+        $query    = HotelModel::field($field);
+
+        $query->where('id', $params['id']);
+
+        $info = $query->where('status', 1)->find();
+        if (!$info){
+            return $this->error('信息不存在');
+        }
+        // 计算距离
+        $info['distance'] = distance_ext($info['distance'] ?? -1);
+
+        // 房间列表
+        $info['room_list'] = HotelRoomModel::where('hotel_id',$info['id'])->where('status',1)->order('weigh desc')->order('id desc')->select();
+
+        return $this->success('success', $info);
+    }
+
+    // 详情
+    public function room_info()
+    {
+        $params = $this->request->param();
+        $info = HotelRoomModel::where('id',$params['id'])->where('status',1)->find();
+        return $this->success('success', $info);
+    }
+
+    public function apply()
+    {
+        $params = $this->request->param();
+        if (empty($params['room_id'])) {
+            return $this->error('参数缺失');
+        }
+        if (empty($params['start_date'])) {
+            return $this->error('参数缺失');
+        }
+        if (empty($params['end_date'])) {
+            return $this->error('参数缺失');
+        }
+        if (empty($params['name'])) {
+            return $this->error('参数缺失');
+        }
+        if (empty($params['phone'])) {
+            return $this->error('参数缺失');
+        }
+        $user_id = $this->auth->id;
+        $info = HotelRoomModel::with([
+            'hotel'=>function ($query) {
+                $query->field(['id','name','image','images','price','original_price']);
+            }
+        ])->where('id',$params['room_id'])->where('status',1)->find();
+        if (!$info) {
+            return $this->error('房间不存在');
+        }
+        if (empty($info['hotel'])) {
+            return $this->error('酒店信息有误');
+        }
+
+        // 开始报名
+        $data = [
+            'hotel_id' => $info['hotel_id'],
+            'room_id' => $info['id'],
+            'user_id' => $user_id,
+            'name' => $params['name'],
+            'phone' => $params['phone'],
+            'start_date' => $params['start_date'],
+            'end_date' => $params['end_date'],
+            'days' => (int)((strtotime($params['end_date']) - strtotime($params['start_date'])) / 86400),
+            'order_no' => createUniqueNo('H', $user_id),
+            'pay_amount' => bcmul($info['price'], 1, 2),
+            'status' => 1,
+            'create_time' => time()
+        ];
+        if (!Db::name('hotel_order')->insertGetId($data)) {
+            return $this->error('订单创建失败');
+        }
+
+        return $this->success('提交成功');
+    }
+
+    // 订单列表
+    public function myApply()
+    {
+        $params = $this->request->param();
+        $user_id = $this->auth->id;
+
+        $query = HotelOrderModel::with([
+            'room'=>function ($query) {
+                $query->field(['id','name','image']);
+            }
+        ])->where('user_id',$user_id);
+
+        switch ($params['status']){
+            case 1:
+                $query->where('start_date','<',date('Y-m-d'))->where('status',1);
+                break;
+            case 2:
+                $query->where('start_date','>=',date('Y-m-d'))->where('status',1);
+                break;
+            case 3:
+                $query->where('status',0);
+                break;
+            default:
+                break;
+        }
+
+        $list = $query->order('id','desc')->autopage()->select();
+        foreach ($list as $key=>$val){
+            if ($val['status'] == 1){
+                if ($val['start_date'] < date('Y-m-d')){
+                    $list[$key]['status_txt'] = '待入住';
+                }else{
+                    $list[$key]['status_txt'] = '已入住';
+                }
+            }else{
+                $list[$key]['status_txt'] = '已取消';
+            }
+        }
+        return $this->success('获取成功',$list);
+    }
+
+    // 订单取消
+    public function applyCancel()
+    {
+        $params = $this->request->param();
+        $user_id = $this->auth->id;
+
+        $order = HotelOrderModel::where('user_id',$user_id)->where('id',$params['order_id'])->where('status',1)->find();
+        if (!$order){
+            return $this->error('订单不存在或已取消');
+        }
+        if (date('Y-m-d H:i:s') > ($order['start_date'] . ' 12:00:00')){
+            return $this->error('订单已入住,无法取消');
+        }
+        if (!HotelOrderModel::where('user_id',$user_id)->where('id',$params['order_id'])->where('status',1)->update(['status'=>0])){
+            return $this->error('操作失败');
+        }
+        return $this->success('操作成功');
+    }
+}

+ 14 - 0
application/common.php

@@ -87,6 +87,7 @@ if (!function_exists('cdnurl')) {
      */
     function cdnurl($url, $domain = false)
     {
+        if (empty($url)) return '';
         $regex = "/^((?:[a-z]+:)?\/\/|data:image\/)(.*)/i";
         $cdnurl = \think\Config::get('upload.cdnurl');
         if (is_bool($domain) || stripos($cdnurl, '/') === 0) {
@@ -1068,6 +1069,19 @@ if (!function_exists('str_limit')){
     }
 }
 
+if (!function_exists('distance_ext')) {
+    function distance_ext(float $distance = 0){
+        if ($distance < 0) return '';
+        // 计算距离
+        if ($distance >= 1000) {
+            $distance = round(($distance / 1000), 2) . 'km';
+        } else {
+            $distance = round($distance, 1) . 'm';
+        }
+        return $distance;
+    }
+}
+
 if (!function_exists('dd')) {
     function dd(...$vars){
         foreach ($vars as $v) {

+ 40 - 0
application/common/model/HotelModel.php

@@ -0,0 +1,40 @@
+<?php
+
+namespace app\common\model;
+
+use think\Db;
+use think\Model;
+
+/**
+ * 群组
+ */
+class HotelModel extends Model
+{
+    // 表名
+    protected $name = 'hotel';
+    // 开启自动写入时间戳字段
+    protected $autoWriteTimestamp = false;
+    // 定义时间戳字段名
+    protected $createTime = false;
+    protected $updateTime = false;
+    protected $deleteTime = false;
+
+    public function getImageAttr($value, $data)
+    {
+        return cdnurl($value);
+    }
+
+    public function getImagesAttr($value, $data)
+    {
+        $value = explode(',',$value);
+        foreach ($value as &$v){
+            $v = cdnurl($v);
+        }
+        return $value;
+    }
+
+    public function room()
+    {
+        return $this->hasOne(HotelRoomModel::class, 'hotel_id', 'id');
+    }
+}

+ 40 - 0
application/common/model/HotelOrderModel.php

@@ -0,0 +1,40 @@
+<?php
+
+namespace app\common\model;
+
+use think\Db;
+use think\Model;
+
+/**
+ * 群组
+ */
+class HotelOrderModel extends Model
+{
+    // 表名
+    protected $name = 'hotel_order';
+    // 开启自动写入时间戳字段
+    protected $autoWriteTimestamp = false;
+    // 定义时间戳字段名
+    protected $createTime = false;
+    protected $updateTime = false;
+    protected $deleteTime = false;
+
+    public function getImageAttr($value, $data)
+    {
+        return cdnurl($value);
+    }
+
+    public function getImagesAttr($value, $data)
+    {
+        $value = explode(',',$value);
+        foreach ($value as &$v){
+            $v = cdnurl($v);
+        }
+        return $value;
+    }
+
+    public function room()
+    {
+        return $this->hasOne(HotelRoomModel::class, 'id', 'room_id');
+    }
+}

+ 62 - 0
application/common/model/HotelRoomModel.php

@@ -0,0 +1,62 @@
+<?php
+
+namespace app\common\model;
+
+use app\api\controller\Hotel;
+use think\Db;
+use think\Model;
+
+/**
+ * 群组
+ */
+class HotelRoomModel extends Model
+{
+    // 表名
+    protected $name = 'hotel_room';
+    // 开启自动写入时间戳字段
+    protected $autoWriteTimestamp = false;
+    // 定义时间戳字段名
+    protected $createTime = false;
+    protected $updateTime = false;
+    protected $deleteTime = false;
+
+    public function getWifiPhoneAttr($value, $data)
+    {
+        return explode(',',$value);
+    }
+    public function getRoomLayoutAttr($value, $data)
+    {
+        return explode(',',$value);
+    }
+    public function getWashUseAttr($value, $data)
+    {
+        return explode(',',$value);
+    }
+    public function getFacilityAttr($value, $data)
+    {
+        return explode(',',$value);
+    }
+    public function getBathroomAttr($value, $data)
+    {
+        return explode(',',$value);
+    }
+
+    public function getImageAttr($value, $data)
+    {
+        return cdnurl($value);
+    }
+
+    public function getImagesAttr($value, $data)
+    {
+        $value = explode(',',$value);
+        foreach ($value as &$v){
+            $v = cdnurl($v);
+        }
+        return $value;
+    }
+
+    public function hotel()
+    {
+        return $this->hasOne(HotelModel::class, 'id', 'hotel_id');
+    }
+}