规范化model去除无用的方法

This commit is contained in:
yumo 2023-09-08 23:13:42 +08:00
parent 6baa1003bf
commit df52b6c56c
20 changed files with 55 additions and 103 deletions

View File

@ -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;

View File

@ -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("删除失败");
}

View File

@ -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();
}

View File

@ -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']);

View File

@ -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']);

View File

@ -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();

View File

@ -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)) {

View File

@ -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)) {

View File

@ -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();

View File

@ -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();

View File

@ -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) {

View File

@ -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();

View File

@ -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();

View File

@ -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();

View File

@ -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();

View File

@ -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

View File

@ -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();

View File

@ -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();

View File

@ -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();

View File

@ -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() : [];