You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StoreServiceLogServices.php 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace app\services\message\service;
  3. use app\dao\message\service\StoreServiceLogDao;
  4. use app\services\BaseServices;
  5. use app\services\order\StoreOrderRefundServices;
  6. use app\services\order\StoreOrderServices;
  7. use app\services\product\product\StoreProductServices;
  8. use Monolog\Formatter\LogglyFormatter;
  9. use think\facade\Log;
  10. /**
  11. * 客服聊天记录
  12. * Class StoreServiceLogServices
  13. * @package app\services\message\service
  14. * @method whereByCount(array $where) 根据条件获取条数
  15. * @method getServiceList(array $where, int $page, int $limit, array $field = ['*']) 获取聊天记录并分页
  16. * @method saveAll(array $data) 插入数据
  17. * @method getMessageNum(array $where) 获取聊天记录条数
  18. */
  19. class StoreServiceLogServices extends BaseServices
  20. {
  21. /**
  22. * 消息类型
  23. * @var array 1=文字 2=表情 3=图片 4=语音 5 = 商品链接 6 = 订单类型
  24. */
  25. const MSN_TYPE = [1, 2, 3, 4, 5, 6, 7];
  26. /**
  27. * 商品链接消息类型
  28. */
  29. const MSN_TYPE_GOODS = 5;
  30. /**
  31. * 订单信息消息类型
  32. */
  33. const MSN_TYPE_ORDER = 6;
  34. /**
  35. * 退款订单消息类型
  36. */
  37. const MSN_TYPE_REFUND_ORDER = 7;
  38. /**
  39. * 构造方法
  40. * StoreServiceLogServices constructor.
  41. * @param StoreServiceLogDao $dao
  42. */
  43. public function __construct(StoreServiceLogDao $dao)
  44. {
  45. $this->dao = $dao;
  46. }
  47. /**
  48. * 获取聊天记录中的uid和to_uid
  49. * @param int $uid
  50. * @return array
  51. */
  52. public function getChatUserIds(int $uid)
  53. {
  54. $list = $this->dao->getServiceUserUids($uid);
  55. $arr_user = $arr_to_user = [];
  56. foreach ($list as $key => $value) {
  57. array_push($arr_user, $value["uid"]);
  58. array_push($arr_to_user, $value["to_uid"]);
  59. }
  60. $uids = array_merge($arr_user, $arr_to_user);
  61. $uids = array_flip(array_flip($uids));
  62. $uids = array_flip($uids);
  63. unset($uids[$uid]);
  64. return array_flip($uids);
  65. }
  66. /**
  67. * 获取某个用户的客服聊天记录
  68. * @param array $where
  69. * @return array
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\DbException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. */
  74. public function getChatLogList(array $where)
  75. {
  76. [$page, $limit] = $this->getPageValue();
  77. $list = $this->dao->getServiceList($where, $page, $limit);
  78. $count = $this->dao->count($where);
  79. return compact('list', 'count');
  80. }
  81. /**
  82. * 获取聊天记录列表
  83. * @param array $where
  84. * @param int $uid
  85. * @return array
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\DbException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. */
  90. public function getChatList(array $where, int $uid)
  91. {
  92. [$page, $limit] = $this->getPageValue();
  93. $list = $this->dao->getServiceList($where, $page, $limit);
  94. return $this->tidyChat($list);
  95. }
  96. /**
  97. * 聊天列表格式化
  98. * @param array $list
  99. * @param int $uid
  100. * @return array
  101. */
  102. public function tidyChat(array $list)
  103. {
  104. $productIds = $orderIds = $productList = $orderInfo = $toUser = $user = $orderIds_refund = [];
  105. $toUid = $list[0]['to_uid'] ?? 0;
  106. $uid = $list[0]['uid'] ?? 0;
  107. foreach ($list as &$item) {
  108. $item['_add_time'] = $item['add_time'];
  109. $item['add_time'] = strtotime($item['_add_time']);
  110. $item['productInfo'] = $item['orderInfo'] = [];
  111. if ($item['msn_type'] == self::MSN_TYPE_GOODS && $item['msn']) {
  112. $productIds[] = $item['msn'];
  113. } elseif ($item['msn_type'] == self::MSN_TYPE_ORDER && $item['msn']) {
  114. $orderIds[] = $item['msn'];
  115. } elseif ($item['msn_type'] == self::MSN_TYPE_REFUND_ORDER && $item['msn']) {
  116. $orderIds_refund[] = $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'));
  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. /** @var StoreOrderRefundServices $orderRefundService */
  139. $orderRefundService = app()->make(StoreOrderRefundServices::class);
  140. if ($orderIds_refund) {
  141. $orderWhere = [
  142. ['order_id', 'in', $orderIds_refund],
  143. ['is_del', '=', 0],
  144. ];
  145. $orderInfo_refund = $orderRefundService->getColumn($orderWhere, '*', 'order_id');
  146. }
  147. if ($toUid && $uid) {
  148. /** @var StoreServiceRecordServices $recordServices */
  149. $recordServices = app()->make(StoreServiceRecordServices::class);
  150. $toUser = $recordServices->get(['user_id' => $uid, 'to_uid' => $toUid], ['nickname', 'avatar']);
  151. $user = $recordServices->get(['user_id' => $toUid, 'to_uid' => $uid], ['nickname', 'avatar']);
  152. }
  153. foreach ($list as &$item) {
  154. if ($item['msn_type'] == self::MSN_TYPE_GOODS && $item['msn']) {
  155. $item['productInfo'] = $productList[$item['msn']] ?? [];
  156. } elseif ($item['msn_type'] == self::MSN_TYPE_ORDER && $item['msn']) {
  157. $order = $orderInfo[$item['msn']] ?? null;
  158. if ($order) {
  159. $order = $orderServices->tidyOrder($order, true, true);
  160. $order['add_time_y'] = date('Y-m-d', $order['add_time']);
  161. $order['add_time_h'] = date('H:i:s', $order['add_time']);
  162. $item['orderInfo'] = $order;
  163. } else {
  164. $item['orderInfo'] = [];
  165. }
  166. } elseif ($item['msn_type'] == self::MSN_TYPE_REFUND_ORDER && $item['msn']) {
  167. $order_refund = $orderInfo_refund[$item['msn']] ?? null;
  168. if ($order_refund) {
  169. $order_refund['cartInfo'] = json_decode($order_refund['cart_info'], true);
  170. $order_refund['add_time_y'] = date('Y-m-d', $order_refund['add_time']);
  171. $order_refund['add_time_h'] = date('H:i:s', $order_refund['add_time']);
  172. $order_refund['total_num'] = $order_refund['refund_num'];
  173. $order_refund['total_price'] = $order_refund['refund_price'];
  174. $order_refund['pay_price'] = $order_refund['refund_price'];
  175. $item['orderInfo'] = $order_refund;
  176. } else {
  177. $item['orderInfo'] = [];
  178. }
  179. }
  180. $item['msn_type'] = (int)$item['msn_type'];
  181. if (!isset($item['nickname'])) {
  182. $item['nickname'] = '';
  183. }
  184. if (!isset($item['avatar'])) {
  185. $item['avatar'] = '';
  186. }
  187. if (!$item['avatar'] && !$item['nickname']) {
  188. if ($item['uid'] == $uid && $item['to_uid'] == $toUid) {
  189. $item['nickname'] = $user['nickname'] ?? '';
  190. $item['avatar'] = $user['avatar'] ?? '';
  191. }
  192. if ($item['uid'] == $toUid && $item['to_uid'] == $uid) {
  193. $item['nickname'] = $toUser['nickname'] ?? '';
  194. $item['avatar'] = $toUser['avatar'] ?? '';
  195. }
  196. }
  197. }
  198. return $list;
  199. }
  200. /**
  201. * 获取聊天记录
  202. * @param array $where
  203. * @param int $page
  204. * @param int $limit
  205. * @param bool $isUp
  206. * @return array
  207. * @throws \think\db\exception\DataNotFoundException
  208. * @throws \think\db\exception\DbException
  209. * @throws \think\db\exception\ModelNotFoundException
  210. */
  211. public function getServiceChatList(array $where, int $limit, int $upperId)
  212. {
  213. return $this->dao->getChatList($where, $limit, $upperId);
  214. }
  215. }