StoreServiceLogServices.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\StoreServiceLogDao;
  13. use app\services\BaseServices;
  14. use app\services\order\StoreOrderServices;
  15. use app\services\product\product\StoreProductServices;
  16. /**
  17. * 客服聊天记录
  18. * Class StoreServiceLogServices
  19. * @package app\services\kefu\service
  20. * @method whereByCount(array $where) 根据条件获取条数
  21. * @method getServiceList(array $where, int $page, int $limit, array $field = ['*']) 获取聊天记录并分页
  22. * @method saveAll(array $data) 插入数据
  23. * @method getMessageNum(array $where) 获取聊天记录条数
  24. */
  25. class StoreServiceLogServices extends BaseServices
  26. {
  27. /**
  28. * 消息类型
  29. * @var array 1=文字 2=表情 3=图片 4=语音 5 = 商品链接 6 = 订单类型
  30. */
  31. const MSN_TYPE = [1, 2, 3, 4, 5, 6];
  32. /**
  33. * 商品链接消息类型
  34. */
  35. const MSN_TYPE_GOODS = 5;
  36. /**
  37. * 订单信息消息类型
  38. */
  39. const MSN_TYPE_ORDER = 6;
  40. /**
  41. * 构造方法
  42. * StoreServiceLogServices constructor.
  43. * @param StoreServiceLogDao $dao
  44. */
  45. public function __construct(StoreServiceLogDao $dao)
  46. {
  47. $this->dao = $dao;
  48. }
  49. /**
  50. * 获取聊天记录中的uid和to_uid
  51. * @param int $uid
  52. * @return array
  53. */
  54. public function getChatUserIds(int $uid)
  55. {
  56. $list = $this->dao->getServiceUserUids($uid);
  57. $arr_user = $arr_to_user = [];
  58. foreach ($list as $key => $value) {
  59. array_push($arr_user, $value["uid"]);
  60. array_push($arr_to_user, $value["to_uid"]);
  61. }
  62. $uids = array_merge($arr_user, $arr_to_user);
  63. $uids = array_flip(array_flip($uids));
  64. $uids = array_flip($uids);
  65. unset($uids[$uid]);
  66. return array_flip($uids);
  67. }
  68. /**
  69. * 获取某个用户的客服聊天记录
  70. * @param array $where
  71. * @return array
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\DbException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. */
  76. public function getChatLogList(array $where)
  77. {
  78. [$page, $limit] = $this->getPageValue();
  79. $list = $this->dao->getServiceList($where, $page, $limit);
  80. $count = $this->dao->count($where);
  81. return compact('list', 'count');
  82. }
  83. /**
  84. * 获取聊天记录列表
  85. * @param array $where
  86. * @param int $uid
  87. * @return array
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\DbException
  90. * @throws \think\db\exception\ModelNotFoundException
  91. */
  92. public function getChatList(array $where, int $uid)
  93. {
  94. [$page, $limit] = $this->getPageValue();
  95. $list = $this->dao->getServiceList($where, $page, $limit);
  96. return $this->tidyChat($list);
  97. }
  98. /**
  99. * 聊天列表格式化
  100. * @param array $list
  101. * @param int $uid
  102. * @return array
  103. */
  104. public function tidyChat(array $list)
  105. {
  106. $productIds = $orderIds = $productList = $orderInfo = $toUser = $user = [];
  107. $toUid = $list[0]['to_uid'] ?? 0;
  108. $uid = $list[0]['uid'] ?? 0;
  109. foreach ($list as &$item) {
  110. $item['_add_time'] = $item['add_time'];
  111. $item['add_time'] = strtotime($item['_add_time']);
  112. $item['productInfo'] = $item['orderInfo'] = null;
  113. if ($item['msn_type'] == self::MSN_TYPE_GOODS && $item['msn']) {
  114. $productIds[] = $item['msn'];
  115. } elseif ($item['msn_type'] == self::MSN_TYPE_ORDER && $item['msn']) {
  116. $orderIds[] = $item['msn'];
  117. }
  118. }
  119. if ($productIds) {
  120. /** @var StoreProductServices $productServices */
  121. $productServices = app()->make(StoreProductServices::class);
  122. $where = [
  123. ['id', 'in', $productIds],
  124. ['is_del', '=', 0],
  125. ['is_show', '=', 1],
  126. ];
  127. $productList = get_thumb_water($productServices->getProductArray($where, '*', 'id'),'mid');
  128. }
  129. /** @var StoreOrderServices $orderServices */
  130. $orderServices = app()->make(StoreOrderServices::class);
  131. if ($orderIds) {
  132. $orderWhere = [
  133. ['order_id|unique', 'in', $orderIds],
  134. ['is_del', '=', 0],
  135. ];
  136. $orderInfo = $orderServices->getColumn($orderWhere, '*', 'order_id');
  137. }
  138. if ($toUid && $uid) {
  139. /** @var StoreServiceRecordServices $recordServices */
  140. $recordServices = app()->make(StoreServiceRecordServices::class);
  141. $toUser = $recordServices->get(['user_id' => $uid, 'to_uid' => $toUid], ['nickname', 'avatar']);
  142. $user = $recordServices->get(['user_id' => $toUid, 'to_uid' => $uid], ['nickname', 'avatar']);
  143. }
  144. foreach ($list as &$item) {
  145. if ($item['msn_type'] == self::MSN_TYPE_GOODS && $item['msn']) {
  146. $item['productInfo'] = $productList[$item['msn']] ?? null;
  147. } elseif ($item['msn_type'] == self::MSN_TYPE_ORDER && $item['msn']) {
  148. $order = $orderInfo[$item['msn']] ?? null;
  149. if ($order) {
  150. $order = $orderServices->tidyOrder($order, true, true);
  151. $order['add_time_y'] = date('Y-m-d', $order['add_time']);
  152. $order['add_time_h'] = date('H:i:s', $order['add_time']);
  153. $item['orderInfo'] = $order;
  154. } else {
  155. $item['orderInfo'] = null;
  156. }
  157. }
  158. $item['msn_type'] = (int)$item['msn_type'];
  159. if (!isset($item['nickname'])) {
  160. $item['nickname'] = '';
  161. }
  162. if (!isset($item['avatar'])) {
  163. $item['avatar'] = '';
  164. }
  165. if (!$item['avatar'] && !$item['nickname']) {
  166. if ($item['uid'] == $uid && $item['to_uid'] == $toUid) {
  167. $item['nickname'] = $user['nickname'] ?? '';
  168. $item['avatar'] = $user['avatar'] ?? '';
  169. }
  170. if ($item['uid'] == $toUid && $item['to_uid'] == $uid) {
  171. $item['nickname'] = $toUser['nickname'] ?? '';
  172. $item['avatar'] = $toUser['avatar'] ?? '';
  173. }
  174. }
  175. }
  176. return $list;
  177. }
  178. /**
  179. * 获取聊天记录
  180. * @param array $where
  181. * @param int $page
  182. * @param int $limit
  183. * @param bool $isUp
  184. * @return array
  185. * @throws \think\db\exception\DataNotFoundException
  186. * @throws \think\db\exception\DbException
  187. * @throws \think\db\exception\ModelNotFoundException
  188. */
  189. public function getServiceChatList(array $where, int $limit, int $upperId)
  190. {
  191. return $this->dao->getChatList($where, $limit, $upperId);
  192. }
  193. }