123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?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\controller\admin\system\config;
- use app\common\AdminBaseController;
- use app\Request;
- use app\services\system\config\SystemConfigServices;
- use app\services\system\config\SystemConfigTabServices;
- use app\validate\admin\SystemConfigValidate;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\facade\App;
- use think\Response;
- /**
- * 系统配置
- * Class SystemConfig
- * @package app\controller\admin\v1\setting
- */
- class SystemConfig extends AdminBaseController
- {
- /**
- * SystemConfig constructor.
- * @param Request $request
- * @param SystemConfigServices $service
- */
- public function __construct(Request $request, SystemConfigServices $service)
- {
- parent::__construct($request);
- $this->service = $service;
- $this->searchable = [
- ['tab_id', 0],
- ['status', -1]
- ];
- $this->createParams = [
- ['menu_name', ''],
- ['type', ''],
- ['input_type', ''],
- ['config_tab_id', []],
- ['parameter', ''],
- ['value', ''],
- ['info', ''],
- ['desc', ''],
- ['sort', 0],
- ['status', 0],
- ['configuration', []],
- ['is_store', 0],
- ];
- }
- /**
- * 显示资源列表
- *
- * @return Response
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function index()
- {
- $where = $this->request->getMore($this->searchable);
- if (!$where['tab_id']) {
- return $this->error('参数错误');
- }
- if ($where['status'] == -1) {
- unset($where['status']);
- }
- $where['is_store'] = 0;
- return $this->success($this->service->getConfigList($where));
- }
- /**
- * 保存新建的资源
- *
- * @return Response
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function save()
- {
- $data = $this->request->postMore($this->createParams);
- if ($data['config_tab_id']) $data['config_tab_id'] = end($data['config_tab_id']);
- if (!$data['info']) return $this->error('请输入配置名称');
- if (!$data['menu_name']) return $this->error('请输入字段名称');
- if ($this->service->be(['menu_name' => $data['menu_name']])) return $this->error('字段名已存在');
- if (!$data['desc']) return $this->error('请输入配置简介');
- if ($data['sort'] < 0) {
- $data['sort'] = 0;
- }
- $data['value'] = json_encode($data['value']);
- // $config = $this->service->getOne(['menu_name' => $data['menu_name']]);
- // if ($config) {
- // $this->service->update($config['id'], $data, 'id');
- // } else {
- $this->service->create($data);
- // }
- event('config.create', [$data]);
- return $this->success('添加配置成功!');
- }
- /**
- * 保存更新的资源
- *
- * @param int $id
- * @return Response
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function update($id)
- {
- $type = request()->post('type');
- $configuration = request()->post('configuration/a');
- if ($type == 'Input' || $type == 'RadioGroup' || $type == 'Select' || $type == 'Datepicker' || ($type == 'Upload' && (($configuration['upload_type'] ?? 1) == 1 || ($configuration['upload_type'] ?? 1) == 3))) {
- $value = request()->post('value');
- } else {
- $value = request()->post('value/a');
- }
- if (!$value) $value = request()->post(request()->post('menu_name'));
- $data = $this->request->postMore(array_merge($this->createParams, [['value', $value]]));
- $data['config_tab_id'] = end($data['config_tab_id']);
- if (!$this->service->get($id)) {
- return $this->error('编辑的记录不存在!');
- }
- $data['value'] = json_encode($data['value']);
- $this->service->update($id, $data);
- event('config.update');
- return $this->success('修改成功!');
- }
- /**
- * 删除指定资源
- *
- * @param int $id
- * @return Response
- */
- public function delete($id)
- {
- if (!$this->service->delete($id))
- return $this->error('删除失败,请稍候再试!');
- else {
- event('config.delete', [$id]);
- return $this->success('删除成功!');
- }
- }
- /**
- * 修改状态
- * @param $id
- * @param $status
- * @return mixed
- */
- public function setStatus($id, $status)
- {
- if ($status == '' || $id == 0) {
- return $this->error('参数错误');
- }
- $this->service->update($id, ['status' => $status]);
- event('config.update');
- return $this->success($status == 0 ? '隐藏成功' : '显示成功');
- }
- /**
- * 显示指定的资源
- *
- * @param int $id
- * @return Response
- */
- public function readConfig($id)
- {
- if (!$id) {
- return $this->error('参数错误,请重新打开');
- }
- $info = $this->service->getReadList((int)$id);
- return $this->success(compact('info'));
- }
- /**
- * 保存数据 true
- * */
- public function saveConfig(Request $request)
- {
- $post = $this->request->post();
- foreach ($post as $k => $v) {
- if (is_array($v)) {
- $res = $this->service->getUploadTypeList($k);
- foreach ($res as $kk => $vv) {
- if ($kk == 'Upload') {
- if (($vv['upload_type'] ?? 1) == 1 || ($vv['upload_type'] ?? 1) == 3) {
- $post[$k] = $v[0];
- }
- }
- }
- }
- }
- $this->validate($post, SystemConfigValidate::class);
- if (isset($post['upload_type'])) {
- $this->service->checkThumbParam($post);
- }
- if (isset($post['spread_banner'])) {
- $num = count($post['spread_banner']);
- if ($num > 5) {
- return $this->error('分销海报不能多于5张');
- }
- }
- if (isset($post['user_extract_min_price'])) {
- if (!preg_match('/[0-9]$/', $post['user_extract_min_price'])) {
- return $this->error('提现最低金额只能为数字!');
- }
- }
- if (isset($post['store_extract_max_price']) && isset($post['store_extract_min_price'])) {
- if ($post['store_extract_max_price'] < $post['store_extract_min_price']) {
- return $this->error('门店提现最低金额不能大于最高金额');
- }
- }
- if (isset($post['default_send_day'])) {
- $post['default_send_day_start_time'] = time();
- }
- foreach ($post as $k => $v) {
- $config_one = $this->service->getOne(['menu_name' => $k]);
- if ($config_one) {
- $config_one['value'] = $v;
- $this->service->update($k, ['value' => json_encode($v)], 'menu_name');
- }
- }
- event('config.update');
- return $this->success('修改成功');
- }
- /**
- * 获取系统设置头部分类
- * @param SystemConfigTabServices $services
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function header_basics(SystemConfigTabServices $services)
- {
- [$pid] = $this->request->getMore([
- [['pid', 'd'], 0]
- ], true);
- $config_tab = $services->getConfigTab($pid);
- return $this->success(compact('config_tab'));
- }
- /**
- * 获取单个配置的值
- * @param $name
- * @return mixed
- */
- public function get_system($name)
- {
- $value = sys_config($name);
- return $this->success(compact('value'));
- }
- }
|