ConfigValue.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-03-24
  7. *
  8. *
  9. */
  10. namespace app\controller\admin\system\config;
  11. use crmeb\basic\BaseController;
  12. use app\common\repositories\system\config\ConfigClassifyRepository;
  13. use app\common\repositories\system\config\ConfigValueRepository;
  14. use think\App;
  15. use think\facade\Db;
  16. /**
  17. * Class ConfigValue
  18. * @package app\controller\admin\system\config
  19. * @author xaboy
  20. * @day 2020-03-27
  21. */
  22. class ConfigValue extends BaseController
  23. {
  24. /**
  25. * @var ConfigClassifyRepository
  26. */
  27. private $repository;
  28. /**
  29. * ConfigValue constructor.
  30. * @param App $app
  31. * @param ConfigValueRepository $repository
  32. */
  33. public function __construct(App $app, ConfigValueRepository $repository)
  34. {
  35. parent::__construct($app);
  36. $this->repository = $repository;
  37. }
  38. /**
  39. * @param string $key
  40. * @return mixed
  41. * @author xaboy
  42. * @day 2020-04-22
  43. */
  44. public function save($key)
  45. {
  46. $formData = $this->request->post();
  47. if (!count($formData)) return app('json')->fail('保存失败');
  48. /** @var ConfigClassifyRepository $make */
  49. $make = app()->make(ConfigClassifyRepository::class);
  50. if (!($cid = $make->keyById($key))) return app('json')->fail('保存失败');
  51. $this->repository->save($cid, $formData, $this->request->merId());
  52. return app('json')->success('保存成功');
  53. }
  54. /**
  55. * @param $name
  56. * @return mixed
  57. * @author xaboy
  58. * @day 2020-03-27
  59. */
  60. public function getByName($name)
  61. {
  62. $data = Db::name('system_config_value')->where('config_key', $name)->where('mer_id', 0)->value('value');
  63. if (!$data)
  64. return app('json')->fail('配置不存在');
  65. else {
  66. if (is_json($data)) {
  67. $data = json_decode($data, true);
  68. }
  69. return app('json')->success($data);
  70. }
  71. }
  72. /**
  73. * @return mixed
  74. * @author xaboy
  75. * @day 2020-04-22
  76. */
  77. public function saveByName()
  78. {
  79. $name = input('name');
  80. $value = input('value');
  81. if (!$name || !$value) return app('json')->fail('保存失败');
  82. /** @var ConfigClassifyRepository $make */
  83. if (in_array($name, ['commission_rate', 'invite_chuangke_rate', 'bonus_setting', 'zhitui_rate'])) {
  84. $value= json_encode($value);
  85. }
  86. Db::name('system_config_value')->where('config_key', $name)->where('mer_id', 0)->update([
  87. 'value' => $value
  88. ]);
  89. return app('json')->success('保存成功');
  90. }
  91. }