Files
heping-api/app/controller/Article.php
2022-09-08 16:43:38 +08:00

116 lines
4.0 KiB
PHP
Raw 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\controller;
use think\facade\Db;
class Article
{
public function plus()
{
$post = $GLOBALS['data']['data'];
$userid = $GLOBALS['data']['userid'];
if(empty($userid)){
return show("请登录后再发布!");
}
if(empty($post['content'])){
return show("内容不能为空!");
}
if(empty($post['imgs'])){
return show("图片不能为空!");
}
Db::name("app_article")->insert([
"text"=>$post['content'],
"imgs"=>$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("请登录后操作!");
}
$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("请登录后操作!");
}
$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;
$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);
}
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("找不到该动态信息!");
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;
$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);
}
}