Message.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------------------------------
  4. * GouGuOPEN [ 左手研发,右手开源,未来可期!]
  5. +-----------------------------------------------------------------------------------------------
  6. * @Copyright (c) 2021~2024 http://www.gouguoa.com All rights reserved.
  7. +-----------------------------------------------------------------------------------------------
  8. * @Licensed 勾股OA,开源且可免费使用,但并不是自由软件,未经授权许可不能去除勾股OA的相关版权信息
  9. +-----------------------------------------------------------------------------------------------
  10. * @Author 勾股工作室 <hdm58@qq.com>
  11. +-----------------------------------------------------------------------------------------------
  12. */
  13. declare (strict_types = 1);
  14. namespace app\home\controller;
  15. use app\base\BaseController;
  16. use app\home\model\Message as MessageList;
  17. use app\home\model\Msg as MsgList;
  18. use think\facade\Db;
  19. use think\facade\View;
  20. class Message extends BaseController
  21. {
  22. /**
  23. * 构造函数
  24. */
  25. protected $model;
  26. protected $model2;
  27. public function __construct() {
  28. parent::__construct(); // 调用父类构造函数
  29. $this->model = new MessageList();
  30. $this->model2 = new MsgList();
  31. }
  32. //收件箱
  33. public function inbox()
  34. {
  35. if (request()->isAjax()) {
  36. $param = get_params();
  37. $where = [];
  38. $where[] = ['to_uid', '=', $this->uid];
  39. $where[] = ['delete_time', '=', 0];
  40. if (!empty($param['keywords'])) {
  41. $where[] = ['title', 'like', '%' . $param['keywords'] . '%'];
  42. }
  43. if (!empty($param['read'])) {
  44. if($param['read']==1){
  45. $where[] = ['read_time', '=', 0];
  46. }else{
  47. $where[] = ['read_time', '>', 0];
  48. }
  49. }
  50. if (!empty($param['types'])) {
  51. if($param['types']==1){
  52. $where[] = ['from_uid', '=', 0];
  53. }else{
  54. $where[] = ['from_uid', '>', 0];
  55. }
  56. }
  57. //按发送时间检索
  58. if (!empty($param['range_time'])) {
  59. $range_time =explode('~', $param['range_time']);
  60. $where[] = ['send_time', 'between',[strtotime(urldecode($range_time[0])),strtotime(urldecode($range_time[1]))]];
  61. }
  62. $list = $this->model2->datalist($where,$param);
  63. return table_assign(0, '', $list);
  64. } else {
  65. $a = MessageList::where([['from_uid', '=', $this->uid],['delete_time', '>', 0],['clear_time', '=', 0]])->count();
  66. $b = MsgList::where([['to_uid', '=', $this->uid],['delete_time', '>', 0],['clear_time', '=', 0]])->count();
  67. $count = [
  68. 'sendbox' => MessageList::where([['from_uid', '=', $this->uid],['is_draft', '=', 1],['delete_time', '=', 0]])->count(),
  69. 'inbox' => MsgList::where([['to_uid', '=', $this->uid],['delete_time', '=', 0]])->count(),
  70. 'star' => MsgList::where([['to_uid', '=', $this->uid],['is_star','=',1],['delete_time', '=', 0]])->count(),
  71. 'draft' => MessageList::where([['from_uid', '=', $this->uid],['is_draft', '=', 2],['delete_time', '=', 0]])->count(),
  72. 'rubbish' => $a+$b
  73. ];
  74. View::assign('count', $count);
  75. View::assign('action', $this->action);
  76. return view();
  77. }
  78. }
  79. //星标信息
  80. public function star()
  81. {
  82. if (request()->isAjax()) {
  83. $param = get_params();
  84. $where = [];
  85. $where[] = ['to_uid', '=', $this->uid];
  86. $where[] = ['delete_time', '=', 0];
  87. $where[] = ['is_star', '=', 1];
  88. if (!empty($param['keywords'])) {
  89. $where[] = ['title', 'like', '%' . $param['keywords'] . '%'];
  90. }
  91. if (!empty($param['read'])) {
  92. if($param['read']==1){
  93. $where[] = ['read_time', '=', 0];
  94. }else{
  95. $where[] = ['read_time', '>', 0];
  96. }
  97. }
  98. if (!empty($param['types'])) {
  99. if($param['types']==1){
  100. $where[] = ['from_uid', '=', 0];
  101. }else{
  102. $where[] = ['from_uid', '>', 0];
  103. }
  104. }
  105. //按发送时间检索
  106. if (!empty($param['range_time'])) {
  107. $range_time =explode('~', $param['range_time']);
  108. $where[] = ['send_time', 'between',[strtotime(urldecode($range_time[0])),strtotime(urldecode($range_time[1]))]];
  109. }
  110. $list = $this->model2->datalist($where,$param);
  111. return table_assign(0, '', $list);
  112. } else {
  113. $a = MessageList::where([['from_uid', '=', $this->uid],['delete_time', '>', 0],['clear_time', '=', 0]])->count();
  114. $b = MsgList::where([['to_uid', '=', $this->uid],['delete_time', '>', 0],['clear_time', '=', 0]])->count();
  115. $count = [
  116. 'sendbox' => MessageList::where([['from_uid', '=', $this->uid],['is_draft', '=', 1],['delete_time', '=', 0]])->count(),
  117. 'inbox' => MsgList::where([['to_uid', '=', $this->uid],['delete_time', '=', 0]])->count(),
  118. 'star' => MsgList::where([['to_uid', '=', $this->uid],['is_star','=',1],['delete_time', '=', 0]])->count(),
  119. 'draft' => MessageList::where([['from_uid', '=', $this->uid],['is_draft', '=', 2],['delete_time', '=', 0]])->count(),
  120. 'rubbish' => $a+$b
  121. ];
  122. View::assign('count', $count);
  123. View::assign('action', $this->action);
  124. return view();
  125. }
  126. }
  127. //发件箱
  128. public function sendbox()
  129. {
  130. if (request()->isAjax()) {
  131. $param = get_params();
  132. $where = [];
  133. if (!empty($param['keywords'])) {
  134. $map[] = ['title', 'like', '%' . $param['keywords'] . '%'];
  135. }
  136. $where[] = ['from_uid', '=', $this->uid];
  137. $where[] = ['is_draft', '=', 1];
  138. $where[] = ['delete_time', '=', 0];
  139. //按发送时间检索
  140. if (!empty($param['range_time'])) {
  141. $range_time =explode('~', $param['range_time']);
  142. $where[] = ['send_time', 'between',[strtotime(urldecode($range_time[0])),strtotime(urldecode($range_time[1]))]];
  143. }
  144. $list = $this->model->datalist($where,$param);
  145. return table_assign(0, '', $list);
  146. } else {
  147. $a = MessageList::where([['from_uid', '=', $this->uid],['delete_time', '>', 0],['clear_time', '=', 0]])->count();
  148. $b = MsgList::where([['to_uid', '=', $this->uid],['delete_time', '>', 0],['clear_time', '=', 0]])->count();
  149. $count = [
  150. 'sendbox' => MessageList::where([['from_uid', '=', $this->uid],['is_draft', '=', 1],['delete_time', '=', 0]])->count(),
  151. 'inbox' => MsgList::where([['to_uid', '=', $this->uid],['delete_time', '=', 0]])->count(),
  152. 'star' => MsgList::where([['to_uid', '=', $this->uid],['is_star','=',1],['delete_time', '=', 0]])->count(),
  153. 'draft' => MessageList::where([['from_uid', '=', $this->uid],['is_draft', '=', 2],['delete_time', '=', 0]])->count(),
  154. 'rubbish' => $a+$b
  155. ];
  156. View::assign('count', $count);
  157. View::assign('action', $this->action);
  158. return view();
  159. }
  160. }
  161. //草稿箱
  162. public function draft()
  163. {
  164. if (request()->isAjax()) {
  165. $param = get_params();
  166. $where = [];
  167. if (!empty($param['keywords'])) {
  168. $map[] = ['title', 'like', '%' . $param['keywords'] . '%'];
  169. }
  170. $where[] = ['from_uid', '=', $this->uid];
  171. $where[] = ['is_draft', '=', 2];
  172. $where[] = ['delete_time', '=', 0];
  173. //按发送时间检索
  174. if (!empty($param['range_time'])) {
  175. $range_time =explode('~', $param['range_time']);
  176. $where[] = ['send_time', 'between',[strtotime(urldecode($range_time[0])),strtotime(urldecode($range_time[1]))]];
  177. }
  178. $list = $this->model->datalist($where,$param);
  179. return table_assign(0, '', $list);
  180. } else {
  181. $a = MessageList::where([['from_uid', '=', $this->uid],['delete_time', '>', 0],['clear_time', '=', 0]])->count();
  182. $b = MsgList::where([['to_uid', '=', $this->uid],['delete_time', '>', 0],['clear_time', '=', 0]])->count();
  183. $count = [
  184. 'sendbox' => MessageList::where([['from_uid', '=', $this->uid],['is_draft', '=', 1],['delete_time', '=', 0]])->count(),
  185. 'inbox' => MsgList::where([['to_uid', '=', $this->uid],['delete_time', '=', 0]])->count(),
  186. 'star' => MsgList::where([['to_uid', '=', $this->uid],['is_star','=',1],['delete_time', '=', 0]])->count(),
  187. 'draft' => MessageList::where([['from_uid', '=', $this->uid],['is_draft', '=', 2],['delete_time', '=', 0]])->count(),
  188. 'rubbish' => $a+$b
  189. ];
  190. View::assign('count', $count);
  191. View::assign('action', $this->action);
  192. return view();
  193. }
  194. }
  195. //垃圾箱
  196. public function rubbish()
  197. {
  198. if (request()->isAjax()) {
  199. $param = get_params();
  200. $uid = $this->uid;
  201. $where = "delete_time > 0";
  202. $where.= " AND clear_time = 0";
  203. if (!empty($param['keywords'])) {
  204. $where.= " AND title like '%" . $param['keywords'] . "%'";
  205. }
  206. $sqlParts = [];
  207. $sqlCounts = [];
  208. $tables=['message','msg'];
  209. $prefix = get_config('database.connections.mysql.prefix');
  210. foreach ($tables as $table) {
  211. $tableName = $prefix.$table;
  212. if($table=='message'){
  213. $wherea= $where.' AND from_uid = '.$uid;
  214. // 假设每个表都有相同的列结构,这里只是简单示例
  215. $sqlPart = "SELECT id,title,from_uid,send_time,'{$table}' as table_name FROM {$tableName} WHERE {$wherea}";
  216. $sqlCount = "SELECT COUNT(*) AS count FROM {$tableName} WHERE {$wherea}";
  217. }
  218. else{
  219. $whereb= $where.' AND to_uid = '.$uid;
  220. $sqlPart = "SELECT id,title,from_uid,create_time as send_time,'{$table}' as table_name FROM {$tableName} WHERE {$whereb}";
  221. $sqlCount = "SELECT COUNT(*) AS count FROM {$tableName} WHERE {$whereb}";
  222. }
  223. // 查询数据库中是否存在该数据表
  224. $is_table = Db::query("SHOW TABLES LIKE '{$tableName}'");
  225. // 判断查询结果
  226. if (!empty($is_table)) {
  227. $sqlParts[] = $sqlPart;
  228. $sqlCounts[] = $sqlCount;
  229. }
  230. }
  231. // 使用implode将各个部分用UNION ALL连接起来
  232. $unionSql = implode(" UNION ALL ", $sqlParts);
  233. $totalCount = 0;
  234. foreach ($sqlCounts as $sql) {
  235. $count = Db::query($sql)[0]['count']; // 假设每个查询都返回了一个包含'count'键的数组
  236. $totalCount += $count;
  237. }
  238. // 添加排序和分页逻辑
  239. // 假设每页显示10条记录,当前页码为$page(需要预先定义或获取)
  240. $pageSize = $param['limit'];
  241. $page = 1; // 示例页码
  242. $offset = ($page - 1) * $pageSize;
  243. // 注意:不同的数据库分页语法可能有所不同,这里以MySQL为例
  244. $finalSql = $unionSql . " ORDER BY send_time DESC LIMIT {$offset}, {$pageSize}";
  245. // 执行查询
  246. $result = Db::query($finalSql);
  247. // 处理结果
  248. foreach ($result as &$row) {
  249. // 处理每一行数据
  250. $row['types'] = 0;
  251. $row['sourse'] = '发件箱';
  252. if($row['send_time'] == 0){
  253. $row['sourse'] = '草稿箱';
  254. $row['types'] = 1;
  255. $row['send_time'] = '-';
  256. }
  257. else{
  258. if($row['table_name'] == 'msg'){
  259. $row['sourse'] = '收件箱';
  260. $row['types'] = 2;
  261. }
  262. $row['send_time'] = date('Y-m-d H:i:s',$row['send_time']);
  263. }
  264. if(!empty($row['from_uid'])){
  265. $row['from_name'] = Db::name('Admin')->where('id','=',$row['from_uid'])->value('name');
  266. }
  267. else{
  268. $row['from_name']='-';
  269. }
  270. }
  271. $list=array(
  272. 'data'=>$result,
  273. 'total'=>$totalCount
  274. );
  275. return table_assign(0, '', $list);
  276. } else {
  277. $a = MessageList::where([['from_uid', '=', $this->uid],['delete_time', '>', 0],['clear_time', '=', 0]])->count();
  278. $b = MsgList::where([['to_uid', '=', $this->uid],['delete_time', '>', 0],['clear_time', '=', 0]])->count();
  279. $count = [
  280. 'sendbox' => MessageList::where([['from_uid', '=', $this->uid],['is_draft', '=', 1],['delete_time', '=', 0]])->count(),
  281. 'inbox' => MsgList::where([['to_uid', '=', $this->uid],['delete_time', '=', 0]])->count(),
  282. 'star' => MsgList::where([['to_uid', '=', $this->uid],['is_star','=',1],['delete_time', '=', 0]])->count(),
  283. 'draft' => MessageList::where([['from_uid', '=', $this->uid],['is_draft', '=', 2],['delete_time', '=', 0]])->count(),
  284. 'rubbish' => $a+$b
  285. ];
  286. View::assign('count', $count);
  287. View::assign('action', $this->action);
  288. return view();
  289. }
  290. }
  291. //新增&编辑信息
  292. public function add()
  293. {
  294. $param = get_params();
  295. $id = isset($param['id']) ? $param['id'] : 0;
  296. $msg_id = 0;
  297. if ($id > 0) {
  298. $detail = $this->model->detail($id);
  299. if (empty($detail)) {
  300. throw new \think\exception\HttpException(404, '找不到记录');
  301. }
  302. if ($detail['from_uid'] != $this->uid) {
  303. throw new \think\exception\HttpException(406, '无权限编辑');
  304. }
  305. if($detail['msg_id']>0){
  306. $msg_id = $detail['msg_id'];
  307. }
  308. View::assign('detail', $detail);
  309. }
  310. View::assign('id', $id);
  311. View::assign('msg_id', $msg_id);
  312. return view();
  313. }
  314. //查看发件箱、草稿箱消息
  315. public function view($id)
  316. {
  317. $detail = $this->model->detail($id);
  318. if (empty($detail)) {
  319. throw new \think\exception\HttpException(404, '找不到记录');
  320. }
  321. if ($detail['from_uid'] != $this->uid) {
  322. throw new \think\exception\HttpException(406, '无权限查看');
  323. }
  324. //已读回执
  325. if($detail['send_time']>0){
  326. $read_user_ids= msgList::where([['message_id','=',$id],['read_time','>',0]])->column('to_uid');
  327. $read_users = Db::name('Admin')->where('status', 1)->where('id', 'in', $read_user_ids)->column('name');
  328. $detail['read_user_names'] = implode(',',$read_users);
  329. }
  330. View::assign('detail', $detail);
  331. return view();
  332. }
  333. //删除发件、草稿
  334. public function del()
  335. {
  336. $param = get_params();
  337. $ids = empty($param['ids']) ? 0 : $param['ids'];
  338. $idArray = explode(',', $ids);
  339. foreach ($idArray as $key => $val) {
  340. MessageList::where(['id' => $val])->update(['delete_time' => time()]);
  341. add_log('delete', $val,[],'消息');
  342. }
  343. return to_assign(0, '操作成功');
  344. }
  345. //保存消息
  346. public function save()
  347. {
  348. $param = get_params();
  349. $id = empty($param['id']) ? 0 : $param['id'];
  350. //接受人类型判断
  351. if ($param['types'] == 1 && empty($param['uids'])) {
  352. return to_assign(1, '收件人员不能为空');
  353. }
  354. if ($param['types'] == 2 && empty($param['dids'])) {
  355. return to_assign(1, '收件部门不能为空');
  356. }
  357. if ($param['types'] == 3 && empty($param['pids'])) {
  358. return to_assign(1, '收件岗位不能为空');
  359. }
  360. if ($param['types'] == 4) {
  361. $param['uids'] = '';
  362. $param['dids'] = '';
  363. $param['pids'] = '';
  364. $param['copy_uids'] = '';
  365. }
  366. $res = false;
  367. if ($id > 0) {
  368. //编辑信息的情况
  369. $param['update_time'] = time();
  370. $res = MessageList::strict(false)->field(true)->update($param);
  371. } else {
  372. //新增信息的情况
  373. $param['create_time'] = time();
  374. $param['update_time'] = time();
  375. $param['from_uid'] = $this->uid;
  376. $res = MessageList::strict(false)->field(true)->insertGetId($param);
  377. }
  378. if ($res !== false) {
  379. if ($id > 0) {
  380. $mid = $id;
  381. } else {
  382. $mid = $res;
  383. }
  384. add_log('save',$mid,$param,'消息');
  385. if($param['is_draft'] == 1){
  386. $res = $this->model->send($mid);
  387. if ($res!==false) {
  388. return to_assign(0, '发送成功');
  389. } else {
  390. return to_assign(1, '发送失败');
  391. }
  392. }
  393. else{
  394. return to_assign(0, '保存成功', $mid);
  395. }
  396. } else {
  397. return to_assign(1, '操作失败');
  398. }
  399. }
  400. //发送消息
  401. public function send_message($id)
  402. {
  403. //查询要发的消息
  404. $msg = MessageList::where(['id' => $id])->find();
  405. if (!empty($msg)) {
  406. $res = $this->model->send($id);
  407. if ($res!==false) {
  408. return to_assign(0, '发送成功');
  409. } else {
  410. return to_assign(1, '发送失败');
  411. }
  412. } else {
  413. return to_assign(1, '发送失败,找不到要发送的内容');
  414. }
  415. }
  416. //查看收件箱消息
  417. public function read($id )
  418. {
  419. $detail = $this->model2->detail($id);
  420. if (empty($detail)) {
  421. throw new \think\exception\HttpException(406, '找不到记录');
  422. }
  423. if ($detail['to_uid'] != $this->uid) {
  424. throw new \think\exception\HttpException(406, '找不到记录');
  425. }
  426. MsgList::where(['id' => $id])->update(['read_time' => time()]);
  427. if($detail['message_id']>0){
  428. View::assign('message', $this->model->detail($detail['message_id']));
  429. }
  430. $detail['from_uname'] = Db::name('Admin')->where('id', $detail['from_uid'])->value('name');
  431. View::assign('detail', $detail);
  432. return view();
  433. }
  434. //回复信息
  435. public function reply()
  436. {
  437. $param = get_params();
  438. $msg_id = isset($param['msg_id']) ? $param['msg_id'] : 0;
  439. $detail = $this->model2->detail($msg_id);
  440. if (empty($detail)) {
  441. throw new \think\exception\HttpException(404, '找不到记录');
  442. }
  443. if ($detail['to_uid'] != $this->uid) {
  444. throw new \think\exception\HttpException(406, '无权限回复');
  445. }
  446. $detail['from_name'] = Db::name('Admin')->where('id', $detail['from_uid'])->value('name');
  447. View::assign('detail', $detail);
  448. return view();
  449. }
  450. //转发信息
  451. public function resend()
  452. {
  453. $param = get_params();
  454. $msg_id = isset($param['msg_id']) ? $param['msg_id'] : 0;
  455. $detail = $this->model2->detail($msg_id);
  456. if (empty($detail)) {
  457. throw new \think\exception\HttpException(404, '找不到记录');
  458. }
  459. if ($detail['to_uid'] != $this->uid) {
  460. throw new \think\exception\HttpException(406, '无权限回复');
  461. }
  462. $detail['from_name'] = Db::name('Admin')->where('id', $detail['from_uid'])->value('name');
  463. View::assign('detail', $detail);
  464. return view();
  465. }
  466. //状态修改
  467. public function check()
  468. {
  469. $param = get_params();
  470. $type = empty($param['type']) ? 0 : $param['type'];
  471. $ids = empty($param['ids']) ? 0 : $param['ids'];
  472. $idArray = explode(',', $ids);
  473. foreach ($idArray as $key => $val) {
  474. if ($type==1) { //设置信息为已读
  475. MsgList::where(['id' => $val])->update(['read_time' => time()]);
  476. add_log('view', $val,[],'消息');
  477. }
  478. else if ($type==2) { //信息进入垃圾箱
  479. MsgList::where(['id' => $val])->update(['delete_time' => time()]);
  480. add_log('delete', $val,[],'消息');
  481. }
  482. else if ($type==3) { //信息从垃圾箱恢复
  483. MsgList::where(['id' => $val])->update(['delete_time' => 0]);
  484. add_log('recovery', $val,[],'消息');
  485. }
  486. else if ($type==4) { //信息彻底删除
  487. MsgList::where(['id' => $val])->update(['clear_time' => 0]);
  488. add_log('clear', $val,[],'消息');
  489. }
  490. else if ($type==5) { //星标信息
  491. MsgList::where(['id' => $val])->update(['is_star' => 1]);
  492. add_log('star', $val,[],'消息');
  493. }
  494. else if ($type==6) { //取消星标信息
  495. MsgList::where(['id' => $val])->update(['is_star' => 0]);
  496. add_log('unstar', $val,[],'消息');
  497. }
  498. }
  499. return to_assign(0, '操作成功');
  500. }
  501. //还原消息
  502. public function recovery()
  503. {
  504. $param = get_params();
  505. $table = empty($param['table']) ? '' : $param['table'];
  506. $ids = empty($param['ids']) ? 0 : $param['ids'];
  507. $idArray = explode(',', $ids);
  508. foreach ($idArray as $key => $val) {
  509. Db::name($table)->update(['id' => $val,'delete_time' => 0]);
  510. add_log('recovery', $val,[],'消息');
  511. }
  512. return to_assign(0, '操作成功');
  513. }
  514. //清除消息
  515. public function clear()
  516. {
  517. $param = get_params();
  518. $table = empty($param['table']) ? '' : $param['table'];
  519. $ids = empty($param['ids']) ? 0 : $param['ids'];
  520. $idArray = explode(',', $ids);
  521. foreach ($idArray as $key => $val) {
  522. Db::name($table)->update(['id' => $val,'clear_time' => time()]);
  523. add_log('clear', $val,[],'消息');
  524. }
  525. return to_assign(0, '操作成功');
  526. }
  527. }