| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- declare (strict_types=1);
- namespace app\model\api;
- use library\basic\BaseModel;
- use think\Model;
- /**
- * 论坛帖子模型
- * @mixin \think\Model
- */
- class ForumPost extends BaseModel
- {
- protected $pk = 'id';
- protected $table = 'forum_post';
- /**
- * 获取帖子列表
- * @param int $page 页码
- * @param int $pageSize 每页数量
- * @param array $where 筛选条件
- * @return array
- */
- public function getList($page = 1, $pageSize = 20, $where = [])
- {
- $pageSize = $pageSize > 50 ? 50 : (int)$pageSize;
- $page = $page <= 0 ? 1 : (int)$page;
- $where[] = ['status', '=', 1];
- $query = $this->alias('p')
- ->field('p.*, u.nickname as author_name, u.avatar as author_avatar')
- ->leftJoin('user u', 'u.uid = p.uid')
- ->where($where)
- ->order('p.is_top', 'desc')
- ->order('p.create_time', 'desc');
- $total = $query->count();
- $list = [];
- if ($total > 0) {
- $list = $query->page($page, $pageSize)->select();
- if (!empty($list)) {
- $list = $list->toArray();
- foreach ($list as &$item) {
- // 处理图片JSON
- if (!empty($item['images'])) {
- $item['images'] = json_decode($item['images'], true) ?: [];
- } else {
- $item['images'] = [];
- }
- // 格式化时间
- if (!empty($item['create_time'])) {
- $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
- }
- }
- }
- }
- return [
- 'list' => $list,
- 'total' => $total,
- 'page' => $page,
- 'pageSize' => $pageSize
- ];
- }
- /**
- * 获取帖子详情
- * @param int $id 帖子ID
- * @return array|null
- */
- public function getDetail($id)
- {
- $post = $this->alias('p')
- ->field('p.*, u.nickname as author_name, u.avatar as author_avatar')
- ->leftJoin('user u', 'u.uid = p.uid')
- ->where('p.id', $id)
- ->where('p.status', 1)
- ->find();
- if (empty($post)) {
- return null;
- }
- $post = $post->toArray();
- // 处理图片JSON
- if (!empty($post['images'])) {
- $post['images'] = json_decode($post['images'], true) ?: [];
- } else {
- $post['images'] = [];
- }
- // 格式化时间
- if (!empty($post['create_time'])) {
- $post['create_time'] = date('Y-m-d H:i:s', $post['create_time']);
- }
- if (!empty($post['update_time'])) {
- $post['update_time'] = date('Y-m-d H:i:s', $post['update_time']);
- }
- // 增加浏览次数
- $this->where('id', $id)->inc('view_count')->update();
- return $post;
- }
- /**
- * 创建帖子
- * @param int $uid 用户ID
- * @param array $data 帖子数据
- * @return array [success, message, data]
- */
- public function createPost($uid, $data)
- {
- try {
- self::beginTrans();
- $insertData = [
- 'uid' => $uid,
- 'title' => trim($data['title'] ?? ''),
- 'content' => trim($data['content'] ?? ''),
- 'images' => !empty($data['images']) ? json_encode($data['images'], JSON_UNESCAPED_UNICODE) : '',
- 'status' => 0, // 待审核
- 'create_time' => time(),
- 'update_time' => time(),
- ];
- $result = $this->insert($insertData);
- if (!$result) {
- self::rollbackTrans();
- return [0, '发布失败'];
- }
- self::commitTrans();
- return [1, '发布成功,等待审核'];
- } catch (\Exception $e) {
- self::rollbackTrans();
- return [0, '发布失败:' . $e->getMessage()];
- }
- }
- /**
- * 获取用户发布的帖子列表
- * @param int $uid 用户ID
- * @param int $page 页码
- * @param int $pageSize 每页数量
- * @return array
- */
- public function getUserPosts($uid, $page = 1, $pageSize = 20)
- {
- $pageSize = $pageSize > 50 ? 50 : (int)$pageSize;
- $page = $page <= 0 ? 1 : (int)$page;
- $total = $this->where('uid', $uid)->count();
- $list = [];
- if ($total > 0) {
- $list = $this->where('uid', $uid)
- ->order('create_time', 'desc')
- ->page($page, $pageSize)
- ->select()
- ->toArray();
- foreach ($list as &$item) {
- if (!empty($item['images'])) {
- $item['images'] = json_decode($item['images'], true) ?: [];
- } else {
- $item['images'] = [];
- }
- if (!empty($item['create_time'])) {
- $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
- }
- }
- }
- return [
- 'list' => $list,
- 'total' => $total,
- 'page' => $page,
- 'pageSize' => $pageSize
- ];
- }
- }
|