Wechat.php 1.8 KB
<?php

namespace app\server;
use EasyWeChat\Factory;


/**
 * easyWechat 插件
 使用
 $app = app\server\Wechat::getInstance()->initWechat();
 $app 为 easyWechat的对象 根据官网接口调用或二开
 */
class Wechat {
	static public $app = null;
	static private $appid= '';
	static private $appsecret= '';
	static private $config= [];
	static private $inc = null;
	
	function __construct(){
		//self::$appid= config("site.appid");
		//self::$appsecret= config("site.appsecret");
		//测试公众号
		self::$appid= 'wxb9b259e7a17bda27';
		self::$appsecret= '2af20d18bf969d8a26691dfd54cc6380';
		//easyWechat配置
		self::$config = [
			'app_id' => self::$appid,
			'secret' => self::$appsecret,
			// 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
			'response_type' => 'array',
			'log' => [
				'default' => 'dev', // 默认使用的 channel,生产环境可以改为下面的 prod
				'channels' => [
					// 测试环境
					'dev' => [
						'driver' => 'single',
						'path' => '/tmp/easywechat.log',
						'level' => 'debug',
					],
					// 生产环境
					'prod' => [
						'driver' => 'daily',
						'path' => '/tmp/easywechat.log',
						'level' => 'info',
					],
				],
			],
			/**
			 * OAuth 配置
			 *
			 * scopes:公众平台(snsapi_userinfo / snsapi_base),开放平台:snsapi_login
			 * callback:OAuth授权完成后的回调页地址:需要前端提供完整的url
			 */
			'oauth' => [
				'scopes'   => ['snsapi_base'],
				'callback' => '/api/oauth_callback.php',
			],
		];
	}
	
	//静态函数
	static public function getInstance(){
		if(self::$inc == null){
			self::$inc = new self();
		}
		return self::$inc;
	}
	
	//初始化 easywechat app
	
    static public function initWechat(){
		self::$app = Factory::officialAccount(self::$config);
		return self::$app;
	}
}