RoutineTemplate.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\wechat;
  12. use app\admin\controller\AuthController;
  13. use service\FormBuilder as Form;
  14. use service\JsonService as Json;
  15. use service\WechatTemplateService;
  16. use service\RoutineTemplateService;
  17. use think\Cache;
  18. use think\Request;
  19. use think\Url;
  20. use app\admin\model\wechat\RoutineTemplate as RoutineTemplateModel;
  21. use app\admin\model\system\SystemConfig;
  22. /**
  23. * 微信订阅消息控制器
  24. * Class RoutineTemplate
  25. * @package app\admin\controller\wechat
  26. */
  27. class RoutineTemplate extends AuthController
  28. {
  29. protected $cacheTag = '_system_routine_wechat';
  30. public function index()
  31. {
  32. $where = parent::getMore([
  33. ['name', ''],
  34. ['status', '']
  35. ], $this->request);
  36. $this->assign('where', $where);
  37. $this->assign(RoutineTemplateModel::SystemPage($where));
  38. try {
  39. $industry = Cache::tag($this->cacheTag)->remember('_wechat_routine_industry', function () {
  40. $cache = RoutineTemplateService::getCategory();
  41. if (!$cache) return [];
  42. Cache::tag($this->cacheTag, ['_wechat_routine_industry']);
  43. return $cache;
  44. }, 0) ?: [];
  45. } catch (\Exception $e) {
  46. $industry = [];
  47. }
  48. !is_array($industry) && $industry = [];
  49. $this->assign('industry', $industry);
  50. return $this->fetch();
  51. }
  52. /**
  53. * 添加模板消息
  54. * @return mixed
  55. */
  56. public function create()
  57. {
  58. $f = array();
  59. $f[] = Form::input('tempkey', '模板编号');
  60. $f[] = Form::input('tempid', '模板ID');
  61. $f[] = Form::input('name', '模板名');
  62. $f[] = Form::input('content', '回复内容')->type('textarea');
  63. $f[] = Form::radio('status', '状态', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  64. $form = Form::make_post_form('添加模板消息', $f, Url::build('save'), 2);
  65. $this->assign(compact('form'));
  66. return $this->fetch('public/form-builder');
  67. }
  68. public function save(Request $request)
  69. {
  70. $data = parent::postMore([
  71. 'tempkey',
  72. 'tempid',
  73. 'name',
  74. 'content',
  75. ['status', 0]
  76. ], $request);
  77. if ($data['tempkey'] == '') return Json::fail('请输入模板编号');
  78. if ($data['tempkey'] != '' && RoutineTemplateModel::be($data['tempkey'], 'tempkey'))
  79. return Json::fail('请输入模板编号已存在,请重新输入');
  80. if ($data['tempid'] == '') return Json::fail('请输入模板ID');
  81. if ($data['name'] == '') return Json::fail('请输入模板名');
  82. if ($data['content'] == '') return Json::fail('请输入回复内容');
  83. $data['add_time'] = time();
  84. RoutineTemplateModel::set($data);
  85. return Json::successful('添加模板消息成功!');
  86. }
  87. /**
  88. * 编辑模板消息
  89. * @param $id
  90. * @return mixed|\think\response\Json|void
  91. */
  92. public function edit($id)
  93. {
  94. if (!$id) return $this->failed('数据不存在');
  95. $product = RoutineTemplateModel::get($id);
  96. if (!$product) return Json::fail('数据不存在!');
  97. $f = array();
  98. $f[] = Form::input('tempkey', '模板编号', $product->getData('tempkey'))->disabled(1);
  99. $f[] = Form::input('name', '模板名', $product->getData('name'))->disabled(1);
  100. $f[] = Form::input('tempid', '模板ID', $product->getData('tempid'));
  101. $f[] = Form::radio('status', '状态', $product->getData('status'))->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  102. $form = Form::make_post_form('编辑模板消息', $f, Url::build('update', compact('id')), 2);
  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. 'tempid',
  110. ['status', 0]
  111. ], $request);
  112. if ($data['tempid'] == '') return Json::fail('请输入模板ID');
  113. if (!$id) return $this->failed('数据不存在');
  114. $product = RoutineTemplateModel::get($id);
  115. if (!$product) return Json::fail('数据不存在!');
  116. RoutineTemplateModel::edit($data, $id);
  117. return Json::successful('修改成功!');
  118. }
  119. /**
  120. * 删除模板消息
  121. * @param $id
  122. * @return \think\response\Json
  123. */
  124. public function delete($id)
  125. {
  126. if (!$id) return Json::fail('数据不存在!');
  127. if (!RoutineTemplateModel::del($id))
  128. return Json::fail(RoutineTemplateModel::getErrorInfo('删除失败,请稍候再试!'));
  129. else
  130. return Json::successful('删除成功!');
  131. }
  132. }