2023-01-29 10:26:52 +08:00

112 lines
2.5 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\common\model;
use think\Db;
/**
* 智能推送
*/
class Intelligent extends Base{
//TYPE=1:必须传UID
/**
* articles [desc]
*
* @param type $type [消息类型 1定制书籍推荐 2猜你喜欢推荐 3关键词回复]
* @param type $uid [用户ID]
* @param type $cps_uid [平台用户ID]
* @param type $appid [平台微信公众号appid]
*
* @return type
**/
public function articles($type,$uid,$cps_uid,$appid) {
$data = array();
$num=4;
$cover="/uploads/history.png";//720*400封面图
$site_url = get_menuurl($cps_uid,0,$appid);
switch($type){
//定制书籍推荐
case '1':
$history = db('history')->where(array("model"=>1,"uid"=>$uid))->order("update_time DESC")->find();
if($history){
$data[] = array(
"title"=>"继续阅读:《".$history["name"]."",
"picurl"=>$site_url.$cover,
"url"=>$site_url."/chapter/read/".$history["articleid"]."/".$history["chapterid"],
);
}else{
$list = $this->random_data(1,"novel",array("status"=>1,"allvisit"=>array(">=",100)),"id,title,cover");
if($list){
foreach ($list as $k=>$v){
$data[] = array(
"title"=>$v["title"],
"picurl"=>$site_url.$cover,
"url"=>$site_url."/book/info/id/".$v["id"],
);
}
}
}
$data["articles"] = $data;
break;
case '2'://猜你喜欢推荐
$list = $this->random_data($num,"novel",array("status"=>1,"allvisit"=>array(">=",100)),"id,title,cover");
$noveldata = array();
if($list){
foreach ($list as $k=>$v){
$noveldata[] = array(
"title"=>$v["title"],
"picurl"=>$site_url.$v["cover"],
"url"=>$site_url."/book/info/id/".$v["id"],
);
}
}
$data["articles"] =$noveldata;
break;
}
return $data;
}
public function random_data($num,$table,$where=[],$field=null)
{
$pk = Db::name($table)->getPK();//获取主键
$info = Db::name($table)->where($where)->field($pk)->select();//查询数据
$list = array();
if(count($info)>=$num){
$n = '';
$qu = '';
foreach($info as $v=>$val){
$n[$val[$pk]]= $val[$pk];
}
$rand = array_rand($n,$num);
if(is_array($rand)){
$qu = implode(',',$rand);
}else{
$qu = $rand;
}
if($field){
$list = Db::name($table)->where($pk,'in',$qu)->field($field)->select();
}else{
$list = Db::name($table)->where($pk,'in',$qu)->select();
}
}
return $list;
}
}