InstantMessage.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace app\common\model;
  3. use GatewayClient\Gateway;
  4. use think\facade\Db;
  5. /**
  6. * DSKMS多用户商城
  7. *
  8. * ----------------------------------------------------------------------------
  9. *
  10. * 数据层模型
  11. */
  12. class InstantMessage extends BaseModel
  13. {
  14. public $page_info;
  15. /**
  16. * 获取服务机构通知列表
  17. * @access public
  18. * @author csdeshang
  19. * @param type $condition
  20. * @param type $pagesize
  21. * @param type $order
  22. * @return type
  23. */
  24. public function getInstantMessageList($condition, $pagesize = '', $order = 'instant_message_id desc')
  25. {
  26. if ($pagesize) {
  27. $result = Db::name('InstantMessage')->where($condition)->order($order)->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
  28. $this->page_info = $result;
  29. return $result->items();
  30. } else {
  31. return Db::name('InstantMessage')->where($condition)->order($order)->select()->toArray();
  32. }
  33. }
  34. /**
  35. * 取得服务机构通知信息
  36. * @access public
  37. * @author csdeshang
  38. * @param array $condition 检索条件
  39. * @param string $fields 字段
  40. * @param string $order 排序
  41. * @return array
  42. */
  43. public function getInstantMessageInfo($condition = array(), $fields = '*')
  44. {
  45. return Db::name('InstantMessage')->where($condition)->field($fields)->order('instant_message_id desc')->find();
  46. }
  47. /**
  48. * 添加服务机构通知信息
  49. * @access public
  50. * @author csdeshang
  51. * @param array $data 参数数据
  52. * @return type
  53. */
  54. public function addInstantMessage($data)
  55. {
  56. switch ($data['instant_message_to_type']) {
  57. case 0:
  58. $member_model = model('member');
  59. $member = $member_model->getMemberInfo(array('member_id' => $data['instant_message_to_id'], 'member_state' => 1));
  60. if (!$member) {
  61. throw new \think\Exception(lang('user_not_exist'), 10006);
  62. }
  63. if ($data['instant_message_from_type'] == 0 && $member['member_id'] == $data['instant_message_from_id']) {
  64. throw new \think\Exception(lang('chat_self_error'), 10006);
  65. }
  66. $to_name = $member['member_name'];
  67. break;
  68. case 1:
  69. $store_model = model('store');
  70. $store = $store_model->getStoreOnlineInfoByID($data['instant_message_to_id']);
  71. if (!$store) {
  72. throw new \think\Exception(lang('store_not_exist'), 10006);
  73. }
  74. if ($data['instant_message_from_type'] == 0 && $store['member_id'] == $data['instant_message_from_id']) {
  75. throw new \think\Exception(lang('chat_self_error'), 10006);
  76. }
  77. if ($data['instant_message_from_type'] == 1 && $store['store_id'] == $data['instant_message_from_id']) {
  78. throw new \think\Exception(lang('chat_self_error'), 10006);
  79. }
  80. $to_name = $store['store_name'];
  81. break;
  82. case 2:
  83. $live_apply_model = model('live_apply');
  84. $live_apply = $live_apply_model->getLiveApplyInfo(array(array('live_apply_id', '=', $data['instant_message_to_id']), array('live_apply_state', '=', 1), array('live_apply_end_time', '>', TIMESTAMP)));
  85. if (!$live_apply) {
  86. throw new \think\Exception(lang('live_not_exit'), 10006);
  87. }
  88. $to_name = $live_apply['live_apply_id'] . lang('live_room');
  89. break;
  90. default:
  91. throw new \think\Exception(lang('param_error'), 10006);
  92. }
  93. $data['instant_message_to_name'] = $to_name;
  94. switch ($data['instant_message_type']) {
  95. case 1:
  96. $goods_id = $data['instant_message'];
  97. if (!$goods_id) {
  98. throw new \think\Exception(lang('param_error'), 10006);
  99. }
  100. $goods_model = model('goods');
  101. $goods = $goods_model->getGoodsInfoByID($goods_id);
  102. if (is_array($goods) && !empty($goods)) {
  103. $data['instant_message'] = json_encode(array(
  104. 'goods_id' => $goods['goods_id'],
  105. 'goods_name' => $goods['goods_name'],
  106. 'goods_price' => $goods['goods_price'],
  107. 'goods_image' => $goods['goods_image'],
  108. ));
  109. } else {
  110. throw new \think\Exception(lang('goods_not_exit'), 10006);
  111. }
  112. break;
  113. }
  114. $instant_message_id = Db::name('InstantMessage')->insertGetId($data);
  115. if (!$instant_message_id) {
  116. throw new \think\Exception(lang('send_fail'), 10006);
  117. }
  118. $data['instant_message_id'] = $instant_message_id;
  119. $data = $this->formatInstantMessage($data);
  120. return $data;
  121. }
  122. public function formatInstantMessage($data)
  123. {
  124. if ($data['instant_message_type'] == 1) {
  125. $data['instant_message'] = json_decode($data['instant_message'], true);
  126. $data['instant_message']['goods_image_url'] = goods_cthumb($data['instant_message']['goods_image']);
  127. }
  128. $member_model = model('member');
  129. $store_model = model('store');
  130. if ($data['instant_message_to_type'] == 0) {
  131. $member = $member_model->getMemberInfo(array('member_id' => $data['instant_message_to_id'], 'member_state' => 1));
  132. $data['instant_message_to_avatar'] = get_member_avatar($member ? $member['member_avatar'] : '');
  133. $data['instant_message_to_info'] = $member;
  134. } elseif ($data['instant_message_to_type'] == 1) {
  135. $store = $store_model->getStoreOnlineInfoByID($data['instant_message_to_id']);
  136. $data['instant_message_to_avatar'] = get_store_logo($store ? $store['store_avatar'] : '');
  137. $data['instant_message_to_info'] = $store;
  138. }
  139. if ($data['instant_message_from_type'] == 0) {
  140. $member = $member_model->getMemberInfo(array('member_id' => $data['instant_message_from_id'], 'member_state' => 1));
  141. $data['instant_message_from_avatar'] = get_member_avatar($member ? $member['member_avatar'] : '');
  142. $data['instant_message_from_info'] = $member;
  143. } elseif ($data['instant_message_from_type'] == 1) {
  144. $store = $store_model->getStoreOnlineInfoByID($data['instant_message_from_id']);
  145. $data['instant_message_from_avatar'] = get_store_logo($store ? $store['store_avatar'] : '');
  146. $data['instant_message_from_info'] = $store;
  147. }
  148. return $data;
  149. }
  150. /**
  151. * 编辑服务机构通知信息
  152. * @access public
  153. * @author csdeshang
  154. * @param array $data 更新数据
  155. * @param array $condition 条件
  156. * @return bool
  157. */
  158. public function editInstantMessage($data, $condition = array())
  159. {
  160. return Db::name('InstantMessage')->where($condition)->update($data);
  161. }
  162. /**
  163. * 获取服务机构通知数量
  164. * @access public
  165. * @author csdeshang
  166. * @param array $condition 条件
  167. * @return bool
  168. */
  169. public function getInstantMessageCount($condition = array())
  170. {
  171. return Db::name('InstantMessage')->where($condition)->count();
  172. }
  173. public function sendInstantMessage($instant_message, $auto = false)
  174. {
  175. if (!config('ds_config.instant_message_register_url')) {
  176. return ds_callback(false, '未设置直播聊天gateway地址');
  177. }
  178. // 设置GatewayWorker服务的Register服务ip和端口,请根据实际情况改成实际值(ip不能是0.0.0.0)
  179. try {
  180. Gateway::$registerAddress = config('ds_config.instant_message_register_url');
  181. if ($instant_message['instant_message_to_type'] == 2) {
  182. Gateway::sendToGroup('live_apply_' . $instant_message['instant_message_to_id'], json_encode($instant_message));
  183. } elseif ($instant_message['instant_message_to_type'] == 0) {
  184. Gateway::sendToUid('0:' . $instant_message['instant_message_to_id'], json_encode($instant_message));
  185. }
  186. } catch (\Exception $e) {
  187. return ds_callback(false, $e->getMessage());
  188. }
  189. return ds_callback(true);
  190. }
  191. }