Message.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. namespace app\user\admin;
  10. use app\admin\controller\Admin;
  11. use app\common\builder\ZBuilder;
  12. use app\user\model\Message as MessageModel;
  13. use app\user\model\User as UserModel;
  14. use app\user\model\Role as RoleModel;
  15. /**
  16. * 消息控制器
  17. * @package app\user\admin
  18. */
  19. class Message extends Admin
  20. {
  21. /**
  22. * 消息列表
  23. * @author 蔡伟明 <314013107@qq.com>
  24. * @return mixed
  25. * @throws \think\Exception
  26. * @throws \think\exception\DbException
  27. */
  28. public function index()
  29. {
  30. $data_list = MessageModel::where($this->getMap())
  31. ->order($this->getOrder('id DESC'))
  32. ->paginate();
  33. return ZBuilder::make('table')
  34. ->setTableName('admin_message')
  35. ->addTopButton('add')
  36. ->addTopButton('delete')
  37. ->addRightButton('edit')
  38. ->addRightButton('delete')
  39. ->addColumns([
  40. ['id', 'ID'],
  41. ['uid_receive', '接收者', 'callback', 'get_nickname'],
  42. ['uid_send', '发送者', 'callback', 'get_nickname'],
  43. ['type', '分类'],
  44. ['content', '内容'],
  45. ['status', '状态', 'status', '', ['未读', '已读']],
  46. ['create_time', '发送时间', 'datetime'],
  47. ['read_time', '阅读时间', 'datetime'],
  48. ['right_button', '操作', 'btn'],
  49. ])
  50. ->addFilter('type')
  51. ->addFilter('status', ['未读', '已读'])
  52. ->setRowList($data_list)
  53. ->fetch();
  54. }
  55. /**
  56. * 新增
  57. * @author 蔡伟明 <314013107@qq.com>
  58. * @return mixed
  59. * @throws \think\Exception
  60. */
  61. public function add()
  62. {
  63. if ($this->request->isPost()) {
  64. $data = $this->request->post();
  65. $data['type'] == '' && $this->error('请填写消息分类');
  66. $data['content'] == '' && $this->error('请填写消息内容');
  67. $list = [];
  68. if ($data['send_type'] == 'uid') {
  69. !isset($data['uid']) && $this->error('请选择接收消息的用户');
  70. } else {
  71. !isset($data['role']) && $this->error('请选择接收消息的角色');
  72. $data['uid'] = UserModel::where('status', 1)
  73. ->where('role', 'in', $data['role'])
  74. ->column('id');
  75. !$data['uid'] && $this->error('所选角色无可发送的用户');
  76. }
  77. foreach ($data['uid'] as $uid) {
  78. $list[] = [
  79. 'uid_receive' => $uid,
  80. 'uid_send' => UID,
  81. 'type' => $data['type'],
  82. 'content' => $data['content'],
  83. ];
  84. }
  85. $MessageModel = new MessageModel;
  86. if (false !== $MessageModel->saveAll($list)) {
  87. $this->success('新增成功', 'index');
  88. } else {
  89. $this->error('新增失败');
  90. }
  91. }
  92. return ZBuilder::make('form')
  93. ->addFormItems([
  94. ['text', 'type', '消息分类'],
  95. ['textarea', 'content', '消息内容'],
  96. ['radio', 'send_type', '发送方式', '', ['uid' => '按指定用户', 'role' => '按指定角色'], 'uid'],
  97. ['select', 'uid', '接收用户', '接收消息的用户', UserModel::where('status', 1)->column('id,nickname'), '', 'multiple'],
  98. ['select', 'role', '接收角色', '接收消息的角色', RoleModel::where('status', 1)->column('id,name'), '', 'multiple'],
  99. ])
  100. ->setTrigger('send_type', 'uid', 'uid')
  101. ->setTrigger('send_type', 'role', 'role')
  102. ->fetch();
  103. }
  104. }