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/app/Helpers/ResponseHelpers.php
<?php


namespace App\Helpers;

use Illuminate\Http\JsonResponse;

trait ResponseHelpers
{

    protected $success_msg = '操作成功';
    protected $success_code = 200;

    protected $error_msg = '操作失败';
    protected $error_code = 401;

    /**
     * 通用响应
     * @param $result
     * @param $data
     * @return JsonResponse
     */
    public function result($result = true, $data = []): JsonResponse
    {
        return $result !== false ? $this->success($data) : $this->error($data);
    }

    /**
     * 成功响应
     * @param $option
     * @return JsonResponse
     */
    public function success($option = []): JsonResponse
    {
        $option['msg'] = $option['msg'] ?? $this->success_msg;
        $option['code'] = $option['code'] ?? $this->success_code;

        return new JsonResponse($option, $option['code']);
    }

    /**
     * 错误响应
     * @param $option
     * @return JsonResponse
     */
    public function error($option = []): JsonResponse
    {
        $option['msg'] = $option['msg'] ?? $this->error_msg;
        $option['code'] = $option['code'] ?? $this->error_code;

        return new JsonResponse($option, $option['code']);
    }

    /**
     * 内容转数组
     * @param string $content
     * @return mixed
     */
    public function contentToArray(string $content)
    {
        return json_decode($content, true);
    }
}