InstantMessage.php 8.6 KB

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