WechatReply.php 6.2 KB

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