StoreServiceLogServices.php 8.6 KB

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