<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    /**
     * 返回一个json
     * @param $code 状态码
     * @param $message 返回说明
     * @param $data 返回数据集合
     * @return false | string
     */
    private function jsonResponse($code, $message, $data)
    {
        $content = [
            'code' => $code,
            'msg' => $message,
            'data' => $data
        ];
        return json_encode($content, JSON_UNESCAPED_UNICODE);
    }

    /**
     * 成功的时候返回结果
     * @param $data 返回数据集合
     * @return false | string
     */
    public function jsonSuccessData($data = [])
    {
        return $this->jsonResponse(200, 'success', $data);
    }

    /**
     * 失败的时候返回
     * @param $code 状态码
     * @param $message 返回说明
     * @param $data 返回数据集合
     * @return false | string
     */
    public function jsonErrorData($code, $message, $data = [])
    {
        return $this->jsonResponse($code, $message, $data);
    }

    //返回
    public function code()
    {
        return [
            /*
             * http请求代码错误
             * code 封装
            * */
            'code' => [
                200 => '成功',
                200001 => '缺少必要的参数',
//                table 返回类型
            ],
        ];
    }
    /*
     * 验证当前登入用户
     * */
    public function isadmin()
    {
        $user = Auth::user();//获取当前用户信息
        $isadmin = DB::table('users')->where([['name', '=', $user['name']], ['email', '=', $user['email']]])->first();
        return get_object_vars($isadmin)['isadmin'] == 1 ? '1' : null;
    }
    public function timeline($instructions)
    {
        return DB::table('timeline')->insertGetId(['instructions'=>$instructions,'operationip'=>$_SERVER["REMOTE_ADDR"],'timestamp'=>date("Y-m-d H:i:s",time())]);
    }

}
