Files
water_new/modules/User/Http/Resources/UserIdentityResource.php
2023-03-09 11:54:13 +08:00

80 lines
2.8 KiB
PHP

<?php
namespace Modules\User\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
use Jason\Api\Api;
use Modules\User\Models\Order;
class UserIdentityResource extends JsonResource
{
public function toArray($request): array
{
$user = Api::user();
$open = $renew = false;
$text = '立即开通';
$price = $this->getCondition('price', '0');
$cost = $this->getCondition('cost', '0');
$identityName = $this->name;
$coupon_price = 0;
if ($user) {
$identity = $user->identityFirst();
if ($identity) {
//当前身份 可以续费
if ($identity->id == $this->id) {
$renew = (bool) $identity->years;
$text = '续费';
} elseif ($identity->order > $this->order) {
$text = '不可降级';
} else {
$identityOrder = Order::ByUser($user)
->where('identity_id', $this->id)
->where('state', Order::STATE_INIT)
->first();
if ($identityOrder) {
$text = '等待审核';
} else {
$open = true;
}
}
} else {
$open = true;
}
}
return [
'identity_id' => $this->id,
'name' => $identityName,
'stock' => $this->stock,
'years' => $this->years,
'times' => $this->when($user && $this->id == $user->identityFirst()->id, function () use ($user) {
return new IdentityMiddleResource($user->identityMiddle()->first());
}, [
'name' => '---',
'serial' => '---',
'started_at' => '---',
'ended_at' => '---',
]),
'cover' => $this->cover_url,
'order' => $this->order,
'description' => $this->description ?? "",
'coupon_price' => floatval($coupon_price),//开通金额
'cost' => floatval($cost),//开通金额
'price' => floatval($price),//开通金额
'can' => [
'buy' => (bool) $this->can_buy,
'open' => $this->can_buy ? $open : false,//开通
'renew' => $this->can_buy ? $renew : false,//续费
],
'buttonText' => $text,
'rights' => $this->rights,
'rules' => $this->getRules(),
'not_rules' => $this->getNotRules(),
'is_open' => $user && $this->id == $user->identityFirst()->id
];
}
}