<?php

namespace App\Models;

use App\Facades\CommonService;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Wechat extends BaseModel
{
    use HasFactory;
    const TYPE_OFFICIAL = "official";
    const TYPE_MP = "mp";
    const TYPE_WORK = "work";
    const OPENID_PREFIX = "health";
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public static function getVirtualOpenid()
    {
        return self::OPENID_PREFIX.'_'.CommonService::randomString(20);
    }

    public static function isVirtualOpenid($openid)
    {
        $prefix = explode('_', $openid)[0];
        if ($prefix == self::OPENID_PREFIX && strlen($openid) == 27) return true;
        return false;
    }
}
