Browse Source

集成BaseModel

Panda 3 weeks ago
parent
commit
a300c44457
35 changed files with 447 additions and 38 deletions
  1. 21 0
      application/api/controller/OfflineShop.php
  2. 21 1
      application/common/model/Area.php
  3. 18 2
      application/common/model/Attachment.php
  4. 257 0
      application/common/model/BaseModel.php
  5. 18 2
      application/common/model/Category.php
  6. 17 3
      application/common/model/Config.php
  7. 18 2
      application/common/model/Ems.php
  8. 17 2
      application/common/model/ExamineApplyModel.php
  9. 1 1
      application/common/model/ExamineModel.php
  10. 1 1
      application/common/model/HotelCanteenModel.php
  11. 1 1
      application/common/model/HotelCanteenOrderModel.php
  12. 1 1
      application/common/model/HotelCanteenRoomModel.php
  13. 1 1
      application/common/model/HotelModel.php
  14. 1 1
      application/common/model/HotelOrderModel.php
  15. 1 1
      application/common/model/HotelRoomModel.php
  16. 1 1
      application/common/model/MoneyLog.php
  17. 34 0
      application/common/model/OfflineTypeModel.php
  18. 1 1
      application/common/model/PayOrderModel.php
  19. 1 1
      application/common/model/ScoreLog.php
  20. 1 1
      application/common/model/Sms.php
  21. 1 1
      application/common/model/UniversityCourseApplyModel.php
  22. 1 1
      application/common/model/UniversityCourseChapterModel.php
  23. 1 1
      application/common/model/UniversityCourseModel.php
  24. 1 1
      application/common/model/UniversityEventApplyModel.php
  25. 1 1
      application/common/model/UniversityEventModel.php
  26. 1 1
      application/common/model/User.php
  27. 1 1
      application/common/model/UserGroup.php
  28. 1 1
      application/common/model/UserModel.php
  29. 1 1
      application/common/model/UserRule.php
  30. 1 1
      application/common/model/Version.php
  31. 1 1
      application/common/model/VipConfigModel.php
  32. 1 1
      application/common/model/VipCouponModel.php
  33. 1 1
      application/common/model/VipCouponUserModel.php
  34. 1 1
      application/common/model/VipOrderModel.php
  35. 1 1
      application/common/model/Wallet.php

+ 21 - 0
application/api/controller/OfflineShop.php

@@ -0,0 +1,21 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use think\Db;
+/**
+ * 示例接口
+ */
+class OfflineShop extends Api
+{
+    protected $noNeedLogin = [''];
+    protected $noNeedRight = ['*'];
+
+    public function type()
+    {
+        $param = $this->request->param();
+
+    }
+
+}

+ 21 - 1
application/common/model/Area.php

@@ -8,9 +8,29 @@ use think\Model;
 /**
  * 地区数据模型
  */
