WechatTemplate.php 5.3 KB

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