113 lines
3.8 KiB
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;
|
|
use think\Exception;
|
|
|
|
class ArticleFabulous extends NewBaseModel{
|
|
|
|
protected $pk = 'id';
|
|
protected $autoWriteTimestamp = false; // 开启自动时间戳
|
|
protected $deleteTime = false; // 软删除字段
|
|
protected $type = [
|
|
'time' => 'timestamp:Y-m-d H:i',
|
|
];
|
|
|
|
/**
|
|
* Common: 判断当前用户是否对该文章点赞
|
|
* Author: wu-hui
|
|
* Time: 2022/11/03 15:11
|
|
* @param $articleId
|
|
* @param $memberId
|
|
* @return bool
|
|
*/
|
|
public function isFabulous($articleId,$memberId){
|
|
return (int)$this
|
|
->where('article_id',$articleId)
|
|
->where('member_id',$memberId)
|
|
->value('id');
|
|
}
|
|
/**
|
|
* Common: 点赞 || 取消点赞
|
|
* Author: wu-hui
|
|
* Time: 2022/11/03 15:18
|
|
* @param $id
|
|
* @param $memberId
|
|
* @return array
|
|
*/
|
|
public function fabulousOperation($id,$memberId){
|
|
$articleModel = new Article();
|
|
$this->startTrans();
|
|
try{
|
|
$fabulousId = $this->isFabulous($id,$memberId);
|
|
if($fabulousId > 0) {
|
|
// 已点赞 取消点赞
|
|
$this->where('id',$fabulousId)->delete();
|
|
// 减少点赞数量
|
|
$articleModel->where('article_id', $id)->dec('dianzan_num', 1)->update();
|
|
}else{
|
|
// 未点赞 进行点赞操作
|
|
$data = [
|
|
'article_id' => $id,
|
|
'member_id' => $memberId,
|
|
'create_time' => time(),
|
|
];
|
|
$this->insert($data);
|
|
// 增加点赞数量
|
|
$articleModel->where('article_id', $id)->inc('dianzan_num', 1)->update();
|
|
}
|
|
|
|
$this->commit();
|
|
return $this->success();
|
|
}catch(Exception $e){
|
|
$this->rollback();
|
|
return $this->error('',$e->getMessage());
|
|
}
|
|
}
|
|
/**
|
|
* Common: 获取用户点赞记录
|
|
* Author: wu-hui
|
|
* Time: 2022/11/07 14:29
|
|
* @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('af')
|
|
->field('a.article_id,a.article_title,a.cover_img,af.create_time as time')
|
|
->join('article a','a.article_id = af.article_id','LEFT')
|
|
->where('a.status',1)
|
|
->where('a.site_id',$this->site_id)
|
|
->where('af.member_id',$memberId)
|
|
->when(strlen($searchText) > 0,function($query) use ($searchText){
|
|
$query->where('a.article_title','like',"%{$searchText}%");
|
|
})
|
|
->order('af.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);
|
|
}
|
|
|
|
|
|
} |