Tomessages.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 听雨 < 389625819@qq.com >
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller;
  12. use app\admin\controller\base\Permissions;
  13. use app\common\model\Messages;
  14. use think\Db;
  15. use think\Log;
  16. class Tomessages extends Permissions
  17. {
  18. public function index()
  19. {
  20. if ($this->request->isAjax()) {
  21. $post = $this->request->param();
  22. $where = [];
  23. if (isset($post['id']) and $post['id'] != '') {
  24. $where['id'] = $post['id'];
  25. }
  26. if (isset($post['from_user_id']) and $post['from_user_id'] != '') {
  27. $where['from_user_id'] = $post['from_user_id'];
  28. }
  29. if (isset($post['to_user_id']) and $post['to_user_id'] != '') {
  30. $where['to_user_id'] = $post['to_user_id'];
  31. }
  32. if (isset($post['keywords']) and !empty($post['keywords'])) {
  33. $where['message'] = ['like', '%' . $post['keywords'] . '%'];
  34. }
  35. if (isset($post['ip']) and $post['ip'] != '') {
  36. $where['ip'] = $post['ip'];
  37. }
  38. if (isset($post['is_look']) and ($post['is_look'] == 1 or $post['is_look'] === '0')) {
  39. $where['is_look'] = $post['is_look'];
  40. }
  41. if (isset($post['status']) and in_array($post['status'], ["0", "1", "-1"], true)) {
  42. $where['status'] = $post['status'];
  43. }
  44. if (isset($post['create_time']) and !empty($post['create_time'])) {
  45. $timerang = explode(' - ', $post['create_time']);
  46. $min_time = strtotime($timerang[0]);
  47. $max_time = strtotime($timerang[1]);
  48. $where['create_time'] = [['>=', $min_time], ['<=', $max_time]];
  49. }
  50. $model = new Messages();
  51. $count = $model->where($where)->count();
  52. $data = $model->where($where)->page($post['page']??0, $post['limit']??15)->order('create_time desc')->select();
  53. foreach ($data as $k => $v) {
  54. $v['from_user'] = $v->fromUser->nickname??($v->from_user_id ?: "系统");
  55. $v['to_user'] = $v->toUser->nickname??($v->to_user_id ?: '');
  56. $v['status_text'] = $v->status_text;
  57. $data[$k] = $v;
  58. }
  59. return array('code' => 0, 'count' => $count, 'data' => $data);
  60. } else {
  61. return $this->fetch();
  62. }
  63. }
  64. public function publish()
  65. {
  66. $id = $this->request->param('id', 0, 'intval');
  67. $post = $this->request->post();
  68. if ($this->request->isPost()) {
  69. $validate = new \think\Validate([
  70. ['from_user_id|发送者id', 'number'],
  71. ['to_user_id|接收者id', 'number'],
  72. ['message', 'require', '消息不能为空'],
  73. ]);
  74. if (!$validate->check($post)) {
  75. $this->error('提交失败:' . $validate->getError());
  76. }
  77. }
  78. $model = new Messages();
  79. if ($id > 0) {
  80. //是修改操作
  81. $link = $model->where('id', $id)->find();
  82. if (empty($link)) {
  83. $this->error('记录id不存在');
  84. }
  85. if ($this->request->isPost()) {
  86. if (false == $model->allowField(true)->save($post, ['id' => $id])) {
  87. $this->error('修改失败');
  88. } else {
  89. $this->success('修改成功', 'index');
  90. }
  91. } else {
  92. $this->assign('link', $link);
  93. return $this->fetch();
  94. }
  95. } else {
  96. //是新增操作
  97. if ($this->request->isPost()) {
  98. $post['ip'] = $this->request->ip();
  99. $post['status'] = Messages::STATUS_PASS;
  100. if (false == $model->allowField(true)->save($post)) {
  101. $this->error('失败');
  102. } else {
  103. $this->success('发送成功,请查看消息管理', 'index');
  104. }
  105. } else {
  106. return $this->fetch();
  107. }
  108. }
  109. }
  110. //批量回复
  111. public function sysmessage()
  112. {
  113. if ($this->request->isAjax()) {
  114. $post = $this->request->param();
  115. $ids = $post['ids'];
  116. $model = new Messages();
  117. $list = $model->where('id', 'in', $ids)->select();
  118. foreach ($list as $item) {
  119. $model = new Messages();
  120. $data = [
  121. 'from_user_id' => 0,
  122. 'to_user_id' => $item->from_user_id,
  123. 'to_msg_id' => $item->id,
  124. 'ip' => $this->request->ip(),
  125. 'message' => $post['message'],
  126. 'status' => Messages::STATUS_PASS,
  127. ];
  128. if (false == $model->allowField(true)->save($data)) {
  129. Log::log("回复失败:" . $item->from_user_id);
  130. }
  131. }
  132. $this->success('回复成功');
  133. }
  134. }
  135. public function look()
  136. {
  137. if ($this->request->isPost()) {
  138. $id = $this->request->param('id', 0, 'intval');
  139. $model = new Messages();
  140. if ($id > 0) {
  141. $post = $this->request->post();
  142. //验证
  143. $message = $model->where('id', $id)->find();
  144. if (empty($message)) {
  145. $this->error('id不正确');
  146. }
  147. $post['is_look'] = $post['status'];
  148. if (false == $model->allowField(true)->save($post, ['id' => $id])) {
  149. $this->error('提交失败');
  150. } else {
  151. $this->success('提交成功', 'admin/tomessages/index');
  152. }
  153. }
  154. }
  155. }
  156. public function status()
  157. {
  158. if ($this->request->isPost()) {
  159. $post = $this->request->post();
  160. if (false == Db::name('messages')->where('id', $post['id'])->update(['status' => $post['status']])) {
  161. $this->error('设置失败');
  162. } else {
  163. $this->success('设置成功', 'index');
  164. }
  165. }
  166. }
  167. public function delete()
  168. {
  169. if ($this->request->isAjax()) {
  170. $id = $this->request->param('id', 0, 'intval');
  171. if (false == Db::name('messages')->where('id', $id)->delete()) {
  172. $this->error('删除失败');
  173. } else {
  174. $this->success('删除成功', 'index');
  175. }
  176. }
  177. }
  178. public function deletes()
  179. {
  180. if ($this->request->isAjax()) {
  181. $post = $this->request->param();
  182. $ids = $post['ids'];
  183. $model = new Messages();
  184. if ($model->where('id', 'in', $ids)->delete()) {
  185. $this->success('删除成功');
  186. }
  187. }
  188. }
  189. }