StoreService.php 6.7 KB

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