<?php

namespace App\Models\Common;

use App\Models\Model;

/**
 * App\Models\Common\TokenModel
 *
 * @property int $id
 * @property string $user_name
 * @property string $nick_name
 * @property string $type
 * @property string $app_id
 * @property string $app_secret
 * @property int $update_at
 * @property string $access_token
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common\TokenModel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common\TokenModel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common\TokenModel query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common\TokenModel whereAccessToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common\TokenModel whereAppId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common\TokenModel whereAppSecret($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common\TokenModel whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common\TokenModel whereNickName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common\TokenModel whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common\TokenModel whereUpdateAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common\TokenModel whereUserName($value)
 * @mixin \Eloquent
 */
class TokenModel extends Model
{
    protected $table = 'public_access_token';

    protected $dateFormat = 'U';

    protected $fillable = ['access_token', 'updated_at'];


    protected $casts = [
        'created_at' => 'integer',
        'updated_at' => 'integer'
    ];

    /**
     * 获取access_token
     * @param  string $public_id 公众号|小程序id
     * @return string access_token
     * @throws \Exception
     */
    public static function getToken(string $public_id)
    {
        $token = self::where('user_name', $public_id)->first();
        return $token->access_token ?? "";
    }
}
