123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?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\SystemConfig;
- use qiniu\basic\BaseServices;
- use qiniu\exceptions\AdminException;
- use qiniu\services\FileService;
- use qiniu\services\wechat\contract\ServeConfigInterface;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\exception\ValidateException;
- /**
- * 系统配置
- * Class SystemConfigServices
- * @package app\services\system\config
- * @mixin SystemConfig
- */
- class SystemConfigServices extends BaseServices implements ServeConfigInterface
- {
- /**
- * SystemConfigServices constructor.
- * @param SystemConfig $model
- */
- public function __construct(SystemConfig $model)
- {
- $this->model = $model;
- }
- /**
- * 获取单个系统配置
- * @param string $configName
- * @param int $store_id
- * @param null $default
- * @return mixed|null
- */
- public function getConfigValue(string $configName, int $store_id = 0, $default = null)
- {
- if ($store_id) {//查门店、供应商
- /** @var SystemStoreConfigServices $service */
- $service = app()->make(SystemStoreConfigServices::class);
- return $service->getConfig($configName, $store_id);
- } else {//平台
- $info = $this->search(['menu_name' => $configName])->find();
- $value = $info['value'];
- $type = $info['type'];
- $detail = $info['configuration'];
- $value = is_null($value) ? $default : json_decode($value, true);
- if ($type == 'Upload' && !empty($value)) {
- $value = set_file_url($value);
- if ($detail['upload_type'] != 2) {
- $value = $value[0] ?? $default;
- }
- }
- return $value;
- }
- }
- /**
- * 获取全部配置
- * @param array $configName
- * @param int $store_id
- * @return array
- */
- public function getConfigAll(array $configName = [], int $store_id = 0)
- {
- if ($store_id) {
- /** @var SystemStoreConfigServices $service */
- $service = app()->make(SystemStoreConfigServices::class);
- return $service->getConfigAll($configName, $store_id);
- } else {
- $get_all = function (array $configName = [], int $storeId = 0) {
- if ($configName) {
- return $this->search(['menu_name' => $configName])->column('value', 'menu_name');
- } else {
- return $this->getModel()->column('value', 'menu_name');
- }
- };
- return array_map(function ($item) {
- return json_decode($item, true);
- }, $get_all($configName));
- }
- }
- /**
- * 获取配置并分页
- * @param array $where
- * @return array
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function getConfigList(array $where)
- {
- [$page, $limit] = $this->getPageValue();
- $list = $this->search($where)->page($page, $limit)->order('sort desc,id asc')->select()->toArray();
- $count = $this->getCount($where);
- foreach ($list as &$item) {
- $item['value'] = $item['value'] ? json_decode($item['value'], true) ?: '' : '';
- if ($item['type'] == 'Upload' && !empty($item['value'])) {
- $item['value'] = set_file_url($item['value']);
- $tidy_srr = [];
- foreach ($item['value'] as $key => $value) {
- $tidy_srr[$key]['filepath'] = $value;
- $tidy_srr[$key]['filename'] = basename($value);
- }
- $item['value'] = $tidy_srr;
- }
- }
- return compact('count', 'list');
- }
- /**
- * 获取系统配置信息
- * @param int $tabId
- * @return array
- */
- public function getReadList(int $tabId)
- {
- $info = $this->getConfigTabAllList($tabId);
- foreach ($info as &$v) {
- $v['value'] = $v['value'] ? json_decode($v['value'], true) ?: '' : '';
- if ($v['type'] == 'Upload' && !empty($v['value'])) {
- $v['value'] = set_file_url($v['value']);
- $tidy_srr = [];
- foreach ($v['value'] as $key => $value) {
- $tidy_srr[$key]['filepath'] = $value;
- $tidy_srr[$key]['filename'] = basename($value);
- }
- $v['value'] = $tidy_srr;
- }
- }
- return $info;
- }
- public function getConfigTabAllList(int $tabId, int $status = 1, int $type = 1, int $relation_id = 0)
- {
- $where['tab_id'] = $tabId;
- if ($status == 1) $where['status'] = $status;
- if ($relation_id != 0) $where['is_store'] = 1;
- return $this->search($where)
- ->when(isset($relation_id) && $relation_id != 0, function ($query) use ($type, $relation_id) {
- $query->with(['storeConfig' => function ($querys) use ($type, $relation_id) {
- $querys->where('type', $type)->where('relation_id', $relation_id);
- }]);
- })
- ->order('sort desc')->select()->toArray();
- }
- /**
- * 获取上传配置中的上传类型
- * @param string $configName
- * @return array
- */
- public function getUploadTypeList(string $configName)
- {
- return $this->search(['menu_name' => $configName])->column('configuration', 'type');
- }
- /**
- * 检测缩略图水印配置是否更改
- * @param array $post
- * @return bool
- */
- public function checkThumbParam(array $post)
- {
- unset($post['upload_type'], $post['image_watermark_status']);
- /** @var SystemConfigTabServices $systemConfigTabServices */
- $systemConfigTabServices = app()->make(SystemConfigTabServices::class);
- //上传配置->基础配置
- $tab_id = $systemConfigTabServices->getColumn(['eng_title' => 'upload_base'], 'id');
- if ($tab_id) {
- $all = $this->getColumn(['config_tab_id' => $tab_id], 'value', 'menu_name');
- if (array_intersect(array_keys($all), array_keys($post))) {
- foreach ($post as $key => $item) {
- //配置更改删除原来生成的缩略图
- if (isset($all[$key]) && $item != $all[$key]) {
- try {
- FileService::delDir(public_path('uploads/thumb_water'));
- break;
- } catch (\Throwable $e) {
- }
- }
- }
- }
- }
- return true;
- }
- /**
- * 获取全部配置
- * @param array $configName
- * @param int $storeId
- * @param int $type 0 正常结构 1:只返回key value
- * @return array
- */
- public function getConfigAllField(array $configName = [], int $storeId = 0, int $type = 0)
- {
- $list = $this->withSearchSelect(['menu_name', 'store_id'], ['menu_name' => $configName, 'store_id' => $storeId])->column(implode(',', ['info', 'type', 'value', 'desc', 'parameter']), 'menu_name');
- foreach ($list as &$item) {
- $item['value'] = json_decode($item['value'], true);
- }
- $value = [];
- foreach ($configName as $key) {
- if ($type) {
- $value[$key] = $list[$key]['value'] ?? '';
- } else {
- $value[$key] = $list[$key] ?? ['info' => '', 'type' => 'text', 'value' => '', 'desc' => '', 'parameter' => ''];
- }
- }
- return $value;
- }
- /**
- * 获取配置
- * @param string $key
- * @param null $default
- * @return mixed
- */
- public function getConfig(string $key, $default = null)
- {
- return sys_config($key, $default);
- }
- }
|