MallHost.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace common\models\mall;
  3. use common\enums\RedisKeyEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use common\models\common\MallSetting;
  6. use PHPUnit\Util\Exception;
  7. /**
  8. * This is the model class for table "{{%mall}}".
  9. *
  10. *
  11. * @property integer $id
  12. * @property integer $mall_id
  13. * @property string $backend_url
  14. * @property string $h5_url
  15. * @property string $api_url
  16. * @property string $static_url
  17. * @property integer $is_custom_host
  18. * @property MallSetting[] $option
  19. */
  20. class MallHost extends BaseActiveRecord
  21. {
  22. public $options;
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%mall_host}}';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['created_at', 'updated_at',], 'safe'],
  37. [['mall_id', 'is_custom_host'], 'integer'],
  38. [['backend_url', 'h5_url', 'api_url', 'static_url', 'ssl_path'], 'string', 'max' => 255],
  39. ];
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'id' => 'ID',
  48. 'mall_id' => '商城id',
  49. 'backend_url' => '后台管理域名',
  50. 'h5_url' => 'h5域名',
  51. 'api_url' => 'api域名',
  52. 'static_url' => '静态资源域名',
  53. 'is_custom_host' => '是否自定义域名:1:是:0:否',
  54. 'ssl_path' => '证书目录',
  55. 'created_at' => '创建时间',
  56. 'updated_at' => '更新时间',
  57. 'status' => '状态',
  58. ];
  59. }
  60. public static function setData(array $params)
  61. {
  62. try {
  63. if (empty($params['mall_id'])) throw new Exception('商城id不能为空');
  64. $model = self::findOne(['mall_id' => $params['mall_id']]);
  65. if (empty($model)) {
  66. $model = new self();
  67. $model->loadDefaultValues();
  68. }
  69. if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  70. \Yii::$app->cache->set(self::getCacheKey($model['mall_id']), $model->toArray());
  71. return $model;
  72. } catch (Exception $e) {
  73. self::$static_error = $e->getMessage();
  74. return false;
  75. }
  76. }
  77. public static function getCacheKey($mall_id){
  78. return RedisKeyEnum::suffix(RedisKeyEnum::MALL_HOST,$mall_id,true);
  79. }
  80. public static function getHost($mall_id, $type)
  81. {
  82. $is_http = function ($url) {
  83. $parsedUrl = parse_url($url);
  84. return isset($parsedUrl['scheme']) && strtolower($parsedUrl['scheme']) === 'https';
  85. };
  86. $mall_host = \Yii::$app->cache->get(self::getCacheKey($mall_id));
  87. if(empty($mall_host)){
  88. $mall_host = self::findOne(['mall_id' => $mall_id]);
  89. if(empty($mall_host)){
  90. return \Yii::$app->services->setting->platform->get('system.'.$type);
  91. }
  92. \Yii::$app->cache->set(self::getCacheKey($mall_id), $mall_host->toArray());
  93. }
  94. if (!empty($mall_host['is_custom_host']) && !empty($mall_host[$type])) {
  95. $host = $is_http($mall_host[$type]) ? $mall_host[$type] : 'https://' . $mall_host[$type];
  96. } else {
  97. $host = \Yii::$app->services->setting->platform->get('system.'.$type);
  98. }
  99. return $host;
  100. }
  101. }