StoreServiceRecordServices.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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\services\message\service;
  12. use app\dao\message\service\StoreServiceRecordDao;
  13. use app\services\BaseServices;
  14. use crmeb\traits\ServicesTrait;
  15. /**
  16. * Class StoreServiceRecordServices
  17. * @package app\services\message\service
  18. * @mixin StoreServiceRecordDao
  19. */
  20. class StoreServiceRecordServices extends BaseServices
  21. {
  22. use ServicesTrait;
  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'] = 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['_update_time'] = date('Y-m-d H:i', $item['update_time']);
  60. }
  61. return $list;
  62. }
  63. /**
  64. * 更新客服用户信息
  65. * @param int $uid
  66. * @param array $data
  67. * @return mixed
  68. */
  69. public function updateRecord(array $where, array $data)
  70. {
  71. return $this->dao->update($where, $data);
  72. }
  73. /**
  74. * 写入聊天相关人数据
  75. * @param int $uid
  76. * @param int $toUid
  77. * @param string $message
  78. * @param int $type
  79. * @param int $messageType
  80. * @param int $num
  81. * @return mixed
  82. * @throws \think\db\exception\DataNotFoundException
  83. * @throws \think\db\exception\DbException
  84. * @throws \think\db\exception\ModelNotFoundException
  85. */
  86. public function saveRecord(int $uid, int $toUid, string $message, int $type, int $messageType, int $num, int $isTourist = 0, string $nickname = '', string $avatar = '')
  87. {
  88. $info = $this->dao->get(['user_id' => $toUid, 'to_uid' => $uid]);
  89. if ($info) {
  90. $info->type = $type;
  91. $info->message = $message;
  92. $info->message_type = $messageType;
  93. $info->update_time = time();
  94. $info->mssage_num = $num;
  95. if ($avatar) $info->avatar = $avatar;
  96. if ($nickname) $info->nickname = $nickname;
  97. $info->save();
  98. $this->dao->update(['user_id' => $uid, 'to_uid' => $toUid], ['message' => $message, 'message_type' => $messageType]);
  99. return $info->toArray();
  100. } else {
  101. return $this->dao->save([
  102. 'user_id' => $toUid,
  103. 'to_uid' => $uid,
  104. 'type' => $type,
  105. 'message' => $message,
  106. 'avatar' => $avatar,
  107. 'nickname' => $nickname,
  108. 'message_type' => $messageType,
  109. 'mssage_num' => $num,
  110. 'add_time' => time(),
  111. 'update_time' => time(),
  112. 'is_tourist' => $isTourist
  113. ])->toArray();
  114. }
  115. }
  116. }