admin/addon/article/model/Article.php

234 lines
8.5 KiB
PHP

<?php
/** ZJMall商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2022-2032 四川正今科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.zjphp.com
* 这不是自由软件!未经允许不得用于商业目或程序代码摘取及修改。
* 任何企业和个人不允许对程序代码以任何形式任何目的再发布传播。
* 唯一发布渠道官方颁发授权证书,无纸质授权凭证书视为侵权行为。
* =========================================================
*/
namespace addon\article\model;
use app\model\NewBaseModel;
use app\model\newModel\common\CaChe;
use app\model\system\Cron;
use think\facade\Db;
use think\facade\Log;
class Article extends NewBaseModel{
protected $pk = 'article_id';
protected $caCheName = 'article_list';
protected $append = [
'category_name'
];
protected $autoWriteTimestamp = 'int'; // 开启自动时间戳
protected $createTime = 'create_time'; // 默认添加时间字段
protected $updateTime = 'update_time'; // 默认编辑时间字段
/**
* Common: 列表获取
* Author: wu-hui
* Time: 2022/11/03 16:39
* @param array $field
* @return array
* @throws \think\db\exception\DbException
*/
public function getList(){
// 参数获取
$page = input('page',1);
$pageSize = input('page_size',PAGE_LIST_ROWS);
$status = (int)input('status',-1);
$searchText = (string)trim(input('search_text'));
$ids = (string)trim(input('ids'),',');
$cateIds = (string)trim(input('cate_ids'),',');
$orders = (string)input('orders','');
$sort = (string)input('sort');
$isRand = (boolean)input('is_rand');
// 排序
$order = ['create_time'=>'DESC'];
if($sort) $order = ['sort'=>$sort];
else if($orders == 'new') $order = ['create_time'=>'DESC'];// 最新
else if($orders == 'recommend') $order = ['sort'=>'DESC','create_time'=>'DESC'];// 推荐
else if($orders == 'hot') $order = ['read_num'=>'DESC','create_time'=>'DESC'];// 热门
else if($orders == 'fabulous') $order = ['dianzan_num'=>'ASC','create_time'=>'DESC'];// 点赞
// 列表获取
$field = [
'article_id',
'article_title',
'category_id',
'status',
'sort',
'create_time',
'update_time',
'article_abstract',
'cover_img',
'is_show_release_time',
'is_show_read_num',
'is_show_dianzan_num',
"(read_num + initial_read_num) as read_num",
"(dianzan_num + initial_dianzan_num) as dianzan_num",
];
$result = $this
->field($field)
->where('site_id',$this->site_id)
->when($status >= 0,function($query) use ($status){
$query->where('status','=',$status);
})
->when(strlen($searchText) > 0,function($query) use ($searchText){
$query->where('article_title','like',"%{$searchText}%");
})
->when($ids,function($query) use ($ids){
$query->where('article_id','in',explode(',',$ids));
})
->when($cateIds,function($query) use ($cateIds){
$query->where('category_id','in',explode(',',$cateIds));
})
->when($isRand,function($query){
$query->orderRaw('rand()');
},function($query) use ($order){
$query->order($order);
})
->paginate(['list_rows' => $pageSize,'page' => $page]);
if($result) {
$result = $result->toArray();
$categoryIds = array_column($result['data'],'category_id');
$cateList = (new ArticleCategory())->getListCateList($categoryIds);
foreach($result['data'] as &$item){
$item['category_name'] = $cateList[$item['category_id']];
}
}
$list = [
'count' => $result['total'],
'list' => $result['data'],
'page_count' => $result['last_page'],
];
return $this->success($list);
}
/**
* Common: 编辑信息
* Author: wu-hui
* Time: 2022/11/07 16:47
* @param $info
* @return array
*/
public function editInfo($info){
// 添加文章的操作
$this->startTrans();
try{
// 判断是添加还是修改
if((int)$info[$this->pk] > 0) {
self::update($info, [$this->pk => $info[$this->pk]]);// 修改内容
}else {
$res = self::create($info);
if($info['status'] == 1) $this->messageRecord($info,$res->article_id);
}
$this->commit();
//清除缓存
CaChe::del('article_list');
return $this->success();
}catch(\Exception $e){
$this->rollback();
return $this->error('',$e->getMessage());
}
}
/**
* Common: 删除信息
* Author: wu-hui
* Time: 2022/11/07 16:44
* @param $id
* @return array
*/
public function delInfo($id){
//清除缓存
CaChe::del('article_list');
//编辑信息
return parent::delInfo($id);
}
/**
* Common: 添加模板消息通知
* Author: wu-hui
* Time: 2022/11/10 15:01
* @param $info
* @param $articleId
* @return int
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function messageRecord($info,$articleId){
// 获取所有存在openid 的用户
$list = Db::name('member')
->field('member_id,nickname,wx_openid')
->where('site_id','=',$info['site_id'])
->where('wx_openid','<>','')
->select();
$insert = [];
foreach($list as $item){
$insert[] = [
'member_id' => $item['member_id'],
'nickname' => $item['nickname'],
'wx_openid' => $item['wx_openid'],
'article_id' => $articleId,
'article_title' => $info['article_title'],
'create_time' => time(),
];
}
Db::name('article_message')->insertAll($insert);
// 开启计划任务
$cronWhere = [
'event' => 'HandleArticleMessage',
];
$cronId = model('cron')->getValue($cronWhere,'id');
if($cronId <= 0) (new Cron())->addCron(2, 5, "处理文章通知消息", "HandleArticleMessage", time(),0);
}
/**
* Common: 发送模板消息通知
* Author: wu-hui
* Time: 2022/11/10 18:46
* @throws \think\db\exception\DbException
*/
public function sendMessage(){
Log::write("文章消息通知 - 开始");
// 基本信息
$messageTemplate = Model('message_template')->getValue([['keywords','=','ARTICLE_MESSAGE']],'wechat_json');
$data["site_id"] = (int)request()->siteid();
$data["message_info"] = Model('message')->getInfo([['keywords','=','ARTICLE_MESSAGE']]);
$data["message_info"]['wechat_json'] = $messageTemplate;
// 获取列表
$list = Db::name('article_message')
->order('id','ASC')
->limit(30)
->select();
if($list) $list = $list->toArray();
$successIds = [];
foreach($list as $item){
$data["openid"] = $item['wx_openid'];
$data["template_data"] = [
'keyword1' => $item['article_title'],
'keyword2' => '成功发布',
'keyword3' => date('Y-m-d H:i:s',$item['create_time']),
];
$data["page"] = '#/pages_aijiu/article/detail?article_id='.$item['article_id'];
$res = (new \addon\wechat\model\Message())->sendMessage($data);
///if($res['code'] === 0) $successIds[] = $item['id'];
$successIds[] = $item['id'];// 由于积累问题 每次通知只会发布一次,无论成功失败 然后删除
}
Log::debug("文章消息通知 - 删除",$successIds);
if(count($successIds) > 0) Db::name('article_message')->where([['id','in',$successIds]])->delete();
Log::debug("文章消息通知 - 结束",$successIds);
}
}