HEX
Server: nginx/1.28.1
System: Linux 10-41-63-61 6.8.0-31-generic #31-Ubuntu SMP PREEMPT_DYNAMIC Sat Apr 20 00:40:06 UTC 2024 x86_64
User: www (1001)
PHP: 7.4.33
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: /www/wwwroot/my.esfyn.top/Modules/Api/Http/Controllers/Admin/DataManageController.php
<?php

namespace Modules\Api\Http\Controllers\Admin;

use App\Http\Controllers\MyAdminController;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Schema\SchemaException;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class DataManageController extends MyAdminController
{

    public $view = 'admin.data_manage.';

    /**
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|string
     * @throws Exception
     */
    public function index()
    {
        $tableName = $this->param('table');

        if (request()->ajax() && request()->wantsJson()) {
            $data = DB::table($tableName)->orderBy('id', 'desc')->paginate()->toArray();
            return $this->success($data);
        }

        $columns = [];
        $fields = Schema::getColumnListing($tableName);
        $obj = DB::connection()->getDoctrineSchemaManager()
            ->listTableDetails($tableName);

        foreach ($fields as $value) {
            $item = $obj->getColumn($value)->toArray();
            $item['type'] = (new $item['type'])->getName();
            $columns[] = $item;
        }

        return $this->view($this->view . 'index', compact('tableName', 'columns'));
    }


    /**
     * @throws SchemaException
     * @throws Exception
     */
    public function create()
    {
        $tableName = $this->param('table');

        $columns = [];
        $fields = Schema::getColumnListing($tableName);
        $fields = array_diff($fields, ["id", "updated_at", "created_at"]);
        $obj = DB::connection()->getDoctrineSchemaManager()
            ->listTableDetails($tableName);

        foreach ($fields as $value) {
            $item = $obj->getColumn($value)->toArray();
            $item['type'] = (new $item['type'])->getName();
            $columns[] = $item;
        }

        return $this->view($this->view . 'create', compact('tableName', 'columns'));
    }


    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function store()
    {
        $tableName = $this->param('table');
        $fields = Schema::getColumnListing($tableName);
        $params = array_diff($fields, ["id", "updated_at", "created_at"]);

        $data = request()->all($params);
        if (in_array("created_at", $fields)) {
            $data['created_at'] = date('Y-m-d H:i:s');
        }

        if (in_array("updated_at", $fields)) {
            $data['updated_at'] = date('Y-m-d H:i:s');
        }

        $result = DB::table($tableName)->insert($data);

        return $this->result($result);
    }


    /**
     * @throws Exception
     * @throws SchemaException
     */
    public function edit()
    {
        $tableName = $this->param('table');
        $id = $this->param('id');

        $columns = [];
        $fields = Schema::getColumnListing($tableName);
        $fields = array_diff($fields, ["id", "updated_at", "created_at"]);
        $obj = DB::connection()->getDoctrineSchemaManager()
            ->listTableDetails($tableName);

        foreach ($fields as $value) {
            $item = $obj->getColumn($value)->toArray();
            $item['type'] = (new $item['type'])->getName();
            $columns[] = $item;
        }

        $data = DB::table($tableName)->where('id', $id)->first();
        $data = json_decode(json_encode($data), true);
        return $this->view($this->view . 'edit', compact('tableName', 'columns', 'data'));
    }


    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function update()
    {
        $id = $this->param('id');
        $tableName = $this->param('table');
        $fields = Schema::getColumnListing($tableName);
        $params = array_diff($fields, ["id", "updated_at", "created_at"]);

        $data = request()->all($params);

        if (in_array("updated_at", $fields)) {
            $data['updated_at'] = date('Y-m-d H:i:s');
        }

        $result = DB::table($tableName)->where('id', $id)->update($data);

        return $this->result($result);
    }


    /**
     * 删除数据
     * @return JsonResponse
     */
    public function destroy(): JsonResponse
    {
        $id = $this->param('id');
        $tableName = $this->param('table');

        $result = DB::table($tableName)->where('id', $id)->delete();
        return $this->result($result);
    }

}