TemplateMessage.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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\wechat;
  12. use think\App;
  13. use crmeb\basic\BaseController;
  14. use app\validate\admin\TemplateMessageValidate;
  15. use app\common\repositories\wechat\TemplateMessageRepository;
  16. use think\exception\ValidateException;
  17. /**
  18. * Class TemplateMessage
  19. * app\controller\admin\wechat
  20. * 微信模板消息 - 弃用
  21. */
  22. class TemplateMessage extends BaseController
  23. {
  24. /**
  25. * @var TemplateMessageRepository
  26. */
  27. protected $repository;
  28. /**
  29. * TemplateMessage constructor.
  30. * @param App $app
  31. * @param TemplateMessageRepository $repository
  32. */
  33. public function __construct(App $app, TemplateMessageRepository $repository)
  34. {
  35. parent::__construct($app);
  36. $this->repository = $repository;
  37. }
  38. /**
  39. * 模板消息列表
  40. * @return mixed
  41. * @author Qinii
  42. * @day 2020-06-18
  43. */
  44. public function lst()
  45. {
  46. [$page, $limit] = $this->getPage();
  47. $where = $this->request->params(['status', 'keyword']);
  48. $where['type'] = 1;
  49. return app('json')->success($this->repository->getList($where, $page, $limit));
  50. }
  51. /**
  52. * 小程序模板消息列表
  53. * @return mixed
  54. * @author Qinii
  55. * @day 2020-06-18
  56. */
  57. public function minList()
  58. {
  59. [$page, $limit] = $this->getPage();
  60. $where = $this->request->params(['status', 'keyword']);
  61. $where['type'] = 0;
  62. return app('json')->success($this->repository->getList($where, $page, $limit));
  63. }
  64. /**
  65. * 创建表单
  66. * @return mixed
  67. * @author Qinii
  68. * @day 2020-06-18
  69. */
  70. public function createForm()
  71. {
  72. return app('json')->success(formToData($this->repository->form(null, 1)));
  73. }
  74. /**
  75. * 创建表单
  76. * @param $type
  77. * @return mixed
  78. * @author Qinii
  79. * @day 2020-06-19
  80. */
  81. public function createMinForm()
  82. {
  83. return app('json')->success(formToData($this->repository->form(null, 0)));
  84. }
  85. /**
  86. * 新增数据
  87. * @param TemplateMessageValidate $validate
  88. * @return mixed
  89. * @author Qinii
  90. * @day 2020-06-18
  91. */
  92. public function create(TemplateMessageValidate $validate)
  93. {
  94. $data = $this->chekcParams($validate);
  95. $this->repository->create($data);
  96. return app('json')->success('添加成功');
  97. }
  98. /**
  99. * 修改表单
  100. * @param $id
  101. * @return mixed
  102. * @author Qinii
  103. * @day 2020-06-18
  104. */
  105. public function updateForm($id)
  106. {
  107. if (!$this->repository->getWhereCount(['template_id' => $id]))
  108. return app('json')->fail('数据不存在');
  109. return app('json')->success(formToData($this->repository->updateForm($id)));
  110. }
  111. /**
  112. * 更新
  113. * @param $id
  114. * @return mixed
  115. * @author Qinii
  116. * @day 2020-06-18
  117. */
  118. public function update($id)
  119. {
  120. $data = $this->request->params(['tempid', 'status']);
  121. if (!$data['tempid'])
  122. return app('json')->fail('请填写模板ID');
  123. if (!$this->repository->getWhereCount(['template_id' => $id]))
  124. return app('json')->fail('数据不存在');
  125. $this->repository->update($id, $data);
  126. return app('json')->success('编辑成功');
  127. }
  128. /**
  129. * 删除
  130. * @param $id
  131. * @return mixed
  132. * @author Qinii
  133. * @day 2020-06-18
  134. */
  135. public function delete($id)
  136. {
  137. if (!$this->repository->getWhereCount(['template_id' => $id]))
  138. return app('json')->fail('数据不存在');
  139. $this->repository->delete($id);
  140. return app('json')->success('删除成功');
  141. }
  142. /**
  143. * 切换状态
  144. * @param $id
  145. * @return mixed
  146. * @author Qinii
  147. * @day 2020-06-19
  148. */
  149. public function switchStatus($id)
  150. {
  151. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  152. if (!$this->repository->getWhereCount(['template_id' => $id]))
  153. return app('json')->fail('数据不存在');
  154. $this->repository->update($id, ['status' => $status]);
  155. return app('json')->success('修改成功');
  156. }
  157. /**
  158. * 验证参数
  159. * @param TemplateMessageValidate $validate
  160. * @return array
  161. * @author Qinii
  162. * @day 2020-06-18
  163. */
  164. public function chekcParams(TemplateMessageValidate $validate)
  165. {
  166. $data = $this->request->params(['tempkey', 'name', 'tempid', 'status', 'content', 'type']);
  167. $validate->check($data);
  168. return $data;
  169. }
  170. /**
  171. * 同步模板消息
  172. * @return \think\response\Json
  173. * @throws \think\db\exception\DataNotFoundException
  174. * @throws \think\db\exception\DbException
  175. * @throws \think\db\exception\ModelNotFoundException
  176. * @author wuhaotian
  177. * @email 442384644@qq.com
  178. * @date 2024/7/25
  179. */
  180. public function sync()
  181. {
  182. $type = $this->request->param('type');
  183. if ($type) {
  184. $msg = $this->repository->syncWechatSubscribe();
  185. } else {
  186. $msg = $this->repository->syncMinSubscribe();
  187. }
  188. return app('json')->success($msg);
  189. }
  190. }