Msg.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace app\sys\controller;
  3. use app\BaseController;
  4. use app\model\Employee;
  5. use app\model\Grant;
  6. use app\model\OperateLog;
  7. use think\facade\View;
  8. use app\model\EmployeeMsg;
  9. use app\model\EmployeeMsgSend;
  10. use think\facade\Request;
  11. use app\model\Org;
  12. class Msg extends BaseController
  13. {
  14. private $rootId;
  15. protected function initialize()
  16. {
  17. $this->rootId = $this->request->employee->root_id;
  18. }
  19. /**
  20. * 消息列表
  21. */
  22. public function index()
  23. {
  24. if (!$this->request->isAjax()) {
  25. return View::fetch();
  26. }
  27. $page = input('get.page',1);
  28. $limit = input('get.limit',10);
  29. $keyword = input('get.keyword');
  30. $where[] = ['type','=','system_msg_'.$this->rootId];
  31. if(!empty($keyword)){
  32. $where[] = ['msg', 'like', '%' . $keyword . '%'];
  33. }
  34. $data = EmployeeMsg::where($where)->group('index,msg,addtime')->order('addtime desc')->field('msg,addtime,index')->page($page,$limit)->select();
  35. $count = EmployeeMsg::field('index')->where($where)->group('index')->count();
  36. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  37. }
  38. /**
  39. * 添加消息
  40. */
  41. public function add_msg()
  42. {
  43. return View::fetch();
  44. }
  45. /**
  46. * 添加消息
  47. */
  48. public function add()
  49. {
  50. $day_start = date("Y-m-d 23:59:59", strtotime("-1 day"));
  51. $day_end = date("Y-m-d 00:00:00", strtotime("+1 day"));
  52. $times = EmployeeMsgSend::where([['root_id', '=', $this->rootId], ['addtime', 'between', [$day_start, $day_end]]])->count();
  53. if ($times > 2) {
  54. return json(['code' => 1, 'msg' => '每天最多发送三条通知']);
  55. }
  56. $msg = input('msg','');
  57. $where[] = ['root_id','=',$this->rootId];
  58. $where[] = ['state','=','在职'];
  59. $where[] = ['uid', '<>', 0];
  60. $eid = input('select','');
  61. $where[] = ['id','in',explode(',',$eid)];
  62. $eids = Employee::where($where)->column('id');
  63. $time = date('Y-m-d H:i:s',time());
  64. $group = (string)microtime(true).(string)mt_rand(100000,9999999);
  65. $saves = [];
  66. foreach ($eids as $v) {
  67. $save = [];
  68. $save['employee_id'] = $v;
  69. $save['msg'] = $msg;
  70. $save['type'] = 'system_msg_'.$this->rootId;
  71. $save['addtime'] = $time;
  72. $save['saw'] = 0;
  73. $save['index'] = $group;
  74. $saves[] = $save;
  75. }
  76. if ($saves) (new EmployeeMsg())->saveAll($saves);
  77. $msg_data['root_id'] = $this->rootId;
  78. $msg_data['content'] = $msg;
  79. EmployeeMsgSend::create($msg_data);
  80. return json(['code' => 0, 'msg' => '添加成功']);
  81. }
  82. /**
  83. * 消息阅读视图
  84. */
  85. public function read_list_view()
  86. {
  87. $id = input('id');
  88. View::assign('id', $id);
  89. return View::fetch();
  90. }
  91. /**
  92. * 消息阅读列表
  93. */
  94. public function read_list()
  95. {
  96. $page = input('get.page',1);
  97. $limit = input('get.limit',10);
  98. $index = input('index','');
  99. $where[] = ['type','=','system_msg_'.$this->rootId];
  100. $where[] = ['index','=',$index];
  101. $data = EmployeeMsg::with(['employee' => function($query) {
  102. $query->field('name,id');
  103. }])->where($where)->order('saw desc')->page($page,$limit)->select();
  104. $count = EmployeeMsg::where($where)->count();
  105. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  106. }
  107. /*
  108. * 发送对象
  109. */
  110. public function get_person()
  111. {
  112. $ids = input('id','');
  113. $root_id = request()->employee->root_id;
  114. $type = input('type',0);
  115. //树形
  116. if ($type==1) {
  117. //所有员工
  118. $w1[] = ['root_id','=',$root_id];
  119. $w1[] = ['state','=','在职'];
  120. $w1[] = ['org_id','>',0];
  121. $employee = Employee::where($w1)->field('id value,name,org_id')->select()->toArray();
  122. if ($ids) {
  123. $ids = explode(',',$ids);
  124. foreach ($employee as $k => &$v) {
  125. $v['selected'] = in_array($v['value'],$ids);
  126. }
  127. }else{
  128. foreach ($employee as $k => &$v) {
  129. $v['selected'] = false;
  130. }
  131. }
  132. $persons = [];
  133. foreach ($employee as $k => $v1) {
  134. $persons[$v1['org_id']][] = $v1;
  135. }
  136. $where = [
  137. ['path', 'like', $root_id . '-%'],
  138. ['status', '=', 1]
  139. ];
  140. $allnodes = Org::where($where)->field('id value,id,pid,name')->order('level asc, id asc')->select()->toArray();
  141. $tree = $this->tree($allnodes,0, $persons);
  142. return json($tree);
  143. }
  144. $w[] = ['root_id','=',$root_id];
  145. $w[] = ['state','=','在职'];
  146. $w[] = ['role','=','领导'];
  147. $employee = Employee::where($w)->field('id value,name')->select()->toArray();
  148. if ($ids) {
  149. $ids = explode(',',$ids);
  150. foreach ($employee as $k => &$v) {
  151. $v['selected'] = in_array($v['value'],$ids);
  152. }
  153. }else{
  154. foreach ($employee as $k => &$v) {
  155. $v['selected'] = false;
  156. }
  157. }
  158. return json($employee);
  159. }
  160. public function tree($data,$pid = 0, $persons)
  161. {
  162. $new_arr = [];
  163. foreach($data as $k => $v){
  164. if($v['pid'] == $pid) {
  165. $persions = isset($persons[$v['id']])?$persons[$v['id']]:[];
  166. $children = $this->tree($data, $v['id'], $persons);
  167. $v['children'] = array_merge_recursive($children,$persions);
  168. if (empty($v['children'])) $v['disabled']=true;
  169. $new_arr[] =$v;
  170. }
  171. }
  172. return $new_arr;
  173. }
  174. }