PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/src/applications/native/controller/NoticeController.php

https://gitlab.com/wuhang2003/phpwind
PHP | 260 lines | 147 code | 19 blank | 94 comment | 23 complexity | 34d538e6c866a357dfa64ee9ba4b9e40 MD5 | raw file
  1. <?php
  2. /**
  3. * 系统消息设置为已读状态
  4. *
  5. * @fileName: NoticeController.php
  6. * @author: yuliang<yuliang.lyl@alibaba-inc.com>
  7. * @license: http://www.phpwind.com
  8. * @version: $Id
  9. * @lastchange: 2014-12-16 19:08:17
  10. * @desc:
  11. * */
  12. Wind::import('APPS:native.controller.NativeBaseController');
  13. class NoticeController extends NativeBaseController {
  14. public function beforeAction($handlerAdapter){
  15. parent::beforeAction($handlerAdapter);
  16. // $this->uid = 1; //测试uid
  17. $this->loginUser = new PwUserBo($this->uid);
  18. $this->loginUser->resetGid($this->loginUser->gid);
  19. if (!$this->uid) {
  20. $this->forwardRedirect(WindUrlHelper::createUrl('u/login/run'));
  21. }
  22. $action = $handlerAdapter->getAction();
  23. $controller = $handlerAdapter->getController();
  24. $this->setOutput($action,'_action');
  25. $this->setOutput($controller,'_controller');
  26. }
  27. /**
  28. * 查看未读系统通知列表,并将已查看的未读消息设为已读
  29. * @access public
  30. * @return string
  31. <pre>
  32. /index.php?m=native&c=notice&type=(reply|system)&page=1&_json=1
  33. type:10 未读通知;0 所有系统消息
  34. response: html
  35. </pre>
  36. */
  37. public function run() {
  38. list($type,$page) = $this->getInput(array('type','page'));
  39. // $type = 3;
  40. if($type=='reply'){
  41. $typeids = array(10);//回复提醒
  42. $exclude = false;
  43. }else{
  44. $typeids = array(1,10);//排除私信提醒、回复提醒
  45. $exclude = true;
  46. }
  47. $page = intval($page);
  48. $page < 1 && $page = 1;
  49. $perpage = 20;
  50. list($start, $limit) = Pw::page2limit($page, $perpage);
  51. $notice_list = Wekit::loadDao('native.dao.PwNativeMessageNoticesDao')->getNoticesByTypeIds($this->uid,$typeids,$start,$limit,$exclude);
  52. $notice_list = $this->_getNoticeService()->formatNoticeList($notice_list);
  53. // $noticeList = $this->_getNoticeDs()->getNotices($this->loginUser->uid,$type,$start, $limit);
  54. // $noticeList = $this->_getNoticeService()->formatNoticeList($noticeList);
  55. // var_dump($noticeList);exit;
  56. $typeCounts = $this->_getNoticeService()->countNoticesByType($this->uid);//获取用户通知总数
  57. //类型
  58. $typeid = intval($type);
  59. //获取所有NOTICE未读通知数
  60. $unreadCount = $this->_getNoticeDs()->getUnreadNoticeCount($this->uid);
  61. // $unread_notice_cnt = Wekit::loadDao('native.dao.PwNativeMessageNoticesDao')->getUnreadCountByTypeIds($this->loginUser->uid,$typeids,$exclude);
  62. // $this->_readNoticeList($unreadCount,$noticeList);//将消息设置为已读
  63. $this->_readNoticeList($unreadCount,$notice_list);//将消息设置为已读
  64. var_dump($notice_list,$unreadCount);exit;
  65. //count
  66. $count = intval($typeCounts[$typeid]['count']);
  67. $this->setOutput($page, 'page');
  68. $this->setOutput($perpage, 'perpage');
  69. $this->setOutput($count, 'count');
  70. $this->setOutput(ceil($count/$perpage), 'totalpage');
  71. $this->setOutput(array('type'=>$typeid),'args');
  72. $this->setOutput($typeid, 'typeid');
  73. $this->setOutput($typeCounts, 'typeCounts');
  74. $this->setOutput($noticeList, 'noticeList');
  75. // seo设置
  76. Wind::import('SRV:seo.bo.PwSeoBo');
  77. $seoBo = PwSeoBo::getInstance();
  78. $lang = Wind::getComponent('i18n');
  79. $seoBo->setCustomSeo($lang->getMessage('SEO:mess.notice.run.title'), '', '');
  80. Wekit::setV('seo', $seoBo);
  81. }
  82. /**
  83. *
  84. * 忽略消息
  85. */
  86. public function ignoreAction(){
  87. list($id,$ignore) = $this->getInput(array('id','ignore'));
  88. if ($this->_getNoticeService()->ignoreNotice($id,$ignore)) {
  89. $this->showMessage('操作成功');
  90. } else {
  91. $this->showError('操作失败');
  92. }
  93. }
  94. /**
  95. *
  96. * 删除消息
  97. */
  98. public function deleteAction(){
  99. list($id,$ids) = $this->getInput(array('id','ids'), 'post');
  100. if (!$ids && $id) $ids = array(intval($id));
  101. if(!is_array($ids))$this->showError('操作失败');
  102. if ($this->_getNoticeDs()->deleteNoticeByIdsAndUid($this->loginUser->uid, $ids)) {
  103. $this->showMessage('操作成功');
  104. } else {
  105. $this->showError('操作失败');
  106. }
  107. }
  108. /**
  109. *
  110. * 顶部快捷列表
  111. */
  112. public function minilistAction() {
  113. $perpage = 20;
  114. $noticeList = $this->_getNoticeDs()->getNoticesOrderByRead($this->loginUser->uid, $perpage);//message.PwMessageNotices;根据用户UID获取通知列表 按未读升序、更新时间倒序
  115. $noticeList = $this->_getNoticeService()->formatNoticeList($noticeList);//message.srv.PwNoticeService
  116. //获取用户未读通知数(系统消息+私信消息)
  117. $unreadCount = $this->_getNoticeDs()->getUnreadNoticeCount($this->loginUser->uid);
  118. // var_dump($unreadCount);exit;
  119. $this->_readNoticeList($unreadCount, $noticeList);
  120. //set layout for common request
  121. if (!$this->getRequest()->getIsAjaxRequest()) {
  122. $this->setLayout('layout_notice_minilist');
  123. }
  124. $this->setOutput($noticeList, 'noticeList');
  125. }
  126. /**
  127. * 设置系统消息全部已读
  128. * lyl
  129. */
  130. public function checkNoticeReadedAction() {
  131. //获取用户未读通知数(系统消息+私信消息)
  132. $unreadCount = $this->_getNoticeDs()->getUnreadNoticeCount($this->uid);
  133. $perpage = intval($unreadCount) ? intval($unreadCount) : 20;
  134. $noticeList = $this->_getNoticeDs()->getNoticesOrderByRead($this->uid, $perpage);//message.PwMessageNotices;根据用户UID获取通知列表 按未读升序、更新时间倒序
  135. // var_dump($noticeList);exit;
  136. $noticeList = $this->_getNoticeService()->formatNoticeList($noticeList);//message.srv.PwNoticeService
  137. // var_dump($unreadCount,$noticeList);exit;
  138. //将未读消息标记已读
  139. $this->_readNoticeList($unreadCount, $noticeList);
  140. //set layout for common request
  141. $this->showMessage('success');
  142. // exit;
  143. // if (!$this->getRequest()->getIsAjaxRequest()) {
  144. // $this->setLayout('layout_notice_minilist');
  145. // }
  146. // $this->setOutput($noticeList, 'noticeList');
  147. }
  148. /**
  149. *
  150. * 具体通知详细页
  151. */
  152. public function detaillistAction(){
  153. $id = $this->getInput('id');
  154. $notice = $this->_getNoticeDs()->getNotice($id);
  155. if (!$notice || $notice['uid'] != $this->loginUser->uid) {
  156. $this->showError('获取内容失败');
  157. }
  158. $detailList = $this->_getNoticeService()->getDetailList($notice);
  159. $this->setOutput($notice, 'notice');
  160. $this->setOutput($detailList,'detailList');
  161. $typeName = $this->_getNoticeService()->getTypenameByTypeid($notice['typeid']);
  162. $this->setOutput($typeName, 'typeName');
  163. //$tpl = $typeName ? sprintf('notice_detail_%s',$typeName) : 'notice_detail';
  164. //$this->setTemplate($tpl);
  165. }
  166. /**
  167. *
  168. * 具体通知详细页
  169. */
  170. public function detailAction(){
  171. $id = $this->getInput('id');
  172. $notice = $this->_getNoticeDs()->getNotice($id);
  173. if (!$notice || $notice['uid'] != $this->loginUser->uid) {
  174. $this->showError('获取内容失败');
  175. }
  176. $prevNotice = $this->_getNoticeDs()->getPrevNotice($this->loginUser->uid,$id);
  177. $nextNotice = $this->_getNoticeDs()->getNextNotice($this->loginUser->uid,$id);
  178. $detailList = $this->_getNoticeService()->getDetailList($notice);
  179. $this->setOutput($notice, 'notice');
  180. $this->setOutput($detailList,'detailList');
  181. $this->setOutput($prevNotice, 'prevNotice');
  182. $this->setOutput($nextNotice, 'nextNotice');
  183. $typeName = $this->_getNoticeService()->getTypenameByTypeid($notice['typeid']);
  184. $this->setOutput($typeName, 'typeName');
  185. //$tpl = $typeName ? sprintf('notice_detail_%s',$typeName) : 'notice_detail';
  186. //$this->setTemplate($tpl);
  187. }
  188. /**
  189. *
  190. * Enter description here ...
  191. * @return PwMessageNotices
  192. */
  193. protected function _getNoticeDs(){
  194. return Wekit::load('message.PwMessageNotices');
  195. }
  196. /**
  197. *
  198. * Enter description here ...
  199. * @return PwNoticeService
  200. */
  201. protected function _getNoticeService(){
  202. return Wekit::load('message.srv.PwNoticeService');
  203. }
  204. /**
  205. *
  206. * Enter description here ...
  207. * @return PwUser
  208. */
  209. protected function _getUserDs(){
  210. return Wekit::load('user.PwUser');
  211. }
  212. /**
  213. *
  214. * 设置已读
  215. * @param int $unreadCount
  216. * @param array $noticeList
  217. */
  218. private function _readNoticeList($unreadCount,$noticeList){
  219. if ($unreadCount && $noticeList) {
  220. //更新用户的通知未读数
  221. $readnum = 0; //本次阅读数
  222. Wind::import('SRV:message.dm.PwMessageNoticesDm');
  223. $dm = new PwMessageNoticesDm();
  224. $dm->setRead(1);
  225. $ids = array();
  226. foreach ($noticeList as $v) {//本次一页消息中未读的数量
  227. if ($v['is_read']) continue;
  228. $readnum ++;
  229. $ids[] = $v['id'];
  230. }
  231. $ids && $this->_getNoticeDs()->batchUpdateNotice($ids,$dm);//message.PwMessageNotices
  232. $newUnreadCount = $unreadCount - $readnum;//所有未读消息-本次已读消息
  233. if ($newUnreadCount != $unreadCount) {
  234. Wind::import('SRV:user.dm.PwUserInfoDm');
  235. $dm = new PwUserInfoDm($this->uid);
  236. $dm->setNoticeCount($newUnreadCount);
  237. $this->_getUserDs()->editUser($dm,PwUser::FETCH_DATA);
  238. }
  239. }
  240. }
  241. }