title = $form['title']; $record->author = $form['author'] ?? ''; $record->sources = $form['sources'] ?? ''; $record->thumb_img = $form['thumb_img'] ?? 0; $record->admin_id = $form['admin_id']; $record->keywords = $form['keywords'] ?? ''; $record->desc = $form['desc']; $record->contents = $form['contents']; return $record->save(); } public function getItem($id) { return $this->with(['file'])->where('id', $id) ->where('is_delete', 0) ->find(); } /** * 新增功能 * @param $form * @return bool */ public function updateOne($form) { $record['title'] = $form['title']; $record['author'] = $form['author'] ?? ''; $record['sources'] = $form['sources'] ?? ''; $record['thumb_img'] = $form['thumb_img']; $record['update_admin_id'] = $form['update_admin_id']; $record['keywords'] = $form['keywords'] ?? ''; $record['desc'] = $form['desc']; $record['contents'] = $form['contents']; try { $this->where('id', $form['id'])->update($record); } catch (\Exception $e) { $this->error = $e->getMessage(); log_record($e->getMessage(), 'error'); return false; } return true; } /** * 发布中/下线 * @param $form * @return bool */ public function publishOne($form) { try { $this->where('id', $form['id'])->update(['pub_status' => $form['pub_status']]); } catch (\Exception $e) { $this->error = $e->getMessage(); log_record($e->getMessage(), 'error'); return false; } return true; } /** * 是否推荐到首页 * @param $form * @return bool */ public function isTopOne($form) { try { $this->where('id', $form['id'])->update(['is_top' => $form['is_top']]); } catch (\Exception $e) { $this->error = $e->getMessage(); log_record($e->getMessage(), 'error'); return false; } return true; } /** * 删除新闻 * @param $form * @return bool */ public function deleteOne($form) { try { $this->where('id', $form['id'])->update(['is_delete' => 1]); } catch (\Exception $e) { $this->error = $e->getMessage(); log_record($e->getMessage(), 'error'); return false; } return true; } public function getItems($isTop = 0) { $model = $this->with(['file'])->where('is_delete', 0) ->where('pub_status', 0); if ($isTop) { $model = $model->where('is_top', 1); } return $model->order('id desc')->field('id,title,admin_id,sources,desc,thumb_img,create_time,author,contents') ->order('id desc') ->select() ->each(function (&$item) { $item['img'] = $item['file']['preview_url']; unset($item['file']); }) ->toArray(); } /** * 后台新闻列表 * @return \think\Paginator * @throws \think\db\exception\DbException */ public function tableItems() { return $this->with(['creator', 'updator']) ->where('is_delete', 0) ->field('id,title,admin_id,update_admin_id,sources,desc,thumb_img,sources,pub_status,is_top,create_time,update_time') ->order('id desc') ->paginate(15); } public function creator() { return $this->belongsTo(User::class, 'admin_id', 'store_user_id') ->field('store_user_id,user_name,real_name'); } public function updator() { return $this->belongsTo(User::class, 'update_admin_id', 'store_user_id') ->field('store_user_id,user_name,real_name'); } }