Created by PhpStorm.
User: YangYongAn Date: 2018/6/20 Time: 21:56
T |
<?php
/**
* Created by PhpStorm.
* User: YangYongAn
* Date: 2018/6/20
* Time: 21:56
*/
namespace app\index\controller;
use think\Controller;
use app\index\model;
use think\Db;
class T extends Controller
{
function post()
{
if (uid()) {
attract_form_id(input('form_id'));
$location = input('location/a');
$address = location2address($location['lat'], $location['lng']);
$data = $address;
$data['content'] = input('content');
$data['simple'] = input('simple');
$data['imgs'] = json_encode(input('imgs/a'));
$data['auth'] = input('auth');
$data['user_id'] = uid();
$data['create_time'] = time();
$data['lat'] = $location['lat'];
$data['lng'] = $location['lng'];
$t = db('t')->insertGetId($data);
if ($t) {
$data['id'] = $t;
$this->success("发布成功", '', $data);
} else {
$this->error("发布失败");
}
} else {
$this->error("请登录使用");
}
}
function index()
{
$T = new model\T();
$t = $T->with(['user' => function ($q) {
$q->field('username,id,nickname,avatar');
}])->with('like')
->withCount('review')
->with(['review' => function ($q) {
$q->field('t_review.id,parent,u.id as uid,nickname,content,avatar')
->order('t_review.id', 'desc')
->join('system_user u', 't_review.user_id = u.id')
->limit(99);
}])
->where('is_deleted', 0)
->order('create_time', 'DESC')
->paginate(5);
if ($t) {
$this->result($t, 1);
} else {
$this->error("暂无数据");
}
}
function do_like()
{
if (uid()) {
$t_id = input('t_id');
$T = new model\T();
if ($T::get($t_id)) {
$map['t_id'] = $t_id;
$map['user_id'] = uid();
$liked = db('t_like')->where($map)->find();
if ($liked) {
$like = db('t_like')->delete($liked['id']);
if ($like) {
$this->success("取消点赞", '', '-');
} else {
$this->error("取消失败,请重试,1");
}
} else {
$like = db('t_like')->insertGetId($map);
if ($like) {
$this->success("点赞成功", '', $like);
} else {
$this->error("点赞失败,请重试,1");
}
}
} else {
$this->error("点赞失败,tid有毛病啊");
}
} else {
$this->error("请登录后再赞哦");
}
}
function add_review()
{
if (uid()) {
$data['parent'] = input('parent');
$data['content'] = input('content');
if ($data['parent'] && $data['content']) {
$data['create_time'] = time();
$data['user_id'] = uid();
$data['imgs'] = json_encode(input('imgs/a'));
$review = db('t_review')->insertGetId($data);
if ($review) {
$data['id'] = $review;
$this->success("评论成功", '', $data);
} else {
$this->error("评论服务器稍微出了点问题,请重新尝试");
}
} else {
$this->error("请检查输入的内容或者目标id");
}
} else {
$this->error("请登录后再评论哦");
}
}
function detail()
{
$id = input('id');
$t = model\T::get($id);
$t['user'] = $t->user;
unset($t['user']['password']);
if ($t) {
$t->setInc('click');
$this->success("ok", '', $t);
} else {
$this->error("详情页被吃掉了");
}
}
}