SystemNotice.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\setting;
  12. use app\admin\controller\AuthController;
  13. use service\FormBuilder as Form;
  14. use app\admin\model\system\SystemAdmin;
  15. use app\admin\model\system\SystemNotice as NoticeModel;
  16. use service\JsonService;
  17. use think\Request;
  18. use think\Url;
  19. /**
  20. * 管理员消息通知 控制器
  21. * Class SystemNotice
  22. * @package app\admin\controller\system
  23. */
  24. class SystemNotice extends AuthController
  25. {
  26. /**
  27. * 展示管理员列表
  28. * @return mixed
  29. */
  30. public function index()
  31. {
  32. $this->assign(NoticeModel::page(function ($notice) {
  33. $notice->push_admin_name = !empty($notice->push_admin) ? implode(',', SystemAdmin::where('id', 'IN', $notice->push_admin)->column('real_name')) : '';
  34. }));
  35. return $this->fetch();
  36. }
  37. public function create()
  38. {
  39. $f = array();
  40. $f[] = Form::input('title', '通知标题');
  41. $f[] = Form::input('type', '通知类型');
  42. $f[] = Form::frameInputOne('icon', '图标', Url::build('admin/widget.widgets/icon', array('fodder' => 'icon')))->icon('ionic');
  43. $f[] = Form::input('template', '通知模板');
  44. $f[] = Form::input('table_title', '通知数据')->type('textarea')->placeholder('数据1-key1,数据2-key2');
  45. $f[] = Form::select('push_admin', '通知管理员')->setOptions(function () {
  46. $list = SystemAdmin::getOrdAdmin('real_name,id') ?: [];
  47. $options = [];
  48. foreach ($list as $admin) {
  49. $options[] = ['label' => $admin['real_name'], 'value' => $admin['id']];
  50. }
  51. return $options;
  52. })->multiple(1);
  53. $f[] = Form::radio('status', '状态', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  54. $form = Form::make_post_form('添加通知模板', $f, Url::build('save'));
  55. $this->assign(compact('form'));
  56. return $this->fetch('public/form-builder');
  57. }
  58. public function save(Request $request)
  59. {
  60. $data = parent::postMore([
  61. 'title', 'type', 'icon', 'template', 'table_title',
  62. ['push_admin', []], ['status', 0]
  63. ], $request);
  64. $data['push_admin'] = array_unique(array_filter($data['push_admin']));
  65. if (!$data['template']) return $this->failed('请填写通知模板');
  66. if (!$data['title']) return $this->failed('请输入模板标题');
  67. if (!$data['type']) return $this->failed('请输入模板类型');
  68. if (NoticeModel::set($data))
  69. return $this->successful('添加通知成功');
  70. else
  71. return $this->failed('添加失败!');
  72. }
  73. /**编辑通知模板
  74. * @param $id
  75. * @return mixed|void
  76. */
  77. public function edit($id)
  78. {
  79. $data = NoticeModel::get($id);
  80. if (!$data) return JsonService::fail('数据不存在!');
  81. $data->tableTitle = implode(',', array_map(function ($value) {
  82. return $value['title'] . '-' . $value['key'];
  83. }, $data->table_title));
  84. $data->tableTitleStr = implode(',', array_map(function ($value) {
  85. return $value['title'] . '-' . $value['key'];
  86. }, $data->table_title));
  87. $f = array();
  88. $f[] = Form::input('title', '通知标题', $data->title);
  89. $f[] = Form::input('type', '通知类型', $data->type);
  90. $f[] = Form::frameInputOne('icon', '图标', Url::build('admin/widget.widgets/icon', array('fodder' => 'icon')), $data->icon)->icon('ionic');
  91. $f[] = Form::input('template', '通知模板', $data->template);
  92. $f[] = Form::input('table_title', '通知数据', $data->tableTitleStr)->type('textarea')->placeholder('数据1-key1,数据2-key2');
  93. $f[] = Form::select('push_admin', '通知管理员', $data->push_admin)->setOptions(function () {
  94. $list = SystemAdmin::getOrdAdmin('real_name,id') ?: [];
  95. $options = [];
  96. foreach ($list as $admin) {
  97. $options[] = ['label' => $admin['real_name'], 'value' => $admin['id']];
  98. }
  99. return $options;
  100. })->multiple(1);
  101. $f[] = Form::radio('status', '状态', $data->status)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  102. $form = Form::make_post_form('编辑通知模板', $f, Url::build('update', array('id' => $id)));
  103. $this->assign(compact('form'));
  104. return $this->fetch('public/form-builder');
  105. }
  106. public function update(Request $request, $id)
  107. {
  108. $data = parent::postMore([
  109. 'title', 'type', 'icon', 'template', 'table_title',
  110. ['push_admin', []], ['status', 0]
  111. ], $request);
  112. $data['push_admin'] = array_unique(array_filter($data['push_admin']));
  113. if (!$data['template']) return $this->failed('请填写通知模板');
  114. if (!$data['title']) return $this->failed('请输入模板标题');
  115. if (!$data['type']) return $this->failed('请输入模板类型');
  116. NoticeModel::edit($data, $id);
  117. return $this->successful('修改成功!');
  118. }
  119. /**
  120. * 删除指定资源
  121. *
  122. * @param int $id
  123. * @return \think\Response
  124. */
  125. public function delete($id)
  126. {
  127. $res = NoticeModel::del($id);
  128. if (!$res)
  129. return $this->failed(('删除失败,请稍候再试!'));
  130. else
  131. return $this->successful('删除成功!');
  132. }
  133. public function message($type = 'all')
  134. {
  135. return $this->fetch();
  136. }
  137. }