SystemNotice.php 5.3 KB

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