ChatLogic.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop开源商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | gitee下载:https://gitee.com/likeshop_gitee
  7. // | github下载:https://github.com/likeshop-github
  8. // | 访问官网:https://www.likeshop.cn
  9. // | 访问社区:https://home.likeshop.cn
  10. // | 访问手册:http://doc.likeshop.cn
  11. // | 微信公众号:likeshop技术社区
  12. // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
  13. // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
  14. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  15. // | likeshop团队版权所有并拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshop.cn.team
  18. // +----------------------------------------------------------------------
  19. namespace app\kefuapi\logic;
  20. use app\common\enum\UserTerminalEnum;
  21. use app\common\logic\{BaseLogic, ChatLogic as CommonChatLogic};
  22. use app\common\service\ConfigService;
  23. use app\common\service\FileService;
  24. use app\common\model\{Order, User, Kefu, UserLevel};
  25. /**
  26. * 客服逻辑
  27. * Class ChatLogic
  28. * @package app\kefuapi\logic
  29. */
  30. class ChatLogic extends BaseLogic
  31. {
  32. /**
  33. * @notes 获取在线客服
  34. * @param int $kefu_id
  35. * @return array
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. * @author 段誉
  40. * @date 2022/3/14 12:16
  41. */
  42. public static function getOnlineKefu(int $kefu_id): array
  43. {
  44. $online = CommonChatLogic::getOnlineKefu();
  45. if (empty($online)) {
  46. return [];
  47. }
  48. $lists = Kefu::field(['id', 'nickname', 'avatar'])
  49. ->where([
  50. ['id', 'in', $online],
  51. ['id', '<>', $kefu_id],
  52. ])
  53. ->select()
  54. ->toArray();
  55. return $lists;
  56. }
  57. /**
  58. * @notes 获取用户信息
  59. * @param int $user_id
  60. * @return array|false
  61. * @author 段誉
  62. * @date 2022/3/14 12:15
  63. */
  64. public static function getUserInfo(int $user_id)
  65. {
  66. try {
  67. $user = User::where(['id' => $user_id])
  68. ->field([
  69. 'id', 'sn', 'nickname', 'avatar',
  70. 'level', 'mobile', 'total_order_amount',
  71. 'birthday', 'register_source', 'create_time'
  72. ])
  73. ->findOrEmpty()
  74. ->toArray();
  75. if (empty($user)) {
  76. throw new \Exception('用户不存在');
  77. }
  78. $user['level_name'] = UserLevel::where('id', $user['level'])->value('name', '');
  79. $user['birthday'] = empty($user['birthday']) ? '-' : $user['birthday'];
  80. $user['mobile'] = empty($user['mobile']) ? '-' : substr_replace($user['mobile'], '****', 3, 4);
  81. $user['register_source'] = UserTerminalEnum::getTermInalDesc($user['register_source']);
  82. return $user;
  83. } catch (\Exception $e) {
  84. self::$error = $e->getMessage();
  85. return false;
  86. }
  87. }
  88. /**
  89. * @notes 获取客服信息
  90. * @param $id
  91. * @return array
  92. * @author 段誉
  93. * @date 2022/3/14 12:15
  94. */
  95. public static function getKefuInfo(int $id): array
  96. {
  97. $result = Kefu::where(['id' => $id])
  98. ->field(['id', 'nickname', 'avatar'])
  99. ->findOrEmpty()
  100. ->toArray();
  101. $online = CommonChatLogic::getOnlineKefu();
  102. $result['online'] = 0;
  103. if (in_array($result['id'], $online)) {
  104. $result['online'] = 1;
  105. }
  106. return $result;
  107. }
  108. /**
  109. * @notes 上传文件域名
  110. * @return array
  111. * @author 段誉
  112. * @date 2022/3/14 12:13
  113. */
  114. public static function getConfig(): array
  115. {
  116. return [
  117. 'copyright' => ConfigService::get('shop', 'copyright', ''),
  118. 'favicon' => FileService::getFileUrl(ConfigService::get('shop', 'favicon')),
  119. 'base_domain' => FileService::getFileUrl(),
  120. 'ws_domain' => env('project.ws_domain', 'ws:127.0.0.1')
  121. ];
  122. }
  123. }