StoreService.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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\api\controller\v1\user;
  12. use app\api\validate\user\StoreServiceFeedbackValidate;
  13. use app\Request;
  14. use app\services\kefu\service\StoreServiceFeedbackServices;
  15. use app\services\kefu\service\StoreServiceLogServices;
  16. use app\services\kefu\service\StoreServiceRecordServices;
  17. use app\services\kefu\service\StoreServiceServices;
  18. use app\services\other\CacheServices;
  19. use crmeb\services\CacheService;
  20. /**
  21. * 客服类
  22. * Class StoreService
  23. * @package app\api\controller\user
  24. */
  25. class StoreService
  26. {
  27. /**
  28. * @var StoreServiceLogServices
  29. */
  30. protected $services;
  31. /**
  32. * StoreService constructor.
  33. * @param StoreServiceLogServices $services
  34. */
  35. public function __construct(StoreServiceLogServices $services)
  36. {
  37. $this->services = $services;
  38. }
  39. /**
  40. * 客服列表
  41. * @param StoreServiceServices $services
  42. * @return mixed
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\DbException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. */
  47. public function lst(StoreServiceServices $services)
  48. {
  49. $serviceInfoList = $services->getServiceList(['status' => 1]);
  50. if (!count($serviceInfoList)) return app('json')->success([]);
  51. return app('json')->success($serviceInfoList['list']);
  52. }
  53. /**
  54. * 客服聊天记录
  55. * @param Request $request
  56. * @param StoreServiceServices $services
  57. * @param StoreServiceRecordServices $recordServices
  58. * @return mixed
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. public function record(Request $request, StoreServiceServices $services, StoreServiceRecordServices $recordServices)
  64. {
  65. list($uidTo) = $request->getMore([
  66. ['uidTo', 0]
  67. ], true);
  68. $serviceInfoList = $services->getServiceList(['status' => 1]);
  69. if (!count($serviceInfoList)) return app('json')->fail(410136);
  70. $uid = $request->uid();
  71. $uids = array_column($serviceInfoList['list'], 'uid');
  72. if (!$uidTo) {
  73. //自己是客服
  74. if (in_array($uid, $uids)) {
  75. $uids = array_merge(array_diff($uids, [$uid]));
  76. if (!$uids) return app('json')->fail(410137);
  77. }
  78. } else {
  79. if (in_array($uid, $uids)) {
  80. $uid = $uidTo;
  81. }
  82. }
  83. if (!$uids) {
  84. return app('json')->fail(410136);
  85. }
  86. //上次聊天客服优先对话
  87. $toUid = $recordServices->value(['user_id' => $uid], 'to_uid');
  88. if (!in_array($toUid, $uids)) {
  89. $toUid = 0;
  90. }
  91. if (!$toUid) {
  92. $toUid = $uids[array_rand($uids)] ?? 0;
  93. }
  94. if (!$toUid) return app('json')->fail(410136);
  95. $result = ['serviceList' => [], 'uid' => $toUid];
  96. $serviceLogList = $this->services->getChatList(['uid' => $uid], $uid);
  97. if (!$serviceLogList) return app('json')->success($result);
  98. $idArr = array_column($serviceLogList, 'id');
  99. array_multisort($idArr, SORT_ASC, $serviceLogList);
  100. $result['serviceList'] = $serviceLogList;
  101. return app('json')->success($result);
  102. }
  103. /**
  104. * 获取客服页面广告内容
  105. * @return mixed
  106. */
  107. public function getKfAdv()
  108. {
  109. /** @var CacheServices $cache */
  110. $cache = app()->make(CacheServices::class);
  111. $content = $cache->getDbCache('kf_adv', '');
  112. return app('json')->success(compact('content'));
  113. }
  114. /**
  115. * 保存反馈信息
  116. * @param Request $request
  117. * @param StoreServiceFeedbackServices $services
  118. * @return mixed
  119. */
  120. public function saveFeedback(Request $request, StoreServiceFeedbackServices $services)
  121. {
  122. $data = $request->postMore([
  123. ['rela_name', ''],
  124. ['phone', ''],
  125. ['content', ''],
  126. ]);
  127. validate(StoreServiceFeedbackValidate::class)->check($data);
  128. $data['content'] = htmlspecialchars($data['content']);
  129. $data['add_time'] = time();
  130. $data['uid'] = $request->uid();
  131. $services->save($data);
  132. return app('json')->success(100000);
  133. }
  134. /**
  135. * 客服反馈页面头部文字
  136. * @return mixed
  137. */
  138. public function getFeedbackInfo()
  139. {
  140. return app('json')->success(['feedback' => sys_config('service_feedback')]);
  141. }
  142. /**
  143. * 确认登录
  144. * @param Request $request
  145. * @param StoreServiceServices $services
  146. * @param string $code
  147. * @return mixed
  148. */
  149. public function setLoginCode(Request $request, StoreServiceServices $services, string $code)
  150. {
  151. if (!$code) {
  152. return app('json')->fail(410020);
  153. }
  154. $cacheCode = CacheService::get($code);
  155. if ($cacheCode === false || $cacheCode === null) {
  156. return app('json')->fail(410021);
  157. }
  158. $userInfo = $services->get(['uid' => $request->uid()]);
  159. if (!$userInfo) {
  160. return app('json')->fail(410138);
  161. }
  162. $userInfo->uniqid = $code;
  163. $userInfo->save();
  164. CacheService::set($code, '0', 600);
  165. return app('json')->success(410001);
  166. }
  167. /**
  168. * 获取当前客服和用户的聊天记录
  169. * @return mixed
  170. * @throws \think\db\exception\DataNotFoundException
  171. * @throws \think\db\exception\DbException
  172. * @throws \think\db\exception\ModelNotFoundException
  173. */
  174. public function recordList(StoreServiceRecordServices $services, Request $request, string $nickname = '', $is_tourist = 0)
  175. {
  176. return app('json')->success($services->getServiceList((int)$request->uid(), $nickname, (int)$is_tourist));
  177. }
  178. }