where($params['where']); } else { if (!empty($params['keyword'])) { switch ($params['type']) { case 1 : $this->where(['name' => ['LIKE', '%' . $params['keyword'] . '%']]); break; } } if (!empty($params['status'])) { $this->where('status', '>=', $params['status']); } // 时间范围查询 if (!empty($params['start_time'])) { $this->where('create_time', '>=', $params['start_time']); } if (!empty($params['end_time'])) { $this->where('create_time', '<=', $params['end_time']); } } // 排序 $order = !empty($params['order']) ? $params['order'] : 'id DESC'; $result = $this ->field($fields) ->order($order) ->paginate($rows, false, [ 'page' => $page ]); $arr = [ 'list' => [], 'total' => 0, 'page' => 1, 'pageSize' => $rows, 'totalSize' => 0 ]; // 格式化数据信息 if (!$result->isEmpty()) { $arr = [ 'list' => $result->items(), 'total' => $result->total(), 'page' => $result->currentPage(), 'pageSize' => $result->listRows(), 'totalSize' => $result->lastPage() ]; } return $arr; } /** * 复制模板 * @param $id * @return int|string * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function copy($id) { $params = $this->getItem($id); return $this->insertGetId([ 'title' => $params->title, 'params' => $params->params, 'status' => $params->status, 'create_time' => time() ]); } /** * 添加 * @param $data * @return bool|int|string */ public function add($data) { if (empty($data) || !is_array($data)) { $this->error = '参数不正确'; return false; } return $this->transaction(function () use ($data) { $id = $this->insertGetId($data); return $id; }); } /** * 获取某条记录 * @param $where * @return array|bool|false|\PDOStatement|string|Model * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getItem($where, $fields = '*') { if (empty($where)) { $this->error = '参数不正确。'; return false; } if (floor($where) > 0) { $where = ['id' => $where]; } return $this->where($where)->field($fields)->find(); } /** * 编辑 * @param $data * @return bool */ public function edit($data) { if (empty($data) || !is_array($data)) { $this->error = '参数不正确'; return false; } return $this->transaction(function () use ($data) { $res = $this->update($data); return $res; }); } // 删除 public function del($id) { if (empty($id)) { $this->error = '参数不正确'; return false; } return $this->transaction(function () use ($id) { return $this->where(['id' => $id])->delete(); }); } }