UserTrait.php 5.0 KB
<?php
/**
 * +-----------------------------------------------------------------------------------------------------------------------
 * trait :对象的用户和修改用户trait
 * +-----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Models\Traits
 * @package   App\Models\Traits
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年6月30日10:18:21
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Models\Traits;

use App\Models\Chat\ChatRecord;
use App\Models\Chat\ChatRecordItem;
use App\Models\System\AccessRecord;
use App\Models\User\Commission;
use App\Models\User\InviteRecord;
use App\Models\User\TimesRecord;
use App\Models\User\User;
use App\Models\User\UserInfo;
use App\Models\User\Wallet;
use App\Models\User\WalletRecord;
use App\Models\User\WithdrawRecord;
use App\Models\UserWalletRecord;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\hasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;

/**
 * Trait UserTrait
 *
 * @category  App\Models\Traits
 * @package   App\Models\Traits
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年6月30日10:18:21
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
trait UserTrait
{
    use notifiable {
        notify as protected laravelNotify;
    }

    use CanOrder, ModelAppendFieldTrait;

    use SoftDeletes;

    protected $message = '';

    /**
     * @return string
     */
    public function getMessage()
    {
        return $this->message ? : __('operate_succeeded');
    }

    /**
     * @return BelongsTo
     */
    public function parent(): BelongsTo
    {
        return $this->belongsTo(get_class($this), 'pid', 'id');
    }

    /**
     * @return BelongsTo
     */
    public function grandfather(): BelongsTo
    {
        return $this->belongsTo(get_class($this), 'gid', 'id');
    }

    /**
     * @return HasMany
     */
    public function children(): HasMany
    {
        return $this->hasMany(get_class($this), 'gid', 'id');
    }

    /**
     * 我的详情
     *
     * @return hasOne
     */
    public function info(): hasOne
    {
        return $this->hasOne(UserInfo::class);
    }

    /**
     * @return HasMany
     */
    public function accessRecords(): HasMany
    {
        return $this->hasMany(AccessRecord::class, 'user_id', 'id');
    }

    /**
     * @return HasMany
     */
    public function timesRecords(): HasMany
    {
        return $this->hasMany(TimesRecord::class);
    }

    /**
     * @return HasMany
     */
    public function chatRecords(): HasMany
    {
        return $this->hasMany(ChatRecord::class);
    }

    /**
     * @return HasMany
     */
    public function chatRecordItems(): HasMany
    {
        return $this->hasMany(ChatRecordItem::class);
    }

    /**
     * 钱包
     *
     * @return hasOne
     */
    public function wallet(): hasOne
    {
        return $this->hasOne(Wallet::class);
    }

    /**
     * @return HasMany
     */
    public function inviteRecords(): HasMany
    {
        return $this->hasMany(InviteRecord::class);
    }

    /**
     * @return HasMany
     */
    public function invitedRecords(): HasMany
    {
        return $this->hasMany(InviteRecord::class, 'invited_user_id');
    }

    /**
     *
     * @return HasMany
     */
    public function walletRecords(): HasMany
    {
        return $this->hasMany(WalletRecord::class);
    }

    /**
     *
     * @return HasMany
     */
    public function commissions(): HasMany
    {
        return $this->hasMany(WalletRecord::class)->where('type', WalletRecord::INCREASE);
    }

    /**
     * @return HasMany
     */
    public function withdrawals(): HasMany
    {
        return $this->hasMany(WithdrawRecord::class);
    }

    /**
     * @param $value
     */
    public function setAvatarAttribute($value)
    {
        // 需要判断前端传来的路径是否是全路径,如果是全路径需要截取为相对路径
        $this->attributes['avatar'] = substr_file_path($value);
    }

    /**
     * @param $value
     * @return string
     * @throws GuzzleException
     */
    public function getAvatarAttribute($value): string
    {
        return splice_file_path($value);
    }

    /**
     * @param $value
     */
    public function setInviteQrCodeAttribute($value)
    {
        // 需要判断前端传来的路径是否是全路径,如果是全路径需要截取为相对路径
        $this->attributes['invite_qr_code'] = substr_file_path($value);
    }

    /**
     * @param $value
     * @return string
     * @throws GuzzleException
     */
    public function getInviteQrCodeAttribute($value): string
    {
        return splice_file_path($value);
    }
}