| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\controllers;
- use app\models\Option;
- use app\models\WechatConfig;
- use EasyWeChat\Factory;
- use Yansongda\Pay\Pay;
- use yii\helpers\ArrayHelper;
- use yii\helpers\Json;
- use yii\web\Controller;
- use app\modules\admin\behaviors\Auth;
- use app\modules\admin\behaviors\ActionLog;
- /**
- * @property WechatConfig $wechat_config
- * @property Factory $wechat
- */
- class BaseController extends Controller
- {
- public $wechat_config; //微信config
- public $wechat; //微信处理类
- public $wechatPay;
- public $wechatMini; // 微信小程序
- public $upload_config; // 上传设置
- public $saasWechatPay;
- public $aliPay;
- /**
- * @return array
- */
- public function behaviors()
- {
- return ArrayHelper::merge([
- [
- 'class' => Auth::class,
- 'except' => ['login'],
- ],
- // 暂时去掉后台操作日志
- // [
- // 'class' => ActionLog::class,
- // ]
- ], parent::behaviors());
- }
- /**
- * 初始化操作
- */
- // public function beforeAction($action)
- // {
- // $this->initWX();
- // $this->initWXPay();
- // parent::beforeAction($action);
- // }
- // public function initWX()
- // {
- // // TODO: 小程序为例
- // // 获取微信配置进行初始化
- // $this->wechat_config = WechatConfig::findOne(['store_id' => get_store_id(), 'type' => 1]);
- // \Yii::error('store_id => ' . \get_store_id());
- // if ($this->wechat_config) {
- // $config = [
- // 'app_id' => $this->wechat_config->app_id,
- // 'secret' => $this->wechat_config->app_secret,
- // 'response_type' => 'array'
- // ];
- // $this->wechat = Factory::miniProgram($config);
- // }
- // }
- /**
- * Undocumented function
- *
- * @Author LGL 24963@qq.com
- * @DateTime 2021-02-03
- * @desc: 实例化支付类
- * @return void
- */
- // protected function initWXPay ()
- // {
- // if (!$this->wechat_config) {
- // return false;
- // }
- // // 证书
- // 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);
- // $this->wechatMini = Factory::miniProgram($config);
- // // return Factory::payment($config);
- // }
- /**
- * 支付宝初始化操作
- */
- public function initAlipay() {
- $config_cache = \Yii::$app->cache->get('alipay_config_cache_' . get_store_id());
- if ($config_cache) {
- $this->alipay_config = Json::decode($config_cache);
- } else {
- $this->alipay_config = Json::decode(Option::get(Option::OPTOPN_KEY, get_store_id(), 'alipay')['value']);
- }
- $config = [
- 'app_id' => $this->alipay_config['app_id'],
- 'notify_url' => 'www.baidu.com',
- 'return_url' => '',
- 'ali_public_key' => $this->alipay_config['alipay_public_key'],
- 'private_key' => preg_replace('# #', '', $this->alipay_config['app_private_key']),
- // 'log' => [ // optional
- // 'file' => '~/var/logs/alipay.log',
- // 'level' => 'info', // 建议生产环境等级调整为 info,开发环境为 debug
- // 'type' => 'single', // optional, 可选 daily.
- // 'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
- // ],
- 'http' => [
- 'timeout' => 5.0,
- 'connect_timeout' => 5.0,
- ],
- // 'mode' => 'dev', // optional,设置此参数,将进入沙箱模式
- ];
- $this->aliPay = Pay::alipay($config);
- }
- }
|