RoutineTemplate.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace app\admin\controller\routine;
  3. use app\admin\controller\AuthController;
  4. use crmeb\services\CacheService;
  5. use crmeb\services\FormBuilder as Form;
  6. use crmeb\services\UtilService as Util;
  7. use crmeb\services\JsonService as Json;
  8. use think\facade\Route as Url;
  9. use app\admin\model\routine\RoutineTemplate as RoutineTemplateModel;
  10. /**
  11. * 小程序模板消息控制器
  12. * Class RoutineTemplate
  13. * @package app\admin\controller\routine
  14. */
  15. class RoutineTemplate extends AuthController
  16. {
  17. public function index()
  18. {
  19. $where = Util::getMore([
  20. ['name', ''],
  21. ['status', '']
  22. ], $this->request);
  23. $this->assign('where', $where);
  24. $this->assign(RoutineTemplateModel::SystemPage($where));
  25. return $this->fetch();
  26. }
  27. /**
  28. * 添加模板消息
  29. * @return mixed
  30. */
  31. public function create()
  32. {
  33. $f = array();
  34. $f[] = Form::input('tempkey', '模板编号');
  35. $f[] = Form::input('tempid', '模板ID');
  36. $f[] = Form::input('name', '模板名');
  37. $f[] = Form::input('content', '回复内容')->type('textarea');
  38. $f[] = Form::radio('status', '状态', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  39. $form = Form::make_post_form('添加模板消息', $f, Url::buildUrl('save'));
  40. $this->assign(compact('form'));
  41. return $this->fetch('public/form-builder');
  42. }
  43. public function save()
  44. {
  45. $data = Util::postMore([
  46. 'tempkey',
  47. 'tempid',
  48. 'name',
  49. 'content',
  50. ['status', 0]
  51. ]);
  52. if ($data['tempkey'] == '') return Json::fail('请输入模板编号');
  53. if ($data['tempkey'] != '' && RoutineTemplateModel::be($data['tempkey'], 'tempkey'))
  54. return Json::fail('请输入模板编号已存在,请重新输入');
  55. if ($data['tempid'] == '') return Json::fail('请输入模板ID');
  56. if ($data['name'] == '') return Json::fail('请输入模板名');
  57. if ($data['content'] == '') return Json::fail('请输入回复内容');
  58. $data['add_time'] = time();
  59. $data['type'] = 0;
  60. RoutineTemplateModel::create($data);
  61. CacheService::clear();
  62. return Json::successful('添加模板消息成功!');
  63. }
  64. /**
  65. * 编辑模板消息
  66. * @param $id
  67. * @return mixed|\think\response\Json|void
  68. */
  69. public function edit($id)
  70. {
  71. if (!$id) return $this->failed('数据不存在');
  72. $product = RoutineTemplateModel::get($id);
  73. if (!$product) return Json::fail('数据不存在!');
  74. $f = array();
  75. $f[] = Form::input('tempkey', '模板编号', $product->getData('tempkey'))->disabled(1);
  76. $f[] = Form::input('name', '模板名', $product->getData('name'))->disabled(1);
  77. $f[] = Form::input('tempid', '模板ID', $product->getData('tempid'));
  78. $f[] = Form::radio('status', '状态', $product->getData('status'))->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  79. $form = Form::make_post_form('编辑模板消息', $f, Url::buildUrl('update', compact('id')));
  80. $this->assign(compact('form'));
  81. return $this->fetch('public/form-builder');
  82. }
  83. public function update($id)
  84. {
  85. $data = Util::postMore([
  86. 'tempid',
  87. ['status', 0]
  88. ]);
  89. if ($data['tempid'] == '') return Json::fail('请输入模板ID');
  90. if (!$id) return $this->failed('数据不存在');
  91. $product = RoutineTemplateModel::get($id);
  92. if (!$product) return Json::fail('数据不存在!');
  93. RoutineTemplateModel::edit($data, $id);
  94. CacheService::clear();
  95. return Json::successful('修改成功!');
  96. }
  97. /**
  98. * 删除模板消息
  99. * @param $id
  100. * @return \think\response\Json
  101. */
  102. public function delete($id)
  103. {
  104. if (!$id) return Json::fail('数据不存在!');
  105. if (!RoutineTemplateModel::del($id))
  106. return Json::fail(RoutineTemplateModel::getErrorInfo('删除失败,请稍候再试!'));
  107. else {
  108. CacheService::clear();
  109. return Json::successful('删除成功!');
  110. }
  111. }
  112. }