TemplateMessage.php 4.3 KB

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