WechatTemplate.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace app\adminapi\controller\v1\application\wechat;
  3. use app\models\wechat\WechatTemplate as WechatTemplateModel;
  4. use app\adminapi\controller\AuthController;
  5. use crmeb\services\{
  6. FormBuilder as Form, UtilService as Util, WechatTemplateService
  7. };
  8. use think\facade\Route as Url;
  9. use think\Request;
  10. use think\facade\Cache;
  11. class WechatTemplate extends AuthController
  12. {
  13. protected $cacheTag = '_system_wechat';
  14. /**
  15. * 显示资源列表
  16. *
  17. * @return \think\Response
  18. */
  19. public function index()
  20. {
  21. $where = Util::getMore([
  22. ['page', 1],
  23. ['limit', 20],
  24. ['name', ''],
  25. ['status', '']
  26. ], $this->request);
  27. $data = WechatTemplateModel::SystemPage($where);
  28. $industry = Cache::tag($this->cacheTag)->remember('_wechat_industry', function () {
  29. try {
  30. $cache = WechatTemplateService::getIndustry();
  31. if (!$cache) return [];
  32. Cache::tag($this->cacheTag, ['_wechat_industry']);
  33. return $cache->toArray();
  34. } catch (\Exception $e) {
  35. return $e->getMessage();
  36. }
  37. }, 0) ?: [];
  38. !is_array($industry) && $industry = [];
  39. $industry['primary_industry'] = isset($industry['primary_industry']) ? $industry['primary_industry']['first_class'] . ' | ' . $industry['primary_industry']['second_class'] : '未选择';
  40. $industry['secondary_industry'] = isset($industry['secondary_industry']) ? $industry['secondary_industry']['first_class'] . ' | ' . $industry['secondary_industry']['second_class'] : '未选择';
  41. $lst = array(
  42. 'industry' => $industry,
  43. 'count' => $data['count'],
  44. 'list' => $data['list']
  45. );
  46. return $this->success($lst);
  47. }
  48. /**
  49. * 显示创建资源表单页.
  50. *
  51. * @return \think\Response
  52. */
  53. public function create()
  54. {
  55. $f = array();
  56. $f[] = Form::input('tempkey', '模板编号');
  57. $f[] = Form::input('tempid', '模板ID');
  58. $f[] = Form::input('name', '模板名');
  59. $f[] = Form::input('content', '回复内容')->type('textarea');
  60. $f[] = Form::radio('status', '状态', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  61. return $this->makePostForm('添加模板消息', $f, Url::buildUrl('/app/wechat/template'), 'POST');
  62. }
  63. /**
  64. * 保存新建的资源
  65. *
  66. * @param \think\Request $request
  67. * @return \think\Response
  68. */
  69. public function save(Request $request)
  70. {
  71. $data = Util::postMore([
  72. 'tempkey',
  73. 'tempid',
  74. 'name',
  75. 'content',
  76. ['status', 0],
  77. ['type', 1]
  78. ]);
  79. if ($data['tempkey'] == '') return $this->fail('请输入模板编号');
  80. if ($data['tempkey'] != '' && WechatTemplateModel::be($data['tempkey'], 'tempkey'))
  81. return $this->fail('请输入模板编号已存在,请重新输入');
  82. if ($data['tempid'] == '') return $this->fail('请输入模板ID');
  83. if ($data['name'] == '') return $this->fail('请输入模板名');
  84. if ($data['content'] == '') return $this->fail('请输入回复内容');
  85. $data['add_time'] = time();
  86. WechatTemplateModel::create($data);
  87. return $this->success('添加模板消息成功!');
  88. }
  89. /**
  90. * 显示指定的资源
  91. *
  92. * @param int $id
  93. * @return \think\Response
  94. */
  95. public function read($id)
  96. {
  97. //
  98. }
  99. /**
  100. * 显示编辑资源表单页.
  101. *
  102. * @param int $id
  103. * @return \think\Response
  104. */
  105. public function edit($id)
  106. {
  107. if (!$id) return $this->fail('数据不存在');
  108. $product = WechatTemplateModel::get($id);
  109. if (!$product) return $this->fail('数据不存在!');
  110. $f = array();
  111. $f[] = Form::input('tempkey', '模板编号', $product->getData('tempkey'))->disabled(1);
  112. $f[] = Form::input('name', '模板名', $product->getData('name'))->disabled(1);
  113. $f[] = Form::input('tempid', '模板ID', $product->getData('tempid'));
  114. $f[] = Form::radio('status', '状态', $product->getData('status'))->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  115. return $this->makePostForm('编辑模板消息', $f, Url::buildUrl('/app/wechat/template/' . $id), 'PUT');
  116. }
  117. /**
  118. * 保存更新的资源
  119. *
  120. * @param \think\Request $request
  121. * @param int $id
  122. * @return \think\Response
  123. */
  124. public function update(Request $request, $id)
  125. {
  126. $data = Util::postMore([
  127. 'tempid',
  128. ['status', 0],
  129. ['type', 1]
  130. ]);
  131. if ($data['tempid'] == '') return $this->fail('请输入模板ID');
  132. if (!$id) return $this->fail('数据不存在');
  133. $product = WechatTemplateModel::get($id);
  134. if (!$product) return $this->fail('数据不存在!');
  135. WechatTemplateModel::edit($data, $id);
  136. return $this->success('修改成功!');
  137. }
  138. /**
  139. * 删除指定资源
  140. *
  141. * @param int $id
  142. * @return \think\Response
  143. */
  144. public function delete($id)
  145. {
  146. if (!$id) return $this->fail('数据不存在!');
  147. if (!WechatTemplateModel::del($id))
  148. return $this->fail(WechatTemplateModel::getErrorInfo('删除失败,请稍候再试!'));
  149. else
  150. return $this->success('删除成功!');
  151. }
  152. /**
  153. * 修改状态
  154. * @param $id
  155. * @param $status
  156. * @return mixed
  157. */
  158. public function set_status($id, $status)
  159. {
  160. if ($status == '' || $id == 0) return $this->fail('参数错误');
  161. WechatTemplateModel::where(['id' => $id])->update(['status' => $status]);
  162. return $this->success($status == 0 ? '关闭成功' : '开启成功');
  163. }
  164. }