StoreServiceRecordServices.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\kefu\service;
  12. use app\dao\service\StoreServiceRecordDao;
  13. use app\services\BaseServices;
  14. use crmeb\utils\Str;
  15. use think\Model;
  16. /**
  17. * Class StoreServiceRecordServices
  18. * @package app\services\kefu\service
  19. * @method array|Model|null getLatelyMsgUid(array $where, string $key) 查询最近和用户聊天的uid用户
  20. */
  21. class StoreServiceRecordServices extends BaseServices
  22. {
  23. /**
  24. * StoreServiceRecordServices constructor.
  25. * @param StoreServiceRecordDao $dao
  26. */
  27. public function __construct(StoreServiceRecordDao $dao)
  28. {
  29. $this->dao = $dao;
  30. }
  31. /**
  32. * 获取客服用户聊天列表
  33. * @param int $userId
  34. * @return array
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\DbException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. */
  39. public function getServiceList(int $userId, string $nickname, int $isTourist = 0)
  40. {
  41. [$page, $limit] = $this->getPageValue();
  42. $list = $this->dao->getServiceList(['user_id' => $userId, 'title' => $nickname, 'is_tourist' => $isTourist], $page, $limit, ['user', 'service']);
  43. foreach ($list as &$item) {
  44. if ($item['message_type'] == 1) {
  45. $item['message'] = Str::substrUTf8($item['message'], '10', 'UTF-8', '');
  46. }
  47. if (isset($item['kefu_nickname']) && $item['kefu_nickname']) {
  48. $item['nickname'] = $item['kefu_nickname'];
  49. }
  50. if (isset($item['wx_nickname']) && $item['wx_nickname'] && !$item['nickname']) {
  51. $item['nickname'] = $item['wx_nickname'];
  52. }
  53. if (isset($item['kefu_avatar']) && $item['kefu_avatar']) {
  54. $item['avatar'] = $item['kefu_avatar'];
  55. }
  56. if (isset($item['wx_avatar']) && $item['wx_avatar'] && !$item['avatar']) {
  57. $item['avatar'] = $item['wx_avatar'];
  58. }
  59. $item['avatar'] = set_file_url($item['avatar']);
  60. $item['kefu_avatar'] = set_file_url($item['kefu_avatar']);
  61. $item['wx_avatar'] = set_file_url($item['wx_avatar']);
  62. $item['_update_time'] = date('Y-m-d H:i', $item['update_time']);
  63. }
  64. return $list;
  65. }
  66. /**
  67. * 更新客服用户信息
  68. * @param int $uid
  69. * @param array $data
  70. * @return mixed
  71. */
  72. public function updateRecord(array $where, array $data)
  73. {
  74. return $this->dao->update($where, $data);
  75. }
  76. /**
  77. * 写入聊天相关人数据
  78. * @param int $uid
  79. * @param int $toUid
  80. * @param string $message
  81. * @param int $type
  82. * @param int $messageType
  83. * @param int $num
  84. * @return mixed
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\DbException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. */
  89. public function saveRecord(int $uid, int $toUid, string $message, int $type, int $messageType, int $num, int $isTourist = 0, string $nickname = '', string $avatar = '')
  90. {
  91. $info = $this->dao->get(['user_id' => $toUid, 'to_uid' => $uid]);
  92. if ($info) {
  93. $info->type = $type;
  94. $info->message = $message;
  95. $info->message_type = $messageType;
  96. $info->update_time = time();
  97. $info->mssage_num = $num;
  98. if ($avatar) $info->avatar = $avatar;
  99. if ($nickname) $info->nickname = $nickname;
  100. $info->save();
  101. $this->dao->update(['user_id' => $uid, 'to_uid' => $toUid], ['message' => $message, 'message_type' => $messageType]);
  102. return $info->toArray();
  103. } else {
  104. return $this->dao->save([
  105. 'user_id' => $toUid,
  106. 'to_uid' => $uid,
  107. 'type' => $type,
  108. 'message' => $message,
  109. 'avatar' => $avatar,
  110. 'nickname' => $nickname,
  111. 'message_type' => $messageType,
  112. 'mssage_num' => $num,
  113. 'add_time' => time(),
  114. 'update_time' => time(),
  115. 'is_tourist' => $isTourist
  116. ])->toArray();
  117. }
  118. }
  119. }