admin/addon/article/model/ArticleHistory.php

113 lines
3.8 KiB
PHP

<?php
/** ZJMall商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2022-2032 四川正今科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.zjphp.com
* 这不是自由软件!未经允许不得用于商业目或程序代码摘取及修改。
* 任何企业和个人不允许对程序代码以任何形式任何目的再发布传播。
* 唯一发布渠道官方颁发授权证书,无纸质授权凭证书视为侵权行为。
* =========================================================
*/
namespace addon\article\model;
use app\model\NewBaseModel;
class ArticleHistory extends NewBaseModel{
protected $pk = 'id';
//protected $autoWriteTimestamp = false; // 开启自动时间戳
protected $autoWriteTimestamp = 'int'; // 开启自动时间戳
protected $createTime = 'create_time'; // 默认添加时间字段
protected $updateTime = 'update_time'; // 默认编辑时间字段
protected $deleteTime = false; // 软删除字段
protected $type = [
'time' => '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');
}
}