Reply.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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\v1\wechat;
  12. use app\controller\admin\AuthController;
  13. use app\services\other\QrcodeServices;
  14. use app\services\wechat\WechatKeyServices;
  15. use app\services\wechat\WechatReplyServices;
  16. use think\facade\App;
  17. /**
  18. * 关键字管理 控制器
  19. * Class Reply
  20. * @package app\controller\admin\v1\application\wechat
  21. */
  22. class Reply extends AuthController
  23. {
  24. /**
  25. * 构造方法
  26. * Menus constructor.
  27. * @param App $app
  28. * @param WechatReplyServices $services
  29. */
  30. public function __construct(App $app, WechatReplyServices $services)
  31. {
  32. parent::__construct($app);
  33. $this->services = $services;
  34. }
  35. /**关注回复
  36. * @return mixed|void
  37. */
  38. public function reply()
  39. {
  40. $where = $this->request->getMore([
  41. ['key', ''],
  42. ]);
  43. if ($where['key'] == '') return $this->fail('请输入参数key');
  44. $info = $this->services->getDataByKey($where['key']);
  45. return $this->success(compact('info'));
  46. }
  47. /**
  48. * 关键字回复列表
  49. * */
  50. public function index()
  51. {
  52. $where = $this->request->getMore([
  53. ['key', ''],
  54. ['type', ''],
  55. ]);
  56. $list = $this->services->getKeyAll($where);
  57. return $this->success($list);
  58. }
  59. /**
  60. * 关键字详情
  61. * */
  62. public function read($id)
  63. {
  64. $info = $this->services->getKeyInfo($id);
  65. return $this->success(compact('info'));
  66. }
  67. /**
  68. * 保存关键字
  69. * */
  70. public function save($id = 0)
  71. {
  72. $data = $this->request->postMore([
  73. 'key',
  74. 'type',
  75. ['status', 0],
  76. ['data', []],
  77. ]);
  78. try {
  79. if (!isset($data['key']) && empty($data['key']))
  80. return $this->fail('请输入关键字');
  81. if (!isset($data['type']) && empty($data['type']))
  82. return $this->fail('请选择回复类型');
  83. if (!in_array($data['type'], $this->services->replyType()))
  84. return $this->fail('回复类型有误!');
  85. if (!isset($data['data']) || !is_array($data['data']))
  86. return $this->fail('回复消息参数有误!');
  87. $res = $this->services->redact($data['data'], $id, $data['key'], $data['type'], $data['status']);
  88. if (!$res)
  89. return $this->fail('保存失败!');
  90. else
  91. return $this->success('保存成功!', $data);
  92. } catch (\Throwable $e) {
  93. return $this->fail($e->getMessage());
  94. }
  95. }
  96. /**
  97. * 删除关键字
  98. * */
  99. public function delete($id)
  100. {
  101. if (!$this->services->delete($id)) {
  102. return $this->fail('删除失败,请稍候再试!');
  103. } else {
  104. /** @var WechatKeyServices $keyServices */
  105. $keyServices = app()->make(WechatKeyServices::class);
  106. $res = $keyServices->delete($id, 'reply_id');
  107. if (!$res) {
  108. return $this->fail('删除失败,请稍候再试!');
  109. }
  110. }
  111. return $this->success('删除成功!');
  112. }
  113. /**
  114. * 修改状态
  115. * @param $id
  116. * @param $status
  117. * @return mixed
  118. */
  119. public function set_status($id, $status)
  120. {
  121. if ($status == '' || $id == 0) return $this->fail('参数错误');
  122. $this->services->update($id, ['status' => $status], 'id');
  123. return $this->success($status == 0 ? '禁用成功' : '启用成功');
  124. }
  125. /**
  126. * 生成关注回复二维码
  127. * @param $id
  128. * @return mixed
  129. * @throws \think\db\exception\DataNotFoundException
  130. * @throws \think\db\exception\DbException
  131. * @throws \think\db\exception\ModelNotFoundException
  132. */
  133. public function code_reply($id)
  134. {
  135. if (!$id) {
  136. return $this->fail('参数错误');
  137. }
  138. /** @var QrcodeServices $qrcode */
  139. $qrcode = app()->make(QrcodeServices::class);
  140. $code = $qrcode->getForeverQrcode('reply', $id);
  141. if (!$code['ticket']) {
  142. return $this->fail('获取二维码失败,请检查是否配置公众号');
  143. }
  144. return $this->success($code->toArray());
  145. }
  146. }