BaseController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\controllers\alipay;
  8. use app\models\Option;
  9. use app\modules\alliance\behaviors\Auth;
  10. use app\modules\alliance\behaviors\UserLogin;
  11. use Overtrue\Socialite\SocialiteManager;
  12. use yii\helpers\ArrayHelper;
  13. use yii\helpers\Json;
  14. use yii\web\Controller;
  15. class BaseController extends Controller
  16. {
  17. // 登录白名单
  18. const ALLOW_LIST = [
  19. 'alliance/alipay/passport/auth',
  20. 'alliance/alipay/passport/phone'
  21. ];
  22. public $socialite;
  23. public $alipayConfig;
  24. /**
  25. * @return array
  26. */
  27. public function behaviors()
  28. {
  29. // $merge = [
  30. // [
  31. // 'class' => Auth::class,
  32. // ]
  33. // ];
  34. $merge = [];
  35. if ($this->isRoute()) {
  36. $merge[] = [
  37. 'class' => UserLogin::class,
  38. ];
  39. }
  40. return ArrayHelper::merge($merge, parent::behaviors());
  41. }
  42. public function isRoute()
  43. {
  44. $route = \Yii::$app->controller->getRoute();
  45. if (in_array($route, self::ALLOW_LIST)) {
  46. return false;
  47. } else {
  48. return true;
  49. }
  50. }
  51. public function init() {
  52. parent::init();
  53. $config_cache = \Yii::$app->cache->get('alipay_config_cache_' . get_store_id());
  54. if ($config_cache) {
  55. $alipay_config = Json::decode($config_cache);
  56. } else {
  57. $keys = [
  58. 'alipay_platform'
  59. ];
  60. $data = Option::get($keys, 0, 'saas');
  61. if(isset($data[0]['value'])) {
  62. $alipay_config = json_decode($data[0]['value'],true);
  63. }else{
  64. return false;
  65. }
  66. }
  67. $this->alipayConfig = $alipay_config;
  68. $config = [
  69. 'alipay' => [
  70. // This can also be named as 'app_id' like the official documentation.
  71. 'client_id' => $alipay_config['app_id'],
  72. // Please refer to the official documentation, in the official management background configuration RSA2.
  73. // Note: This is your own private key.
  74. // Note: Do not allow the private key content to have extra characters.
  75. // Recommendation: For security, you can read directly from the file. But here as long as the value, please remember to remove the head and tail of the decoration.
  76. 'rsa_private_key' => $alipay_config['app_private_key'],
  77. // Be sure to set this value and make sure that it is the same address value as set in the official admin system.
  78. // This can also be named as 'redirect_url' like the official documentation.
  79. // 'redirect' => 'http://localhost/socialite/callback.php',
  80. ]
  81. ];
  82. \Yii::warning($config);
  83. $this->socialite = new SocialiteManager($config);
  84. }
  85. }