JsonResource.php 5.73 KB
<?php


namespace App\Helpers;

use Illuminate\Http\JsonResponse;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\Response;

class JsonResource
{
    public const SUCCESS = '获取成功';

    public const CREATE_SUCCESS = '创建成功';

    public const UPDATE_SUCCESS = '更新成功';

    public const DELETE_SUCCESS = '删除成功';

    public const LOGIN_SUCCESS = '登录成功';

    public const LOGOUT_SUCCESS = '登出成功';

    public const UNAUTHORIZED = '未携带身份令牌';

    public const AUTHORIZED_EXP = '身份令牌失效';

    public const NO_PERMISSION = '无权限进行此操作';

    public const USER_FOUND_ERROR = '用户不存在';

    public const LOGIN_FOUND_ERROR = '账号不存在';

    public const LOGIN_FORBID_ERROR = '账户已被封禁,请联系管理员';

    public const LOGIN_PASSWORD_ERROR = '密码错误';

    public const REPEAT_SUBMIT = '请勿重复提交';

    public const IS_TOP_LIMIT = '最多允许置顶3条内容';

    public const INVITE_SUCCESS = '邀请成功';

    public const APPLY_SUCCESS = '申请成功';

    public const INVITE_LIMIT = '1天内仅能向他发起一次,请不要频繁邀请';

    public const APPLY_LIMIT = '1天内仅能向他发起一次,请不要频繁申请';


    public const TEAM_INVITE_SINGER_LIMIT = '团队内歌手名额已满,暂无法邀请 ~';

    public const TEAM_JOIN_SINGER_LIMIT = '团队内歌手名额已满';

    public const JOINED_TEAM = '对方已在其他团队';

    public const MEMBER_JOINED_TEAM      = 'Ta已加入他人团队,您暂无法同意 ~';
    public const SELF_MEMBER_JOINED_TEAM = '你已加入他人团队,您暂无法同意 ~';

    public const INVITE_JOINED_TEAM      = 'Ta已加入他人团队,您暂无法加入 ~';
    public const SELF_INVITE_JOINED_TEAM = '你已加入他人团队,您暂无法同意 ~';


    public const  JOIN_TEAM_INVALID = '超时,已失效';

    public const  INVALIDED = '已失效';


    public const  HANDLE_SUCCESS = '处理成功';

    public const OPERATION_SUCCESS = '操作成功';

    public const  MEMBER_TOP_LIMIT = '最多可置顶5人';

    public const  MEMBER_OUT_INVALID = '退出申请已被处理';

    public const  PROJECT_INVALIDED = '厂牌已被禁用或删除';

    public const  HANDEL_FAIL = '操作失败,请刷新后再试';

    public const  IS_PROJECT_MEMBER = '操作失败,您已经是厂牌成员了';

    public const  IS_NOT_MASTER = '操作失败,对方已经不是该厂牌管理员';

    public const  YOU_NOT_MASTER = '操作失败,您已经不是该厂牌管理员';

    public const  OUT_IS_HANDLE = '操作失败,该申请已被其他管理员处理';


    /**
     * @param string|null $message
     * @param int         $code
     * @return JsonResponse
     */
    public static function empty(string $message = NULL, int $code = Response::HTTP_OK): JsonResponse
    {
        return self::send('success', $code, $message ?? Response::$statusTexts[$code]);
    }

    /**
     * @param string|null $message
     * @param mixed       $data
     * @param int         $code
     * @param array       $extend
     * @return JsonResponse
     */
    public static function success(string $message = NULL, $data = NULL, int $code = Response::HTTP_OK, array $extend = []): JsonResponse
    {
        return self::send('success', $code, $message ?? Response::$statusTexts[$code], $data, $extend);
    }

    /**
     * @param mixed       $data
     * @param string|null $message
     * @param int         $code
     * @return JsonResponse
     */
    public static function fail(string $message = NULL, $data = NULL, int $code = Response::HTTP_UNPROCESSABLE_ENTITY): JsonResponse
    {
        return self::send('fail', $code, $message ?? Response::$statusTexts[$code], $data);

    }

    public static function unauthenticated(string $message = NULL, $data = NULL, int $code = 401): JsonResponse
    {
        return self::send('fail', $code, $message ?? Response::$statusTexts[$code], $data);

    }


    /**
     * @param mixed       $data
     * @param string|null $message
     * @param int         $code
     * @return JsonResponse
     */
    public static function error(string $message = NULL, $data = NULL, int $code = Response::HTTP_INTERNAL_SERVER_ERROR): JsonResponse
    {
        return self::send('error', $code, $message ?? Response::$statusTexts[$code], config('app.debug') && $code >= 500 ? $data : []);
    }

    /**
     * @param LengthAwarePaginator|\Illuminate\Contracts\Pagination\LengthAwarePaginator $data
     * @return array
     */
    public static function paginator(LengthAwarePaginator $data): array
    {
        return [
            'data' => $data->items(), 'meta' => ['total' => (int)$data->total(), 'page' => (int)$data->currentPage(), 'size' => (int)$data->perPage()]
        ];
    }


    /**
     * @param string   $status
     * @param int      $code
     * @param string   $message
     * @param          $data
     * @param array    $extend
     * @return JsonResponse
     */
    public static function send(string $status, int $code, string $message, $data = NULL, array $extend = []): JsonResponse
    {
        $resource = array_merge(
            ['status' => $status, 'code' => $code, 'message' => $message],
            $data instanceof LengthAwarePaginator ? self::paginator($data) : compact('data'),
        );

        if (is_null($resource['data']) && Str::startsWith(request()?->getRequestUri(), '/mapi')) {
            unset($resource['data']);
        }

        if (count($extend) !== 0) {
            Arr::set($resource, 'meta', array_merge(Arr::get($resource, 'meta', []), $extend));
        }

        return response()->json($resource);
    }
}