Message.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. namespace app\home\model;
  14. use think\Model;
  15. use think\facade\Db;
  16. class Message extends Model
  17. {
  18. /**
  19. * 获取发件箱分页列表
  20. * @param $where
  21. * @param $param
  22. */
  23. public function datalist($where, $param)
  24. {
  25. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  26. $order = empty($param['order']) ? 'id desc' : $param['order'];
  27. try {
  28. $list = self::where($where)
  29. ->order($order)
  30. ->paginate(['list_rows'=> $rows])
  31. ->each(function ($item, $key){
  32. $item->send_time_str = empty($item->send_time) ? '草稿' : date('Y-m-d H:i:s', $item->send_time);
  33. if(empty($item->uids)){
  34. $item->to_name = '-';
  35. }
  36. else{
  37. $to_name = Db::name('Admin')->where([['id','in',$item->uids]])->column('name');
  38. $item->to_name = implode(',',$to_name);
  39. }
  40. if(empty($item->dids)){
  41. $item->to_department = '-';
  42. }
  43. else{
  44. $to_department = Db::name('Department')->where([['id','in',$item->dids]])->column('title');
  45. $item->to_department = implode(',',$to_department);
  46. }
  47. if(empty($item->pids)){
  48. $item->to_position = '-';
  49. }
  50. else{
  51. $to_position = Db::name('Position')->where([['id','in',$item->pids]])->column('title');
  52. $item->to_position = implode(',',$to_position);
  53. }
  54. if(empty($item->copy_uids)){
  55. $item->copy_names = '-';
  56. }
  57. else{
  58. $copy_name = Db::name('Admin')->where([['id','in',$item->copy_uids]])->column('name');
  59. $item->copy_names = implode(',',$copy_name);
  60. }
  61. $item['create_time'] = to_date($item['create_time'],'Y-m-d H:i:s');
  62. });
  63. return $list;
  64. } catch(\Exception $e) {
  65. return ['code' => 1, 'data' => [], 'msg' => $e->getMessage()];
  66. }
  67. }
  68. //发件箱消息详情
  69. public function detail($id)
  70. {
  71. $detail = self::find($id);
  72. if(!empty($detail)){
  73. if ($detail['types'] == 1) { //人员
  74. $users = Db::name('Admin')->where('status', 1)->where('id', 'in', $detail['uids'])->column('name');
  75. $detail['unames'] = implode(',',$users);
  76. } elseif ($detail['types'] == 2) { //部门
  77. $departments = Db::name('Department')->where('id', 'in', $detail['dids'])->column('title');
  78. $detail['dnames'] = implode(',',$departments);
  79. } elseif ($detail['types'] == 3) { //角色
  80. $positions = Db::name('Position')->where('id', 'in', $detail['pids'])->column('title');
  81. $detail['pnames'] = implode(',',$positions);
  82. }
  83. $copy_users = Db::name('Admin')->where('status', 1)->where('id', 'in', $detail['copy_uids'])->column('name');
  84. $detail['copy_names'] = implode(',',$copy_users);
  85. //消息附件
  86. $file_array = Db::name('File')->order('create_time desc')->where([['id','in',$detail['file_ids']]])->select()->toArray();
  87. $detail['file_array'] = $file_array;
  88. //引用消息附件
  89. if($detail['msg_id']>0){
  90. $from_msg = Db::name('Msg')->field('content,file_ids,template,action_id')->where(['id' => $detail['msg_id']])->find();
  91. $detail['from_template'] = $from_msg['template'];
  92. $detail['from_action_id'] = $from_msg['action_id'];
  93. $detail['from_content'] = $from_msg['content'];
  94. $detail['from_file_ids'] = $from_msg['file_ids'];
  95. $detail['from_file_array'] = Db::name('File')->order('create_time desc')->where([['id','in',$detail['from_file_ids']]])->select()->toArray();
  96. }
  97. }
  98. return $detail;
  99. }
  100. //发送消息
  101. public function send($id)
  102. {
  103. //查询要发的消息
  104. $msg = self::find($id);
  105. $users = [];
  106. //查询全部收件人
  107. if ($msg['types'] == 1) { //人员
  108. $users = Db::name('Admin')->where('status', 1)->where('id', 'in', $msg['uids'])->column('id');
  109. } elseif ($msg['types'] == 2) { //部门
  110. $users = Db::name('Admin')->where('status', 1)->where('did', 'in', $msg['dids'])->column('id');
  111. } elseif ($msg['types'] == 3) { //角色
  112. $users = Db::name('Admin')->where('status', 1)->where('position_id', 'in', $msg['pids'])->column('id');
  113. } elseif ($msg['types'] == 4) { //全部
  114. $users = Db::name('Admin')->where('status', 1)->column('id');
  115. }
  116. if(!empty($msg['copy_uids'])){
  117. $copy_uids = explode(',',$msg['copy_uids']);
  118. $users_tem = array_merge($users,$copy_uids);
  119. $users = array_unique($users_tem);
  120. }
  121. //组合要发的消息
  122. $send_data = [];
  123. foreach ($users as $key => $value) {
  124. if ($value == $msg['from_uid']) {
  125. continue;
  126. }
  127. $send_data[] = array(
  128. 'message_id' => $msg['id'],//来源发件箱关联id
  129. 'to_uid' => $value,//接收人
  130. 'msg_id' => $msg['msg_id'],//转发或回复消息关联id
  131. 'title' => $msg['title'],
  132. 'content' => $msg['content'],
  133. 'file_ids' => $msg['file_ids'],
  134. 'from_uid' => $msg['from_uid'],//发送人
  135. 'create_time' => time()
  136. );
  137. }
  138. $res = Db::name('Msg')->strict(false)->field(true)->insertAll($send_data);
  139. if ($res!==false) {
  140. //草稿消息变成已发消息
  141. self::where(['id' => $msg['id']])->update(['is_draft' => '1', 'send_time' => time(), 'update_time' => time()]);
  142. add_log('send',$msg['id'],[],'消息');
  143. return true;
  144. } else {
  145. return false;
  146. }
  147. }
  148. }