Dialogue.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\servicer\model;
  11. use app\model\BaseModel;
  12. use think\db\exception\DataNotFoundException;
  13. use think\db\exception\DbException;
  14. use think\db\exception\ModelNotFoundException;
  15. use think\Model;
  16. class Dialogue extends BaseModel
  17. {
  18. /**
  19. * 消息类型,字符串
  20. */
  21. const CONTENTTYPE_STRING = 0;
  22. /**
  23. * 消息类型,商品SKU
  24. */
  25. const CONTENTTYPE_GOODSKU = 1;
  26. /**
  27. * 消息类型,订单
  28. */
  29. const CONTENTTYPE_ORDER = 2;
  30. /**
  31. * 消息类型,图片
  32. */
  33. const CONTENTTYPE_IMAGE = 3;
  34. /**
  35. * 状态
  36. * @param mixed $content_type
  37. * @param string $type
  38. * @return array
  39. */
  40. public static function contentType($content_type = null, $type = 'content_type')
  41. {
  42. $data = array(
  43. array(
  44. 'content_type' => self::CONTENTTYPE_STRING,
  45. 'name' => '字符串',
  46. 'type' => 'string',
  47. 'const' => 'CONTENTTYPE_STRING',
  48. ),
  49. array(
  50. 'content_type' => self::CONTENTTYPE_GOODSKU,
  51. 'name' => '商品',
  52. 'type' => 'goodssku',
  53. 'const' => 'CONTENTTYPE_GOODSKU',
  54. ),
  55. array(
  56. 'content_type' => self::CONTENTTYPE_ORDER,
  57. 'name' => '订单',
  58. 'type' => 'order',
  59. 'const' => 'CONTENTTYPE_ORDER',
  60. ),
  61. array(
  62. 'content_type' => self::CONTENTTYPE_IMAGE,
  63. 'name' => '图片',
  64. 'type' => 'image',
  65. 'const' => 'CONTENTTYPE_IMAGE',
  66. ),
  67. );
  68. $result = [];
  69. if (is_null($content_type)) {
  70. $result = $data;
  71. } else {
  72. if (is_array($content_type)) {
  73. foreach ($data as $val) {
  74. if (in_array($val[$type], $content_type)) {
  75. $result[] = $val;
  76. }
  77. }
  78. } else {
  79. foreach ($data as $val) {
  80. if ($content_type == $val[$type]) {
  81. $result = $val;
  82. break;
  83. }
  84. }
  85. }
  86. }
  87. return $result;
  88. }
  89. /**
  90. * 插入对话数据
  91. * @param integer $type 会话方向,0 客户,1 客服
  92. * @param integer $memberId 会员ID
  93. * @param integer $serviceId 客服ID
  94. * @param integer $contentType 内容类型
  95. * @param integer $read 是否已读
  96. * @param integer $shopId 商户ID
  97. * @param integer $userId 用户ID
  98. * @param string $consumerSay 客户咨询内容
  99. * @param string $servicerSay 客服回答内容
  100. * @param integer $goodSkuId 商品ID
  101. * @param integer $orderId 订单ID
  102. * @return integer
  103. */
  104. public function createDialogue($type, $memberId, $serviceId, $contentType, $read, $shopId, $userId, $consumerSay = '', $servicerSay = '', $goodSkuId = 0, $orderId = 0, $relate_data = '')
  105. {
  106. return model('servicer_dialogue')->add([
  107. 'member_id' => $memberId,
  108. 'servicer_id' => $serviceId,
  109. 'create_day' => date('Y-m-d'),
  110. 'create_time' => date('H:i:s'),
  111. 'add_time' => time(),
  112. 'content_type' => $contentType,
  113. 'read' => $read,
  114. 'shop_id' => $shopId,
  115. 'goods_sku_id' => $goodSkuId,
  116. 'order_id' => $orderId,
  117. 'consumer_say' => $consumerSay,
  118. 'servicer_say' => $servicerSay,
  119. 'type' => $type,
  120. 'relate_data' => $relate_data,
  121. 'content' => empty($servicerSay) ? $consumerSay : $servicerSay,
  122. 'message' => $this->handleMessage($consumerSay, $servicerSay, $contentType),
  123. ]);
  124. }
  125. /**
  126. * 处理纯文本消息
  127. * @param string $consumerSay
  128. * @param string $servicerSay
  129. * @param $contentType
  130. * @return mixed
  131. */
  132. private function handleMessage(string $consumerSay, string $servicerSay, $contentType = 0)
  133. {
  134. $message = '';
  135. if ($contentType == self::CONTENTTYPE_STRING) {
  136. if (!empty($servicerSay)) {
  137. $message = $servicerSay;
  138. }
  139. if (!empty($consumerSay)) {
  140. $message = $consumerSay;
  141. }
  142. $message = strip_tags($message);
  143. $message = str_replace(['&nbsp;', '\r\n', '\r', '\n'], '', $message);
  144. }
  145. return $message;
  146. }
  147. /**
  148. * 设置消息已读状态
  149. * @param $id
  150. * @param bool $read
  151. * @return int
  152. * @throws DbException
  153. */
  154. public function setDialogueRead($id, $read = true)
  155. {
  156. return model('servicer_dialogue')->setFieldValue(['id' => $id], 'read', $read ? 1 : 0);
  157. }
  158. /**
  159. * 获取会话详情
  160. * @param $id
  161. * @return array|Model|null
  162. * @throws DataNotFoundException
  163. * @throws DbException
  164. * @throws ModelNotFoundException
  165. */
  166. public function getDialogue($id)
  167. {
  168. return model('servicer_dialogue')->getInfo(['id' => $id]);
  169. }
  170. /**
  171. * 查询聊天记录
  172. * @param $memberId
  173. * @param int $page
  174. * @param int $limit
  175. * @param int $siteId
  176. * @param int $servicerId
  177. * @return mixed
  178. * @throws DataNotFoundException
  179. * @throws DbException
  180. * @throws ModelNotFoundException
  181. */
  182. public function getDialogueList($memberId, $page = 1, $limit = 20, $siteId = 0, $servicerId=0)
  183. {
  184. $alias = 'sd';
  185. $field = ['sd.id', 'sd.create_day', 'sd.type', 'sd.create_time', 'sd.content_type', 'sd.read', 'sd.goods_sku_id', 'sd.order_id', 'sd.consumer_say', 'sd.servicer_say'];
  186. $order = ['sd.id' => 'desc'];
  187. $join = [
  188. ['servicer s', 's.id = sd.servicer_id', 'LEFT'], // 关联客服
  189. ['goods_sku g', 'g.sku_id = sd.goods_sku_id', 'LEFT'], // 关联商品,聊天记录为商品时
  190. ['order o', 'o.order_id = sd.order_id', 'LEFT'],
  191. ['order_goods og', 'og.order_id = o.order_id', 'LEFT']
  192. ];
  193. $field = array_merge($field, ['s.is_platform']);
  194. $field = array_merge($field, ['g.goods_name', 'g.sku_image', 'g.price']);
  195. $field = array_merge($field, ['o.order_no', 'o.goods_money', 'o.order_money', 'o.order_status', 'o.order_status_name']);
  196. $field = array_merge($field, ['og.sku_name', 'og.sku_image']);
  197. $condition[] = ['sd.member_id', '=', $memberId];
  198. $condition[] = ['sd.shop_id', '=', $siteId];
  199. if(!empty($servicerId)){
  200. $condition[] = ['sd.servicer_id', '=', $servicerId];
  201. }
  202. return model('servicer_dialogue')->pageList($condition, $field, $order, $page, $limit, $alias, $join, null, $limit);
  203. }
  204. /**
  205. * 获取聊天列表相关信息-分页查询
  206. * @param array $map
  207. * @param bool $field
  208. * @param string $order
  209. * @param int $page
  210. * @param int $list_rows
  211. * @param string $alias
  212. * @param array $join
  213. * @param null $group
  214. * @param null $limit
  215. * @return mixed
  216. * @throws DataNotFoundException
  217. * @throws DbException
  218. * @throws ModelNotFoundException
  219. */
  220. public function getPageList($map = [], $field = true, $order = '', $page = 1, $list_rows = PAGE_LIST_ROWS, $alias = '', $join = [], $group = null, $limit = null)
  221. {
  222. return model('servicer_dialogue')->pageList($map, $field, $order, $page, $list_rows, $alias, $join, $group, $limit);
  223. }
  224. /**
  225. * 设置消息读取状态
  226. * @param $condition
  227. * @param bool $read
  228. * @return int
  229. */
  230. public function setDialoguesRead($condition, $read = true)
  231. {
  232. return model('servicer_dialogue')->setFieldValue($condition, 'read', $read ? 1 : 0);
  233. }
  234. /**
  235. * 获取信息
  236. * @param $condition
  237. * @param bool $field
  238. * @return array
  239. */
  240. public function getInfo($condition, $field = true)
  241. {
  242. $res = model('servicer_dialogue')->getInfo($condition, $field);
  243. $res = $this->handleData($res);
  244. return $this->success($res);
  245. }
  246. /**
  247. * 处理数据
  248. * @param $info
  249. * @return mixed
  250. */
  251. private function handleData($info)
  252. {
  253. if (!empty($info)) {
  254. if (isset($info['relate_data'])) {
  255. $info['relate_data_parse'] = !empty($info['relate_data']) ? json_decode($info['relate_data'], true) : [];
  256. }
  257. }
  258. return $info;
  259. }
  260. /**
  261. * 接入所有未读消息给客服
  262. * @param $siteId
  263. * @param $servicerId
  264. * @return int
  265. */
  266. public function joinDialogues($siteId, $servicerId)
  267. {
  268. $condition = [['shop_id', '=', $siteId], ['servicer_id', '=', 0], ['type', '=', 0]];
  269. return model('servicer_dialogue')->setFieldValue($condition, 'servicer_id', $servicerId);
  270. }
  271. }