BaseController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\plugins\integral\controllers;
  8. use app\models\Plugins;
  9. use app\models\WechatConfig;
  10. use EasyWeChat\Factory;
  11. use yii\base\Module;
  12. use yii\web\Controller;
  13. class BaseController extends Controller
  14. {
  15. public $wechat_config;
  16. public $wechat;
  17. public $wechatPay;
  18. /**
  19. * 原路由
  20. * @var string
  21. */
  22. public $route = null;
  23. public function __construct(string $id, Module $module, array $config = [])
  24. {
  25. $this->route = $config['callback'];
  26. $route = isset($_GET['r']) ? $_GET['r'] : null;
  27. if ($route && strpos($route, 'plugin') !== false) {
  28. if ($from = explode('/', $route)[0]) {
  29. switch ($from) {
  30. case 'client':
  31. $this->clientEvent();
  32. break;
  33. case 'admin':
  34. $this->adminEvent();
  35. break;
  36. default:
  37. break;
  38. }
  39. }
  40. }
  41. }
  42. /**
  43. * 客户端接口事件绑定
  44. */
  45. public function clientEvent() {
  46. $plugin = explode('/', $this->route)[0];
  47. if (!$plugin) {
  48. return;
  49. }
  50. $plugins_data = cache()->get('plugins_data_cache');
  51. // 取缓存
  52. if (!$plugins_data) {
  53. $plugins_data = Plugins::find()->asArray()->all();
  54. if (!$plugins_data) {
  55. return;
  56. }
  57. \Yii::$app->cache->set('plugins_data_cache', $plugins_data, 3600);
  58. }
  59. $temp_key = array_column($plugins_data, 'key');
  60. $data = array_combine($temp_key, $plugins_data);
  61. if (isset($data[$plugin]) && $data[$plugin]['status'] == Plugins::STATUS_OPEN) {
  62. require __DIR__ .'/../PluginInit.php';
  63. $str = '\app\plugins\%s\PluginInit';
  64. $class = sprintf($str, $plugin);
  65. if (method_exists($class, 'init')) {
  66. call_user_func($class. '::init', ['route' => $this->route]);
  67. }
  68. }
  69. }
  70. /**
  71. * 后台接口事件绑定
  72. */
  73. public function adminEvent() {
  74. }
  75. public function init ()
  76. {
  77. parent::init();
  78. // 获取微信配置进行初始化
  79. $this->wechat_config = WechatConfig::findOne(['store_id' => get_store_id(), 'type' => 1]);
  80. if (!$this->wechat_config) {
  81. throw new \Exception('Wechat App Is Null');
  82. }
  83. // 证书
  84. if (!is_dir(\Yii::$app->runtimePath . '/pem')) {
  85. mkdir(\Yii::$app->runtimePath . '/pem');
  86. file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', '');
  87. }
  88. $cert_pem_file = null;
  89. if ($this->wechat_config->cert_pem) {
  90. $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($this->wechat_config->cert_pem);
  91. if (!file_exists($cert_pem_file)) {
  92. file_put_contents($cert_pem_file, $this->wechat_config->cert_pem);
  93. }
  94. }
  95. $key_pem_file = null;
  96. if ($this->wechat_config->key_pem) {
  97. $key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($this->wechat_config->key_pem);
  98. if (!file_exists($key_pem_file)) {
  99. file_put_contents($key_pem_file, $this->wechat_config->key_pem);
  100. }
  101. }
  102. $config = [
  103. 'app_id' => $this->wechat_config->app_id,
  104. 'secret' => $this->wechat_config->app_secret,
  105. 'key' => $this->wechat_config->pay_key,
  106. 'mch_id' => $this->wechat_config->mch_id,
  107. 'cert_path' => $cert_pem_file,
  108. 'key_path' => $key_pem_file,
  109. 'response_type' => 'array'
  110. ];
  111. $this->wechatPay = Factory::payment($config);
  112. }
  113. }