route = $config['callback']; $route = isset($_GET['r']) ? $_GET['r'] : null; if ($route && strpos($route, 'plugin') !== false) { if ($from = explode('/', $route)[0]) { switch ($from) { case 'client': $this->clientEvent(); break; case 'admin': $this->adminEvent(); break; default: break; } } } } /** * 客户端接口事件绑定 */ public function clientEvent() { $plugin = explode('/', $this->route)[0]; if (!$plugin) { return; } $plugins_data = cache()->get('plugins_data_cache'); // 取缓存 if (!$plugins_data) { $plugins_data = Plugins::find()->asArray()->all(); if (!$plugins_data) { return; } \Yii::$app->cache->set('plugins_data_cache', $plugins_data, 3600); } $temp_key = array_column($plugins_data, 'key'); $data = array_combine($temp_key, $plugins_data); if (isset($data[$plugin]) && $data[$plugin]['status'] == Plugins::STATUS_OPEN) { require __DIR__ .'/../PluginInit.php'; $str = '\app\plugins\%s\PluginInit'; $class = sprintf($str, $plugin); if (method_exists($class, 'init')) { call_user_func($class. '::init', ['route' => $this->route]); } } } /** * 后台接口事件绑定 */ public function adminEvent() { } public function init () { parent::init(); // 获取微信配置进行初始化 $this->wechat_config = WechatConfig::findOne(['store_id' => get_store_id(), 'type' => 1]); if (!$this->wechat_config) { throw new \Exception('Wechat App Is Null'); } // 证书 if (!is_dir(\Yii::$app->runtimePath . '/pem')) { mkdir(\Yii::$app->runtimePath . '/pem'); file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', ''); } $cert_pem_file = null; if ($this->wechat_config->cert_pem) { $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($this->wechat_config->cert_pem); if (!file_exists($cert_pem_file)) { file_put_contents($cert_pem_file, $this->wechat_config->cert_pem); } } $key_pem_file = null; if ($this->wechat_config->key_pem) { $key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($this->wechat_config->key_pem); if (!file_exists($key_pem_file)) { file_put_contents($key_pem_file, $this->wechat_config->key_pem); } } $config = [ 'app_id' => $this->wechat_config->app_id, 'secret' => $this->wechat_config->app_secret, 'key' => $this->wechat_config->pay_key, 'mch_id' => $this->wechat_config->mch_id, 'cert_path' => $cert_pem_file, 'key_path' => $key_pem_file, 'response_type' => 'array' ]; $this->wechatPay = Factory::payment($config); } }