Message.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 办公消息模块
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\oa\controller;
  8. use app\admin\controller\ApiCommon;
  9. use think\Hook;
  10. use think\Request;
  11. use think\Db;
  12. class Message extends ApiCommon
  13. {
  14. /**
  15. * 用于判断权限
  16. * @permission 无限制
  17. * @allow 登录用户可访问
  18. * @other 其他根据系统设置
  19. **/
  20. public function _initialize()
  21. {
  22. $action = [
  23. 'permission'=>[''],
  24. 'allow'=>['num']
  25. ];
  26. Hook::listen('check_auth',$action);
  27. $request = Request::instance();
  28. $a = strtolower($request->action());
  29. if (!in_array($a, $action['permission'])) {
  30. parent::_initialize();
  31. }
  32. }
  33. /**
  34. * 消息数
  35. * @author Michael_xu
  36. * @return
  37. */
  38. public function num()
  39. {
  40. $param = $this->param;
  41. $userInfo = $this->userInfo;
  42. $user_id = $userInfo['id'];
  43. $structure_id = $userInfo['structure_id'];
  44. $type = $param['type'] ? : 'all';
  45. $todayTime = getTimeByType('today');
  46. $start_time = $todayTime[0];
  47. $end_time = $todayTime[1];
  48. $eventNum = 0; //日程数
  49. $taskNum = 0; //任务数
  50. $announcementNum = 0; //公告数
  51. $logNum = 0; //日志数
  52. $examineNum = 0; //审批数
  53. $data = [];
  54. //日程
  55. if ($type == 'event' || $type == 'all') {
  56. $eventWhere = '( ( start_time BETWEEN '.$start_time.' AND '.$end_time.' ) AND ( create_user_id = '.$user_id.' or owner_user_ids like "%,'.$user_id.',%" ) ) OR ( ( end_time BETWEEN '.$start_time.' AND '.$end_time.' ) AND ( create_user_id = '.$user_id.' or owner_user_ids like "%,'.$user_id.',%" ) ) OR ( start_time < '.$start_time.' AND end_time > '.$end_time.' AND ( create_user_id = '.$user_id.' or owner_user_ids like "%,'.$user_id.',%" ) )';
  57. $eventNum = db('oa_event')->where($eventWhere)->count();
  58. $data['eventNum'] = $eventNum ? : 0;
  59. }
  60. //任务(我负责的和我参与的未完成的任务提醒)
  61. if ($type == 'task' || $type == 'all') {
  62. $taskWhere = [];
  63. $str = ','.$userInfo['id'].',';
  64. $task = 'main_user_id ='.$userInfo['id'].' or create_user_id ='.$user_id.' or ( is_open = 1 and owner_user_id like "%'.$str.'%")';
  65. $taskWhere['pid'] = 0;
  66. $taskWhere['status'] = array('neq',5);
  67. $taskWhere['work_id'] = 0;
  68. $taskNum = db('task')->where(' ishidden=0 and ( '.$task.' )')->where($taskWhere)->count();
  69. $data['taskNum'] = $taskNum ? : 0;
  70. }
  71. //公告(未读公告)
  72. if ($type == 'announcement' || $type == 'all') {
  73. $time = strtotime(date('Y-m-d',time()));
  74. $announcementWhere['start_time'] = array('elt',$time);
  75. $announcementWhere['end_time'] = array('egt',$time);
  76. $announcementWhere['read_user_ids'] = array('not like','%,'.$user_id.',%');
  77. $announcementNum = db('oa_announcement')->where(' ( owner_user_ids LIKE "%,'.$userInfo['id'].',%" OR structure_ids LIKE "%,'.$userInfo['structure_id'].',%" OR create_user_id = '.$user_id.' OR (owner_user_ids = "" AND structure_ids = ""))')->where($announcementWhere)->count();
  78. $data['announcementNum'] = $announcementNum ? : 0;
  79. }
  80. //日志(发送给自己并未读)
  81. if ($type == 'log' || $type == 'all') {
  82. $dataWhere['user_id'] = $user_id;
  83. $dataWhere['structure_id'] = $structure_id;
  84. $logMap = function($query) use ($dataWhere){
  85. $query->where('send_user_ids',array('like','%,'.$dataWhere['user_id'].',%'))
  86. ->whereOr('send_structure_ids',array('like','%,'.$dataWhere['structure_id'].',%'));
  87. };
  88. $logWhere['read_user_ids'] = ['not like','%,'.$user_id.',%'];
  89. $logNum = db('oa_log')->where($logWhere)->where($logMap)->count();
  90. $data['logNum'] = $logNum ? : 0;
  91. }
  92. //审批
  93. if ($type == 'examine' || $type == 'all') {
  94. $examineWhere['check_status'] = array('elt',1);
  95. $map_str = "( `check_user_id` LIKE '%,".$user_id.",%' OR `check_user_id` = ".$user_id." )";
  96. $examineNum = db('oa_examine')->where($map_str)->where($examineWhere)->count();
  97. $data['examineNum'] = $examineNum ? : 0;
  98. }
  99. return resultArray(['data'=>$data]);
  100. }
  101. }