Reply.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace app\admin\controller\wechat;
  3. use app\admin\controller\AuthController;
  4. use app\admin\model\system\SystemAttachment;
  5. use app\admin\model\wechat\WechatReply;
  6. use crmeb\services\UtilService as Util;
  7. use crmeb\services\JsonService as Json;
  8. use crmeb\services\upload\Upload;
  9. /**
  10. * 关键字管理 控制器
  11. * Class Reply
  12. * @package app\admin\controller\wechat
  13. */
  14. class Reply extends AuthController
  15. {
  16. /**关注回复
  17. * @return mixed|void
  18. */
  19. public function index()
  20. {
  21. if (empty(input('key'))) return $this->failed('请输入参数key');
  22. if (empty(input('title'))) return $this->failed('请输入参数title');
  23. $key = input('key');
  24. switch ($key) {
  25. case 'subscribe':
  26. $title = '编辑关注回复';
  27. $type = 0;
  28. break;
  29. case 'default':
  30. $title = '编辑关键字默认回复';
  31. $type = 1;
  32. break;
  33. default:
  34. $title = '编辑关键字回复';
  35. $type = 1;
  36. break;
  37. }
  38. $this->assign('replay_arr', WechatReply::getDataByKey($key));
  39. $this->assign('key', $key);
  40. $this->assign('type', $type);
  41. $this->assign('title', $title);
  42. return $this->fetch();
  43. }
  44. public function one_reply()
  45. {
  46. $where = Util::postMore([
  47. ['key'],
  48. ['add', 0],
  49. ], $this->request);
  50. if (!empty($where['key'])) $replay_arr = WechatReply::getDataByKey($where['key']);
  51. $replay_arr['code'] = 200;
  52. if (empty($replay_arr)) {
  53. $replay_arr['code'] = 0;
  54. }
  55. if ($where['add'] && empty($where['key'])) {
  56. $replay_arr['code'] = 0;
  57. }
  58. exit(json_encode($replay_arr));
  59. }
  60. public function save()
  61. {
  62. $data = Util::postMore([
  63. 'type',
  64. 'key',
  65. ['status', 0],
  66. ['data', []],
  67. ]);
  68. if (!isset($data['type']) && empty($data['type']))
  69. return Json::fail('请选择回复类型');
  70. if (!in_array($data['type'], WechatReply::$reply_type))
  71. return Json::fail('回复类型有误!');
  72. if (!isset($data['data']) || !is_array($data['data']))
  73. return Json::fail('回复消息参数有误!');
  74. $res = WechatReply::redact($data['data'], $data['key'], $data['type'], $data['status']);
  75. if (!$res)
  76. return Json::fail(WechatReply::getErrorInfo());
  77. else
  78. return Json::successful('保存成功!', $data);
  79. }
  80. public function upload_img()
  81. {
  82. $name = $this->request->post('file');
  83. if (!$name) return Json::fail('请上传图片');
  84. $upload = new Upload('local');
  85. $info = $upload->to('wechat/image')->validate()->move($name);
  86. if ($info === false) {
  87. return Json::fail($upload->getError());
  88. }
  89. $res = $upload->getUploadInfo();
  90. SystemAttachment::attachmentAdd($res['name'], $res['size'], $res['type'], $res['dir'], $res['thumb_path'], 0, 1, $res['time'], 2);
  91. return Json::successful('上传成功', $res['dir']);
  92. }
  93. public function upload_file()
  94. {
  95. $name = $this->request->post('file');
  96. if (!$name) return Json::fail('请上传声音');
  97. $upload = new Upload('local');
  98. $info = $upload->to('wechat/voice')->validate()->move($name);
  99. if ($info === false) {
  100. return Json::fail($upload->getError());
  101. }
  102. return Json::successful('上传成功', $info->filePath);
  103. }
  104. /**
  105. * 关键字回复
  106. * */
  107. public function keyword()
  108. {
  109. $where = Util::getMore([
  110. ['key', ''],
  111. ['type', ''],
  112. ]);
  113. $this->assign('where', $where);
  114. $this->assign(WechatReply::getKeyAll($where));
  115. return $this->fetch();
  116. }
  117. /**
  118. * 添加关键字
  119. * */
  120. public function add_keyword()
  121. {
  122. $key = input('key');
  123. if (empty($key)) $key = '';
  124. $this->assign('key', $key);
  125. $this->assign('dis', 1);
  126. $this->assign('replay_arr', []);
  127. return $this->fetch();
  128. }
  129. /**
  130. * 修改关键字
  131. * */
  132. public function info_keyword()
  133. {
  134. $key = input('key');
  135. if (empty($key)) return $this->failed('参数错误,请重新修改');
  136. $this->assign('replay_arr', WechatReply::getDataByKey($key));
  137. $this->assign('key', $key);
  138. $this->assign('dis', 2);
  139. return $this->fetch('add_keyword');
  140. }
  141. /**
  142. * 保存关键字
  143. * */
  144. public function save_keyword()
  145. {
  146. $data = Util::postMore([
  147. 'key',
  148. 'type',
  149. ['status', 0],
  150. ['data', []],
  151. ]);
  152. if (!isset($data['key']) && empty($data['key']))
  153. return Json::fail('请输入关键字');
  154. if (isset($data['key']) && !empty($data['key'])) {
  155. if (trim($data['key']) == 'subscribe') return Json::fail('请重新输入关键字');
  156. if (trim($data['key']) == 'default') return Json::fail('请重新输入关键字');
  157. }
  158. if (!isset($data['type']) && empty($data['type']))
  159. return Json::fail('请选择回复类型');
  160. if (!in_array($data['type'], WechatReply::$reply_type))
  161. return Json::fail('回复类型有误!');
  162. if (!isset($data['data']) || !is_array($data['data']))
  163. return Json::fail('回复消息参数有误!');
  164. $res = WechatReply::redact($data['data'], $data['key'], $data['type'], $data['status']);
  165. if (!$res)
  166. return Json::fail(WechatReply::getErrorInfo());
  167. else
  168. return Json::successful('保存成功!', $data);
  169. }
  170. /**
  171. * 删除关键字
  172. * */
  173. public function delete($id)
  174. {
  175. if (!WechatReply::del($id))
  176. return Json::fail(WechatReply::getErrorInfo('删除失败,请稍候再试!'));
  177. else
  178. return Json::successful('删除成功!');
  179. }
  180. }