WechatTemplate.php 4.7 KB

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