<?php
namespace app\service\wx;

use EasyWeChat\Factory;
use EasyWeChat\OfficialAccount\Application;
use think\facade\Config;

/**
 * 微信公众号
 * Class Wechat
 * @package app\service\wx
 */
class Wechat
{
    private static $app = null;

    private function __construct()
    {
    }

    private function __clone()
    {
    }

    //微信实例  单例模式
    public static function getInstance(): ?Application
    {
        if (self::$app == null) {
            Config::load('extra/wechat', 'wechat');
            $conf = config('wechat');

            $config = [
                'app_id'        => $conf['appId'],
                'secret'        => $conf['appSecret'],

                // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
                'response_type' => 'array',

                //...
            ];

            self::$app = Factory::officialAccount($config);
        }
        return self::$app;
    }
}