WechatReply.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace app\controller\admin\wechat;
  3. use ln\basic\BaseController;
  4. use app\common\repositories\wechat\WechatReplyRepository;
  5. use app\validate\admin\WechatReplyValidate;
  6. use think\App;
  7. use think\db\exception\DataNotFoundException;
  8. use think\db\exception\DbException;
  9. use think\db\exception\ModelNotFoundException;
  10. use think\facade\Filesystem;
  11. /**
  12. * Class WechatReply
  13. * @package app\controller\admin\wechat
  14. * @author zfy
  15. * @day 2020-04-24
  16. */
  17. class WechatReply extends BaseController
  18. {
  19. /**
  20. * @var WechatReplyRepository
  21. */
  22. protected $repository;
  23. /**
  24. * WechatReply constructor.
  25. * @param App $app
  26. * @param WechatReplyRepository $repository
  27. */
  28. public function __construct(App $app, WechatReplyRepository $repository)
  29. {
  30. parent::__construct($app);
  31. $this->repository = $repository;
  32. }
  33. /**
  34. * @return mixed
  35. * @throws DataNotFoundException
  36. * @throws DbException
  37. * @throws ModelNotFoundException
  38. * @author zfy
  39. * @day 2020-04-27
  40. */
  41. public function lst()
  42. {
  43. [$page, $limit] = $this->getPage();
  44. $where = $this->request->params(['keyword']);
  45. return app('json')->success($this->repository->getLst($where, $page, $limit));
  46. }
  47. /**
  48. * @param $id
  49. * @return mixed
  50. * @throws DataNotFoundException
  51. * @throws DbException
  52. * @throws ModelNotFoundException
  53. * @author zfy
  54. * @day 2020-04-27
  55. */
  56. public function info($id)
  57. {
  58. $type = $this->request->param('type', 0);
  59. $reply = !$type ? $this->repository->get((int)$id) : $this->repository->keyByReply($id);
  60. if ($reply)
  61. return app('json')->success($reply->toArray());
  62. else
  63. return app('json')->fail('数据不存在');
  64. }
  65. /**
  66. * @param int $id
  67. * @return mixed
  68. * @throws DbException
  69. * @author zfy
  70. * @day 2020-04-24
  71. */
  72. public function changeStatus($id)
  73. {
  74. $status = $this->request->param('status');
  75. if (!$this->repository->exists($id))
  76. return app('json')->fail('数据不存在');
  77. $this->repository->update($id, ['status' => $status == 1 ? 1 : 0]);
  78. return app('json')->success('修改成功');
  79. }
  80. /**
  81. * @param WechatReplyValidate $validate
  82. * @return mixed
  83. * @author zfy
  84. * @day 2020-04-27
  85. */
  86. public function create(WechatReplyValidate $validate)
  87. {
  88. $data = $this->request->params(['key', 'type', 'data', 'status']);
  89. $validate->check($data);
  90. if ($this->repository->fieldExists('key', $data['key']))
  91. return app('json')->fail('关键字已存在');
  92. $this->repository->create($data);
  93. return app('json')->success('保存成功');
  94. }
  95. /**
  96. * @param $id
  97. * @param WechatReplyValidate $validate
  98. * @return mixed
  99. * @throws DataNotFoundException
  100. * @throws DbException
  101. * @throws ModelNotFoundException
  102. * @author zfy
  103. * @day 2020-04-27
  104. */
  105. public function update($id, WechatReplyValidate $validate)
  106. {
  107. $data = $this->request->params(['key', 'type', 'data', 'status']);
  108. $validate->check($data);
  109. if ($this->repository->fieldExists('key', $data['key'], $id))
  110. return app('json')->fail('关键字已存在');
  111. $this->repository->updateReply($id, $data);
  112. return app('json')->success('保存成功');
  113. }
  114. /**
  115. * @param string $key
  116. * @param WechatReplyValidate $validate
  117. * @return mixed
  118. * @throws DataNotFoundException
  119. * @throws DbException
  120. * @throws ModelNotFoundException
  121. * @author zfy
  122. * @day 2020-04-24
  123. */
  124. public function save($key, WechatReplyValidate $validate)
  125. {
  126. [$type, $data, $status] = $this->request->params(['type', 'data', 'status'], true);
  127. $validate->isUpdate()->check(compact('type', 'data', 'status'));
  128. if (!in_array($key, ['default', 'subscribe']))
  129. return app('json')->fail('修改失败');
  130. $this->repository->save($key, $type, (array)$data, $status == 1 ? 1 : 0, 1);
  131. return app('json')->success('保存成功');
  132. }
  133. /**
  134. * @param $id
  135. * @return mixed
  136. * @throws DbException
  137. * @author zfy
  138. * @day 2020-04-24
  139. */
  140. public function delete($id)
  141. {
  142. $this->repository->delete($id);
  143. return app('json')->success('删除成功');
  144. }
  145. public function uploadImage()
  146. {
  147. $file = $this->request->file('file');
  148. if (!$file)
  149. return app('json')->fail('请上传图片');
  150. $file = is_array($file) ? $file[0] : $file;
  151. validate(["file|图片" => [
  152. 'fileSize' => config('upload.filesize'),
  153. 'fileExt' => 'jpg,jpeg,png,bmp,gif',
  154. 'fileMime' => 'image/jpeg,image/png,image/gif',
  155. ]])->check(['file' => $file]);
  156. $path = Filesystem::putFile('wechat/img', $file, 'md5');
  157. return app('json')->success(['src' => '/uploads/' . $path]);
  158. }
  159. public function uploadVoice()
  160. {
  161. $file = $this->request->file('file');
  162. if (!$file)
  163. return app('json')->fail('请上传声音');
  164. $file = is_array($file) ? $file[0] : $file;
  165. validate(["file|声音" => [
  166. 'fileSize' => config('upload.filesize'),
  167. 'fileExt' => 'wav,aif,mp3',
  168. 'fileMime' => 'audio/x-wav,audio/x-aiff,audio/x-mpeg,audio/mpeg,audio/wav,audio/aiff',
  169. ]])->check(['file' => $file]);
  170. $path = Filesystem::putFile('wechat/voice', $file, 'md5');
  171. return app('json')->success(['src' => '/uploads/' . $path]);
  172. }
  173. }