WechatTemplate.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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\controller\admin\v1\wechat;
  12. use app\controller\admin\AuthController;
  13. use app\jobs\notice\template\WechatTemplateJob;
  14. use app\services\message\SystemNotificationServices;
  15. use app\services\message\TemplateMessageServices;
  16. use crmeb\services\{FormBuilder as Form, template\Template};
  17. use crmeb\services\CacheService;
  18. use think\facade\App;
  19. use think\facade\Route as Url;
  20. use think\Request;
  21. /**
  22. * 微信模板消息
  23. * Class WechatTemplate
  24. * @package app\controller\admin\v1\application\wechat
  25. */
  26. class WechatTemplate extends AuthController
  27. {
  28. protected $cacheTag = 'system_wechat';
  29. /**
  30. * 构造方法
  31. * WechatTemplate constructor.
  32. * @param App $app
  33. * @param TemplateMessageServices $services
  34. */
  35. public function __construct(App $app, TemplateMessageServices $services)
  36. {
  37. parent::__construct($app);
  38. $this->services = $services;
  39. }
  40. /**
  41. * 显示资源列表
  42. *
  43. * @return \think\Response
  44. */
  45. public function index()
  46. {
  47. $where = $this->request->getMore([
  48. ['name', ''],
  49. ['status', '']
  50. ]);
  51. $where['type'] = 1;
  52. $data = $this->services->getTemplateList($where);
  53. $industry = CacheService::get($this->cacheTag . '_wechat_industry', function () {
  54. try {
  55. $cache = (new Template('wechat'))->getIndustry();
  56. if (!$cache) return [];
  57. return $cache->toArray();
  58. } catch (\Exception $e) {
  59. return $e->getMessage();
  60. }
  61. }, 0) ?: [];
  62. !is_array($industry) && $industry = [];
  63. $industry['primary_industry'] = isset($industry['primary_industry']) ? $industry['primary_industry']['first_class'] . ' | ' . $industry['primary_industry']['second_class'] : '未选择';
  64. $industry['secondary_industry'] = isset($industry['secondary_industry']) ? $industry['secondary_industry']['first_class'] . ' | ' . $industry['secondary_industry']['second_class'] : '未选择';
  65. $lst = array(
  66. 'industry' => $industry,
  67. 'count' => $data['count'],
  68. 'list' => $data['list']
  69. );
  70. return $this->success($lst);
  71. }
  72. /**
  73. * 显示创建资源表单页.
  74. *
  75. * @return \think\Response
  76. */
  77. public function create()
  78. {
  79. $f = array();
  80. $f[] = Form::input('tempkey', '模板编号');
  81. $f[] = Form::input('tempid', '模板ID');
  82. $f[] = Form::input('name', '模板名');
  83. $f[] = Form::input('content', '回复内容')->type('textarea');
  84. $f[] = Form::radio('status', '状态', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  85. return $this->makePostForm('添加模板消息', $f, Url::buildUrl('/app/wechat/template'), 'POST');
  86. }
  87. /**
  88. * 保存新建的资源
  89. *
  90. * @param \think\Request $request
  91. * @return \think\Response
  92. */
  93. public function save(Request $request)
  94. {
  95. $data = $this->request->postMore([
  96. 'tempkey',
  97. 'tempid',
  98. 'name',
  99. 'content',
  100. ['status', 0],
  101. ['type', 1]
  102. ]);
  103. if ($data['tempkey'] == '') return $this->fail('请输入模板编号');
  104. if ($data['tempkey'] != '' && $this->services->getOne(['tempkey' => $data['tempkey']]))
  105. return $this->fail('请输入模板编号已存在,请重新输入');
  106. if ($data['tempid'] == '') return $this->fail('请输入模板ID');
  107. if ($data['name'] == '') return $this->fail('请输入模板名');
  108. if ($data['content'] == '') return $this->fail('请输入回复内容');
  109. $data['add_time'] = time();
  110. $this->services->save($data);
  111. return $this->success('添加模板消息成功!');
  112. }
  113. /**
  114. * 显示指定的资源
  115. *
  116. * @param int $id
  117. * @return \think\Response
  118. */
  119. public function read($id)
  120. {
  121. //
  122. }
  123. /**
  124. * 显示编辑资源表单页.
  125. *
  126. * @param int $id
  127. * @return \think\Response
  128. */
  129. public function edit($id)
  130. {
  131. if (!$id) return $this->fail('数据不存在');
  132. $product = $this->services->get($id);
  133. if (!$product) return $this->fail('数据不存在!');
  134. $f = array();
  135. $f[] = Form::input('tempkey', '模板编号', $product->getData('tempkey'))->disabled(1);
  136. $f[] = Form::input('name', '模板名', $product->getData('name'))->disabled(1);
  137. $f[] = Form::input('tempid', '模板ID', $product->getData('tempid'));
  138. $f[] = Form::radio('status', '状态', $product->getData('status'))->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  139. return $this->makePostForm('编辑模板消息', $f, Url::buildUrl('/app/wechat/template/' . $id), 'PUT');
  140. }
  141. /**
  142. * 保存更新的资源
  143. *
  144. * @param \think\Request $request
  145. * @param int $id
  146. * @return \think\Response
  147. */
  148. public function update(Request $request, $id)
  149. {
  150. $data = $this->request->postMore([
  151. 'tempid',
  152. ['status', 0],
  153. ['type', 1]
  154. ]);
  155. if ($data['tempid'] == '') return $this->fail('请输入模板ID');
  156. if (!$id) return $this->fail('数据不存在');
  157. $product = $this->services->get($id);
  158. if (!$product) return $this->fail('数据不存在!');
  159. $this->services->update($id, $data, 'id');
  160. return $this->success('修改成功!');
  161. }
  162. /**
  163. * 删除指定资源
  164. *
  165. * @param int $id
  166. * @return \think\Response
  167. */
  168. public function delete($id)
  169. {
  170. if (!$id) return $this->fail('数据不存在!');
  171. if (!$this->services->delete($id))
  172. return $this->fail('删除失败,请稍候再试!');
  173. else
  174. return $this->success('删除成功!');
  175. }
  176. /**
  177. * 修改状态
  178. * @param $id
  179. * @param $status
  180. * @return mixed
  181. */
  182. public function set_status($id, $status)
  183. {
  184. if ($status == '' || $id == 0) return $this->fail('参数错误');
  185. $this->services->update($id, ['status' => $status], 'id');
  186. return $this->success($status == 0 ? '关闭成功' : '开启成功');
  187. }
  188. /**
  189. * 同步订阅消息
  190. * @return mixed
  191. * @throws \think\db\exception\DataNotFoundException
  192. * @throws \think\db\exception\DbException
  193. * @throws \think\db\exception\ModelNotFoundException
  194. */
  195. public function syncSubscribe()
  196. {
  197. if (!sys_config('wechat_appid') || !sys_config('wechat_appsecret')) {
  198. return $this->fail('请先配置微信公众号appid、appSecret等参数');
  199. }
  200. $tempall = $this->services->getTemplateList(['status' => 1, 'type' => 1]);
  201. $temp = $tempall['list'] ?? [];
  202. if (!$temp) {
  203. return $this->fail('请先检查`eb_template_message`数据完整性,完整sql路径/public/install/crmeb.sql');
  204. }
  205. WechatTemplateJob::dispatch([$temp]);
  206. /** @var SystemNotificationServices $systemNotificationServices */
  207. $systemNotificationServices = app()->make(SystemNotificationServices::class);
  208. //清空模版缓存
  209. $systemNotificationServices->clearTemplateCache();
  210. return $this->success('队列执行中,请稍后查看');
  211. }
  212. }