SystemStoreConfigServices.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\system\config;
  12. use app\model\system\config\SystemStoreConfig;
  13. use qiniu\basic\BaseServices;
  14. use think\db\exception\DbException;
  15. /**
  16. * Class StoreConfigServices
  17. * @package app\services\store
  18. * @mixin SystemStoreConfig
  19. */
  20. class SystemStoreConfigServices extends BaseServices
  21. {
  22. /**
  23. * 表单数据切割符号
  24. * @var string
  25. */
  26. protected $cuttingStr = '=>';
  27. /**
  28. * StoreConfigServices constructor.
  29. * @param SystemStoreConfig $model
  30. */
  31. public function __construct(SystemStoreConfig $model)
  32. {
  33. $this->model = $model;
  34. }
  35. /**
  36. * 保存或者更新门店配置
  37. * @param array $data
  38. * @param int $store_id
  39. * @throws DbException
  40. */
  41. public function saveConfig(array $data, int $store_id = 0)
  42. {
  43. $config = [];
  44. foreach ($data as $key => $value) {
  45. if ($this->getCount(['key_name' => $key, 'store_id' => $store_id])) {
  46. $this->update(['key_name' => $key, 'store_id' => $store_id], ['value' => json_encode($value)]);
  47. } else {
  48. $config[] = [
  49. 'key_name' => $key,
  50. 'store_id' => $store_id,
  51. 'value' => json_encode($value)
  52. ];
  53. }
  54. }
  55. if ($config) {
  56. $this->saveRows($config);
  57. }
  58. }
  59. /**
  60. * 获取配置
  61. * @param string $key
  62. * @param int $store_id
  63. * @param null $default
  64. * @return mixed|null
  65. */
  66. public function getConfig(string $key, int $store_id = 0, $default = null)
  67. {
  68. $value = $this->search(['key_name' => $key, 'store_id' => $store_id])->value('value');
  69. return is_null($value) ? $default : json_decode($value, true);
  70. }
  71. /**
  72. * 获取门店配置
  73. * @param array $keys
  74. * @param int $store_id
  75. * @return array
  76. */
  77. public function getConfigAll(array $keys, int $store_id = 0)
  78. {
  79. $config = $this->search(['key_name' => $keys, 'store_id' => $store_id])->column('value', 'key_name');
  80. return array_map(function ($item) {
  81. return json_decode($item, true);
  82. }, $config);
  83. }
  84. public function getOptions(string $parameter)
  85. {
  86. $parameter = explode("\n", $parameter);
  87. $options = [];
  88. foreach ($parameter as $v) {
  89. if (strstr($v, $this->cuttingStr) !== false) {
  90. $pdata = explode($this->cuttingStr, $v);
  91. $options[] = ['label' => $pdata[1], 'value' => (int)$pdata[0]];
  92. }
  93. }
  94. return $options;
  95. }
  96. /**
  97. * @param array $configName
  98. * @param int $store_id
  99. * @param int $group
  100. * @return array
  101. */
  102. public function getConfigAllField(array $configName = [], int $store_id = 0, int $group = 0)
  103. {
  104. /** @var SystemConfigServices $systemConfigServices */
  105. $systemConfigServices = app()->make(SystemConfigServices::class);
  106. $list = $systemConfigServices->getConfigAllListByWhere(['menu_name' => $configName], $store_id, ['menu_name', 'info', 'type', 'value', 'desc', 'parameter']);
  107. if ($list) {
  108. foreach ($list as &$item) {
  109. if ($store_id != 0) {
  110. $item['value'] = $item['store_value'] ?? '';
  111. }
  112. $item['value'] = json_decode($item['value'], true);
  113. }
  114. $list = array_combine(array_column($list, 'menu_name'), $list);
  115. }
  116. $value = [];
  117. foreach ($configName as $key) {
  118. if ($group) {
  119. $value[$key] = $list[$key]['value'] ?? '';
  120. } else {
  121. $value[$key] = $list[$key] ?? ['info' => '', 'type' => 'text', 'value' => '', 'desc' => '', 'parameter' => ''];
  122. }
  123. }
  124. return $value;
  125. }
  126. }