StoreServiceSpeechcraft.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller\v1\kefu;
  12. use app\Request;
  13. use think\facade\App;
  14. use app\adminapi\controller\AuthController;
  15. use app\services\kefu\service\StoreServiceSpeechcraftServices;
  16. use app\adminapi\validate\service\StoreServiceSpeechcraftValidata;
  17. /**
  18. * 话术空控制器
  19. * Class StoreServiceSpeechcraft
  20. * @package app\adminapi\controller\v1\application\wechat
  21. */
  22. class StoreServiceSpeechcraft extends AuthController
  23. {
  24. /**
  25. * StoreServiceSpeechcraft constructor.
  26. * @param App $app
  27. * @param StoreServiceSpeechcraftServices $services
  28. */
  29. public function __construct(App $app, StoreServiceSpeechcraftServices $services)
  30. {
  31. parent::__construct($app);
  32. $this->services = $services;
  33. }
  34. /**
  35. * 显示资源列表
  36. * @param Request $request
  37. * @return mixed
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\DbException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. */
  42. public function index(Request $request)
  43. {
  44. $where = $request->getMore([
  45. ['title', ''],
  46. ['message', ''],
  47. [['cate_id', 'd'], ''],
  48. ]);
  49. $where['kefu_id'] = 0;
  50. return app('json')->success($this->services->getSpeechcraftList($where));
  51. }
  52. /**
  53. * 显示创建资源表单页
  54. * @return mixed
  55. */
  56. public function create()
  57. {
  58. return app('json')->success($this->services->createForm());
  59. }
  60. /**
  61. * 保存新建的资源
  62. * @param Request $request
  63. * @return \think\Response
  64. */
  65. public function save(Request $request)
  66. {
  67. $data = $request->postMore([
  68. ['title', ''],
  69. ['message', ''],
  70. [['cate_id', 'd'], 0],
  71. ['sort', 0],
  72. ]);
  73. $this->validate($data, StoreServiceSpeechcraftValidata::class);
  74. $data['add_time'] = time();
  75. $data['kefu_id'] = 0;
  76. if ($this->services->count(['message' => $data['message']])) {
  77. return app('json')->fail(400269);
  78. }
  79. if ($this->services->save($data)) {
  80. return app('json')->success(400270);
  81. } else {
  82. return app('json')->fail(400271);
  83. }
  84. }
  85. /**
  86. * 显示指定的资源
  87. * @param int $id
  88. * @return \think\Response
  89. */
  90. public function read($id)
  91. {
  92. $info = $this->services->get($id);
  93. if (!$info) {
  94. return app('json')->fail(100026);
  95. }
  96. return app('json')->success($info);
  97. }
  98. /**
  99. * 显示编辑资源表单页
  100. * @param $id
  101. * @return mixed
  102. * @throws \FormBuilder\Exception\FormBuilderException
  103. * @throws \think\db\exception\DataNotFoundException
  104. * @throws \think\db\exception\DbException
  105. * @throws \think\db\exception\ModelNotFoundException
  106. */
  107. public function edit($id)
  108. {
  109. return app('json')->success($this->services->updateForm((int)$id));
  110. }
  111. /**
  112. * 保存更新的资源
  113. * @param Request $request
  114. * @param int $id
  115. * @return \think\Response
  116. */
  117. public function update(Request $request, $id)
  118. {
  119. $data = $request->postMore([
  120. ['title', ''],
  121. ['message', ''],
  122. ['sort', 0],
  123. [['cate_id', 'd'], 0],
  124. ]);
  125. $this->validate($data, StoreServiceSpeechcraftValidata::class);
  126. $message = $this->services->get(['message' => $data['message']]);
  127. if ($message && $message['id'] != $id) {
  128. return app('json')->fail(400269);
  129. }
  130. if ($this->services->update($id, $data)) {
  131. return app('json')->success(100001);
  132. } else {
  133. return app('json')->fail(100007);
  134. }
  135. }
  136. /**
  137. * 删除指定资源
  138. * @param int $id
  139. * @return \think\Response
  140. */
  141. public function delete($id)
  142. {
  143. if (!$id || !($info = $this->services->get($id))) {
  144. return app('json')->fail(400272);
  145. }
  146. if ($info->delete()) {
  147. return app('json')->success(100002);
  148. } else {
  149. return app('json')->fail(100008);
  150. }
  151. }
  152. }