input('id/d',0), 'title' => input('title','','trim'), 'content' => input('content','','trim'), 'attachment' => input('attachment','','trim'), 'auths' => input('auths','','trim'), 'enable' => input('enable/d',0), 'type' => input('type/d',1), 'user_id' => $userId, 'org_id' => $orgId ]; $logdata = json_encode($data); $result = validate('Article')->check($data,[],''); if(true !== $result){ HelpHander::error(validate('Article')->getError()); } $id = $data['id']; unset($data['id']); $this->startTrans(); try{ if($id > 0){ unset($data['user_id']); $data['update_time'] = date('Y-m-d H:i:s'); $ret = $this->allowField(true)->save($data,['id'=>$id]); if(!$ret){ \exception('操作失败'); } }else{ $data['create_time'] = date('Y-m-d H:i:s'); $ret = $this->allowField(true)->save($data); if(!$ret){ \exception('操作失败'); } $id = $this->id; } if($data['enable'] == 1){ // 创建发布记录 $res = $this->createArticleLog($id,$data['title'],$orgId,$data['auths']); if(!$res){ \exception('记录添加失败'); } } $this->commit(); }catch (Exception $e){ $this->rollback(); HelpHander::error('操作失败'); } if($id > 0){ $content = '修改文章'; }else{ $content = '添加文章'; } model('ActionLog')->add(11,$content,0,$logdata); return true; } /** * 创建文章记录 * @param $noticeId * @param $orgId * @param $auths * @return bool * @throws \Exception */ private function createArticleLog($articleId,$title,$orgId,$auths){ try{ $userids = model('User')->getUserByAuths($orgId,$auths); $arr = []; foreach ($userids as $k=>$v){ $arr[] = [ 'user_id' => $v, 'article_id' => $articleId, 'status' => 0 ]; if(count($arr) == 500){ $ret = Db::name('article_log')->insertAll($arr); if($ret != count($arr)){ \exception('记录添加失败'); } $arr = []; } } if($arr){ // 不足500条的处理 $ret = Db::name('article_log')->insertAll($arr); if($ret != count($arr)){ \exception('记录添加失败'); } } foreach ($userids as $k=>$v){ $res = model('Message')->add(6,$articleId,11,$v,$orgId,$title); if(!$res){ \exception('消息发送失败'); } } }catch (Exception $e){ return false; } return true; } public function info($id){ $info = $this->where('id',$id)->find(); if(!$info){ HelpHander::error('数据不存在'); } return $info->toArray(); } public function lists($page,$size,$orgId,$title,$enable,$type=1){ $map[] = ['org_id','=',$orgId]; $map[] = ['type','=',$type]; if($title != ''){ $map[] = ['title','like','%'.$title.'%']; } if(in_array($enable,[0,1])){ $map[] = ['enable','=',$enable]; } $lists = $this ->where($map) ->page($page,$size) ->order('id desc') ->select(); $total = $this->where($map)->count(); $data = [ 'total' => $total, 'list' => $lists?$lists->toArray():[] ]; return $data; } /** * 获取用户文章 * @param $page * @param $size * @param $userId * @param $status * @return array */ public function userLists($page,$size,$userId,$status,$title,$type){ $map[] = ['nl.user_id','=',$userId]; if(in_array($status,[0,1])){ $map[] = ['nl.status','=',$status]; } if($type > 0){ $map[] = ['n.type','=',$type]; } if($title){ $map[] = ['n.title','like','%'.$title.'%']; } $lists = Db::name('article_log') ->alias('nl') ->join('article n','n.id = nl.article_id') ->where($map) ->page($page,$size) ->order('nl.id desc') ->field('nl.status,n.*') ->select(); $total = Db::name('article_log') ->alias('nl') ->join('article n','n.id = nl.article_id') ->where($map) ->count(); $data = [ 'total' => $total, 'list' => $lists?$lists:[] ]; return $data; } public function recentNoticeList($userId){ $map[] = ['nl.user_id','=',$userId]; $lists = Db::name('article_log') ->alias('nl') ->join('article n','n.id = nl.article_id') ->where($map) ->order('nl.id desc') ->field('nl.status,n.id,n.title,n.create_time') ->limit(5) ->select(); return $lists?$lists:[]; } /** * 根据公告id获取查看结果 * @param $page * @param $size * @param $noticeId * @param $status * @return array */ public function noticeLog($page,$size,$noticeId,$status){ $map[] = ['nl.article_id','=',$noticeId]; if(in_array($status,[0,1])){ $map[] = ['nl.status','=',$status]; } $lists = Db::name('article_log') ->alias('nl') ->join('user_info u','u.user_id = nl.user_id') ->where($map) ->page($page,$size) ->order('nl.id desc') ->field('nl.id,nl.user_id,nl.read_time,nl.status,u.name as user_name') ->select(); $total = Db::name('article_log')->alias('nl')->join('user_info u','u.user_id = nl.user_id')->where($map)->count(); $data = [ 'total' => $total, 'list' => $lists?$lists:[] ]; return $data; } public function del($id){ Db::startTrans(); try{ $ret = $this->where('id',$id)->delete(); if(!$ret){ \exception('操作失败'); } // 删除记录 Db::name('article_log')->where('article_id',$id)->delete(); // 删除消息 Db::name('message')->where('bus_id',$id)->where('type',6)->delete(); $logdata = json_encode(['id' => $id]); model('ActionLog')->add(11,'删除文章',0,$logdata); Db::commit(); }catch (Exception $e){ Db::rollback(); HelpHander::error('操作失败'); } return true; } // 未读状态改为已读 public function changeStatus($id,$userId){ Db::startTrans(); try{ Db::name('message') ->where('user_id',$userId) ->where('bus_type',11) ->where('type',6) ->where('bus_id',$id) ->setField('status',1); $ret = Db::name('article_log') ->where('article_id',$id) ->where('user_id',$userId) ->update(['status'=>1,'read_time'=>date('Y-m-d H:i:s')]); if(!$ret){ \exception('操作失败'); } Db::commit(); }catch (Exception $e){ Db::rollback(); HelpHander::error('操作失败'); } // websocket推送 Gateway::$registerAddress = config('app.gateway_register_ip'); if(Gateway::isUidOnline($userId)){ // 在线发送消息 Gateway::sendToUid($userId, json_encode(['type'=>'msgcount','data'=>[],'msg'=>''])); } return true; } public function queryAppDetail($id){ $info = Db::name('article') ->where('id',$id) ->where('enable',1) ->field('id,title,content,org_id,create_time,attachment,type') ->find(); if(!$info){ HelpHander::error('记录不存在'); } $info['orgName'] = Db::name('org')->where('id',$info['org_id'])->value('name'); $info['readNum'] = Db::name('article_log')->where('article_id',$id)->where('status',1)->count(); $info['unReadNum'] = Db::name('article_log')->where('article_id',$id)->where('status',0)->count(); unset($info['org_id']); return $info; } // 全部已读 public function setAllNoticeRead($userId,$orgId){ $ids = Db::name('article_log') ->alias('nl') ->join('article n','n.id = nl.article_id') ->where('nl.user_id',$userId) ->where('n.org_id',$orgId) ->where('nl.status',0) ->column('n.id'); if($ids){ Db::startTrans(); try{ Db::name('article_log') ->where('user_id',$userId) ->where('article_id','in',$ids) ->update(['status'=>1,'read_time'=>date('Y-m-d H:i:s')]); Db::name('message') ->where('org_id',$orgId) ->where('user_id',$userId) ->where('status',0) ->where('bus_type',11) ->where('bus_id','in',$ids) ->where('type',6) ->setField('status',1); Db::commit(); }catch (Exception $e){ Db::rollback(); HelpHander::error('操作失败'); } } // websocket推送 Gateway::$registerAddress = config('app.gateway_register_ip'); if(Gateway::isUidOnline($userId)){ // 在线发送消息 Gateway::sendToUid($userId, json_encode(['type'=>'msgcount','data'=>[],'msg'=>''])); } return true; } }