TemplateMessage.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace app\controller\admin\wechat;
  3. use think\App;
  4. use ln\basic\BaseController;
  5. use app\validate\admin\TemplateMessageValidate;
  6. use app\common\repositories\wechat\TemplateMessageRepository;
  7. class TemplateMessage extends BaseController
  8. {
  9. /**
  10. * @var TemplateMessageRepository
  11. */
  12. protected $repository;
  13. /**
  14. * TemplateMessage constructor.
  15. * @param App $app
  16. * @param TemplateMessageRepository $repository
  17. */
  18. public function __construct(App $app, TemplateMessageRepository $repository)
  19. {
  20. parent::__construct($app);
  21. $this->repository = $repository;
  22. }
  23. /**
  24. * TODO
  25. * @return mixed
  26. * @author Qinii
  27. * @day 2020-06-18
  28. */
  29. public function lst()
  30. {
  31. [$page,$limit] = $this->getPage();
  32. $where = $this->request->params(['status','keyword']);
  33. $where['type'] = 1;
  34. return app('json')->success($this->repository->getList($where,$page,$limit));
  35. }
  36. /**
  37. * TODO
  38. * @return mixed
  39. * @author Qinii
  40. * @day 2020-06-18
  41. */
  42. public function minList()
  43. {
  44. [$page,$limit] = $this->getPage();
  45. $where = $this->request->params(['status','keyword']);
  46. $where['type'] = 0;
  47. return app('json')->success($this->repository->getList($where,$page,$limit));
  48. }
  49. /**
  50. * TODO
  51. * @return mixed
  52. * @author Qinii
  53. * @day 2020-06-18
  54. */
  55. public function createForm()
  56. {
  57. return app('json')->success(formToData($this->repository->form(null,1)));
  58. }
  59. /**
  60. * TODO
  61. * @param $type
  62. * @return mixed
  63. * @author Qinii
  64. * @day 2020-06-19
  65. */
  66. public function createMinForm()
  67. {
  68. return app('json')->success(formToData($this->repository->form(null,0)));
  69. }
  70. /**
  71. * TODO
  72. * @param TemplateMessageValidate $validate
  73. * @return mixed
  74. * @author Qinii
  75. * @day 2020-06-18
  76. */
  77. public function create(TemplateMessageValidate $validate)
  78. {
  79. $data = $this->chekcParams($validate);
  80. $this->repository->create($data);
  81. return app('json')->success('添加成功');
  82. }
  83. /**
  84. * TODO
  85. * @param $id
  86. * @return mixed
  87. * @author Qinii
  88. * @day 2020-06-18
  89. */
  90. public function updateForm($id)
  91. {
  92. if(!$this->repository->getWhereCount(['template_id' => $id]))
  93. return app('json')->fail('数据不存在');
  94. return app('json')->success(formToData($this->repository->updateForm($id)));
  95. }
  96. /**
  97. * TODO
  98. * @param $id
  99. * @return mixed
  100. * @author Qinii
  101. * @day 2020-06-18
  102. */
  103. public function update($id)
  104. {
  105. $data = $this->request->params(['tempid','status']);
  106. if(!$data['tempid'])
  107. return app('json')->fail('请填写模板ID');
  108. if(!$this->repository->getWhereCount(['template_id' => $id]))
  109. return app('json')->fail('数据不存在');
  110. $this->repository->update($id,$data);
  111. return app('json')->success('编辑成功');
  112. }
  113. /**
  114. * TODO
  115. * @param $id
  116. * @return mixed
  117. * @author Qinii
  118. * @day 2020-06-18
  119. */
  120. public function delete($id)
  121. {
  122. if(!$this->repository->getWhereCount(['template_id' => $id]))
  123. return app('json')->fail('数据不存在');
  124. $this->repository->delete($id);
  125. return app('json')->success('删除成功');
  126. }
  127. /**
  128. * TODO
  129. * @param $id
  130. * @return mixed
  131. * @author Qinii
  132. * @day 2020-06-19
  133. */
  134. public function switchStatus($id)
  135. {
  136. $status = $this->request->param('status',0) == 1 ? 1:0;
  137. if(!$this->repository->getWhereCount(['template_id' => $id]))
  138. return app('json')->fail('数据不存在');
  139. $this->repository->update($id,['status' => $status]);
  140. return app('json')->success('修改成功');
  141. }
  142. /**
  143. * TODO
  144. * @param TemplateMessageValidate $validate
  145. * @return array
  146. * @author Qinii
  147. * @day 2020-06-18
  148. */
  149. public function chekcParams(TemplateMessageValidate $validate)
  150. {
  151. $data = $this->request->params(['tempkey','name','tempid','status','content','type']);
  152. $validate->check($data);
  153. return $data;
  154. }
  155. }