BaseController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\controllers;
  8. use app\models\Option;
  9. use app\models\WechatConfig;
  10. use EasyWeChat\Factory;
  11. use Yansongda\Pay\Pay;
  12. use yii\helpers\ArrayHelper;
  13. use yii\helpers\Json;
  14. use yii\web\Controller;
  15. use app\modules\admin\behaviors\Auth;
  16. use app\modules\admin\behaviors\ActionLog;
  17. /**
  18. * @property WechatConfig $wechat_config
  19. * @property Factory $wechat
  20. */
  21. class BaseController extends Controller
  22. {
  23. public $wechat_config; //微信config
  24. public $wechat; //微信处理类
  25. public $wechatPay;
  26. public $wechatMini; // 微信小程序
  27. public $upload_config; // 上传设置
  28. public $saasWechatPay;
  29. public $aliPay;
  30. /**
  31. * @return array
  32. */
  33. public function behaviors()
  34. {
  35. return ArrayHelper::merge([
  36. [
  37. 'class' => Auth::class,
  38. 'except' => ['login'],
  39. ],
  40. // 暂时去掉后台操作日志
  41. // [
  42. // 'class' => ActionLog::class,
  43. // ]
  44. ], parent::behaviors());
  45. }
  46. /**
  47. * 初始化操作
  48. */
  49. // public function beforeAction($action)
  50. // {
  51. // $this->initWX();
  52. // $this->initWXPay();
  53. // parent::beforeAction($action);
  54. // }
  55. // public function initWX()
  56. // {
  57. // // TODO: 小程序为例
  58. // // 获取微信配置进行初始化
  59. // $this->wechat_config = WechatConfig::findOne(['store_id' => get_store_id(), 'type' => 1]);
  60. // \Yii::error('store_id => ' . \get_store_id());
  61. // if ($this->wechat_config) {
  62. // $config = [
  63. // 'app_id' => $this->wechat_config->app_id,
  64. // 'secret' => $this->wechat_config->app_secret,
  65. // 'response_type' => 'array'
  66. // ];
  67. // $this->wechat = Factory::miniProgram($config);
  68. // }
  69. // }
  70. /**
  71. * Undocumented function
  72. *
  73. * @Author LGL 24963@qq.com
  74. * @DateTime 2021-02-03
  75. * @desc: 实例化支付类
  76. * @return void
  77. */
  78. // protected function initWXPay ()
  79. // {
  80. // if (!$this->wechat_config) {
  81. // return false;
  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. // $this->wechatMini = Factory::miniProgram($config);
  113. // // return Factory::payment($config);
  114. // }
  115. /**
  116. * 支付宝初始化操作
  117. */
  118. public function initAlipay() {
  119. $config_cache = \Yii::$app->cache->get('alipay_config_cache_' . get_store_id());
  120. if ($config_cache) {
  121. $this->alipay_config = Json::decode($config_cache);
  122. } else {
  123. $this->alipay_config = Json::decode(Option::get(Option::OPTOPN_KEY, get_store_id(), 'alipay')['value']);
  124. }
  125. $config = [
  126. 'app_id' => $this->alipay_config['app_id'],
  127. 'notify_url' => 'www.baidu.com',
  128. 'return_url' => '',
  129. 'ali_public_key' => $this->alipay_config['alipay_public_key'],
  130. 'private_key' => preg_replace('# #', '', $this->alipay_config['app_private_key']),
  131. // 'log' => [ // optional
  132. // 'file' => '~/var/logs/alipay.log',
  133. // 'level' => 'info', // 建议生产环境等级调整为 info,开发环境为 debug
  134. // 'type' => 'single', // optional, 可选 daily.
  135. // 'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
  136. // ],
  137. 'http' => [
  138. 'timeout' => 5.0,
  139. 'connect_timeout' => 5.0,
  140. ],
  141. // 'mode' => 'dev', // optional,设置此参数,将进入沙箱模式
  142. ];
  143. $this->aliPay = Pay::alipay($config);
  144. }
  145. }