From df52b6c56c4b9e45f7c7a54d00eeb8beb3fe274a Mon Sep 17 00:00:00 2001 From: yumo Date: Fri, 8 Sep 2023 23:13:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=84=E8=8C=83=E5=8C=96model=E5=8E=BB?= =?UTF-8?q?=E9=99=A4=E6=97=A0=E7=94=A8=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/AuthController.php | 2 +- app/admin/controller/Image.php | 4 +- app/admin/controller/Index.php | 8 +- app/common/model/Admin.php | 2 +- app/common/model/AdminLog.php | 2 +- app/common/model/AdminNotify.php | 2 +- app/common/model/Advert.php | 2 +- app/common/model/AdvertInfo.php | 2 +- app/common/model/Attachment.php | 2 +- app/common/model/Comment.php | 2 +- app/common/model/Document.php | 2 +- app/common/model/DocumentProduct.php | 2 +- app/common/model/FriendLink.php | 2 +- app/common/model/InvitationCode.php | 2 +- app/common/model/MessageForm.php | 2 +- app/common/model/ModelTrait.php | 112 +++++++----------------- app/common/model/SystemConfig.php | 2 +- app/common/model/SystemConfigTab.php | 2 +- app/common/model/Tag.php | 2 +- app/common/model/User.php | 2 +- 20 files changed, 55 insertions(+), 103 deletions(-) diff --git a/app/admin/controller/AuthController.php b/app/admin/controller/AuthController.php index 90794bd..5abbcf6 100644 --- a/app/admin/controller/AuthController.php +++ b/app/admin/controller/AuthController.php @@ -135,7 +135,7 @@ abstract class AuthController extends SystemBasic * 记录日志 * @return bool */ - protected function createLog() + protected function createLog(): bool { // 不需要登录不能记录日志 if (in_array($this->action, $this->noNeedLogin) || $this->noNeedLogin == ['*'] || $this->noNeedLogin == "*") return true; diff --git a/app/admin/controller/Image.php b/app/admin/controller/Image.php index ebec0ac..a93c374 100644 --- a/app/admin/controller/Image.php +++ b/app/admin/controller/Image.php @@ -122,8 +122,8 @@ class Image extends AuthController public function delCategory($id) { if ($id == 0) return app("json")->fail("未选择分类"); - if (Attachment::be($id, "cid")) return app("json")->fail("该分类下有图片不能删除"); - if (AttachmentCategory::be($id, "pid")) return app("json")->fail("该分类下有子分类不能删除"); + if (Attachment::isExist($id, "cid")) return app("json")->fail("该分类下有图片不能删除"); + if (AttachmentCategory::isExist($id, "pid")) return app("json")->fail("该分类下有子分类不能删除"); return AttachmentCategory::del($id) ? app("json")->success("删除成功") : app("json")->fail("删除失败"); } diff --git a/app/admin/controller/Index.php b/app/admin/controller/Index.php index da64659..9c9efae 100644 --- a/app/admin/controller/Index.php +++ b/app/admin/controller/Index.php @@ -28,10 +28,10 @@ class Index extends AuthController */ public function main() { - $this->assign("document_count", Document::counts()); - $this->assign("user_count", User::counts(new User())); - $this->assign("friend_link_count", FriendLink::counts(new FriendLink())); - $this->assign("message_form_count", MessageForm::counts(new MessageForm())); + $this->assign("document_count", (new Document)->count()); + $this->assign("user_count", (new User())->count()); + $this->assign("friend_link_count",(new FriendLink())->count()); + $this->assign("message_form_count", (new MessageForm())->count()); $this->assign("article_list", Document::systemPage(["page"=> 0 ,"limit" => 5])['data']); return $this->fetch(); } diff --git a/app/common/model/Admin.php b/app/common/model/Admin.php index 33cc489..d0ad3da 100644 --- a/app/common/model/Admin.php +++ b/app/common/model/Admin.php @@ -78,7 +78,7 @@ class Admin extends BaseModel if ($where['tel'] != '') $model = $model->where("tel|mail", "like", "%$where[tel]%"); if ($where['status'] != '') $model = $model->where("status", $where['status']); if ($where['role_id'] != '') $model = $model->where("role_id", $where['role_id']); - $count = self::counts($model); + $count = self::count(); if ($where['page'] && $where['limit']) $model = $model->page((int)$where['page'], (int)$where['limit']); $data = $model->select()->each(function ($item) { unset($item['password']); diff --git a/app/common/model/AdminLog.php b/app/common/model/AdminLog.php index e01018a..7375e56 100644 --- a/app/common/model/AdminLog.php +++ b/app/common/model/AdminLog.php @@ -50,7 +50,7 @@ class AdminLog extends BaseModel if ($where['ip'] != '') $model = $model->where('ip', "like", "%$where[ip]%"); if ($where['start_time'] != '') $model = $model->where('create_time', '>', strtotime($where['start_time'] . " 00:00:00")); if ($where['end_time'] != '') $model = $model->where('create_time', '<', strtotime($where['end_time'] . " 23:59:59")); - $count = self::counts($model); + $count = self::count(); if (!empty($where['page']) && !empty($where['limit'])) $model = $model->page((int)$where['page'], (int)$where['limit']); $data = $model->select()->each(function ($item) { $item['name'] = AdminAuth::getNameByAction($item['module'], $item['controller'], $item['action']); diff --git a/app/common/model/AdminNotify.php b/app/common/model/AdminNotify.php index 15ad4e9..0d81dba 100644 --- a/app/common/model/AdminNotify.php +++ b/app/common/model/AdminNotify.php @@ -58,7 +58,7 @@ class AdminNotify extends BaseModel { $model = new self; $model = $model->where("is_read", 0); - $count = self::counts($model); + $count = self::count(); $model = $model->order("create_time desc"); $model = $model->page(1, $num); $data = $model->select(); diff --git a/app/common/model/Advert.php b/app/common/model/Advert.php index 6926204..0d86c51 100644 --- a/app/common/model/Advert.php +++ b/app/common/model/Advert.php @@ -32,7 +32,7 @@ class Advert extends BaseModel if ($where['start_time'] != '') $model = $model->where("create_time", ">", strtotime($where['start_time'] . " 00:00:00")); if ($where['end_time'] != '') $model = $model->where("create_time", "<", strtotime($where['end_time'] . " 23:59:59")); if ($where['status'] != '') $model = $model->where("status", $where['status']); - $count = self::counts($model); + $count = self::count(); if ($where['page'] && $where['limit']) $model = $model->page((int)$where['page'], (int)$where['limit']); $data = $model->select()->each(function ($item) { if (!empty($item->pic)) { diff --git a/app/common/model/AdvertInfo.php b/app/common/model/AdvertInfo.php index f00068d..93842eb 100644 --- a/app/common/model/AdvertInfo.php +++ b/app/common/model/AdvertInfo.php @@ -33,7 +33,7 @@ class AdvertInfo extends BaseModel if ($where['start_time'] != '') $model = $model->where("create_time", ">", strtotime($where['start_time'] . " 00:00:00")); if ($where['end_time'] != '') $model = $model->where("create_time", "<", strtotime($where['end_time'] . " 23:59:59")); if ($where['status'] != '') $model = $model->where("status", $where['status']); - $count = self::counts($model); + $count = $model->count(); if ($where['page'] && $where['limit']) $model = $model->page((int)$where['page'], (int)$where['limit']); $data = $model->select()->each(function ($item) { if (!empty($item->pic)) { diff --git a/app/common/model/Attachment.php b/app/common/model/Attachment.php index 491f820..5667fe6 100644 --- a/app/common/model/Attachment.php +++ b/app/common/model/Attachment.php @@ -43,7 +43,7 @@ class Attachment extends BaseModel { $model = self::where("type", $where['type']); if ($where['cid'] != "") $model = $model->where("cid", $where['cid']); - $count = self::counts($model); + $count = self::count(); $model = $model->order("id desc"); $model = $model->field("id,path"); $data = $model->page((int)$where['page'], (int)$where['limit'])->select(); diff --git a/app/common/model/Comment.php b/app/common/model/Comment.php index 65cddda..61644ec 100644 --- a/app/common/model/Comment.php +++ b/app/common/model/Comment.php @@ -34,7 +34,7 @@ class Comment extends BaseModel if (isset($where['tel']) && $where['tel'] !== '') $model->where('tel', "like", "%$where[tel]%"); if (isset($where['start_time']) && $where['start_time'] != '') $model = $model->where("m.created_at", ">", strtotime($where['start_time'] . " 00:00:00")); if (isset($where['end_time']) && $where['end_time'] != '') $model = $model->where("m.created_at", "<", strtotime($where['end_time'] . " 23:59:59")); - $count = self::counts($model); + $count = self::count(); if ($where['page'] && $where['limit']) $model = $model->page((int)$where['page'], (int)$where['limit']); $data = $model->select(); if ($data) $data = $data->toArray(); diff --git a/app/common/model/Document.php b/app/common/model/Document.php index b6cdd97..c8e3188 100644 --- a/app/common/model/Document.php +++ b/app/common/model/Document.php @@ -38,7 +38,7 @@ class Document extends BaseModel if (!empty($where['end_time'])) $model = $model->where("create_time", "<", strtotime($where['end_time'] . " 23:59:59")); if (!empty($where['status'])) $model = $model->where("status", $where['status']); $model = $model->order("sort desc")->order("id desc"); - $count = self::counts($model); + $count = self::count(); if ($where['page'] && $where['limit']) $model = $model->page((int)$where['page'], (int)$where['limit']); $categoryList = DocumentCategory::column('title', 'id'); $data = $model->select()->each(function ($item) use ($categoryList) { diff --git a/app/common/model/DocumentProduct.php b/app/common/model/DocumentProduct.php index 124d21d..f078a8f 100644 --- a/app/common/model/DocumentProduct.php +++ b/app/common/model/DocumentProduct.php @@ -29,7 +29,7 @@ class DocumentProduct extends BaseModel public static function systemPage($where): array { $model = new self; - $count = self::counts($model); + $count = self::count(); if ($where['page'] && $where['limit']) $model = $model->page((int)$where['page'], (int)$where['limit']); $data = $model->select(); if ($data) $data = $data->toArray(); diff --git a/app/common/model/FriendLink.php b/app/common/model/FriendLink.php index 8db9bb5..8fd5c1c 100644 --- a/app/common/model/FriendLink.php +++ b/app/common/model/FriendLink.php @@ -33,7 +33,7 @@ class FriendLink extends BaseModel if ($where['start_time'] != '') $model = $model->where("create_time", ">", strtotime($where['start_time'] . " 00:00:00")); if ($where['end_time'] != '') $model = $model->where("create_time", "<", strtotime($where['end_time'] . " 23:59:59")); if ($where['status'] != '') $model = $model->where("status", $where['status']); - $count = self::counts($model); + $count = self::count(); if ($where['page'] && $where['limit']) $model = $model->page((int)$where['page'], (int)$where['limit']); $data = $model->select(); if ($data) $data = $data->toArray(); diff --git a/app/common/model/InvitationCode.php b/app/common/model/InvitationCode.php index 37ab343..fa4eb19 100644 --- a/app/common/model/InvitationCode.php +++ b/app/common/model/InvitationCode.php @@ -31,7 +31,7 @@ class InvitationCode extends BaseModel $model = new self; $model = $model->order('id', 'desc'); if (isset($where['code']) && $where['code'] !== '') $model->where('code', "like", "%$where[code]%"); - $count = self::counts($model); + $count = self::count(); if ($where['page'] && $where['limit']) $model = $model->page((int)$where['page'], (int)$where['limit']); $data = $model->select(); if ($data) $data = $data->toArray(); diff --git a/app/common/model/MessageForm.php b/app/common/model/MessageForm.php index 312f220..6c22517 100644 --- a/app/common/model/MessageForm.php +++ b/app/common/model/MessageForm.php @@ -34,7 +34,7 @@ class MessageForm extends BaseModel if (isset($where['tel']) && $where['tel'] !== '') $model->where('tel', "like", "%$where[tel]%"); if (isset($where['start_time']) && $where['start_time'] != '') $model = $model->where("m.created_at", ">", strtotime($where['start_time'] . " 00:00:00")); if (isset($where['end_time']) && $where['end_time'] != '') $model = $model->where("m.created_at", "<", strtotime($where['end_time'] . " 23:59:59")); - $count = self::counts($model); + $count = self::count(); if ($where['page'] && $where['limit']) $model = $model->page((int)$where['page'], (int)$where['limit']); $data = $model->select(); if ($data) $data = $data->toArray(); diff --git a/app/common/model/ModelTrait.php b/app/common/model/ModelTrait.php index 8c16efd..ebd8649 100644 --- a/app/common/model/ModelTrait.php +++ b/app/common/model/ModelTrait.php @@ -4,6 +4,9 @@ namespace app\common\model; +use think\db\exception\DataNotFoundException; +use think\db\exception\DbException; +use think\db\exception\ModelNotFoundException; use think\db\Raw; use think\Model; @@ -25,18 +28,28 @@ trait ModelTrait return $this->error; } + /** + * @throws DataNotFoundException + * @throws ModelNotFoundException + * @throws DbException + */ public static function get($where) { if (!is_array($where)) { - return self::find($where); + return (new BaseModel)->find($where); } else { - return self::where($where)->find(); + return (new BaseModel)->where($where)->find(); } } + /** + * @throws ModelNotFoundException + * @throws DataNotFoundException + * @throws DbException + */ public static function all($function) { - $query = self::newQuery(); + $query = (new BaseModel)->newQuery(); $function($query); return $query->select(); } @@ -45,11 +58,11 @@ trait ModelTrait * 添加多条数据 * @param $group * @param bool $replace - * @return mixed + * @return int */ - public static function setAll($group, $replace = false) + public static function setAll($group, bool $replace = false) { - return self::insertAll($group, $replace); + return (new BaseModel)->insertAll($group, $replace); } /** @@ -59,7 +72,7 @@ trait ModelTrait * @param $field * @return bool $type 返回成功失败 */ - public static function edit($data, $id, $field = null) + public static function edit($data, $id, $field = null): bool { $model = new self; if (!$field) $field = $model->getPk(); @@ -80,8 +93,9 @@ trait ModelTrait * @param $map * @param string $field * @return bool 是否存在 + * @throws DbException */ - public static function be($map, $field = '') + public static function isExist($map, $field = ''): bool { $model = (new self); if (!is_array($map) && empty($field)) $field = $model->getPk(); @@ -106,9 +120,9 @@ trait ModelTrait * @param null $eachFn 处理结果函数 * @param array $params 分页参数 * @param int $limit 分页数 - * @return array + * @return ModelTrait */ - public static function page($model = null, $eachFn = null, $params = [], $limit = 20) + public static function page($model = null, $eachFn = null, $params = [], $limit = 20): ModelTrait { if (is_numeric($eachFn) && is_numeric($model)) { return parent::page($model, $eachFn); @@ -178,12 +192,12 @@ trait ModelTrait if (!empty($fieldOr) && is_array($fieldOr) && isset($fieldOr[0])) { if (($count = strpos($fieldOr[0], '.')) === false) { if (isset($where[$fieldOr[0]]) && $where[$fieldOr[0]] != '') { - $model = $model->where(self::get_field($fieldOr), $like, "%" . $where[$fieldOr[0]] . "%"); + $model = $model->where(self::getField($fieldOr), $like, "%" . $where[$fieldOr[0]] . "%"); } } else { $item_l = substr($fieldOr[0], $count + 1); if (isset($where[$item_l]) && $where[$item_l] != '') { - $model = $model->where(self::get_field($fieldOr), $like, "%" . $where[$item_l] . "%"); + $model = $model->where(self::getField($fieldOr), $like, "%" . $where[$item_l] . "%"); } } } @@ -197,7 +211,7 @@ trait ModelTrait * @param string $str * @return string */ - private static function get_field($id, $str = '|') + private static function getField($id, $str = '|') { if (is_array($id)) { $sql = ""; @@ -282,14 +296,14 @@ trait ModelTrait * 截取中文指定字节 * @param string $str * @param int $utf8len - * @param string $chaet + * @param string $charset * @param string $file * @return string */ - public static function getSubstrUTf8($str, $utf8len = 100, $chaet = 'UTF-8', $file = '....') + public static function getSubstrUTf8($str, int $utf8len = 100, string $charset = 'UTF-8', string $file = '....'): string { - if (mb_strlen($str, $chaet) > $utf8len) { - $str = mb_substr($str, 0, $utf8len, $chaet) . $file; + if (mb_strlen($str, $charset) > $utf8len) { + $str = mb_substr($str, 0, $utf8len, $charset) . $file; } return $str; } @@ -300,7 +314,7 @@ trait ModelTrait * @param string $ceil * @return array */ - public static function getMonth($time = '', $ceil = 0) + public static function getMonth($time = '', $ceil = 0): array { if ($ceil != 0) $season = ceil(date('n') / 3) - $ceil; @@ -311,68 +325,6 @@ trait ModelTrait return array($firstday, $lastday); } - /** - * 高精度 加法 - * @param int|string $uid id - * @param string $decField 相加的字段 - * @param float|int $dec 加的值 - * @param string $keyField id的字段 - * @param int $acc 精度 - * @return bool - */ - public static function bcInc($key, $incField, $inc, $keyField = null, $acc = 2) - { - if (!is_numeric($inc)) return false; - $model = new self(); - if ($keyField === null) $keyField = $model->getPk(); - $result = self::where($keyField, $key)->find(); - if (!$result) return false; - $new = bcadd($result[$incField], $inc, $acc); - return false !== $model->where($keyField, $key)->update([$incField => $new]); - } - - - /** - * 高精度 减法 - * @param int|string $uid id - * @param string $decField 相减的字段 - * @param float|int $dec 减的值 - * @param string $keyField id的字段 - * @param bool $minus 是否可以为负数 - * @param int $acc 精度 - * @return bool - */ - public static function bcDec($key, $decField, $dec, $keyField = null, $minus = false, $acc = 2) - { - if (!is_numeric($dec)) return false; - $model = new self(); - if ($keyField === null) $keyField = $model->getPk(); - $result = self::where($keyField, $key)->find(); - if (!$result) return false; - if (!$minus && $result[$decField] < $dec) return false; - $new = bcsub($result[$decField], $dec, $acc); - return false !== $model->where($keyField, $key)->update([$decField => $new]); - } - - /** - * @param null $model - * @return Model - */ - protected static function getSelfModel($model = null) - { - return $model == null ? (new self()) : $model; - } - - /** - * 数据个数 - * @param string|Raw $model - * @return int - */ - public static function counts($model): int - { - return $model->count(); - } - /** * 横线 * @param int $num diff --git a/app/common/model/SystemConfig.php b/app/common/model/SystemConfig.php index 4ffe76e..1b7c378 100644 --- a/app/common/model/SystemConfig.php +++ b/app/common/model/SystemConfig.php @@ -27,7 +27,7 @@ class SystemConfig extends BaseModel { $model = new self; if ($where['tab_id']) $model = $model->where('tab_id', $where['tab_id']); - $count = self::counts($model); + $count = self::count(); if ($where['page'] && $where['limit']) $model = $model->page((int)$where['page'], (int)$where['limit']); $data = $model->select(); if ($data) $data = $data->toArray(); diff --git a/app/common/model/SystemConfigTab.php b/app/common/model/SystemConfigTab.php index 9e317fa..d69def7 100644 --- a/app/common/model/SystemConfigTab.php +++ b/app/common/model/SystemConfigTab.php @@ -25,7 +25,7 @@ class SystemConfigTab extends BaseModel $model = new self; if ($where['status'] != "") $model = $model->where("status", $where['status']); if (!empty($where['name'])) $model = $model->where("name", "like", "%$where[name]%"); - $count = self::counts($model); + $count = self::count(); if (isset($where['page']) && isset($where['limit'])) $model = $model->page((int)$where['page'], (int)$where['limit']); $data = $model->select(); if ($data) $data = $data->toArray(); diff --git a/app/common/model/Tag.php b/app/common/model/Tag.php index 0b73075..11a5c2a 100644 --- a/app/common/model/Tag.php +++ b/app/common/model/Tag.php @@ -36,7 +36,7 @@ class Tag extends BaseModel if ($where['document_id'] != '') $model = $model->where("document_id", $where['document_id']); if ($where['start_time'] != '') $model = $model->where("create_time", ">", strtotime($where['start_time'] . " 00:00:00")); if ($where['end_time'] != '') $model = $model->where("create_time", "<", strtotime($where['end_time'] . " 23:59:59")); - $count = self::counts($model); + $count = self::count(); if ($where['page'] && $where['limit']) $model = $model->page((int)$where['page'], (int)$where['limit']); $data = $model->select(); if ($data) $data = $data->toArray(); diff --git a/app/common/model/User.php b/app/common/model/User.php index 49207b7..bf47397 100644 --- a/app/common/model/User.php +++ b/app/common/model/User.php @@ -182,7 +182,7 @@ class User extends BaseModel if ($where['tel'] != '') $model = $model->where("tel|email", "like", "%$where[tel]%"); if ($where['status'] != '') $model = $model->where("status", $where['status']); if ($where['is_admin'] != '') $model = $model->where("is_admin", $where['is_admin']); - $count = self::counts($model); + $count = self::count(); if ($where['page'] && $where['limit']) $model = $model->page((int)$where['page'], (int)$where['limit']); $data = $model->select(); $data = $data ? $data->toArray() : [];