Government.php 6.8 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/12/26
 * Time: 17:37
 */

namespace app\api\controller;


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

/**
 * 农户认证
 */
class Government extends Api
{
    /**
     * @ApiTitle    (农场列表)
     * @ApiSummary  (农场列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/government/index)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="type", type="inter", 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
            "createtime"://申请日期
            "name"://农场名称
            "avatar"://用户头像
            "content"://农场简介
            "address"://农场地址
            }
        })
     */
    public function index(){
        $user_id = $this->getUserId();
        //首先查出政府入驻信息
        $government = Db::name('government')
            ->where('user_id',$user_id)
            ->find();
        $type = $this->request->param('type');
        if(empty($type) || $type == 1){
            //待认证

            //查出是属于该政府审核的农场信息
            $data = Db::name('attestation')
                ->where('institution_id',$government['id'])
                ->field('id,farm_id,createtime,user_id')
                ->where('status',0)
                ->select();
            foreach ($data as &$v){
                $v['createtime'] = date('Y-m-d',$v['createtime']);
                $farm = Db::name('farm')
                    ->alias('a')
                    ->join('user b','a.user_id = b.id')
                    ->where('a.id',$v['farm_id'])
                    ->field('a.name,a.content,a.address,b.avatar')
                    ->find();
                $v['name'] = $farm['name'];
                $v['content'] = $farm['content'];
                $v['address'] = $farm['address'];
                $v['avatar'] = $farm['avatar'];
            }
            $this->success('success',$data);
        }elseif($type == 2){
            //已通过

            //查出是属于该政府审核的农场信息
            $data = Db::name('attestation')
                ->where('institution_id',$government['id'])
                ->field('id,farm_id,createtime,user_id')
                ->where('status',1)
                ->select();
            foreach ($data as &$v){
                $v['createtime'] = date('Y-m-d',$v['createtime']);
                $farm = Db::name('farm')
                    ->alias('a')
                    ->join('user b','a.user_id = b.id')
                    ->where('a.id',$v['farm_id'])
                    ->field('a.name,a.content,a.address,b.avatar')
                    ->find();
                $v['name'] = $farm['name'];
                $v['content'] = $farm['content'];
                $v['address'] = $farm['address'];
                $v['avatar'] = $farm['avatar'];
            }
            $this->success('success',$data);
        }elseif ($type == 3){
            //已拒绝

            //查出是属于该政府审核的农场信息
            $data = Db::name('attestation')
                ->where('institution_id',$government['id'])
                ->field('id,farm_id,createtime,user_id')
                ->where('status',2)
                ->select();
            foreach ($data as &$v){
                $v['createtime'] = date('Y-m-d',$v['createtime']);
                $farm = Db::name('farm')
                    ->alias('a')
                    ->join('user b','a.user_id = b.id')
                    ->where('a.id',$v['farm_id'])
                    ->field('a.name,a.content,a.address,b.avatar')
                    ->find();
                $v['name'] = $farm['name'];
                $v['content'] = $farm['content'];
                $v['address'] = $farm['address'];
                $v['avatar'] = $farm['avatar'];
            }
            $this->success('success',$data);
        }
    }

    /**
     * @ApiTitle    (认证通过)
     * @ApiSummary  (认证通过)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/government/successful)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="id", type="inter", required=false, description="审核的ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {

    }
    })
     */
    public function successful()
    {
        $user_id = $this->getUserId();
        $id = $this->request->param('id');
        if(empty($id)){
            $this->error('缺少必要参数');
        }
        $res = Db::name('attestation')->where('id',$id)->find();
        if($res['status'] == 1 || $res['status'] == 2){
            $this->success('已经认证过了');
        }
        $ren['user_id'] = $res['user_id'];
        $ren['title'] = '您的认证申请已通过';
        $ren['createtime'] = time();
        Db::name('message')->insertGetId($ren);
        $data = Db::name('attestation')->where('id',$id)->update(['status'=>1]);
        if(empty($data)){
            $this->error('审核失败');
        }else{
            $this->success('审核成功');
        }
    }


    /**
     * @ApiTitle    (认证拒绝)
     * @ApiSummary  (认证拒绝)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/government/failed)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="id", type="inter", required=false, description="审核的ID")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1553839125",
    "data": {

    }
    })
     */
    public function failed()
    {
        $user_id = $this->getUserId();
        $id = $this->request->param('id');
        if(empty($id)){
            $this->error('缺少必要参数');
        }
        $res = Db::name('attestation')->where('id',$id)->find();
        if($res['status'] == 1 || $res['status'] == 2){
            $this->success('已经认证过了');
        }
        $ren['user_id'] = $res['user_id'];
        $ren['title'] = '您的认证申请已拒绝';
        $ren['createtime'] = time();
        Db::name('message')->insertGetId($ren);
        $data = Db::name('attestation')->where('id',$id)->update(['status'=>2]);
        if(empty($data)){
            $this->error('审核失败');
        }else{
            $this->success('审核成功');
        }
    }
}