Config.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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\controller\admin\system\config;
  12. use crmeb\basic\BaseController;
  13. use app\common\repositories\system\config\ConfigClassifyRepository;
  14. use app\common\repositories\system\config\ConfigRepository;
  15. use app\validate\admin\ConfigValidate;
  16. use crmeb\services\UploadService;
  17. use FormBuilder\Exception\FormBuilderException;
  18. use think\App;
  19. use think\db\exception\DataNotFoundException;
  20. use think\db\exception\DbException;
  21. use think\db\exception\ModelNotFoundException;
  22. /**
  23. * Class Config
  24. * @package app\controller\admin\system\config
  25. * @author xaboy
  26. * @day 2020-03-27
  27. */
  28. class Config extends BaseController
  29. {
  30. /**
  31. * @var ConfigRepository
  32. */
  33. protected $repository;
  34. /**
  35. * Config constructor.
  36. * @param App $app
  37. * @param ConfigRepository $repository
  38. */
  39. public function __construct(App $app, ConfigRepository $repository)
  40. {
  41. parent::__construct($app);
  42. $this->repository = $repository;
  43. }
  44. /**
  45. * @return mixed
  46. * @throws DataNotFoundException
  47. * @throws DbException
  48. * @throws ModelNotFoundException
  49. * @author xaboy
  50. * @day 2020-03-31
  51. */
  52. public function lst()
  53. {
  54. $where = $this->request->params(['keyword']);
  55. [$page, $limit] = $this->getPage();
  56. $lst = $this->repository->lst($where, $page, $limit);
  57. return app('json')->success($lst);
  58. }
  59. /**
  60. * @return mixed
  61. * @throws FormBuilderException
  62. * @author xaboy
  63. * @day 2020-03-31
  64. */
  65. public function createTable()
  66. {
  67. $form = $this->repository->form();
  68. return app('json')->success(formToData($form));
  69. }
  70. /**
  71. * @param int $id
  72. * @return mixed
  73. * @throws DataNotFoundException
  74. * @throws DbException
  75. * @throws ModelNotFoundException
  76. * @throws FormBuilderException
  77. * @author xaboy
  78. * @day 2020-03-31
  79. */
  80. public function updateTable($id)
  81. {
  82. if (!$this->repository->exists($id)) app('json')->fail('数据不存在');
  83. $form = $this->repository->updateForm($id);
  84. return app('json')->success(formToData($form));
  85. }
  86. /**
  87. * @param int $id
  88. * @return mixed
  89. * @throws DbException
  90. * @author xaboy
  91. * @day 2020-03-31
  92. */
  93. public function switchStatus($id)
  94. {
  95. $status = $this->request->param('status', 0);
  96. if (!$this->repository->exists($id))
  97. return app('json')->fail('分类不存在');
  98. $this->repository->switchStatus($id, $status == 1 ? 1 : 0);
  99. return app('json')->success('修改成功');
  100. }
  101. /**
  102. * @param int $id
  103. * @return mixed
  104. * @throws DataNotFoundException
  105. * @throws DbException
  106. * @throws ModelNotFoundException
  107. * @author xaboy
  108. * @day 2020-03-27
  109. */
  110. public function get($id)
  111. {
  112. $data = $this->repository->get($id);
  113. if (!$data)
  114. return app('json')->fail('配置不存在');
  115. else
  116. return app('json')->success($data->hidden(['mer_id', 'value']));
  117. }
  118. /**
  119. * @param ConfigValidate $validate
  120. * @param ConfigClassifyRepository $configClassifyRepository
  121. * @return mixed
  122. * @author xaboy
  123. * @day 2020-03-27
  124. */
  125. public function create(ConfigValidate $validate, ConfigClassifyRepository $configClassifyRepository)
  126. {
  127. $data = $this->request->params(['user_type', 'config_classify_id', 'config_name', 'config_key', 'config_type', 'config_rule', 'required', 'info', 'sort', 'status']);
  128. $validate->check($data);
  129. if (!$configClassifyRepository->exists($data['config_classify_id']))
  130. return app('json')->fail('配置分类不已存在');
  131. if ($this->repository->keyExists($data['config_key']))
  132. return app('json')->fail('配置key已存在');
  133. $this->repository->create($data);
  134. return app('json')->success('添加成功');
  135. }
  136. /**
  137. * @param int $id
  138. * @param ConfigValidate $validate
  139. * @param ConfigClassifyRepository $configClassifyRepository
  140. * @return mixed
  141. * @throws DbException
  142. * @author xaboy
  143. * @day 2020-03-27
  144. */
  145. public function update($id, ConfigValidate $validate, ConfigClassifyRepository $configClassifyRepository)
  146. {
  147. $data = $this->request->params(['user_type', 'config_classify_id', 'config_name', 'config_key', 'config_type', 'config_rule', 'required', 'info', 'sort', 'status']);
  148. $validate->check($data);
  149. if (!$this->repository->exists($id))
  150. return app('json')->fail('分类不存在');
  151. if (!$configClassifyRepository->exists($data['config_classify_id']))
  152. return app('json')->fail('配置分类不已存在');
  153. if ($this->repository->keyExists($data['config_key'], $id))
  154. return app('json')->fail('配置key已存在');
  155. $this->repository->update($id, $data);
  156. return app('json')->success('修改成功');
  157. }
  158. /**
  159. * @param int $id
  160. * @return mixed
  161. * @throws DbException
  162. * @author xaboy
  163. * @day 2020-03-27
  164. */
  165. public function delete($id)
  166. {
  167. $this->repository->delete($id);
  168. return app('json')->success('删除成功');
  169. }
  170. /**
  171. * @param string $key
  172. * @param ConfigClassifyRepository $configClassifyRepository
  173. * @return mixed
  174. * @throws DataNotFoundException
  175. * @throws DbException
  176. * @throws FormBuilderException
  177. * @throws ModelNotFoundException
  178. * @author xaboy
  179. * @day 2020-04-22
  180. */
  181. public function form($key, ConfigClassifyRepository $configClassifyRepository)
  182. {
  183. if (!$configClassifyRepository->keyExists($key) || !($configClassfiy = $configClassifyRepository->keyByData($key)))
  184. return app('json')->fail('配置分类不存在');
  185. $form = $this->repository->cidByFormRule($configClassfiy, $this->request->merId());
  186. return app('json')->success(formToData($form));
  187. }
  188. public function upload($field)
  189. {
  190. $file = $this->request->file($field);
  191. if (!$file)
  192. return app('json')->fail('请上传附件');
  193. $upload = UploadService::create(1);
  194. $data = $upload->to('attach')->validate()->move($field);
  195. if ($data === false) {
  196. return app('json')->fail($upload->getError());
  197. }
  198. $res = $upload->getUploadInfo();
  199. $res['dir'] = path_to_url($res['dir']);
  200. return app('json')->success(['src' => $res['dir']]);
  201. }
  202. }