Article.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use GatewayClient\Gateway;
  5. use think\Db;
  6. use think\Exception;
  7. use think\Model;
  8. class Article extends Model
  9. {
  10. public function add($userId,$orgId){
  11. $data = [
  12. 'id' => input('id/d',0),
  13. 'title' => input('title','','trim'),
  14. 'content' => input('content','','trim'),
  15. 'attachment' => input('attachment','','trim'),
  16. 'auths' => input('auths','','trim'),
  17. 'enable' => input('enable/d',0),
  18. 'type' => input('type/d',1),
  19. 'user_id' => $userId,
  20. 'org_id' => $orgId
  21. ];
  22. $logdata = json_encode($data);
  23. $result = validate('Article')->check($data,[],'');
  24. if(true !== $result){
  25. HelpHander::error(validate('Article')->getError());
  26. }
  27. $id = $data['id'];
  28. unset($data['id']);
  29. $this->startTrans();
  30. try{
  31. if($id > 0){
  32. unset($data['user_id']);
  33. $data['update_time'] = date('Y-m-d H:i:s');
  34. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  35. if(!$ret){
  36. \exception('操作失败');
  37. }
  38. }else{
  39. $data['create_time'] = date('Y-m-d H:i:s');
  40. $ret = $this->allowField(true)->save($data);
  41. if(!$ret){
  42. \exception('操作失败');
  43. }
  44. $id = $this->id;
  45. }
  46. if($data['enable'] == 1){ // 创建发布记录
  47. $res = $this->createArticleLog($id,$data['title'],$orgId,$data['auths']);
  48. if(!$res){
  49. \exception('记录添加失败');
  50. }
  51. }
  52. $this->commit();
  53. }catch (Exception $e){
  54. $this->rollback();
  55. HelpHander::error('操作失败');
  56. }
  57. if($id > 0){
  58. $content = '修改文章';
  59. }else{
  60. $content = '添加文章';
  61. }
  62. model('ActionLog')->add(11,$content,0,$logdata);
  63. return true;
  64. }
  65. /**
  66. * 创建文章记录
  67. * @param $noticeId
  68. * @param $orgId
  69. * @param $auths
  70. * @return bool
  71. * @throws \Exception
  72. */
  73. private function createArticleLog($articleId,$title,$orgId,$auths){
  74. try{
  75. $userids = model('User')->getUserByAuths($orgId,$auths);
  76. $arr = [];
  77. foreach ($userids as $k=>$v){
  78. $arr[] = [
  79. 'user_id' => $v,
  80. 'article_id' => $articleId,
  81. 'status' => 0
  82. ];
  83. if(count($arr) == 500){
  84. $ret = Db::name('article_log')->insertAll($arr);
  85. if($ret != count($arr)){
  86. \exception('记录添加失败');
  87. }
  88. $arr = [];
  89. }
  90. }
  91. if($arr){ // 不足500条的处理
  92. $ret = Db::name('article_log')->insertAll($arr);
  93. if($ret != count($arr)){
  94. \exception('记录添加失败');
  95. }
  96. }
  97. foreach ($userids as $k=>$v){
  98. $res = model('Message')->add(6,$articleId,11,$v,$orgId,$title);
  99. if(!$res){
  100. \exception('消息发送失败');
  101. }
  102. }
  103. }catch (Exception $e){
  104. return false;
  105. }
  106. return true;
  107. }
  108. public function info($id){
  109. $info = $this->where('id',$id)->find();
  110. if(!$info){
  111. HelpHander::error('数据不存在');
  112. }
  113. return $info->toArray();
  114. }
  115. public function lists($page,$size,$orgId,$title,$enable,$type=1){
  116. $map[] = ['org_id','=',$orgId];
  117. $map[] = ['type','=',$type];
  118. if($title != ''){
  119. $map[] = ['title','like','%'.$title.'%'];
  120. }
  121. if(in_array($enable,[0,1])){
  122. $map[] = ['enable','=',$enable];
  123. }
  124. $lists = $this
  125. ->where($map)
  126. ->page($page,$size)
  127. ->order('id desc')
  128. ->select();
  129. $total = $this->where($map)->count();
  130. $data = [
  131. 'total' => $total,
  132. 'list' => $lists?$lists->toArray():[]
  133. ];
  134. return $data;
  135. }
  136. /**
  137. * 获取用户文章
  138. * @param $page
  139. * @param $size
  140. * @param $userId
  141. * @param $status
  142. * @return array
  143. */
  144. public function userLists($page,$size,$userId,$status,$title,$type){
  145. $map[] = ['nl.user_id','=',$userId];
  146. if(in_array($status,[0,1])){
  147. $map[] = ['nl.status','=',$status];
  148. }
  149. if($type > 0){
  150. $map[] = ['n.type','=',$type];
  151. }
  152. if($title){
  153. $map[] = ['n.title','like','%'.$title.'%'];
  154. }
  155. $lists = Db::name('article_log')
  156. ->alias('nl')
  157. ->join('article n','n.id = nl.article_id')
  158. ->where($map)
  159. ->page($page,$size)
  160. ->order('nl.id desc')
  161. ->field('nl.status,n.*')
  162. ->select();
  163. $total = Db::name('article_log')
  164. ->alias('nl')
  165. ->join('article n','n.id = nl.article_id')
  166. ->where($map)
  167. ->count();
  168. $data = [
  169. 'total' => $total,
  170. 'list' => $lists?$lists:[]
  171. ];
  172. return $data;
  173. }
  174. public function recentNoticeList($userId){
  175. $map[] = ['nl.user_id','=',$userId];
  176. $lists = Db::name('article_log')
  177. ->alias('nl')
  178. ->join('article n','n.id = nl.article_id')
  179. ->where($map)
  180. ->order('nl.id desc')
  181. ->field('nl.status,n.id,n.title,n.create_time')
  182. ->limit(5)
  183. ->select();
  184. return $lists?$lists:[];
  185. }
  186. /**
  187. * 根据公告id获取查看结果
  188. * @param $page
  189. * @param $size
  190. * @param $noticeId
  191. * @param $status
  192. * @return array
  193. */
  194. public function noticeLog($page,$size,$noticeId,$status){
  195. $map[] = ['nl.article_id','=',$noticeId];
  196. if(in_array($status,[0,1])){
  197. $map[] = ['nl.status','=',$status];
  198. }
  199. $lists = Db::name('article_log')
  200. ->alias('nl')
  201. ->join('user_info u','u.user_id = nl.user_id')
  202. ->where($map)
  203. ->page($page,$size)
  204. ->order('nl.id desc')
  205. ->field('nl.id,nl.user_id,nl.read_time,nl.status,u.name as user_name')
  206. ->select();
  207. $total = Db::name('article_log')->alias('nl')->join('user_info u','u.user_id = nl.user_id')->where($map)->count();
  208. $data = [
  209. 'total' => $total,
  210. 'list' => $lists?$lists:[]
  211. ];
  212. return $data;
  213. }
  214. public function del($id){
  215. Db::startTrans();
  216. try{
  217. $ret = $this->where('id',$id)->delete();
  218. if(!$ret){
  219. \exception('操作失败');
  220. }
  221. // 删除记录
  222. Db::name('article_log')->where('article_id',$id)->delete();
  223. // 删除消息
  224. Db::name('message')->where('bus_id',$id)->where('type',6)->delete();
  225. $logdata = json_encode(['id' => $id]);
  226. model('ActionLog')->add(11,'删除文章',0,$logdata);
  227. Db::commit();
  228. }catch (Exception $e){
  229. Db::rollback();
  230. HelpHander::error('操作失败');
  231. }
  232. return true;
  233. }
  234. // 未读状态改为已读
  235. public function changeStatus($id,$userId){
  236. Db::startTrans();
  237. try{
  238. Db::name('message')
  239. ->where('user_id',$userId)
  240. ->where('bus_type',11)
  241. ->where('type',6)
  242. ->where('bus_id',$id)
  243. ->setField('status',1);
  244. $ret = Db::name('article_log')
  245. ->where('article_id',$id)
  246. ->where('user_id',$userId)
  247. ->update(['status'=>1,'read_time'=>date('Y-m-d H:i:s')]);
  248. if(!$ret){
  249. \exception('操作失败');
  250. }
  251. Db::commit();
  252. }catch (Exception $e){
  253. Db::rollback();
  254. HelpHander::error('操作失败');
  255. }
  256. // websocket推送
  257. Gateway::$registerAddress = config('app.gateway_register_ip');
  258. if(Gateway::isUidOnline($userId)){ // 在线发送消息
  259. Gateway::sendToUid($userId, json_encode(['type'=>'msgcount','data'=>[],'msg'=>'']));
  260. }
  261. return true;
  262. }
  263. public function queryAppDetail($id){
  264. $info = Db::name('article')
  265. ->where('id',$id)
  266. ->where('enable',1)
  267. ->field('id,title,content,org_id,create_time,attachment,type')
  268. ->find();
  269. if(!$info){
  270. HelpHander::error('记录不存在');
  271. }
  272. $info['orgName'] = Db::name('org')->where('id',$info['org_id'])->value('name');
  273. $info['readNum'] = Db::name('article_log')->where('article_id',$id)->where('status',1)->count();
  274. $info['unReadNum'] = Db::name('article_log')->where('article_id',$id)->where('status',0)->count();
  275. unset($info['org_id']);
  276. return $info;
  277. }
  278. // 全部已读
  279. public function setAllNoticeRead($userId,$orgId){
  280. $ids = Db::name('article_log')
  281. ->alias('nl')
  282. ->join('article n','n.id = nl.article_id')
  283. ->where('nl.user_id',$userId)
  284. ->where('n.org_id',$orgId)
  285. ->where('nl.status',0)
  286. ->column('n.id');
  287. if($ids){
  288. Db::startTrans();
  289. try{
  290. Db::name('article_log')
  291. ->where('user_id',$userId)
  292. ->where('article_id','in',$ids)
  293. ->update(['status'=>1,'read_time'=>date('Y-m-d H:i:s')]);
  294. Db::name('message')
  295. ->where('org_id',$orgId)
  296. ->where('user_id',$userId)
  297. ->where('status',0)
  298. ->where('bus_type',11)
  299. ->where('bus_id','in',$ids)
  300. ->where('type',6)
  301. ->setField('status',1);
  302. Db::commit();
  303. }catch (Exception $e){
  304. Db::rollback();
  305. HelpHander::error('操作失败');
  306. }
  307. }
  308. // websocket推送
  309. Gateway::$registerAddress = config('app.gateway_register_ip');
  310. if(Gateway::isUidOnline($userId)){ // 在线发送消息
  311. Gateway::sendToUid($userId, json_encode(['type'=>'msgcount','data'=>[],'msg'=>'']));
  312. }
  313. return true;
  314. }
  315. }