<?php
namespace app\controller;

use app\BaseController;
use app\model\Ry;
use app\model\ZdCpmc;
use app\service\NszyService;
use app\service\QyBasicService;
use app\service\RyService;
use app\service\ZdCpmcService;
use app\validate\UserLogin;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\exception\ValidateException;
use think\response\Json;
use think\annotation\Route;
use think\annotation\route\Middleware;
use hg\apidoc\annotation as Apidoc;
use thans\jwt\facade\JWTAuth;
class Index extends BaseController
{
    protected $middleware = [
        'jwt' 	=> ['except' 	=> ['login'] ],
    ];
    use ResponseJson;

    /**
     * @Apidoc\Title("基础的接口演示")
     * @Apidoc\Tag("基础,示例")
     * @Apidoc\Method ("GET")
     * @Route("/")
     * @Middleware({})
     */
    public function index()
    {
        return $this->JsonSucess(["info"=>"这是后端接口"]);
    }

    public function hello($name = 'ThinkPHP6')
    {
        return 'hello,' . $name;
    }

    /**
     * @Apidoc\Title("登录接口")
     * @Apidoc\Tag("用户登录")
     * @Apidoc\Method ("GET")
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     * @Route("login")
     * @Middleware({})
     */
    public function login():Json{
       $loginInfo =  $this->request->get();
        try {
            validate(UserLogin::class)->check($loginInfo);
        }catch (ValidateException $e){
            return $this->JsonError($e->getError(),0);
        }
       $userInfo=RyService::selectLoginInfo($loginInfo);
       if ($userInfo){
           $token = JWTAuth::builder(['qydm' => $userInfo["qydm"]]);//参数为用户认证的信息，请自行添加
           return $this->JsonSucess(["token"=>$token,"userInfo"=>$userInfo],1001);
       }else{
           return $this->JsonError(1002);
       }


    }

    /**
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     * @Route("/getQyBasicList")
     * @Middleware({"jwt"})
     */
    public function getQyBasicList(): Json
    {
        getQydm();

        return $this->JsonSucess(QyBasicService::selectQyBasic());
    }

    /**
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     * @Route("getNszyByQydm")
     * @Middleware({"jwt"})
     */
    public function getNszyByQydm():Json{
        return $this->JsonSucess(NszyService::selectNszyByQydm(getQydm()));
    }
    /**
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     * @Route("getJiangdou")
     *
     */
    public function getJiangdou(){
        return json(ZdCpmcService::getQydmBycpmc());
    }
}
