StoreServiceSpeechcraft.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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\StoreServiceSpeechcraftServices;
  15. use app\validate\admin\service\StoreServiceSpeechcraftValidate;
  16. use think\facade\App;
  17. /**
  18. * 话术空控制器
  19. * Class StoreServiceSpeechcraft
  20. * @package app\controller\admin\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. *
  37. * @return \think\Response
  38. */
  39. public function index(Request $request)
  40. {
  41. $where = $request->getMore([
  42. ['title', ''],
  43. ['message', ''],
  44. [['cate_id', 'd'], ''],
  45. ]);
  46. $where['kefu_id'] = 0;
  47. return $this->success($this->services->getSpeechcraftList($where));
  48. }
  49. /**
  50. * 显示创建资源表单页.
  51. *
  52. * @return \think\Response
  53. */
  54. public function create()
  55. {
  56. [$cate_id] = $this->request->getMore([
  57. ['cate_id', 0]
  58. ], true);
  59. return $this->success($this->services->createForm((int)$cate_id));
  60. }
  61. /**
  62. * 保存新建的资源
  63. *
  64. * @param \app\Request $request
  65. * @return \think\Response
  66. */
  67. public function save(Request $request)
  68. {
  69. $data = $request->postMore([
  70. ['title', ''],
  71. ['message', ''],
  72. [['cate_id', 'd'], 0],
  73. ['sort', 0],
  74. ]);
  75. $this->validate($data, StoreServiceSpeechcraftValidate::class);
  76. $data['add_time'] = time();
  77. $data['kefu_id'] = 0;
  78. if ($this->services->count(['message' => $data['message']])) {
  79. return $this->fail('话术不能重复添加');
  80. }
  81. if ($this->services->save($data)) {
  82. return $this->success('创建话术成功');
  83. } else {
  84. return $this->fail('创建话术失败');
  85. }
  86. }
  87. /**
  88. * 显示指定的资源
  89. *
  90. * @param int $id
  91. * @return \think\Response
  92. */
  93. public function read($id)
  94. {
  95. $info = $this->services->get($id);
  96. if (!$info) {
  97. return $this->fail('获取失败');
  98. }
  99. return $this->success($info);
  100. }
  101. /**
  102. * 显示编辑资源表单页.
  103. *
  104. * @param int $id
  105. * @return \think\Response
  106. */
  107. public function edit($id)
  108. {
  109. return $this->success($this->services->updateForm((int)$id));
  110. }
  111. /**
  112. * 保存更新的资源
  113. *
  114. * @param \app\Request $request
  115. * @param int $id
  116. * @return \think\Response
  117. */
  118. public function update(Request $request, $id)
  119. {
  120. $data = $request->postMore([
  121. ['title', ''],
  122. ['message', ''],
  123. ['sort', 0],
  124. [['cate_id', 'd'], 0],
  125. ]);
  126. $this->validate($data, StoreServiceSpeechcraftValidate::class);
  127. $message = $this->services->get(['message' => $data['message']]);
  128. if ($message && $message['id'] != $id) {
  129. return $this->fail('话术不能重复添加');
  130. }
  131. if ($this->services->update($id, $data)) {
  132. return $this->success('修改成功');
  133. } else {
  134. return $this->fail('修改失败');
  135. }
  136. }
  137. /**
  138. * 删除指定资源
  139. *
  140. * @param int $id
  141. * @return \think\Response
  142. */
  143. public function delete($id)
  144. {
  145. if (!$id || !($info = $this->services->get($id))) {
  146. return $this->fail('删除的话术不存在!');
  147. }
  148. if ($info->delete()) {
  149. return $this->success('删除成功');
  150. } else {
  151. return $this->fail('删除失败');
  152. }
  153. }
  154. }