| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- namespace common\models\mall;
- use common\enums\RedisKeyEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\common\MallSetting;
- use PHPUnit\Util\Exception;
- /**
- * This is the model class for table "{{%mall}}".
- *
- *
- * @property integer $id
- * @property integer $mall_id
- * @property string $backend_url
- * @property string $h5_url
- * @property string $api_url
- * @property string $static_url
- * @property integer $is_custom_host
- * @property MallSetting[] $option
- */
- class MallHost extends BaseActiveRecord
- {
- public $options;
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%mall_host}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['created_at', 'updated_at',], 'safe'],
- [['mall_id', 'is_custom_host'], 'integer'],
- [['backend_url', 'h5_url', 'api_url', 'static_url', 'ssl_path'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城id',
- 'backend_url' => '后台管理域名',
- 'h5_url' => 'h5域名',
- 'api_url' => 'api域名',
- 'static_url' => '静态资源域名',
- 'is_custom_host' => '是否自定义域名:1:是:0:否',
- 'ssl_path' => '证书目录',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- 'status' => '状态',
- ];
- }
- public static function setData(array $params)
- {
- try {
- if (empty($params['mall_id'])) throw new Exception('商城id不能为空');
- $model = self::findOne(['mall_id' => $params['mall_id']]);
- if (empty($model)) {
- $model = new self();
- $model->loadDefaultValues();
- }
- if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
- \Yii::$app->cache->set(self::getCacheKey($model['mall_id']), $model->toArray());
- return $model;
- } catch (Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function getCacheKey($mall_id){
- return RedisKeyEnum::suffix(RedisKeyEnum::MALL_HOST,$mall_id,true);
- }
- public static function getHost($mall_id, $type)
- {
- $is_http = function ($url) {
- $parsedUrl = parse_url($url);
- return isset($parsedUrl['scheme']) && strtolower($parsedUrl['scheme']) === 'https';
- };
- $mall_host = \Yii::$app->cache->get(self::getCacheKey($mall_id));
- if(empty($mall_host)){
- $mall_host = self::findOne(['mall_id' => $mall_id]);
- if(empty($mall_host)){
- return \Yii::$app->services->setting->platform->get('system.'.$type);
- }
- \Yii::$app->cache->set(self::getCacheKey($mall_id), $mall_host->toArray());
- }
- if (!empty($mall_host['is_custom_host']) && !empty($mall_host[$type])) {
- $host = $is_http($mall_host[$type]) ? $mall_host[$type] : 'https://' . $mall_host[$type];
- } else {
- $host = \Yii::$app->services->setting->platform->get('system.'.$type);
- }
- return $host;
- }
- }
|