cxgj/models/Option.php
2023-11-27 09:45:13 +08:00

262 lines
7.7 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "{{%option}}".
*
* @property int $id ID
* @property int $cx_mch_id 平台商户ID
* @property string $key 键
* @property string $value 值
* @property int $group 组0=系统配置1=安全配置2=自定义配置
* @property int $is_autoload 是否自动加载0=否1=是
* @property string|null $desc 描述
*/
class Option extends \yii\db\ActiveRecord
{
const GROUP_SYSTEM = 0; //系统设置
const GROUP_SECURE = 1; //安全设置
const GROUP_SELFDEF = 2; //自定义配置
const MAX_AGE = 3600; //缓存的时间
/**
* {@inheritdoc}
*/
public static function tableName()
{
return '{{%option}}';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['cx_mch_id', 'group', 'is_autoload'], 'integer'],
[['key', 'value'], 'required'],
[['value'], 'string'],
[['key', 'desc'], 'string', 'max' => 2048],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'cx_mch_id' => '平台商户ID',
'key' => '键',
'value' => '值',
'group' => '组0=系统配置1=安全配置2=自定义配置',
'is_autoload' => '是否自动加载0=否1=是',
'desc' => '描述',
];
}
public function beforeSave($insert) {
if(parent::beforeSave($insert)){
if($this->isNewRecord){
}
return true;
} else {
return false;
}
}
/**
* 获得缓存对象
*/
public static function getCacheObj()
{
$cache = Yii::$app->cache ? Yii::$app->cache : new yii\caching\DummyCache();
return $cache;
}
/**
* 更新配置缓存
* @param string $key 缓存键值
* @param string|array $val 缓存值
*/
public static function updateOptionCache($key,$value)
{
$cache = self::getCacheObj();
return $cache->set($key, $value, self::getMaxAge());
}
/**
* 删除缓存
* @param string $key 缓存键值
*/
public static function deleteOptionCache($key)
{
$cache = self::getCacheObj();
return $cache->delete($key);
}
/**
* 设置缓存key
* @param string $key 缓存键值
* @param integer $cx_mch_id 平台商户ID
* @param integer $group 配置组
*/
public static function setCacheKey($key, $cx_mch_id = 0, $group = 0)
{
return $cx_mch_id . '_' . $group . '_' . $key;
}
/**
* 设置配置
* @param string $key 缓存键值
* @param string|array $value 缓存值
* @param integer $cx_mch_id 平台商户ID
* @param integer $group 配置组
* @param integer $is_autoload 是否自动加载
* return boolean
*/
public static function setOption($key, $value, $cx_mch_id = 0, $group = 0, $is_autoload = 0)
{
if(empty($key))
return false;
$model = Option::findOne([
'cx_mch_id' => $cx_mch_id,
'group' => $group,
'key' => $key
]);
if($model == null){
$model = new Option();
$model->cx_mch_id = $cx_mch_id;
$model->group = $group;
$model->key = $key;
}
$model->value = serialize($value);
$model->is_autoload = $is_autoload;
//更新缓存
if($group == 0 || $group == 1)
self::updateOptionCache(self::setCacheKey($key, $cx_mch_id , $group), $value);
return $model->save();
}
/**
* 获取配置
* @param string $key 缓存键值
* @param integer $cx_mch_id 平台商户ID
* @param integer $group 配置组
* @param integer|string|array $default 不存在时默认值
*/
public static function getOption($key, $cx_mch_id = 0, $group = 0, $default = null)
{
$cache = self::getCacheObj();
$value = $cache->get(self::setCacheKey($key, $cx_mch_id , $group));
//开发环境不缓存
$value = YII_ENV_DEV ? false : $value;
if($value === false){
$model = Option::findOne([
'cx_mch_id' => $cx_mch_id,
'group' => $group,
'key' => $key
]);
if($model == null)
return $default;
$val = preg_replace_callback(
'/s:([0-9]+):\"(.*?)\";/',
function ($matches) {
return "s:" . strlen($matches[2]) . ':"' . $matches[2] . '";';
},
$model->value
);
$value = unserialize($val);
$cache->set(self::setCacheKey($key, $cx_mch_id , $group), $value, self::getMaxAge());
}
return $value;
}
/**
* 删除配置
* @param string $key 缓存键值
* @param integer $cx_mch_id 平台商户ID
* @param integer $group 配置组
*/
public function deleteOption($key, $cx_mch_id = 0, $group = 0)
{
$model = Option::findOne([
'cx_mch_id' => $cx_mch_id,
'group' => $group,
'key' => $key
]);
if($model != null){
//清除缓存
if($group == 0 || $group == 1)
self::deleteOptionCache(self::setCacheKey($key, $cx_mch_id , $group));
return $model->delete();
}
return false;
}
/**
* 随机获取缓存时间
* @param integer $min 最小整数
* @param integer $max 最大整数
* return integer
*/
public static function getMaxAge($min = 1800, $max = 3600)
{
return mt_rand($min,$max);
}
/**
* 批量设置
* @param arrar $list [
* ['key' => 'keyA', 'value' => 'valueA' , 'cx_mch_id' => 'cx_mch_id', 'group' => 'group', 'is_autoload' => 'is_autoload'],
* ]
* return boolean
*/
public static function setOptionList($list, $cx_mch_id = 0, $group = 0, $is_autoload = 0)
{
if(!is_array($list))
return false;
foreach ($list as $item){
self::setOption(
$item['key'],
$item['value'],
(isset($item['cx_mch_id']) ? $item['cx_mch_id'] : $cx_mch_id),
(isset($item['group']) ? $item['group'] : $group),
(isset($item['is_autoload']) ? $item['is_autoload'] : $is_autoload)
);
}
return true;
}
/**
* 根据keys获得缓存
* @param string $keys 缓存键值,多值使用,隔开
* @param integer $cx_mch_id 平台商户ID
* @param integer $group 配置组
* @param integer|string|array $default 不存在时默认值
* return boolean|array
*/
public static function getOptionList($keys, $cx_mch_id = 0, $group = 0, $default = null)
{
if(is_string($keys))
$keys = explode (',', $keys);
if(!is_array($keys))
return false;
$list = [];
foreach ($keys as $key){
if(empty($key))
continue;
$value = self::getOption($key, $cx_mch_id, $group, $default);
$list[$key] = $value;
}
return $list;
}
}