WechatReply.php 6.5 KB

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