123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- 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;
- }
- }
|