| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\plugins\scanCodePay\controllers;
- use app\models\Plugins;
- use app\models\WechatConfig;
- use EasyWeChat\Factory;
- use yii\base\Module;
- use yii\web\Controller;
- class BaseController extends Controller
- {
- public $wechat_config;
- public $wechat;
- public $wechatPay;
- /**
- * 原路由
- * @var string
- */
- public $route = null;
- public function __construct(string $id, Module $module, array $config = [])
- {
- $this->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);
- }
- }
|