TemplateMessage.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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. class TemplateMessage extends BaseController
  18. {
  19. /**
  20. * @var TemplateMessageRepository
  21. */
  22. protected $repository;
  23. /**
  24. * TemplateMessage constructor.
  25. * @param App $app
  26. * @param TemplateMessageRepository $repository
  27. */
  28. public function __construct(App $app, TemplateMessageRepository $repository)
  29. {
  30. parent::__construct($app);
  31. $this->repository = $repository;
  32. }
  33. /**
  34. * TODO
  35. * @return mixed
  36. * @author Qinii
  37. * @day 2020-06-18
  38. */
  39. public function lst()
  40. {
  41. [$page,$limit] = $this->getPage();
  42. $where = $this->request->params(['status','keyword']);
  43. $where['type'] = 1;
  44. return app('json')->success($this->repository->getList($where,$page,$limit));
  45. }
  46. /**
  47. * TODO
  48. * @return mixed
  49. * @author Qinii
  50. * @day 2020-06-18
  51. */
  52. public function minList()
  53. {
  54. [$page,$limit] = $this->getPage();
  55. $where = $this->request->params(['status','keyword']);
  56. $where['type'] = 0;
  57. return app('json')->success($this->repository->getList($where,$page,$limit));
  58. }
  59. /**
  60. * TODO
  61. * @return mixed
  62. * @author Qinii
  63. * @day 2020-06-18
  64. */
  65. public function createForm()
  66. {
  67. return app('json')->success(formToData($this->repository->form(null,1)));
  68. }
  69. /**
  70. * TODO
  71. * @param $type
  72. * @return mixed
  73. * @author Qinii
  74. * @day 2020-06-19
  75. */
  76. public function createMinForm()
  77. {
  78. return app('json')->success(formToData($this->repository->form(null,0)));
  79. }
  80. /**
  81. * TODO
  82. * @param TemplateMessageValidate $validate
  83. * @return mixed
  84. * @author Qinii
  85. * @day 2020-06-18
  86. */
  87. public function create(TemplateMessageValidate $validate)
  88. {
  89. $data = $this->chekcParams($validate);
  90. $this->repository->create($data);
  91. return app('json')->success('添加成功');
  92. }
  93. /**
  94. * TODO
  95. * @param $id
  96. * @return mixed
  97. * @author Qinii
  98. * @day 2020-06-18
  99. */
  100. public function updateForm($id)
  101. {
  102. if(!$this->repository->getWhereCount(['template_id' => $id]))
  103. return app('json')->fail('数据不存在');
  104. return app('json')->success(formToData($this->repository->updateForm($id)));
  105. }
  106. /**
  107. * TODO
  108. * @param $id
  109. * @return mixed
  110. * @author Qinii
  111. * @day 2020-06-18
  112. */
  113. public function update($id)
  114. {
  115. $data = $this->request->params(['tempid','status']);
  116. if(!$data['tempid'])
  117. return app('json')->fail('请填写模板ID');
  118. if(!$this->repository->getWhereCount(['template_id' => $id]))
  119. return app('json')->fail('数据不存在');
  120. $this->repository->update($id,$data);
  121. return app('json')->success('编辑成功');
  122. }
  123. /**
  124. * TODO
  125. * @param $id
  126. * @return mixed
  127. * @author Qinii
  128. * @day 2020-06-18
  129. */
  130. public function delete($id)
  131. {
  132. if(!$this->repository->getWhereCount(['template_id' => $id]))
  133. return app('json')->fail('数据不存在');
  134. $this->repository->delete($id);
  135. return app('json')->success('删除成功');
  136. }
  137. /**
  138. * TODO
  139. * @param $id
  140. * @return mixed
  141. * @author Qinii
  142. * @day 2020-06-19
  143. */
  144. public function switchStatus($id)
  145. {
  146. $status = $this->request->param('status',0) == 1 ? 1:0;
  147. if(!$this->repository->getWhereCount(['template_id' => $id]))
  148. return app('json')->fail('数据不存在');
  149. $this->repository->update($id,['status' => $status]);
  150. return app('json')->success('修改成功');
  151. }
  152. /**
  153. * TODO
  154. * @param TemplateMessageValidate $validate
  155. * @return array
  156. * @author Qinii
  157. * @day 2020-06-18
  158. */
  159. public function chekcParams(TemplateMessageValidate $validate)
  160. {
  161. $data = $this->request->params(['tempkey','name','tempid','status','content','type']);
  162. $validate->check($data);
  163. return $data;
  164. }
  165. public function sync()
  166. {
  167. $type = $this->request->param('type');
  168. if ($type){
  169. $msg = $this->repository->syncWechatSubscribe();
  170. } else {
  171. $msg = $this->repository->syncMinSubscribe();
  172. }
  173. return app('json')->success($msg);
  174. }
  175. }