Index.php 8.4 KB
<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * @ApiTitle    (首页项目列表)
     * @ApiSummary  (农场项目列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/index)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="type", type="int", required=false, description="类型(不传或者1为已收获2将收获3培育中)")
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
            "id":项目ID,
            "farm_id":农场ID
            'project_name'://项目名称,
            "image"://项目图片
            'farm_name'://农场名称,
            "avatar"://头像
            'look_num'://浏览量,
            "good_num"://点赞量
            'thumbnail'://图片
            "address"://项目地址
            "status"://是否点过赞(1已经点过2没有点过)
            "hong_status"://0未授权1已经授权
            'createtime'://创建时间
        }
        })
     */
    public function index()
    {
        $token = $this->request->header('token');
        $type = $this->request->param('type');
        $page = $this->request->param('page',1,'intval');
        $pageNum = $this->request->param('pageNum',6,'intval');

        //判断用户是否登录
       if($token){
           $user_id = $this->getUserId();
           if(empty($type) || $type == 1){
               $data = Db::name('project')
                   ->where('status',2)
                   ->field('id,farm_id,project_name,address,image,good_num')
                   ->order('good_num desc,id desc')
                   ->page($page,$pageNum)
                   ->select();
               foreach ($data as &$v){
                   $v['image'] = 'http://q2ugvq3qf.bkt.clouddn.com'.$v['image'];
                   $zan = Db::name('good')
                       ->where('project_id',$v['id'])
                       ->where('user_id',$user_id)
                       ->find();
                   if($zan){
                       $v['statu'] = 1;
                   }else{
                       $v['statu'] = 2;
                   }
                   $farm = Db::name('farm')
                       ->alias('a')
                       ->join('user b','a.user_id = b.id')
                       ->field('a.name,b.avatar')
                       ->where('a.id',$v['farm_id'])
                       ->find();
                   $v['avatar'] = $farm['avatar'];
                   $v['farm_name'] = $farm['name'];
                   $v['hong_status'] = 1;
               }
               $this->success('success',$data);
           }elseif ($type == 2){
               $data = Db::name('project')
                   ->where('status',1)
                   ->field('id,farm_id,project_name,address,image,good_num')
                   ->order('good_num desc,id desc')
                   ->page($page,$pageNum)
                   ->select();
               foreach ($data as &$v){
                   $v['image'] = 'http://q2ugvq3qf.bkt.clouddn.com'.$v['image'];
                   $zan = Db::name('good')
                       ->where('project_id',$v['id'])
                       ->where('user_id',$user_id)
                       ->find();
                   if($zan){
                       $v['statu'] = 1;
                   }else{
                       $v['statu'] = 2;
                   }
                   $farm = Db::name('farm')
                       ->alias('a')
                       ->join('user b','a.user_id = b.id')
                       ->field('a.name,b.avatar')
                       ->where('a.id',$v['farm_id'])
                       ->find();
                   $v['avatar'] = $farm['avatar'];
                   $v['farm_name'] = $farm['name'];
                   $v['hong_status'] = 1;
               }
               $this->success('success',$data);
           }elseif ($type == 3){
               $data = Db::name('project')
                   ->where('status',0)
                   ->field('id,farm_id,project_name,address,image,good_num')
                   ->order('good_num desc,id desc')
                   ->page($page,$pageNum)
                   ->select();
               foreach ($data as &$v){
                   $v['image'] = 'http://q2ugvq3qf.bkt.clouddn.com'.$v['image'];
                   $zan = Db::name('good')
                       ->where('project_id',$v['id'])
                       ->where('user_id',$user_id)
                       ->find();
                   if($zan){
                       $v['statu'] = 1;
                   }else{
                       $v['statu'] = 2;
                   }
                   $farm = Db::name('farm')
                       ->alias('a')
                       ->join('user b','a.user_id = b.id')
                       ->field('a.name,b.avatar')
                       ->where('a.id',$v['farm_id'])
                       ->find();
                   $v['avatar'] = $farm['avatar'];
                   $v['farm_name'] = $farm['name'];
                   $v['hong_status'] = 1;
               }
               $this->success('success',$data);
           }else{
               $this->error('类型错误');
           }
       }else{
           if(empty($type) || $type == 1){
               $data = Db::name('project')
                   ->where('status',2)
                   ->field('id,farm_id,project_name,address,image,good_num')
                   ->order('good_num desc,id desc')
                   ->page($page,$pageNum)
                   ->select();
               foreach ($data as &$v){
                   $v['image'] = 'http://q2ugvq3qf.bkt.clouddn.com'.$v['image'];
                   $farm = Db::name('farm')
                       ->alias('a')
                       ->join('user b','a.user_id = b.id')
                       ->field('a.name,b.avatar')
                       ->where('a.id',$v['farm_id'])
                       ->find();
                   $v['avatar'] = $farm['avatar'];
                   $v['farm_name'] = $farm['name'];
                   $v['hong_status'] = 0;
               }
               $this->success('success',$data);
           }elseif ($type == 2){
               $data = Db::name('project')
                   ->where('status',1)
                   ->field('id,farm_id,project_name,address,image,good_num')
                   ->order('good_num desc,id desc')
                   ->page($page,$pageNum)
                   ->select();
               foreach ($data as &$v){
                   $v['image'] = 'http://q2ugvq3qf.bkt.clouddn.com'.$v['image'];
                   $farm = Db::name('farm')
                       ->alias('a')
                       ->join('user b','a.user_id = b.id')
                       ->field('a.name,b.avatar')
                       ->where('a.id',$v['farm_id'])
                       ->find();
                   $v['avatar'] = $farm['avatar'];
                   $v['farm_name'] = $farm['name'];
                   $v['hong_status'] = 0;
               }
               $this->success('success',$data);
           }elseif ($type == 3){
               $data = Db::name('project')
                   ->where('status',0)
                   ->field('id,farm_id,project_name,address,image,good_num')
                   ->order('good_num desc,id desc')
                   ->page($page,$pageNum)
                   ->select();
               foreach ($data as &$v){
                   $v['image'] = 'http://q2ugvq3qf.bkt.clouddn.com'.$v['image'];
                   $farm = Db::name('farm')
                       ->alias('a')
                       ->join('user b','a.user_id = b.id')
                       ->field('a.name,b.avatar')
                       ->where('a.id',$v['farm_id'])
                       ->find();
                   $v['avatar'] = $farm['avatar'];
                   $v['farm_name'] = $farm['name'];
                   $v['hong_status'] = 0;
               }
               $this->success('success',$data);
           }else{
               $this->error('类型错误');
           }
       }



    }
}