Msg.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace app\index\controller\wxapp;
  3. use app\common\model\Msg AS Model;
  4. use app\common\model\Msguser AS MsguserModel;
  5. use app\common\controller\IndexBase;
  6. class Msg extends IndexBase{
  7. public function index(){
  8. return $this->ok_js();
  9. }
  10. /**
  11. * 更新用户列表及消息是否已读
  12. * @param number $uid 正数其它用户UID,负数,圈子ID
  13. * @param number $id 消息ID
  14. */
  15. public function update_user($uid=0,$id=0){
  16. if (empty($this->user)) {
  17. return $this->err_js('还没登录');
  18. }elseif($uid==$this->user['uid']){
  19. return $this->err_js('不能自己给自己更新');
  20. }elseif(empty($uid)){
  21. return $this->err_js('UID不存在');
  22. }
  23. MsguserModel::add($this->user['uid'],$uid);
  24. if($uid>0){ //私聊当中的状态
  25. $map = [
  26. 'id'=>$id,
  27. 'ifread'=>0,
  28. 'touid'=>$this->user['uid'],
  29. ];
  30. Model::where($map)->update([
  31. 'ifread'=>1,
  32. ]);
  33. }
  34. return $this->ok_js();
  35. }
  36. /**
  37. * 设置当前用户的群发消息组
  38. * @param number $uid 负数是圈子ID,正数是私聊时对方的UID
  39. * @param string $client_id WS生成的客户ID
  40. */
  41. public function bind_group($uid=0,$client_id=''){
  42. if (empty($uid)) {
  43. return $this->err_js('用户UID或者圈子ID不存在');
  44. }elseif (empty($client_id)) {
  45. return $this->err_js('客户ID不存在');
  46. }
  47. fun("Gatewayclient@user_join_group",$this->user['uid'],$uid,$client_id);
  48. return $this->ok_js();
  49. }
  50. /**
  51. * 获取某个人跟它人或者是圈子的会话记录
  52. * @param number $uid 正数用户UID,负数圈子ID
  53. * @param number $id 某条消息的ID
  54. * @param number $rows 取几条
  55. * @param number $maxid 消息中最新的那条记录ID
  56. * @param number $is_live 是否在视频直播
  57. * @param number $msg_id 归属主题 ID
  58. * @param number $msg_sys 归属主题的频道 ID
  59. * @return void|\think\response\Json|void|unknown|\think\response\Json
  60. */
  61. public function get_more($uid=0,$id=0,$rows=5,$maxid=0,$is_live=0,$msg_id=0,$msg_sys=0,$my_uid=0){
  62. if ($my_uid) {
  63. if ( (empty($this->user) && $my_uid<9999999) || ($this->user&&$this->user['uid']!=$my_uid) ) {
  64. return $this->err_js("my_uid数值有误");
  65. }
  66. }else{
  67. $my_uid = $this->user['uid'];
  68. }
  69. if (($uid>=0 || $id>0) && empty($this->user) && empty($my_uid)) {
  70. return $this->err_js("请先登录");
  71. }
  72. $qun_user = $qun_info = '';
  73. if ($uid<0) {
  74. if(!modules_config('qun')){
  75. return $this->err_js('你没有安装圈子模块!');
  76. }
  77. if($maxid<1){ //首次加载
  78. $qun_info = \app\qun\model\Content::getInfoByid(abs($uid),true);
  79. if ($this->user) {
  80. $qun_user = \app\qun\model\Member::where([
  81. 'aid'=>abs($uid),
  82. 'uid'=>$this->user['uid'],
  83. ])->find();
  84. $qun_user = $qun_user?getArray($qun_user):[];
  85. }
  86. }
  87. isset($qun_info['_viewlimit']) || $qun_info['_viewlimit'] = $qun_info['viewlimit'];
  88. if($qun_info['_viewlimit'] && empty($this->admin) && $qun_info['uid']!=$this->user['uid']){
  89. if (empty($qun_user)) {
  90. return $this->err_js('你不是本圈子成员,无权查看聊天内容!');
  91. }elseif ($qun_user['type']==0){
  92. return $this->err_js('你还没通过审核,无权查看聊天内容!');
  93. }
  94. }
  95. unset($qun_info['sncode'],$qun_info['password']);
  96. }
  97. $array = model::list_moremsg($my_uid,$uid,$id,$rows,$maxid,$msg_id,$msg_sys);
  98. $array['qun_info'] = $qun_info;
  99. $array['qun_userinfo'] = $qun_user;
  100. if ($this->user) {
  101. $array['userinfo'] = [
  102. 'uid'=>$this->user['uid'],
  103. 'username'=>$this->user['username'],
  104. 'nickname'=>$this->user['nickname'],
  105. 'icon'=>tempdir($this->user['icon']),
  106. 'groupid'=>$this->user['groupid'],
  107. ];
  108. }else{
  109. $array['userinfo'] = ['uid'=>$my_uid?:0,'username'=>'','groupid'=>0,'icon'=>''];
  110. }
  111. if ($maxid<1) { //首次加载
  112. $array['ws_url'] = fun('Gatewayclient@client_url'); //APP要用到
  113. if ($this->user) {
  114. //更新最后的访问时间,也即把历史消息标为已读
  115. MsguserModel::where('uid',$this->user['uid'])->where('aid',$uid)->update(['list'=>time()]);
  116. if($uid>=0){ //干脆点,把第二页第三页未读的也一起标注为已读了
  117. model::where('touid',$this->user['uid'])->where('uid',$uid)->where('ifread',0)->update(['ifread'=>1]);
  118. }
  119. }
  120. }
  121. if ($is_live) {
  122. // $live_array = cache('live_qun');
  123. // $data = $this->request->post();
  124. // if($live_array['qun'.$uid]['flv_url']!=$data['flv_url']){
  125. // fun('alilive@add',$this->user['uid'],$uid,'qun',$data);
  126. // }
  127. // $live_array['qun'.$uid] = [
  128. // 'flv_url'=>$data['flv_url'],
  129. // 'm3u8_url'=>$data['m3u8_url'],
  130. // 'rtmp_url'=>$data['rtmp_url'],
  131. // 'time'=>time(),
  132. // ];
  133. // cache('live_qun',$live_array);
  134. }elseif($uid<1){ //代表群聊
  135. // $live_array = cache('live_qun'); //这里有个BUG,如果进后台操作过东西,缓存就会被清空,导致这里没数据
  136. // if($live_array['qun'.$uid]){
  137. // $live_array['qun'.$uid]['time'] = 0; //此参数将弃用
  138. // $live_array['qun'.$uid]['push_url']='';
  139. // $array['live_video'] = $live_array['qun'.$uid];
  140. // }elseif ($live_array['vod_voice'.$uid]){
  141. // $array['vod_voice'] = $live_array['vod_voice'.$uid];
  142. // }
  143. $array['live'] = fun('Qun@live',abs($uid))?:''; //圈子的活跃信息,比如直播之类的
  144. if($array['live']){unset($array['live']['service_video']);}
  145. //unset($array['live']['service_video']['push_url']);
  146. }
  147. $array['chatmod'] = $this->get_chat_mod($uid,$uid<0?get_user($qun_info['uid'])['groupid']:$this->user['groupid']); //群聊模块
  148. return $this->ok_js($array);
  149. }
  150. /**
  151. * 获取群聊模块
  152. * @param number $uid 用户是正数,圈子是负数
  153. * @param number $groupid uid是负数则是圈主用户组,否则就是当前用户的用户组
  154. * @return array
  155. */
  156. public function get_chat_mod($uid=0,$groupid=0){
  157. if(input('inapp')){
  158. $pcwap = 3;
  159. }elseif(in_wap()){
  160. $pcwap = 1;
  161. }else{
  162. $pcwap = 2;
  163. }
  164. $array = fun('chatmod@get',$pcwap,$uid<1?1:(in_wap()?2:0),$uid<0?abs($uid):0,$groupid); //群聊模块
  165. return $array;
  166. }
  167. /**
  168. * 调取各个圈子的最新留言
  169. * @return void|unknown|\think\response\Json
  170. */
  171. public function newmsg(){
  172. $data = [];
  173. $array = model::where('qun_id','>',0)->order('id desc')->limit(10)->column(true);
  174. foreach($array AS $rs){
  175. $rs['username'] = get_user_name($rs['uid']);
  176. $rs['icon'] = get_user_icon($rs['uid']);
  177. $rs['time'] = format_time($rs['create_time'],true);
  178. $rs['qun_name'] = fun('qun@getByid',$rs['qun_id'])['title'];
  179. $rs['user_url'] = get_url('user',$rs['uid']);
  180. $rs['qun_url'] = get_url('msg',-$rs['qun_id']);
  181. $rs['title'] = get_word(del_html($rs['content']), 50);
  182. $data[] = $rs;
  183. }
  184. return $this->ok_js($data);
  185. }
  186. }