-class Area extends Model
+class Area extends BaseModel
 {
+    // 表名
+    protected $name = 'area';
 
+    // 自动写入时间戳字段
+    protected $autoWriteTimestamp = 'int';
+
+    // 定义时间戳字段名
+    protected $createTime = 'createtime';
+    protected $updateTime = 'updatetime';
+
+    protected int $is_status_search = 0;// 默认使用 status = 1 筛选
+    protected int $is_delete_search = 0;// 默认使用 is_delete = 0 筛选
+
+    /**
+     * 默认查询字段
+     *
+     * @var array|string[]
+     */
+    public array $select = [
+        '*'
+    ];
     /**
      * 根据经纬度获取当前地区信息
      *

+ 18 - 2
application/common/model/Attachment.php

@@ -4,14 +4,30 @@ namespace app\common\model;
 
 use think\Model;
 
-class Attachment extends Model
+class Attachment extends BaseModel
 {
+    // 表名
+    protected $name = 'attachment';
 
-    // 开启自动写入时间戳字段
+    // 自动写入时间戳字段
     protected $autoWriteTimestamp = 'int';
+
     // 定义时间戳字段名
     protected $createTime = 'createtime';
     protected $updateTime = 'updatetime';
+
+    protected int $is_status_search = 0;// 默认使用 status = 1 筛选
+    protected int $is_delete_search = 0;// 默认使用 is_delete = 0 筛选
+
+    /**
+     * 默认查询字段
+     *
+     * @var array|string[]
+     */
+    public array $select = [
+        '*'
+    ];
+
     // 定义字段类型
     protected $type = [
     ];

+ 257 - 0
application/common/model/BaseModel.php

@@ -0,0 +1,257 @@
+<?php
+namespace app\common\model;
+
+use think\db\Query;
+use think\Model;
+use think\helper\Str;
+
+class BaseModel extends Model
+{
+    protected $message = '';
+    protected $data = [];
+    protected $query;
+    protected array $select = [];
+    protected int $is_status_search = 1;// 默认使用 status = 1 筛选
+    protected int $is_delete_search = 1;// 默认使用 is_delete = 0 筛选
+
+    /**
+     * 筛选条件
+     *
+     * @param Query $query
+     * @param $value
+     * @param array $params
+     * @return mixed
+     */
+    public function searchIdAttribute($query, $value, array $params)
+    {
+        if (empty($value)) {
+            return $query;
+        }
+        return $query->where('id', $value);
+    }
+    public function searchIdInAttribute($query, $value, array $params)
+    {
+        if ($value === []){
+            return $query->whereIn('id', []);
+        }
+        if (empty($value)) {
+            return $query;
+        }
+        return $query->whereIN('id', $value);
+    }
+
+    /**
+     * 设置 select
+     * @param array $select
+     * @return BaseModel
+     */
+    public function setSelect(array $select = ['*'])
+    {
+        $this->select = $select;
+        return $this;
+    }
+
+    /**
+     * 设置 默认使用 status = 1 筛选
+     * @param int $is_status_search
+     * @return $this
+     */
+    public function setIsStatusSearchValue(int $is_status_search = 1)
+    {
+        $this->is_status_search = $is_status_search;
+        return $this;
+    }
+
+    /**
+     * 设置 默认使用 is_delete = 0 筛选
+     * @param int $is_delete_search
+     * @return $this
+     */
+    public function setIsDeleteSearchValue(int $is_delete_search = 1)
+    {
+        $this->is_delete_search = $is_delete_search;
+        return $this;
+    }
+
+    /**
+     * 列表查询
+     *
+     * @param array $params
+     * @param array $orderBy
+     * @param array $select
+     * @param array $with
+     * @return mixed
+     */
+    public function getList(array $params = [], array $orderBy = [], array $select = [], array $with = [])
+    {
+        $query = $this->catchSearch($params)
+            ->catchPages($params)
+            ->sortTool($orderBy)
+            ->getQueryObj()
+            ->field($select ?: $this->select);
+
+        // 模型关联
+        count($with) > 0 && $query->with($with);
+
+        // 规避 禁用 和 删除 数据
+        $this->is_status_search === 1 && $query->where('status', 'normal');
+        $this->is_delete_search === 1 && $query->whereNull('deletetime');
+
+        return $this->catchData(json_decode(json_encode($query->select()),true));
+    }
+
+    /**
+     * 单条数据查询
+     * @param array $params
+     * @param array $orderBy
+     * @param array $select
+     * @param array $with
+     * @return array
+     */
+    public function getDetail(array $params = [], array $orderBy = [], array $select = [], array $with = [])
+    {
+        $query = $this->catchSearch($params)
+            ->catchPages($params)
+            ->sortTool($orderBy)
+            ->getQueryObj()
+            ->field($select ?: $this->select);
+
+        // 模型关联
+        count($with) > 0 && $query->with($with);
+
+        // 规避 禁用 和 删除 数据
+        $this->is_status_search === 1 && $query->where('status', 'normal');
+        $this->is_delete_search === 1 && $query->whereNull('deletetime');
+
+        $detail = $query->find();
+        if ($detail){
+            $detail = json_decode(json_encode($detail),true);
+        }else{
+            $detail = [];
+        }
+
+        return $this->catchData($detail);
+    }
+
+    /**
+     * 查询器
+     *
+     * @param array $params
+     * @return $this
+     */
+    protected function catchSearch(array $params = [])
+    {
+        $this->query = !empty($this->query) ? $this->query : $this;
+        if (empty($params)) {
+            return $this;
+        }
+
+        foreach ($params as $field => $value) {
+            $method = 'search' . Str::studly($field) . 'Attribute';
+            if ($value !== null && $value !== '' && method_exists($this, $method)) {
+                $this->query = $this->$method($this->query,$value,$params);
+            }
+        }
+
+        return $this;
+    }
+
+    /**
+     * 分页器
+     *
+     * @param $params
+     * @return $this|BaseModel
+     */
+    protected function catchPages($params)
+    {
+        $this->query = !empty($this->query) ? $this->query : $this;
+        if (empty($params['page']) && empty($params['list_row'])){
+            return $this;
+        }
+        $page = $params['page'] ?? 1;
+        $size = $params['list_row'] ?? 15;
+        $this->query = $this->query->page($page,$size);
+        return $this;
+    }
+
+    /**
+     * 排序工具
+     *
+     * @param $orderBy
+     * @return $this|BaseModel
+     */
+    protected function sortTool($orderBy)
+    {
+        $this->query = !empty($this->query) ? $this->query : $this;
+        if (empty($orderBy)){
+            return $this;
+        }
+        foreach ($orderBy as $sort=>$order){
+            if (empty($sort) || empty($order)){
+                continue;
+            }
+            $this->query = $this->query->order($sort,$order);
+        }
+        return $this;
+    }
+
+    /**
+     * 数据集处理器
+     * @param array $data
+     * @return array
+     */
+    protected function catchData(array $data): array
+    {
+        if (isset($data[0])){
+            foreach ($data as $key=>$val){
+                foreach ($val as $k=>$v){
+                    $method = 'data' . Str::studly($k) . 'Attribute';
+                    if (method_exists($this, $method)) {
+                        $data[$key][$k] = $this->$method($v,$data);
+                    }
+                }
+            }
+        }else{
+            foreach ($data as $key=>$val){
+                $method = 'data' . Str::studly($key) . 'Attribute';
+                if (method_exists($this, $method)) {
+                    $data[$key] = $this->$method($val,$data);
+                }
+            }
+        }
+
+        return $data;
+    }
+
+    /**
+     * @return mixed
+     */
+    protected function getQueryObj()
+    {
+        return $this->query;
+    }
+
+    protected function success($message = '', $data = [])
+    {
+        $this->message = $message;
+        $this->data = $data;
+        return true;
+    }
+
+    protected function error($message = '', $data = [])
+    {
+        $this->message = $message;
+        $this->data = $data;
+        return false;
+    }
+
+    public function getMessage()
+    {
+        return $this->message;
+    }
+
+    public function getData($name = '')
+    {
+        return !empty($name) ? $this->data[$name] ?? '' : $this->data;
+    }
+}

+ 18 - 2
application/common/model/Category.php

@@ -7,14 +7,30 @@ use think\Model;
 /**
  * 分类模型
  */
-class Category extends Model
+class Category extends BaseModel
 {
+    // 表名
+    protected $name = 'category';
 
-    // 开启自动写入时间戳字段
+    // 自动写入时间戳字段
     protected $autoWriteTimestamp = 'int';
+
     // 定义时间戳字段名
     protected $createTime = 'createtime';
     protected $updateTime = 'updatetime';
+
+    protected int $is_status_search = 0;// 默认使用 status = 1 筛选
+    protected int $is_delete_search = 0;// 默认使用 is_delete = 0 筛选
+
+    /**
+     * 默认查询字段
+     *
+     * @var array|string[]
+     */
+    public array $select = [
+        '*'
+    ];
+
     // 追加属性
     protected $append = [
         'type_text',

+ 17 - 3
application/common/model/Config.php

@@ -9,16 +9,30 @@ use think\Model;
 /**
  * 配置模型
  */
-class Config extends Model
+class Config extends BaseModel
 {
-
-    // 表名,不含前缀
+    // 表名
     protected $name = 'config';
+
     // 自动写入时间戳字段
     protected $autoWriteTimestamp = false;
+
     // 定义时间戳字段名
     protected $createTime = false;
     protected $updateTime = false;
+
+    protected int $is_status_search = 0;// 默认使用 status = 1 筛选
+    protected int $is_delete_search = 0;// 默认使用 is_delete = 0 筛选
+
+    /**
+     * 默认查询字段
+     *
+     * @var array|string[]
+     */
+    public array $select = [
+        '*'
+    ];
+
     // 追加属性
     protected $append = [
         'extend_html'

+ 18 - 2
application/common/model/Ems.php

@@ -7,14 +7,30 @@ use think\Model;
 /**
  * 邮箱验证码
  */
-class Ems extends Model
+class Ems extends BaseModel
 {
+    // 表名
+    protected $name = 'ems';
 
-    // 开启自动写入时间戳字段
+    // 自动写入时间戳字段
     protected $autoWriteTimestamp = 'int';
+
     // 定义时间戳字段名
     protected $createTime = 'createtime';
     protected $updateTime = false;
+
+    protected int $is_status_search = 0;// 默认使用 status = 1 筛选
+    protected int $is_delete_search = 0;// 默认使用 is_delete = 0 筛选
+
+    /**
+     * 默认查询字段
+     *
+     * @var array|string[]
+     */
+    public array $select = [
+        '*'
+    ];
+
     // 追加属性
     protected $append = [
     ];

+ 17 - 2
application/common/model/ExamineApplyModel.php

@@ -8,15 +8,30 @@ use think\Model;
 /**
  * 群组
  */
-class ExamineApplyModel extends Model
+class ExamineApplyModel extends BaseModel
 {
     // 表名
     protected $name = 'examine_apply';
-    // 开启自动写入时间戳字段
+
+    // 自动写入时间戳字段
     protected $autoWriteTimestamp = false;
+
     // 定义时间戳字段名
     protected $createTime = false;
     protected $updateTime = false;
+
+    protected int $is_status_search = 0;// 默认使用 status = 1 筛选
+    protected int $is_delete_search = 0;// 默认使用 is_delete = 0 筛选
+
+    /**
+     * 默认查询字段
+     *
+     * @var array|string[]
+     */
+    public array $select = [
+        '*'
+    ];
+
     protected $deleteTime = false;
 
     public function examine()

+ 1 - 1
application/common/model/ExamineModel.php

@@ -8,7 +8,7 @@ use think\Model;
 /**
  * 群组
  */
-class ExamineModel extends Model
+class ExamineModel extends BaseModel
 {
     // 表名
     protected $name = 'examine';

+ 1 - 1
application/common/model/HotelCanteenModel.php

@@ -8,7 +8,7 @@ use think\Model;
 /**
  * 群组
  */
-class HotelCanteenModel extends Model
+class HotelCanteenModel extends BaseModel
 {
     // 表名
     protected $name = 'hotel_canteen';

+ 1 - 1
application/common/model/HotelCanteenOrderModel.php

@@ -8,7 +8,7 @@ use think\Model;
 /**
  * 群组
  */
-class HotelCanteenOrderModel extends Model
+class HotelCanteenOrderModel extends BaseModel
 {
     // 表名
     protected $name = 'hotel_canteen_order';

+ 1 - 1
application/common/model/HotelCanteenRoomModel.php

@@ -8,7 +8,7 @@ use think\Model;
 /**
  * 群组
  */
-class HotelCanteenRoomModel extends Model
+class HotelCanteenRoomModel extends BaseModel
 {
     // 表名
     protected $name = 'hotel_canteen_room';

+ 1 - 1
application/common/model/HotelModel.php

@@ -8,7 +8,7 @@ use think\Model;
 /**
  * 群组
  */
-class HotelModel extends Model
+class HotelModel extends BaseModel
 {
     // 表名
     protected $name = 'hotel';

+ 1 - 1
application/common/model/HotelOrderModel.php

@@ -8,7 +8,7 @@ use think\Model;
 /**
  * 群组
  */
-class HotelOrderModel extends Model
+class HotelOrderModel extends BaseModel
 {
     // 表名
     protected $name = 'hotel_order';

+ 1 - 1
application/common/model/HotelRoomModel.php

@@ -9,7 +9,7 @@ use think\Model;
 /**
  * 群组
  */
-class HotelRoomModel extends Model
+class HotelRoomModel extends BaseModel
 {
     // 表名
     protected $name = 'hotel_room';

+ 1 - 1
application/common/model/MoneyLog.php

@@ -7,7 +7,7 @@ use think\Model;
 /**
  * 会员余额日志模型
  */
-class MoneyLog extends Model
+class MoneyLog extends BaseModel
 {
 
     // 表名

+ 34 - 0
application/common/model/OfflineTypeModel.php

@@ -0,0 +1,34 @@
+<?php
+
+namespace app\common\model;
+
+use think\Db;
+use think\Model;
+
+/**
+ * 群组
+ */
+class OfflineTypeModel extends BaseModel
+{
+    // 表名
+    protected $name = 'offline_type';
+
+    // 自动写入时间戳字段
+    protected $autoWriteTimestamp = 'int';
+
+    // 定义时间戳字段名
+    protected $createTime = 'createtime';
+    protected $updateTime = 'updatetime';
+
+    protected int $is_status_search = 0;// 默认使用 status = 1 筛选
+    protected int $is_delete_search = 0;// 默认使用 is_delete = 0 筛选
+
+    /**
+     * 默认查询字段
+     *
+     * @var array|string[]
+     */
+    public array $select = [
+        '*'
+    ];
+}

+ 1 - 1
application/common/model/PayOrderModel.php

@@ -8,7 +8,7 @@ use think\Model;
 /**
  * 支付订单
  */
-class PayOrderModel extends Model
+class PayOrderModel extends BaseModel
 {
     // 表名
     protected $name = 'pay_order';

+ 1 - 1
application/common/model/ScoreLog.php

@@ -7,7 +7,7 @@ use think\Model;
 /**
  * 会员积分日志模型
  */
-class ScoreLog extends Model
+class ScoreLog extends BaseModel
 {
 
     // 表名

+ 1 - 1
application/common/model/Sms.php

@@ -7,7 +7,7 @@ use think\Model;
 /**
  * 短信验证码
  */
-class Sms extends Model
+class Sms extends BaseModel
 {
 
     // 开启自动写入时间戳字段

+ 1 - 1
application/common/model/UniversityCourseApplyModel.php

@@ -8,7 +8,7 @@ use think\Model;
 /**
  * 群组
  */
-class UniversityCourseApplyModel extends Model
+class UniversityCourseApplyModel extends BaseModel
 {
     // 表名
     protected $name = 'university_course_apply';

+ 1 - 1
application/common/model/UniversityCourseChapterModel.php

@@ -8,7 +8,7 @@ use think\Model;
 /**
  * 群组
  */
-class UniversityCourseChapterModel extends Model
+class UniversityCourseChapterModel extends BaseModel
 {
     // 表名
     protected $name = 'university_course_chapter';

+ 1 - 1
application/common/model/UniversityCourseModel.php

@@ -8,7 +8,7 @@ use think\Model;
 /**
  * 群组
  */
-class UniversityCourseModel extends Model
+class UniversityCourseModel extends BaseModel
 {
     // 表名
     protected $name = 'university_course';

+ 1 - 1
application/common/model/UniversityEventApplyModel.php

@@ -8,7 +8,7 @@ use think\Model;
 /**
  * 群组
  */
-class UniversityEventApplyModel extends Model
+class UniversityEventApplyModel extends BaseModel
 {
     // 表名
     protected $name = 'university_event_apply';

+ 1 - 1
application/common/model/UniversityEventModel.php

@@ -8,7 +8,7 @@ use think\Model;
 /**
  * 群组
  */
-class UniversityEventModel extends Model
+class UniversityEventModel extends BaseModel
 {
     // 表名
     protected $name = 'university_event';

+ 1 - 1
application/common/model/User.php

@@ -8,7 +8,7 @@ use think\Model;
 /**
  * 会员模型
  */
-class User extends Model
+class User extends BaseModel
 {
 
     // 开启自动写入时间戳字段

+ 1 - 1
application/common/model/UserGroup.php

@@ -4,7 +4,7 @@ namespace app\common\model;
 
 use think\Model;
 
-class UserGroup extends Model
+class UserGroup extends BaseModel
 {
 
     // 表名

+ 1 - 1
application/common/model/UserModel.php

@@ -9,7 +9,7 @@ use think\Model;
 /**
  * 动态评论
  */
-class UserModel extends Model
+class UserModel extends BaseModel
 {
     // 表名
     protected $name = 'user';

+ 1 - 1
application/common/model/UserRule.php

@@ -4,7 +4,7 @@ namespace app\common\model;
 
 use think\Model;
 
-class UserRule extends Model
+class UserRule extends BaseModel
 {
 
     // 表名

+ 1 - 1
application/common/model/Version.php

@@ -4,7 +4,7 @@ namespace app\common\model;
 
 use think\Model;
 
-class Version extends Model
+class Version extends BaseModel
 {
 
     // 开启自动写入时间戳字段

+ 1 - 1
application/common/model/VipConfigModel.php

@@ -8,7 +8,7 @@ use think\Model;
 /**
  * 群组
  */
-class VipConfigModel extends Model
+class VipConfigModel extends BaseModel
 {
     // 表名
     protected $name = 'vip_config';

+ 1 - 1
application/common/model/VipCouponModel.php

@@ -8,7 +8,7 @@ use think\Model;
 /**
  * 群组
  */
-class VipCouponModel extends Model
+class VipCouponModel extends BaseModel
 {
     // 表名
     protected $name = 'vip_coupon';

+ 1 - 1
application/common/model/VipCouponUserModel.php

@@ -8,7 +8,7 @@ use think\Model;
 /**
  * 群组
  */
-class VipCouponUserModel extends Model
+class VipCouponUserModel extends BaseModel
 {
     // 表名
     protected $name = 'vip_coupon_user';

+ 1 - 1
application/common/model/VipOrderModel.php

@@ -8,7 +8,7 @@ use think\Model;
 /**
  * 群组
  */
-class VipOrderModel extends Model
+class VipOrderModel extends BaseModel
 {
     // 表名
     protected $name = 'vip_order';

+ 1 - 1
application/common/model/Wallet.php

@@ -8,7 +8,7 @@ use think\Db;
 /**
  * 货币模型
  */
-class Wallet extends Model
+class Wallet extends BaseModel
 {
     // 日志变动类型
     const log_type = [