'timestamp:Y-m-d H:i', ]; /** * Common: 文章浏览历史变更 * Author: wu-hui * Time: 2022/11/04 14:10 * @param $memberId * @param $id * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function addInfo($memberId,$id){ // 阅读数量添加 (new Article())->where('article_id', $id)->inc('read_num', 1)->update(); // 记录浏览历史 $info = $this ->where('article_id',$id) ->where('member_id',$memberId) ->find(); if(!$info) $info = new self(); $info->article_id = $id; $info->member_id = $memberId; $info->frequency += 1; $info->article_id = $id; $info->save(); } /** * Common: 获取用户浏览记录 * Author: wu-hui * Time: 2022/11/07 14:32 * @param $memberId * @return array */ public function record($memberId){ // 参数获取 $page = input('page',1); $pageSize = input('page_size',PAGE_LIST_ROWS); $searchText = (string)trim(input('search_text')); // 列表获取 $result = $this->alias('ah') ->field('a.article_id,a.article_title,a.cover_img,ah.create_time as time') ->join('article a','a.article_id = ah.article_id','LEFT') ->where('a.status',1) ->where('a.site_id',$this->site_id) ->where('ah.member_id',$memberId) ->when(strlen($searchText) > 0,function($query) use ($searchText){ $query->where('a.article_title','like',"%{$searchText}%"); }) ->order('ah.create_time','DESC') ->paginate(['list_rows' => $pageSize,'page' => $page]); if($result) $result = $result->toArray(); $list = [ 'count' => $result['total'], 'list' => $result['data'], 'page_count' => $result['last_page'], ]; return $this->success($list); } /** * Common: 删除浏览记录 * Author: wu-hui * Time: 2022/11/07 14:54 * @param $articleId * @param $memberId * @return array */ public function delRecord($articleId,$memberId){ $res = $this->where('article_id',$articleId) ->where('member_id',$memberId) ->delete(); return $this->success($res); } /** * Common: 关联文章表 * Author: wu-hui * Time: 2022/11/08 20:06 * @return \think\model\relation\HasOne */ public function article(){ return $this->hasOne(Article::class,'article_id','article_id'); } }