144 lines
5.1 KiB
PHP
144 lines
5.1 KiB
PHP
<?php
|
||
namespace app\controller;
|
||
|
||
use think\facade\Db;
|
||
|
||
class Article
|
||
{
|
||
public function plus()
|
||
{
|
||
$post = $GLOBALS['data']['data'];
|
||
$userid = $GLOBALS['data']['userid'];
|
||
if(empty($userid)){
|
||
return show("请登录后再发布!",NEED_LOGIN);
|
||
}
|
||
if(empty($post['content'])){
|
||
return show("内容不能为空!");
|
||
}
|
||
|
||
if(empty($post['imgs'])){
|
||
return show("图片不能为空!");
|
||
}
|
||
|
||
Db::name("app_article")->insert([
|
||
"text"=>$post['content'],
|
||
"imgs"=>json_encode($post['imgs']),
|
||
"userid"=>$userid,
|
||
"createtime"=>time(),
|
||
"click"=>0
|
||
]);
|
||
Db::name("student")->inc("article_count",1)->where("id",$userid)->update();
|
||
return show("发布成功!",SUCCESS_CODE,[]);
|
||
}
|
||
|
||
public function delete(){
|
||
$userid = $GLOBALS['data']['userid'];
|
||
if(empty($userid)){
|
||
return show("请登录后操作!",NEED_LOGIN);
|
||
}
|
||
$article_id = $GLOBALS['data']['data']["article_id"];
|
||
if(empty($article_id)){
|
||
return show("请上输入动态ID");
|
||
}
|
||
|
||
$info = Db::name("app_article")->where("id",$article_id)->find();
|
||
if(empty($info) || $info['userid']!=$userid){
|
||
return show("找不到该动态信息!");
|
||
}
|
||
Db::name("app_article")->where("id",$article_id)->delete();
|
||
return show("删除成功!",SUCCESS_CODE,[]);
|
||
}
|
||
|
||
public function lists(){
|
||
$userid = $GLOBALS['data']['userid'];
|
||
if(empty($userid)){
|
||
return show("请登录后操作!",NEED_LOGIN);
|
||
}
|
||
$lastIndex = empty($GLOBALS['data']['data']["lastindex"])?0:$GLOBALS['data']['data']["lastindex"];
|
||
if($lastIndex == 0){
|
||
$where = "userid={$userid} and id>0";
|
||
}else{
|
||
$where = "userid={$userid} and id<".$lastIndex;
|
||
}
|
||
$result["lastIndex"] = 0;
|
||
$result["list"] = [];
|
||
$list = Db::name("app_article")->where($where)->order('id desc')->limit(env("page_count"))->select()->toArray();
|
||
foreach($list as $vo){
|
||
$result['lastIndex'] = $vo['id'];
|
||
$result["list"][] = $vo;
|
||
}
|
||
if(count($list)<env("PAGE_COUNT")){
|
||
$result["lastIndex"] = 0;
|
||
}
|
||
return show(SUCCESS_MESSAGE,SUCCESS_CODE,$result);
|
||
}
|
||
|
||
public function detail(){
|
||
if(empty($GLOBALS['data']['data']['article_id'])){
|
||
return show("请上传动态ID!");
|
||
}
|
||
$article_id = $GLOBALS['data']['data']['article_id'];
|
||
$info = Db::name('app_article')->where("id",$article_id)->find();
|
||
if(empty($info)) return show("找不到该动态信息!");
|
||
if(!empty($info['imgs'])) {
|
||
$info['imgs'] = json_decode(str_replace(['{', '}'], ['[', ']'], $info['imgs']), true);
|
||
}
|
||
if(empty($info['imgs'])) $info['imgs'] = [];
|
||
$info['other_list'] = [];
|
||
$lists = Db::name('app_article')
|
||
->where("userid",$info["userid"])
|
||
->where("id","<>",$info['id'])
|
||
->order("id desc")
|
||
->limit(env("page_count"))
|
||
->select();
|
||
foreach($lists as $vo){
|
||
if(!empty($vo['imgs'])) {
|
||
$vo['imgs'] = json_decode(str_replace(['{', '}'], ['[', ']'], $vo['imgs']), true);
|
||
$vo['imgs']= $vo['imgs'][0];
|
||
}
|
||
if(empty($vo['imgs'])) $vo['imgs'] = "";
|
||
$info['other_list'][] = $vo;
|
||
}
|
||
if(!empty($info['other_list'])) shuffle($info['other_list']);
|
||
$info['userinfo']=[];
|
||
$userinfo = Db::name("student")->where("id",$info['userid'])->field("nickname,avatar,age,type,disabled as is_disabled,city,school,hot")->find();
|
||
$info['userinfo'] = $userinfo;
|
||
|
||
return show(SUCCESS_MESSAGE,SUCCESS_CODE,$info);
|
||
}
|
||
|
||
public function getlists(){
|
||
/*
|
||
$userid = $GLOBALS['data']['userid'];
|
||
if(empty($userid)){
|
||
return show("请登录后操作!");
|
||
}
|
||
*/
|
||
if(empty($GLOBALS['data']['data']['student_userid'])){
|
||
return show("请输入用户ID!");
|
||
}
|
||
$student_userid = $GLOBALS['data']['data']['student_userid'];
|
||
|
||
$student = Db::name("student")->field("id")->where("id",$student_userid)->find();
|
||
|
||
if(empty($student)) return show("找不到用户信息!");
|
||
|
||
$lastIndex = empty($GLOBALS['data']['data']["lastindex"])?0:$GLOBALS['data']['data']["lastindex"];
|
||
if($lastIndex == 0){
|
||
$where = "userid={$student_userid} and id>0";
|
||
}else{
|
||
$where = "userid={$student_userid} and id<".$lastIndex;
|
||
}
|
||
$result["lastIndex"] = 0;
|
||
$result["list"] = [];
|
||
$list = Db::name("app_article")->where($where)->order("id desc")->order('id desc')->limit(env("page_count"))->select()->toArray();
|
||
foreach($list as $vo){
|
||
$result['lastIndex'] = $vo['id'];
|
||
$result["list"][] = $vo;
|
||
}
|
||
if(count($list)<env("PAGE_COUNT")){
|
||
$result["lastIndex"] = 0;
|
||
}
|
||
return show(SUCCESS_MESSAGE,SUCCESS_CODE,$result);
|
||
}
|
||
} |