Service.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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\kefuapi\controller;
  12. use app\Request;
  13. use think\facade\App;
  14. use app\services\kefu\KefuServices;
  15. use app\services\other\CategoryServices;
  16. use app\kefuapi\validate\SpeechcraftValidate;
  17. use app\services\kefu\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 app('json')->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 app('json')->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 app('json')->fail(410095);
  87. }
  88. $data['add_time'] = time();
  89. $data['owner_id'] = $this->kefuId;
  90. $data['type'] = 1;
  91. $services->save($data);
  92. return app('json')->success(100021);
  93. }
  94. /**
  95. * 修改分类
  96. * @param Request $request
  97. * @param CategoryServices $services
  98. * @param $id
  99. * @return mixed
  100. */
  101. public function editCate(Request $request, CategoryServices $services, $id)
  102. {
  103. $data = $request->postMore([
  104. ['name', ''],
  105. [['sort', 'd'], 0],
  106. ]);
  107. if (!$data['name']) {
  108. return app('json')->fail(410095);
  109. }
  110. $cateInfo = $services->get($id);
  111. if (!$cateInfo) {
  112. return app('json')->fail(100026);
  113. }
  114. $cateInfo->name = $data['name'];
  115. $cateInfo->sort = $data['sort'];
  116. if ($cateInfo->save()) {
  117. return app('json')->success(100001);
  118. } else {
  119. return app('json')->fail(100007);
  120. }
  121. }
  122. /**
  123. * 删除分类
  124. * @param CategoryServices $services
  125. * @param $id
  126. * @return mixed
  127. */
  128. public function deleteCate(CategoryServices $services, $id)
  129. {
  130. $cateInfo = $services->get($id);
  131. if (!$cateInfo) {
  132. return app('json')->fail(410096);
  133. }
  134. if ($cateInfo->delete()) {
  135. return app('json')->success(100002);
  136. } else {
  137. return app('json')->fail(100008);
  138. }
  139. }
  140. /**
  141. * 获取当前客服分类
  142. * @param CategoryServices $services
  143. * @return mixed
  144. * @throws \think\db\exception\DataNotFoundException
  145. * @throws \think\db\exception\DbException
  146. * @throws \think\db\exception\ModelNotFoundException
  147. */
  148. public function getCateList(CategoryServices $services, $type)
  149. {
  150. return app('json')->success($services->getCateList(['owner_id' => $type ? $this->kefuId : 0, 'type' => 1], ['id', 'name', 'sort']));
  151. }
  152. /**
  153. * 添加话术
  154. * @param Request $request
  155. * @param StoreServiceSpeechcraftServices $services
  156. * @return mixed
  157. */
  158. public function saveSpeechcraft(Request $request, StoreServiceSpeechcraftServices $services, CategoryServices $categoryServices)
  159. {
  160. $data = $request->postMore([
  161. ['title', ''],
  162. ['cate_id', 0],
  163. ['message', ''],
  164. ['sort', 0]
  165. ]);
  166. validate(SpeechcraftValidate::class)->check($data);
  167. if (!$categoryServices->count(['owner_id' => $this->kefuId, 'type' => 1, 'id' => $data['cate_id']])) {
  168. return app('json')->fail(410096);
  169. }
  170. if ($services->count(['message' => $data['message']])) {
  171. return app('json')->fail(410097);
  172. }
  173. $data['add_time'] = time();
  174. $data['kefu_id'] = $this->kefuId;
  175. $res = $services->save($data);
  176. if ($res) {
  177. return app('json')->success(100021, null, $res->toArray());
  178. } else {
  179. return app('json')->fail(100022);
  180. }
  181. }
  182. /**
  183. * 修改话术
  184. * @param Request $request
  185. * @param StoreServiceSpeechcraftServices $services
  186. * @param $id
  187. * @return mixed
  188. */
  189. public function editSpeechcraft(Request $request, StoreServiceSpeechcraftServices $services, CategoryServices $categoryServices, $id)
  190. {
  191. $data = $request->postMore([
  192. ['title', ''],
  193. ['cate_id', 0],
  194. ['message', ''],
  195. ]);
  196. if (!$data['message']) {
  197. return app('json')->fail(410102);
  198. }
  199. if (!$categoryServices->count(['owner_id' => $this->kefuId, 'type' => 1, 'id' => $data['cate_id']])) {
  200. return app('json')->fail(100026);
  201. }
  202. $speechcraft = $services->get($id);
  203. if (!$speechcraft) {
  204. return app('json')->fail(100026);
  205. }
  206. if (!$speechcraft->kefu_id) {
  207. return app('json')->fail(410101);
  208. }
  209. $speechcraft->title = $data['title'];
  210. if ($data['cate_id']) {
  211. $speechcraft->cate_id = $data['cate_id'];
  212. }
  213. $speechcraft->message = $data['message'];
  214. if ($speechcraft->save()) {
  215. return app('json')->success(100001);
  216. } else {
  217. return app('json')->fail(100007);
  218. }
  219. }
  220. /**
  221. * 删除话术
  222. * @param StoreServiceSpeechcraftServices $services
  223. * @param $id
  224. * @return mixed
  225. */
  226. public function deleteSpeechcraft(StoreServiceSpeechcraftServices $services, $id)
  227. {
  228. $speechcraft = $services->get($id);
  229. if (!$speechcraft) {
  230. return app('json')->fail(410100);
  231. }
  232. if ($speechcraft->delete()) {
  233. return app('json')->success(100002);
  234. } else {
  235. return app('json')->fail(100008);
  236. }
  237. }
  238. /**
  239. * 聊天记录
  240. * @param Request $request
  241. * @return mixed
  242. * @throws \think\db\exception\DataNotFoundException
  243. * @throws \think\db\exception\DbException
  244. * @throws \think\db\exception\ModelNotFoundException
  245. */
  246. public function getChatList(Request $request)
  247. {
  248. [$uid, $upperId, $is_tourist] = $request->postMore([
  249. ['uid', 0],
  250. ['upperId', 0],
  251. ['is_tourist', 0],
  252. ], true);
  253. if (!$uid) {
  254. return app('json')->fail(100100);
  255. }
  256. return app('json')->success($this->services->getChatList($this->kefuInfo['uid'], $uid, (int)$upperId, $is_tourist));
  257. }
  258. /**
  259. * 当前客服详细信息
  260. * @return mixed
  261. */
  262. public function getServiceInfo()
  263. {
  264. $this->kefuInfo['site_name'] = sys_config('site_name');
  265. return app('json')->success($this->kefuInfo->toArray());
  266. }
  267. /**
  268. * 客服转接
  269. * @return mixed
  270. */
  271. public function transfer()
  272. {
  273. [$kefuToUid, $uid] = $this->request->postMore([
  274. ['kefuToUid', 0],
  275. ['uid', 0]
  276. ], true);
  277. if (!$kefuToUid || !$uid) {
  278. return app('json')->fail(410098);
  279. }
  280. $this->services->setTransfer($this->kefuInfo['uid'], (int)$uid, (int)$kefuToUid);
  281. return app('json')->success(410099);
  282. }
  283. }