123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- declare (strict_types=1);
- namespace app\store\model\enterprise;
- use app\common\model\enterprise\EnterpriseNews as EnModel;
- use app\store\model\store\User;
- /**
- * 官网新闻
- * Class EnterpriseNews
- * @package app\common\model
- */
- class EnterpriseNews extends EnModel
- {
- public function getContentsAttr($data)
- {
- return htmlspecialchars_decode($data);
- }
- /**
- * 新增功能
- * @param $form
- * @return bool
- */
- public function addOne($form)
- {
- $record = new self();
- $record->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');
- }
- }
|