Notice.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace app\admin\controller\notice;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\exception\ValidateException;
  7. /**
  8. * 消息通知
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Notice extends Backend
  13. {
  14. /**
  15. * Notice模型对象
  16. * @var \app\admin\model\notice\Notice
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\notice\Notice;
  23. $this->view->assign("typeList", $this->model->getTypeList());
  24. $this->assignconfig("typeList", $this->model->getTypeList());
  25. $this->view->assign("platformList", $this->model->getPlatformList());
  26. }
  27. public function import()
  28. {
  29. parent::import();
  30. }
  31. /**
  32. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  33. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  34. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  35. */
  36. /**
  37. * 查看
  38. */
  39. public function index()
  40. {
  41. // $noticeParams = [
  42. // 'event' => 'timeout_order',
  43. // 'params' => array (
  44. // 'receiver_admin_ids' => '',
  45. // 'receiver_admin_group_ids' => '1',
  46. // 'user_id' =>1,
  47. // 'order_id' =>54,
  48. // 'orderno' => 'W2307069109',
  49. // 'title' => '123123[巡检计划]',
  50. // 'type' => '巡检工单',
  51. // 'name' => '测试账号密码 ',
  52. // )];
  53. // \Think\Hook::listen('send_notice',$noticeParams);
  54. //当前是否为关联查询
  55. $this->relationSearch = true;
  56. //设置过滤方法
  57. $this->request->filter(['strip_tags', 'trim']);
  58. if ($this->request->isAjax()) {
  59. //如果发送的来源是Selectpage,则转发到Selectpage
  60. if ($this->request->request('keyField')) {
  61. return $this->selectpage();
  62. }
  63. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  64. $list = $this->model
  65. ->with(['noticetemplate'])
  66. ->where($where)
  67. ->order($sort, $order)
  68. ->paginate($limit);
  69. foreach ($list as $row) {
  70. $row->getRelation('noticetemplate')->visible(['id']);
  71. }
  72. $result = array("total" => $list->total(), "rows" => $list->items());
  73. return json($result);
  74. }
  75. return $this->view->fetch();
  76. }
  77. public function add()
  78. {
  79. if ($this->request->isPost()) {
  80. $params = $this->request->post("row/a");
  81. if ($params) {
  82. $params = $this->preExcludeFields($params);
  83. $toIds = '';
  84. if (input('row.platform') == 'user') {
  85. $toIds = input('row.id1');
  86. } else {
  87. $toIds = input('row.id2');
  88. }
  89. if (empty($toIds)) {
  90. $this->error('请选择接收人');
  91. }
  92. $toIds = explode(',', $toIds);
  93. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  94. $params[$this->dataLimitField] = $this->auth->id;
  95. }
  96. $result = false;
  97. Db::startTrans();
  98. try {
  99. //是否采用模型验证
  100. if ($this->modelValidate) {
  101. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  102. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  103. $this->model->validateFailException(true)->validate($validate);
  104. }
  105. foreach ($toIds as $id) {
  106. $params['to_id'] = $id;
  107. $this->model::create($params, true);
  108. }
  109. $result = true;
  110. Db::commit();
  111. } catch (ValidateException $e) {
  112. Db::rollback();
  113. $this->error($e->getMessage());
  114. } catch (PDOException $e) {
  115. Db::rollback();
  116. $this->error($e->getMessage());
  117. } catch (Exception $e) {
  118. Db::rollback();
  119. $this->error($e->getMessage());
  120. }
  121. if ($result !== false) {
  122. $this->success();
  123. } else {
  124. $this->error(__('No rows were inserted'));
  125. }
  126. }
  127. $this->error(__('Parameter %s can not be empty', ''));
  128. }
  129. return $this->view->fetch();
  130. }
  131. }