ChannelCate.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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\v1\work;
  12. use app\controller\admin\AuthController;
  13. use app\Request;
  14. use app\services\work\WorkChannelCodeServices;
  15. use think\facade\App;
  16. use app\services\work\WorkChannelCategoryServices;
  17. /**
  18. * 渠道二维码分类
  19. * Class ClientCate
  20. * @package app\controller\admin\v1\work
  21. */
  22. class ChannelCate extends AuthController
  23. {
  24. /**
  25. * ClientCate constructor.
  26. * @param App $app
  27. * @param WorkChannelCategoryServices $services
  28. */
  29. public function __construct(App $app, WorkChannelCategoryServices $services)
  30. {
  31. parent::__construct($app);
  32. $this->services = $services;
  33. }
  34. /**
  35. * 获取列表
  36. * @return mixed
  37. */
  38. public function index()
  39. {
  40. return $this->success($this->services->getCateAll());
  41. }
  42. /**
  43. * @return mixed
  44. * @throws \FormBuilder\Exception\FormBuilderException
  45. */
  46. public function create()
  47. {
  48. return $this->success($this->services->createForm());
  49. }
  50. /**
  51. * @param $id
  52. * @return mixed
  53. * @throws \FormBuilder\Exception\FormBuilderException
  54. * @throws \think\db\exception\DataNotFoundException
  55. * @throws \think\db\exception\DbException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. */
  58. public function edit($id)
  59. {
  60. return $this->success($this->services->updateForm((int)$id));
  61. }
  62. /**
  63. * 保存数据
  64. * @param Request $request
  65. * @return mixed
  66. */
  67. public function save(Request $request)
  68. {
  69. $data = $request->postMore([
  70. ['name', ''],
  71. ['sort', 0]
  72. ]);
  73. if (!$data['name']) {
  74. return $this->fail('请输入分类名称');
  75. }
  76. if ($this->services->count(['nowName' => $data['name'], 'group' => WorkChannelCategoryServices::TYPE])) {
  77. return $this->fail('分类名称已存在');
  78. }
  79. $data['type'] = 0;
  80. $data['relation_id'] = 0;
  81. $data['group'] = WorkChannelCategoryServices::TYPE;
  82. if ($this->services->save($data)) {
  83. return $this->success('添加成功');
  84. } else {
  85. return $this->fail('添加失败');
  86. }
  87. }
  88. /**
  89. * 修改分类
  90. * @param Request $request
  91. * @param $id
  92. * @return mixed
  93. */
  94. public function update(Request $request, $id)
  95. {
  96. $data = $this->request->postMore([
  97. ['name', ''],
  98. ['sort', 0]
  99. ]);
  100. if (!$data['name']) {
  101. return $this->fail('请输入分类名称');
  102. }
  103. if ($this->services->count(['notId' => $id, 'nowName' => $data['name'], 'group' => WorkChannelCategoryServices::TYPE])) {
  104. return $this->fail('分类名称已存在');
  105. }
  106. if ($this->services->update($id, $data)) {
  107. return $this->success('修改成功');
  108. } else {
  109. return $this->fail('修改失败');
  110. }
  111. }
  112. /**
  113. * @param WorkChannelCodeServices $channelCodeServices
  114. * @param $id
  115. * @return mixed
  116. */
  117. public function delete(WorkChannelCodeServices $channelCodeServices, $id)
  118. {
  119. if (!$id) {
  120. return $this->fail('缺少参数');
  121. }
  122. if ($channelCodeServices->count(['cate_id' => $id])) {
  123. return $this->success('分类下有渠道码不删除');
  124. }
  125. if ($this->services->delete($id)) {
  126. return $this->success('删除成功');
  127. } else {
  128. return $this->fail('删除失败');
  129. }
  130. }
  131. }