<?php
declare (strict_types = 1);

namespace app\index\controller;

use app\index\BaseController;
use app\index\exception\ApiException;
use app\index\model\AdminModel;
use app\common\until\Until;
use think\Request;

class Admin extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }


    public function login() {
        $input = Until::getInput();
        $rule = [
            'username|用户名'   => 'require',
            'password|内容'   => 'require',
        ];
        Until::check($rule, $input);
        $info = (new AdminModel())::where(['account' => $input['username'], 'passwd' => $input['password']])->find();
        if (empty($info)) {
            throw new ApiException('账号或密码错误');
        }
        $tokenService = new \app\common\until\Token();
        $token = $tokenService->getToken($info['id']);
        Until::output(['token' => $token,'id' => $info['id']]);
    }


    public function logout() {
        Until::output(['name' => 'tom']);
    }
    /**
     * 显示指定的资源
     *
     * @return \think\Response
     */
    public function read()
    {
        $info = (new AdminModel())::where(['id' => $this->userId])->field('account,avatar')->find();
        if (empty($info)) {
            throw new ApiException('无此用户');
        }
        Until::output(['username' => $info['account'],'avatar' => $info['avatar']]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

}
