ChatLogic.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\shopapi\logic;
  20. use app\shopapi\lists\ChatRecordLists;
  21. use app\common\{enum\ChatGoodsEnum,
  22. logic\ChatLogic as CommonChatlogic,
  23. model\ChatRelation,
  24. model\Goods,
  25. model\Kefu,
  26. logic\BaseLogic};
  27. /**
  28. * 用户聊天
  29. * Class ChatLogic
  30. * @package app\shopapi\logic
  31. */
  32. class ChatLogic extends BaseLogic
  33. {
  34. /**
  35. * @notes 获取与客服聊天记录
  36. * @param $userId
  37. * @return array
  38. * @author 段誉
  39. * @date 2022/3/14 14:47
  40. */
  41. public static function getChatRecord($userId): array
  42. {
  43. // 聊天记录
  44. $records = (new ChatRecordLists());
  45. $records = [
  46. 'lists' => $records->lists(),
  47. 'count' => $records->count(),
  48. 'page_no' => $records->pageNo,
  49. 'page_size' => $records->pageSize,
  50. 'more' => is_more($records->count(), $records->pageNo, $records->pageSize)
  51. ];
  52. // 当前在线的所有客服
  53. $online = CommonChatlogic::getOnlineKefu();
  54. // 后台在线客服状态 0-关闭 1-开启
  55. $config = CommonChatlogic::getConfigSetting();
  56. // 后台配置为 人工客服
  57. if ($config != 1) {
  58. return [ 'config' => $config, 'kefu' => [], 'record' => $records ];
  59. }
  60. // 上一个客服关系
  61. $kefu = ChatRelation::where('user_id', $userId)->order('update_time desc')->findOrEmpty();
  62. $kefuId = $kefu['kefu_id'] ?? 0;
  63. // 没有聊天记录(未与客服聊天) 或者 曾经的聊天客服不在线
  64. if (empty($kefu) || ! in_array($kefuId, $online)) {
  65. // 随机分配客服
  66. $randKufuIds = $online ? : Kefu::where('disable', 0)->column('id');
  67. $rand = rand(0, count($randKufuIds) - 1);
  68. $kefuId = $randKufuIds[$rand];
  69. }
  70. $kefu = Kefu::where('id', $kefuId)->field([ 'id', 'nickname', 'avatar' ])->findOrEmpty();
  71. return [
  72. 'config' => $config,
  73. 'kefu' => $kefu,
  74. 'record' => $records
  75. ];
  76. }
  77. }