<?php

namespace Modules\User\Models\Traits;

trait WechatAttribute
{

    /**
     * Notes   : 获取微信公众号openid
     *
     * @Date   : 2021/5/26 17:03
     * @Author : Mr.wang
     * @return string
     */
    public function getOfficialOpenidAttribute(): string
    {
        return $this->official->openid ?? '';
    }

    /**
     * Notes: description
     *
     * @Author: 玄尘
     * @Date: 2022/8/3 16:22
     */
    public function isOfficialSubscribe(): bool
    {
        return $this->official()->where('subscribe', 1)->exists();
    }

    /**
     * Notes   : 获取微信小程序openid
     *
     * @Date   : 2021/5/26 17:03
     * @Author : Mr.wang
     * @return string
     */
    public function getMiniOpenidAttribute(): string
    {
        return $this->mini->openid ?? '';
    }


    /**
     * Notes: 获取用户openid
     *
     * @Author: 玄尘
     * @Date: 2022/9/26 13:52
     * @param $channel
     * @return mixed
     */
    public function getUserOpenid($channel)
    {
        $channel = strtolower($channel);
        if ($channel == 'mp') {
            return Arr::get($this->getOpenids(), 'official');
        } else {
            return Arr::get($this->getOpenids(), 'mini');
        }
    }

    /**
     * Notes: 获取openids
     *
     * @Author: 玄尘
     * @Date: 2022/9/26 15:06
     * @return array
     */
    public function getOpenids(): array
    {
        return [
            'mini'     => $this->mini()->value('openid') ?? '',
            'official' => $this->official()->value('openid') ?? '',
        ];
    }

}
