Reply.php 6.7 KB

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