Controller.php 2.26 KB
Newer Older
冯超鹏's avatar
冯超鹏 committed
1 2 3 4 5 6 7 8
<?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;
冯超鹏's avatar
冯超鹏 committed
9 10
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
冯超鹏's avatar
冯超鹏 committed
11 12
use Illuminate\Http\Request;

冯超鹏's avatar
冯超鹏 committed
13 14 15
class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
冯超鹏's avatar
冯超鹏 committed
16

冯超鹏's avatar
冯超鹏 committed
17 18 19 20 21 22 23
    /**
     * 返回一个json
     * @param $code 状态码
     * @param $message 返回说明
     * @param $data 返回数据集合
     * @return false | string
     */
冯超鹏's avatar
冯超鹏 committed
24 25
    private function jsonResponse($code, $message, $data)
    {
冯超鹏's avatar
冯超鹏 committed
26 27
        $content = [
            'code' => $code,
冯超鹏's avatar
冯超鹏 committed
28
            'msg' => $message,
冯超鹏's avatar
冯超鹏 committed
29 30 31 32
            'data' => $data
        ];
        return json_encode($content);
    }
冯超鹏's avatar
冯超鹏 committed
33

冯超鹏's avatar
冯超鹏 committed
34 35 36 37 38
    /**
     * 成功的时候返回结果
     * @param $data 返回数据集合
     * @return false | string
     */
冯超鹏's avatar
冯超鹏 committed
39 40
    public function jsonSuccessData($data = [])
    {
冯超鹏's avatar
冯超鹏 committed
41 42 43 44 45 46 47 48 49 50
        return $this->jsonResponse(200, 'success', $data);
    }

    /**
     * 失败的时候返回
     * @param $code 状态码
     * @param $message 返回说明
     * @param $data 返回数据集合
     * @return false | string
     */
冯超鹏's avatar
冯超鹏 committed
51 52
    public function jsonErrorData($code, $message, $data = [])
    {
冯超鹏's avatar
冯超鹏 committed
53 54
        return $this->jsonResponse($code, $message, $data);
    }
冯超鹏's avatar
冯超鹏 committed
55

冯超鹏's avatar
冯超鹏 committed
56
    //返回
冯超鹏's avatar
冯超鹏 committed
57 58 59
    public function code()
    {
        return [
冯超鹏's avatar
冯超鹏 committed
60 61 62 63 64 65 66 67 68 69 70
            /*
             * http请求代码错误
             * code 封装
            * */
            'code' => [
                200 => '成功',
                200001 => '缺少必要的参数',
//                table 返回类型
            ],
        ];
    }
冯超鹏's avatar
冯超鹏 committed
71

冯超鹏's avatar
冯超鹏 committed
72 73 74
    /*
     * 验证当前登入用户
     * */
冯超鹏's avatar
冯超鹏 committed
75 76
    public function isadmin()
    {
冯超鹏's avatar
冯超鹏 committed
77 78
        $user = Auth::user();//获取当前用户信息
        $isadmin = DB::table('users')->where([['name', '=', $user['name']], ['email', '=', $user['email']]])->first();
79
        return get_object_vars($isadmin)['isadmin'] == 1 ? '1' : null;
冯超鹏's avatar
冯超鹏 committed
80
    }
冯超鹏's avatar
冯超鹏 committed
81

82 83 84 85
    public function timeline($instructions)
    {
        return DB::table('timeline')->insertGetId(['instructions'=>$instructions,'operationip'=>$_SERVER["REMOTE_ADDR"],'timestamp'=>date("Y-m-d H:i:s",time())]);
    }
冯超鹏's avatar
冯超鹏 committed
86

冯超鹏's avatar
冯超鹏 committed
87
}