Reply.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller\v1\application\wechat;
  12. use app\services\other\QrcodeServices;
  13. use EasyWeChat\Core\Exceptions\HttpException;
  14. use app\adminapi\controller\AuthController;
  15. use app\services\wechat\WechatReplyServices;
  16. use app\services\wechat\WechatKeyServices;
  17. use think\facade\App;
  18. /**
  19. * 关键字管理 控制器
  20. * Class Reply
  21. * @package app\admin\controller\wechat
  22. */
  23. class Reply extends AuthController
  24. {
  25. /**
  26. * 构造方法
  27. * Menus constructor.
  28. * @param App $app
  29. * @param WechatReplyServices $services
  30. */
  31. public function __construct(App $app, WechatReplyServices $services)
  32. {
  33. parent::__construct($app);
  34. $this->services = $services;
  35. }
  36. /**
  37. * 关注回复
  38. * @return mixed
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function reply()
  44. {
  45. $where = $this->request->getMore([
  46. ['key', ''],
  47. ]);
  48. if ($where['key'] == '') return app('json')->fail(100100);
  49. $info = $this->services->getDataByKey($where['key']);
  50. return app('json')->success(compact('info'));
  51. }
  52. /**
  53. * 关键字回复列表
  54. * @return mixed
  55. */
  56. public function index()
  57. {
  58. $where = $this->request->getMore([
  59. ['key', ''],
  60. ['type', ''],
  61. ]);
  62. $where['key_type'] = 0;
  63. $list = $this->services->getKeyAll($where);
  64. return app('json')->success($list);
  65. }
  66. /**
  67. * 关键字详情
  68. * @param $id
  69. * @return mixed
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. */
  73. public function read($id)
  74. {
  75. $info = $this->services->getKeyInfo($id);
  76. return app('json')->success(compact('info'));
  77. }
  78. /**
  79. * 保存关键字
  80. * @param int $id
  81. * @return mixed
  82. */
  83. public function save($id = 0)
  84. {
  85. $data = $this->request->postMore([
  86. 'key',
  87. 'type',
  88. ['status', 0],
  89. ['data', []],
  90. ]);
  91. try {
  92. if (!isset($data['key']) && empty($data['key']))
  93. return app('json')->fail(400239);
  94. if (!isset($data['type']) && empty($data['type']))
  95. return app('json')->fail(400240);
  96. if (!in_array($data['type'], $this->services->replyType()))
  97. return app('json')->fail(400241);
  98. if (!isset($data['data']) || !is_array($data['data']))
  99. return app('json')->fail(400242);
  100. $res = $this->services->redact($data['data'], $id, $data['key'], $data['type'], $data['status']);
  101. if (!$res)
  102. return app('json')->fail(100006);
  103. else
  104. return app('json')->success(100000, $data);
  105. } catch (HttpException $e) {
  106. return app('json')->fail($e->getMessage());
  107. }
  108. }
  109. /**
  110. * 删除关键字
  111. * @param $id
  112. * @return mixed
  113. */
  114. public function delete($id)
  115. {
  116. if (!$this->services->delete($id)) {
  117. return app('json')->fail(100008);
  118. } else {
  119. /** @var WechatKeyServices $keyServices */
  120. $keyServices = app()->make(WechatKeyServices::class);
  121. $res = $keyServices->delete($id, 'reply_id');
  122. if (!$res) {
  123. return app('json')->fail(100008);
  124. }
  125. }
  126. return app('json')->success(100002);
  127. }
  128. /**
  129. * 修改状态
  130. * @param $id
  131. * @param $status
  132. * @return mixed
  133. */
  134. public function set_status($id, $status)
  135. {
  136. if ($status == '' || $id == 0) return app('json')->fail(100100);
  137. $this->services->update($id, ['status' => $status], 'id');
  138. return app('json')->success(100014);
  139. }
  140. /**
  141. * 生成关注回复二维码
  142. * @param $id
  143. * @return mixed
  144. * @throws \think\db\exception\DataNotFoundException
  145. * @throws \think\db\exception\DbException
  146. * @throws \think\db\exception\ModelNotFoundException
  147. */
  148. public function code_reply($id)
  149. {
  150. if (!$id) {
  151. return app('json')->fail('100100');
  152. }
  153. /** @var QrcodeServices $qrcode */
  154. $qrcode = app()->make(QrcodeServices::class);
  155. $code = $qrcode->getForeverQrcode('reply', $id);
  156. if (!$code['ticket']) {
  157. return app('json')->fail(400237);
  158. }
  159. return app('json')->success($code->toArray());
  160. }
  161. }