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