// +---------------------------------------------------------------------- namespace app\services\system\config; use app\model\system\config\SystemStoreConfig; use qiniu\basic\BaseServices; use think\db\exception\DbException; /** * Class StoreConfigServices * @package app\services\store * @mixin SystemStoreConfig */ class SystemStoreConfigServices extends BaseServices { /** * 表单数据切割符号 * @var string */ protected $cuttingStr = '=>'; /** * StoreConfigServices constructor. * @param SystemStoreConfig $model */ public function __construct(SystemStoreConfig $model) { $this->model = $model; } /** * 保存或者更新门店配置 * @param array $data * @param int $store_id * @throws DbException */ public function saveConfig(array $data, int $store_id = 0) { $config = []; foreach ($data as $key => $value) { if ($this->getCount(['key_name' => $key, 'store_id' => $store_id])) { $this->update(['key_name' => $key, 'store_id' => $store_id], ['value' => json_encode($value)]); } else { $config[] = [ 'key_name' => $key, 'store_id' => $store_id, 'value' => json_encode($value) ]; } } if ($config) { $this->saveRows($config); } } /** * 获取配置 * @param string $key * @param int $store_id * @param null $default * @return mixed|null */ public function getConfig(string $key, int $store_id = 0, $default = null) { $value = $this->search(['key_name' => $key, 'store_id' => $store_id])->value('value'); return is_null($value) ? $default : json_decode($value, true); } /** * 获取门店配置 * @param array $keys * @param int $store_id * @return array */ public function getConfigAll(array $keys, int $store_id = 0) { $config = $this->search(['key_name' => $keys, 'store_id' => $store_id])->column('value', 'key_name'); return array_map(function ($item) { return json_decode($item, true); }, $config); } public function getOptions(string $parameter) { $parameter = explode("\n", $parameter); $options = []; foreach ($parameter as $v) { if (strstr($v, $this->cuttingStr) !== false) { $pdata = explode($this->cuttingStr, $v); $options[] = ['label' => $pdata[1], 'value' => (int)$pdata[0]]; } } return $options; } /** * @param array $configName * @param int $store_id * @param int $group * @return array */ public function getConfigAllField(array $configName = [], int $store_id = 0, int $group = 0) { /** @var SystemConfigServices $systemConfigServices */ $systemConfigServices = app()->make(SystemConfigServices::class); $list = $systemConfigServices->getConfigAllListByWhere(['menu_name' => $configName], $store_id, ['menu_name', 'info', 'type', 'value', 'desc', 'parameter']); if ($list) { foreach ($list as &$item) { if ($store_id != 0) { $item['value'] = $item['store_value'] ?? ''; } $item['value'] = json_decode($item['value'], true); } $list = array_combine(array_column($list, 'menu_name'), $list); } $value = []; foreach ($configName as $key) { if ($group) { $value[$key] = $list[$key]['value'] ?? ''; } else { $value[$key] = $list[$key] ?? ['info' => '', 'type' => 'text', 'value' => '', 'desc' => '', 'parameter' => '']; } } return $value; } }