BaseController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\controllers\v1\alipay;
  8. use app\models\Option;
  9. use app\modules\client\behaviors\Auth;
  10. use app\modules\client\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. 'client/v1/alipay/passport/auth',
  20. 'client/v1/alipay/passport/phone',
  21. 'client/v1/alipay/wifi/index',
  22. 'client/v1/alipay/wifi/query',
  23. 'client/v1/alipay/wifi/connect',
  24. 'client/v1/alipay/wifi/notify',
  25. 'client/v1/alipay/order/get-freight',
  26. 'client/v1/alipay/order/create-order'
  27. ];
  28. public $socialite;
  29. public $alipayConfig;
  30. /**
  31. * @return array
  32. */
  33. public function behaviors()
  34. {
  35. $merge = [
  36. [
  37. 'class' => Auth::class,
  38. ]
  39. ];
  40. if ($this->isRoute()) {
  41. $merge[] = [
  42. 'class' => UserLogin::class,
  43. ];
  44. }
  45. return ArrayHelper::merge($merge, parent::behaviors());
  46. }
  47. public function isRoute()
  48. {
  49. $route = \Yii::$app->controller->getRoute();
  50. if (in_array($route, self::ALLOW_LIST)) {
  51. return false;
  52. } else {
  53. return true;
  54. }
  55. }
  56. public function init() {
  57. parent::init();
  58. if (\Yii::$app->prod_is_dandianpu()) {
  59. $alipay_config = Option::get('one_store_alipay_config', 0, 'saas')['value'];
  60. if ($alipay_config) {
  61. $alipay_config = json_decode($alipay_config, true);
  62. }
  63. } else {
  64. $config_cache = \Yii::$app->cache->get('alipay_config_cache_' . get_store_id());
  65. if ($config_cache) {
  66. $alipay_config = Json::decode($config_cache);
  67. } else {
  68. $alipay_config = Json::decode(Option::get(Option::OPTOPN_KEY, get_store_id(), 'alipay')['value']);
  69. }
  70. }
  71. $this->alipayConfig = $alipay_config;
  72. $config = [
  73. 'alipay' => [
  74. // This can also be named as 'app_id' like the official documentation.
  75. 'client_id' => $alipay_config['app_id'],
  76. // Please refer to the official documentation, in the official management background configuration RSA2.
  77. // Note: This is your own private key.
  78. // Note: Do not allow the private key content to have extra characters.
  79. // 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.
  80. 'rsa_private_key' => $alipay_config['app_private_key'],
  81. // Be sure to set this value and make sure that it is the same address value as set in the official admin system.
  82. // This can also be named as 'redirect_url' like the official documentation.
  83. // 'redirect' => 'http://localhost/socialite/callback.php',
  84. ]
  85. ];
  86. \Yii::warning($config);
  87. $this->socialite = new SocialiteManager($config);
  88. }
  89. }