WechatReply.php 5.6 KB

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