PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/src/service/message/srv/PwNoticeService.php

https://gitlab.com/wuhang2003/phpwind
PHP | 423 lines | 268 code | 31 blank | 124 comment | 33 complexity | 1fe687d884b3ebe8b20340860fed05cc MD5 | raw file
  1. <?php
  2. /**
  3. * 通知业务
  4. *
  5. * @author peihong <peihong.zhangph@aliyun-inc.com>
  6. * @copyright ©2003-2103 phpwind.com
  7. * @license http://www.phpwind.com
  8. * @version $Id: PwNoticeService.php 3833 2012-01-12 03:32:27Z peihong.zhangph $
  9. * @package src.service.message.srv
  10. */
  11. class PwNoticeService {
  12. /**
  13. * 发送通知
  14. * @param int $uid
  15. * @param string $type
  16. * @param int $param
  17. * @param array $extendParams
  18. * @param $updateUnRead 是否更新未读数
  19. */
  20. public function sendNotice($uid,$type,$param = 0,$extendParams = array(),$updateUnRead = true){
  21. $action = $this->_getAction($type);
  22. if (!$action) return new PwError('MESSAGE::notice.type.undefined');
  23. $typeId = $this->_getTypeId($type);
  24. // 看是否发通知
  25. if ($this->_checkPrivate($uid,$typeId) !== true) {
  26. return false;
  27. }
  28. //aggregated notice
  29. Wind::import('SRV:message.dm.PwMessageNoticesDm');
  30. $dm = new PwMessageNoticesDm();
  31. $action->aggregate && $notice = $this->_getNoticesDs()->getNoticeByUid($uid,$typeId,$param);
  32. $extendParams = $action->formatExtendParams($extendParams,$notice);
  33. $noticeTitle = $action->buildTitle($param,$extendParams,$notice);
  34. $dm->setToUid($uid)
  35. ->setRead(0)
  36. ->setType($typeId)
  37. ->setParam($param)
  38. ->setExtendParams($extendParams)
  39. ->setTitle($noticeTitle);
  40. if (!$notice) {
  41. $noticeId = $this->_getNoticesDs()->addNotice($dm);
  42. } else {
  43. $dm->setId($notice['id']);
  44. $dm->setModifiedTime(Pw::getTime());
  45. $this->_getNoticesDs()->updateNotice($dm);
  46. $noticeId = $notice['id'];
  47. }
  48. //更新通知未读数
  49. if ($updateUnRead && (!$notice || $notice['is_read'])){
  50. Wind::import('SRV:user.dm.PwUserInfoDm');
  51. $dm = new PwUserInfoDm($uid);
  52. $dm->addNotice(1);
  53. $this->_getUserDs()->editUser($dm,PwUser::FETCH_DATA);
  54. }
  55. return true;
  56. }
  57. /**
  58. *
  59. * 发送一般通知(无类型)
  60. * @param int $uid
  61. * @param string $content
  62. * @param string $title
  63. */
  64. public function sendDefaultNotice($uid,$content,$title = ''){
  65. $extendParams = array('content' => $content, 'title' => $title);
  66. return $this->sendNotice($uid, 'default', 0,$extendParams);
  67. }
  68. /**
  69. *
  70. * 按类型统计
  71. * @param unknown_type $uid
  72. */
  73. public function countNoticesByType($uid){
  74. $list = $this->_getNoticesDs()->countNoticesByType($uid);
  75. $data = array();
  76. if (is_array($list)) {
  77. $typeNames = $this->_getTypeNames();
  78. $typeIds = array_flip($this->_getTypes());
  79. foreach ($list as $v) {
  80. $type = $typeIds[$v['typeid']];
  81. if (!$type) continue;
  82. $data[0]['count'] += $v['num'];
  83. $data[$v['typeid']] = array(
  84. 'typename' => $typeNames[$type],
  85. 'type' => $type,
  86. 'count' => $v['num']
  87. );
  88. }
  89. $data[0] && $data[0]['typename'] = '全部';
  90. }
  91. return $data;
  92. }
  93. /**
  94. *
  95. * (忽略|取消忽略)一个通知
  96. */
  97. public function ignoreNotice($id,$ignore = 1){
  98. $id = intval($id);
  99. $ignore = intval($ignore);
  100. $ignore = $ignore ? 1 : 0;
  101. $notice = $this->_getNoticesDs()->getNotice($id);
  102. if (!$notice) {
  103. return false;
  104. } else {
  105. Wind::import('SRV:message.dm.PwMessageNoticesDm');
  106. $dm = new PwMessageNoticesDm($id);
  107. $dm->setIgnore($ignore);
  108. $this->_getNoticesDs()->updateNotice($dm);
  109. //ingore to app
  110. $noticeAction = $this->_getActionByTypeid($notice['typeid']);
  111. if ($noticeAction && $noticeAction->ignoreNotice) {
  112. $noticeAction->ignoreNotice($notice,$ignore);
  113. }
  114. return true;
  115. }
  116. }
  117. /**
  118. *
  119. *
  120. * @param array $notice
  121. */
  122. public function getDetailList($notice){
  123. if (!is_array($notice)) return null;
  124. $action = $this->_getActionByTypeid($notice['typeid']);
  125. if (!$action) return null;
  126. return $action->getDetailList($notice);
  127. }
  128. public function formatNoticeList($noticeList){
  129. $typeIds = array_flip($this->_getTypes());
  130. $messageFromUids = array();
  131. $uid = 0;
  132. foreach ($noticeList as $k=>$v) {
  133. $v['extend_params'] = @unserialize($v['extend_params']);
  134. $v['type'] = $typeIds[$v['typeid']];
  135. if ($v['type'] == 'message') {
  136. $uid = $v['extend_params']['to_uid'];
  137. $messageFromUids[$k] = $v['param'];
  138. }
  139. $noticeList[$k] = $v;
  140. }
  141. //取私信相关信息
  142. $messageInfos = $this->_getWindid()->getDialogByUsers($uid,$messageFromUids);
  143. if ($messageInfos) {
  144. foreach ($messageInfos as $v) {
  145. $noticeKey = array_search($v['from_uid'], $messageFromUids);
  146. $extend = array(
  147. 'title' => $this->_parseUrl($v['last_message']['content']),
  148. 'unread_count' => $v['unread_count'],
  149. 'message_count' => $v['message_count'],
  150. );
  151. $noticeList[$noticeKey]['message_extend_params'] = $extend;
  152. }
  153. }
  154. return $noticeList;
  155. }
  156. /**
  157. *
  158. * 根据类型ID获取类型名
  159. * @param int $typeid
  160. * @return string
  161. */
  162. public function getTypenameByTypeid($typeid){
  163. $typeNames = $this->_getTypeNames();
  164. $typeIds = array_flip($this->_getTypes());
  165. return $typeIds[$typeid];
  166. }
  167. /**
  168. * 根据类型删除通知
  169. *
  170. * @param int $uid
  171. * @param string $type
  172. * @param int $param
  173. * @param bool
  174. */
  175. public function deleteNoticeByType($uid,$type,$param) {
  176. $typeId = $this->_getTypeId($type);
  177. return $this->_getNoticesDs()->deleteNoticeByType($uid,$typeId,$param);
  178. }
  179. /**
  180. * 根据uid删除通知
  181. *
  182. * @param int $uid
  183. * @param bool
  184. */
  185. public function deleteNoticeByUid($uid){
  186. $this->_getNoticesDs()->deleteNoticeByUid($uid);
  187. Wind::import('SRV:user.dm.PwUserInfoDm');
  188. $user = Wekit::load('user.PwUser');
  189. $dm = new PwUserInfoDm($uid);
  190. $dm->setNoticeCount(0);
  191. $user->editUser($dm, PwUser::FETCH_DATA);
  192. }
  193. /**
  194. * 根据类型批量删除通知
  195. *
  196. * @param int $uid
  197. * @param string $type
  198. * @param array $params
  199. * @param bool
  200. */
  201. public function detchDeleteNoticeByType($uid,$type,$params) {
  202. $typeId = $this->_getTypeId($type);
  203. return $this->_getNoticesDs()->betchDeleteNoticeByType($uid,$typeId,$params);
  204. }
  205. /**
  206. * 根据类型ID设置忽略
  207. *
  208. * @param int $typeId
  209. * @param int $uid
  210. * @return bool
  211. */
  212. public function setIgnoreNotice($typeId,$uid,$ignore = 1){
  213. $config = $this->_getMessagesDs()->getMessageConfig($uid);
  214. $noticeValue = $config['notice_types'] ? unserialize($config['notice_types']) : array();
  215. $newArray = array($typeId=>$typeId);
  216. if ($ignore) {
  217. $noticeValue = $noticeValue+$newArray;
  218. } else {
  219. $noticeValue = array_diff_key($noticeValue,$newArray);
  220. }
  221. return $this->_getMessagesDs()->setMessageConfig($uid,$config['privacy'],serialize($noticeValue));
  222. }
  223. /**
  224. * 获取通知设置忽略类型
  225. *
  226. * @return array
  227. */
  228. public function getNoticeTypeSet(){
  229. $privateType = $this->_getNoticePrivateType();
  230. $types = $this->_getTypeNames();
  231. $tmpTypes = array();
  232. foreach ($privateType as $k => $v) {
  233. if (in_array($k, array_keys($types))) {
  234. $tmpTypes[$v] = $types[$k];
  235. }
  236. }
  237. return $tmpTypes;
  238. }
  239. /**
  240. * 某个类型是否被忽略
  241. *
  242. * @param int $uid
  243. * @param int $typeId
  244. * @return array
  245. */
  246. public function isIgnoreNoticeType($uid, $typeId){
  247. if (!in_array($typeId, $this->_getNoticePrivateType())) {
  248. return false;
  249. }
  250. $config = $this->_getMessagesDs()->getMessageConfig($uid);
  251. if (!$config['notice_types']) return false;
  252. $types = unserialize($config['notice_types']);
  253. return !in_array($typeId,$types) ? false : true;
  254. }
  255. /**
  256. *
  257. * Enter description here ...
  258. * @param string $type
  259. * @return PwNoticeAction
  260. */
  261. protected function _getAction($type){
  262. if (!$type || !in_array($type,array_keys($this->_getTypes()))) return false;
  263. list($type) = explode('_',$type);
  264. $actionMethod = sprintf('_get%sAction',ucfirst($type));
  265. if (!method_exists($this, $actionMethod)) {
  266. $type = strtolower($type);
  267. $className = sprintf('PwNotice%s', ucfirst($type));
  268. $fliePath = 'SRV:message.srv.notice.'.$className;
  269. Wind::import($fliePath);
  270. return new $className();
  271. } else {
  272. return $this->$actionMethod();
  273. }
  274. }
  275. private function _parseUrl($message) {
  276. $searcharray = array(
  277. "/\[url=((https?|ftp|gopher|news|telnet|mms|rtsp|thunder|ed2k)?[^\[\s]+?)(\,(1)\/?)?\](.+?)\[\/url\]/eis",
  278. "/\[url\]((https?|ftp|gopher|news|telnet|mms|rtsp|thunder|ed2k)?[^\[\s]+?)\[\/url\]/eis"
  279. );
  280. preg_match("/\[url\]((https?|ftp|gopher|news|telnet|mms|rtsp|thunder|ed2k)?[^\[\s]+?)\[\/url\]/eis", $message, $match);
  281. return $match[1] ? $match[1] : $message;
  282. }
  283. /**
  284. *
  285. * Enter description here ...
  286. * @param int $typeId
  287. * @return PwNoticeAction
  288. */
  289. protected function _getActionByTypeid($typeId){
  290. $typeId = intval($typeId);
  291. $types = array_flip($this->_getTypes());
  292. return $this->_getAction($types[$typeId]);
  293. }
  294. private function _getNoticePrivateType(){
  295. return array(
  296. 'medal' => 4,
  297. 'task' => 5,
  298. 'credit' => 14,
  299. );
  300. }
  301. private function _getTypes(){
  302. return array(
  303. 'message' => 1,
  304. 'default' => 2,
  305. 'threadmanage' => 3,
  306. 'medal' => 4,
  307. 'task' => 5,
  308. 'massmessage' => 6,
  309. 'report_thread' => 7,
  310. 'report_post' => 8,
  311. 'report_message' => 9,
  312. 'threadreply' => 10,
  313. 'attention' => 11,
  314. 'remind' => 12,
  315. 'ban' => 13,
  316. 'credit' => 14,
  317. 'postreply' => 15,
  318. 'report_photo' => 16,
  319. 'app' => 99
  320. );
  321. }
  322. private function _getTypeNames(){
  323. return array(
  324. 'default' => '通知',
  325. 'message' => '私信',
  326. 'threadreply' => '回复提醒',
  327. 'threadmanage' => '管理提醒',
  328. 'medal' => '勋章',
  329. 'task' => '任务',
  330. 'massmessage' => '群发消息',
  331. 'report_thread' => '帖子举报',
  332. 'report_post' => '回复举报',
  333. 'report_message' => '私信举报',
  334. 'attention' => '关注',
  335. 'remind' => '@提醒',
  336. 'ban' => '帐号管理',
  337. 'credit' => '积分变动',
  338. 'postreply' => '楼层回复',
  339. 'report_photo' => '照片举报',
  340. 'app' => '应用通知',
  341. );
  342. }
  343. /**
  344. * 检查通知设置权限
  345. *
  346. * @param int $uid
  347. * @param int $type
  348. * @return bool
  349. */
  350. public function _checkPrivate($uid,$typeId) {
  351. $config = $this->_getMessagesDs()->getMessageConfig($uid);
  352. if (!$config['notice_types']) return true;
  353. $noticeValue = unserialize($config['notice_types']);
  354. $noticeType = array_intersect_key($noticeValue,$this->getNoticeTypeSet());
  355. if (in_array($typeId, array_keys($noticeType))) {
  356. return false;
  357. }
  358. return true;
  359. }
  360. private function _getTypeId($typeName){
  361. $types = $this->_getTypes();
  362. if (!is_array($types) || !isset($types[$typeName])) return 0;
  363. return $types[$typeName];
  364. }
  365. private function _getWindid() {
  366. return WindidApi::api('message');
  367. }
  368. /**
  369. *
  370. * Enter description here ...
  371. * @return PwMessageNotices
  372. */
  373. private function _getNoticesDs(){
  374. return Wekit::load('message.PwMessageNotices');
  375. }
  376. /**
  377. *
  378. * Enter description here ...
  379. * @return PwMessageMessages
  380. */
  381. private function _getMessagesDs(){
  382. return Wekit::load('message.PwMessageMessages');
  383. }
  384. /**
  385. *
  386. * Enter description here ...
  387. * @return PwUser
  388. */
  389. private function _getUserDs(){
  390. return Wekit::load('user.PwUser');
  391. }
  392. }