Reply.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\adminapi\controller\v1\application\wechat;
  3. use app\models\wechat\WechatKey;
  4. use app\models\wechat\WechatReply;
  5. use app\adminapi\controller\AuthController;
  6. use EasyWeChat\Core\Exceptions\HttpException;
  7. use crmeb\services\{UtilService as Util};
  8. /**
  9. * 关键字管理 控制器
  10. * Class Reply
  11. * @package app\admin\controller\wechat
  12. */
  13. class Reply extends AuthController
  14. {
  15. /**关注回复
  16. * @return mixed|void
  17. */
  18. public function reply()
  19. {
  20. $where = Util::getMore([
  21. ['key', ''],
  22. ]);
  23. if ($where['key'] == '') return $this->fail('请输入参数key');
  24. $info = WechatReply::getDataByKey($where['key']);
  25. return $this->success(compact('info'));
  26. }
  27. /**
  28. * 关键字回复列表
  29. * */
  30. public function index()
  31. {
  32. $where = Util::getMore([
  33. ['page', 1],
  34. ['limit', 15],
  35. ['key', ''],
  36. ['type', ''],
  37. ]);
  38. $list = WechatReply::getKeyAll($where);
  39. return $this->success($list);
  40. }
  41. /**
  42. * 关键字详情
  43. * */
  44. public function read($id)
  45. {
  46. $info = WechatReply::getKeyInfo($id);
  47. return $this->success(compact('info'));
  48. }
  49. /**
  50. * 保存关键字
  51. * */
  52. public function save($id = 0)
  53. {
  54. $data = Util::postMore([
  55. 'key',
  56. 'type',
  57. ['status', 0],
  58. ['data', []],
  59. ]);
  60. try {
  61. if (!isset($data['key']) && empty($data['key']))
  62. return $this->fail('请输入关键字');
  63. if (!isset($data['type']) && empty($data['type']))
  64. return $this->fail('请选择回复类型');
  65. if (!in_array($data['type'], WechatReply::$reply_type))
  66. return $this->fail('回复类型有误!');
  67. if (!isset($data['data']) || !is_array($data['data']))
  68. return $this->fail('回复消息参数有误!');
  69. $res = WechatReply::redact($data['data'], $id, $data['key'], $data['type'], $data['status']);
  70. if (!$res)
  71. return $this->fail(WechatReply::getErrorInfo());
  72. else
  73. return $this->success('保存成功!', $data);
  74. } catch (HttpException $e) {
  75. return $this->fail($e->getMessage());
  76. }
  77. }
  78. /**
  79. * 删除关键字
  80. * */
  81. public function delete($id)
  82. {
  83. if (!WechatReply::del($id))
  84. return $this->fail(WechatReply::getErrorInfo('删除失败,请稍候再试!'));
  85. else{
  86. $res = WechatKey::where('reply_id',$id)->delete();
  87. if (!$res){
  88. return $this->fail(WechatKey::getErrorInfo('删除失败,请稍候再试!'));
  89. }
  90. }
  91. return $this->success('删除成功!');
  92. }
  93. /**
  94. * 修改状态
  95. * @param $id
  96. * @param $status
  97. * @return mixed
  98. */
  99. public function set_status($id, $status)
  100. {
  101. if ($status == '' || $id == 0) return $this->fail('参数错误');
  102. WechatReply::where(['id' => $id])->update(['status' => $status]);
  103. return $this->success($status == 0 ? '禁用成功' : '启用成功');
  104. }
  105. }