Service.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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\kefu;
  12. use app\Request;
  13. use think\facade\App;
  14. use app\services\kefu\KefuServices;
  15. use app\services\other\CategoryServices;
  16. use app\validate\kefu\SpeechcraftValidate;
  17. use app\services\message\service\StoreServiceSpeechcraftServices;
  18. /**
  19. * Class Service
  20. * @package app\kefuapi\controller
  21. */
  22. class Service extends AuthController
  23. {
  24. /**
  25. * Service constructor.
  26. * @param App $app
  27. * @param KefuServices $services
  28. */
  29. public function __construct(App $app, KefuServices $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 getServiceList(Request $request, $uid = 0)
  43. {
  44. $where = $request->getMore([
  45. ['nickname', ''],
  46. ]);
  47. return $this->success($this->services->getServiceList($where, [$this->kefuInfo['uid'], $uid]));
  48. }
  49. /**
  50. * 话术列表
  51. * @param Request $request
  52. * @param StoreServiceSpeechcraftServices $services
  53. * @return mixed
  54. * @throws \think\db\exception\DataNotFoundException
  55. * @throws \think\db\exception\DbException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. */
  58. public function getSpeechcraftList(Request $request, StoreServiceSpeechcraftServices $services)
  59. {
  60. $where = $request->getMore([
  61. ['title', ''],
  62. ['cate_id', ''],
  63. ['type', 0]
  64. ]);
  65. if ($where['type']) {
  66. $where['kefu_id'] = $this->kefuId;
  67. } else {
  68. $where['kefu_id'] = 0;
  69. }
  70. $data = $services->getSpeechcraftList($where);
  71. return $this->success($data['list']);
  72. }
  73. /**
  74. * 添加分类
  75. * @param Request $request
  76. * @param CategoryServices $services
  77. * @return mixed
  78. */
  79. public function saveCate(Request $request, CategoryServices $services)
  80. {
  81. $data = $request->postMore([
  82. ['name', ''],
  83. [['sort', 'd'], 0],
  84. ]);
  85. if (!$data['name']) {
  86. return $this->fail('分类名称不能为空');
  87. }
  88. $data['add_time'] = time();
  89. $data['owner_id'] = $this->kefuId;
  90. $data['type'] = 1;
  91. $data['group'] = 1;
  92. $services->save($data);
  93. return $this->success('添加成功');
  94. }
  95. /**
  96. * 修改分类
  97. * @param Request $request
  98. * @param CategoryServices $services
  99. * @param $id
  100. * @return mixed
  101. */
  102. public function editCate(Request $request, CategoryServices $services, $id)
  103. {
  104. $data = $request->postMore([
  105. ['name', ''],
  106. [['sort', 'd'], 0],
  107. ]);
  108. if (!$data['name']) {
  109. return $this->fail('分类不能为空');
  110. }
  111. $cateInfo = $services->get($id);
  112. if (!$cateInfo) {
  113. return $this->fail('分类没有查到无法删除');
  114. }
  115. $cateInfo->name = $data['name'];
  116. $cateInfo->sort = $data['sort'];
  117. if ($cateInfo->save()) {
  118. return $this->success('分类修改成功');
  119. } else {
  120. return $this->fail('分类没有查到无法删除');
  121. }
  122. }
  123. /**
  124. * 删除分类
  125. * @param CategoryServices $services
  126. * @param $id
  127. * @return mixed
  128. */
  129. public function deleteCate(CategoryServices $services, $id)
  130. {
  131. $cateInfo = $services->get($id);
  132. if (!$cateInfo) {
  133. return $this->fail('分类不存在');
  134. }
  135. if ($cateInfo->delete()) {
  136. return $this->success('删除成功');
  137. } else {
  138. return $this->fail('删除失败');
  139. }
  140. }
  141. /**
  142. * 获取当前客服分类
  143. * @param CategoryServices $services
  144. * @return mixed
  145. * @throws \think\db\exception\DataNotFoundException
  146. * @throws \think\db\exception\DbException
  147. * @throws \think\db\exception\ModelNotFoundException
  148. */
  149. public function getCateList(CategoryServices $services, $type)
  150. {
  151. return $this->success($services->getCateList(['owner_id' => $type ? $this->kefuId : 0, 'type' => 1, 'group' => 1], ['id', 'name', 'sort']));
  152. }
  153. /**
  154. * 添加话术
  155. * @param Request $request
  156. * @param StoreServiceSpeechcraftServices $services
  157. * @return mixed
  158. */
  159. public function saveSpeechcraft(Request $request, StoreServiceSpeechcraftServices $services, CategoryServices $categoryServices)
  160. {
  161. $data = $request->postMore([
  162. ['title', ''],
  163. ['cate_id', 0],
  164. ['message', ''],
  165. ['sort', 0]
  166. ]);
  167. validate(SpeechcraftValidate::class)->check($data);
  168. if ($data['cate_id'] && !$categoryServices->count(['owner_id' => $this->kefuId, 'type' => 1, 'id' => $data['cate_id']])) {
  169. return $this->fail('您选择的分类不存在');
  170. }
  171. if ($services->count(['message' => $data['message']])) {
  172. return $this->fail('添加的内容重复');
  173. }
  174. $data['add_time'] = time();
  175. $data['kefu_id'] = $this->kefuId;
  176. $res = $services->save($data);
  177. if ($res) {
  178. return $this->success('添加话术成功', $res->toArray());
  179. } else {
  180. return $this->fail('添加话术失败');
  181. }
  182. }
  183. /**
  184. * 修改话术
  185. * @param Request $request
  186. * @param StoreServiceSpeechcraftServices $services
  187. * @param $id
  188. * @return mixed
  189. */
  190. public function editSpeechcraft(Request $request, StoreServiceSpeechcraftServices $services, CategoryServices $categoryServices, $id)
  191. {
  192. $data = $request->postMore([
  193. ['title', ''],
  194. ['cate_id', 0],
  195. ['message', ''],
  196. ]);
  197. if (!$data['message']) {
  198. return $this->fail('话术标题内容不能为空');
  199. }
  200. if (!$categoryServices->count(['owner_id' => $this->kefuId, 'type' => 1, 'id' => $data['cate_id']])) {
  201. return $this->fail('您选择的分类不存在');
  202. }
  203. $speechcraft = $services->get($id);
  204. if (!$speechcraft) {
  205. return $this->fail('话术没有被查到');
  206. }
  207. if (!$speechcraft->kefu_id) {
  208. return $this->fail('公共话术不能修改');
  209. }
  210. $speechcraft->title = $data['title'];
  211. if ($data['cate_id']) {
  212. $speechcraft->cate_id = $data['cate_id'];
  213. }
  214. $speechcraft->message = $data['message'];
  215. if ($speechcraft->save()) {
  216. return $this->success('修改成功');
  217. } else {
  218. return $this->fail('修改失败');
  219. }
  220. }
  221. /**
  222. * 删除话术
  223. * @param StoreServiceSpeechcraftServices $services
  224. * @param $id
  225. * @return mixed
  226. */
  227. public function deleteSpeechcraft(StoreServiceSpeechcraftServices $services, $id)
  228. {
  229. $speechcraft = $services->get($id);
  230. if (!$speechcraft) {
  231. return $this->fail('话术没有被查到');
  232. }
  233. if ($speechcraft->delete()) {
  234. return $this->success('删除成功');
  235. } else {
  236. return $this->fail('删除失败');
  237. }
  238. }
  239. /**
  240. * 聊天记录
  241. * @param $uid
  242. * @return mixed
  243. */
  244. public function getChatList(Request $request)
  245. {
  246. [$uid, $upperId, $is_tourist] = $request->postMore([
  247. ['uid', 0],
  248. ['upperId', 0],
  249. ['is_tourist', 0],
  250. ], true);
  251. if (!$uid) {
  252. return $this->fail('缺少参数');
  253. }
  254. return $this->success($this->services->getChatList($this->kefuInfo['uid'], $uid, (int)$upperId, $is_tourist));
  255. }
  256. /**
  257. * 当前客服详细信息
  258. * @return mixed
  259. */
  260. public function getServiceInfo()
  261. {
  262. $this->kefuInfo['site_name'] = sys_config('site_name');
  263. $this->kefuInfo['config_export_open'] = sys_config('config_export_open');
  264. return $this->success($this->kefuInfo->toArray());
  265. }
  266. /**
  267. * 客服转接
  268. * @return mixed
  269. */
  270. public function transfer()
  271. {
  272. [$kefuToUid, $uid] = $this->request->postMore([
  273. ['kefuToUid', 0],
  274. ['uid', 0]
  275. ], true);
  276. if (!$kefuToUid || !$uid) {
  277. return $this->fail('缺少转接人id');
  278. }
  279. $this->services->setTransfer($this->kefuInfo['uid'], (int)$uid, (int)$kefuToUid);
  280. return $this->success('转接成功');
  281. }
  282. }