Autoreply.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. <?php
  2. namespace app\admin\controller\wechat;
  3. use app\common\controller\Backend;
  4. use app\common\library\Redis;
  5. use app\common\model\AutoreplyCollect;
  6. use app\common\model\WechatAutoreply;
  7. use app\main\constants\CacheConstants;
  8. use app\main\constants\ErrorCodeConstants;
  9. use app\main\constants\WechatSubscribeConstants;
  10. use app\main\service\AutoreplyService;
  11. use app\main\service\BookService;
  12. use app\main\service\UrlService;
  13. use app\main\service\VisitLimitService;
  14. use think\Collection;
  15. use think\Exception;
  16. /**
  17. * 微信自动回复管理
  18. *
  19. * @icon fa fa-circle-o
  20. */
  21. class Autoreply extends Backend
  22. {
  23. /**
  24. * @var WechatAutoreply
  25. */
  26. protected $model = null;
  27. protected $noNeedRight = ['check_text_unique','save', 'signreply'];
  28. protected $dataLimit = 'personal';
  29. public function _initialize()
  30. {
  31. parent::_initialize();
  32. $this->model = model('WechatAutoreply');
  33. }
  34. /**
  35. * 查看
  36. */
  37. public function index()
  38. {
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags']);
  41. if ($this->request->isAjax())
  42. {
  43. //如果发送的来源是Selectpage,则转发到Selectpage
  44. if ($this->request->request('pkey_name'))
  45. {
  46. return $this->selectpage();
  47. }
  48. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  49. $total = $this->model
  50. ->whereNotIn('title', ['签到', 'subscribe'])
  51. ->where($where)
  52. ->order($sort, $order)
  53. ->count();
  54. $list = $this->model
  55. ->whereNotIn('title', ['签到', 'subscribe'])
  56. ->where($where)
  57. ->order($sort, $order)
  58. ->limit($offset, $limit)
  59. ->select();
  60. if ($list) {
  61. $collect = new AutoreplyCollect();
  62. $ids = array_column($list, 'id');
  63. if (VisitLimitService::instance()->checkMigratedV2()) {
  64. $collect_list = AutoreplyService::instance()->getMigrateData($ids)->data;
  65. } else {
  66. $collect_list = $collect->whereIn('id', $ids)->column('id,uv,subscribe,money');
  67. }
  68. // if (row.type == 'text') {
  69. // list = JSON.parse(row.text_content);
  70. // } else if (row.type == 'news') {
  71. // list = JSON.parse(row.news_content);
  72. // } else {
  73. // return '-';
  74. // }
  75. $list = Collection::make($list)->toArray();
  76. foreach ($list as $index => $item) {
  77. if ($item['type'] == 'text') {
  78. $resource_list = json_decode($item['text_content'], true);
  79. } else {
  80. $resource_list = json_decode($item['news_content'], true);
  81. }
  82. $book_names = [];
  83. if ($resource_list) {
  84. foreach ($resource_list as $resource) {
  85. $resource['image_type'] = $resource['image_type'] ?? 1;
  86. if ($resource['image_type'] == 1 && array_key_exists('book_id', $resource)) {
  87. $chapter = model('Book')::getChapterInfo($resource['book_id'], $resource['chapter_id']);
  88. $book = model('Book')->BookInfo($resource['book_id']);
  89. if ($book && !$chapter['code']) {
  90. $book_names[] = $book['name'].' '.$chapter['data']['name'];
  91. }
  92. }
  93. }
  94. }
  95. if (array_key_exists($item['id'], $collect_list)) {
  96. $list[$index] = array_merge($item, $collect_list[$item['id']]);
  97. }
  98. if (array_key_exists('books', $item) && $item['books']) {
  99. $list[$index]['books'] = json_decode($item['books'], true);
  100. }
  101. $list[$index]['book_names'] = $book_names;
  102. }
  103. }
  104. $result = array("total" => $total, "rows" => $list);
  105. return json($result);
  106. }
  107. if($this->auth->agent_id || $this->group == 3){
  108. if(!model('AdminConfig')->checkWechatConfig($this->auth->id)){
  109. $this->error('请先授权微信服务号给本系统,正在跳转授权设置页面~', url('admin/config'));
  110. }
  111. }
  112. return $this->view->fetch();
  113. }
  114. /**
  115. * 添加
  116. */
  117. public function add()
  118. {
  119. if ($this->request->isPost())
  120. {
  121. $params = $this->request->post("row/a");
  122. if ($params)
  123. {
  124. $params['title'] = $params['text'];
  125. $keywords = explode(',', $params['title']);
  126. if (in_array('subscribe', $keywords)) {
  127. $this->error('请在新版被关注回复中设置关注回复内容');
  128. }
  129. /*
  130. * 已经弃用,如果为了兼容老版可取消注释
  131. foreach ($params as $k => &$v)
  132. {
  133. $v = is_array($v) ? implode(',', $v) : $v;
  134. }
  135. */
  136. if ($this->dataLimit)
  137. {
  138. $params[$this->dataLimitField] = $this->auth->id;
  139. }
  140. try
  141. {
  142. //是否采用模型验证
  143. if ($this->modelValidate)
  144. {
  145. $name = basename(str_replace('\\', '/', get_class($this->model)));
  146. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  147. $this->model->validate($validate);
  148. }
  149. $result = $this->model->allowField(true)->save($params);
  150. if ($result !== false)
  151. {
  152. $this->success();
  153. }
  154. else
  155. {
  156. $this->error($this->model->getError());
  157. }
  158. }
  159. catch (\think\exception\PDOException $e)
  160. {
  161. $this->error($e->getMessage());
  162. }
  163. }
  164. $this->error(__('Parameter %s can not be empty', ''));
  165. }
  166. return $this->view->fetch();
  167. }
  168. /**
  169. * 编辑
  170. */
  171. public function edit($ids = NULL)
  172. {
  173. $row = $this->model->get(['id' => $ids, 'admin_id' => $this->auth->id]);
  174. if (!$row)
  175. $this->error(__('No Results were found'));
  176. if ($row['title'] == 'subscribe')
  177. $this->error(__('请在新版被关注回复中设置关注回复内容'));
  178. if ($this->request->isPost())
  179. {
  180. $params = $this->request->post("row/a");
  181. if ($params)
  182. {
  183. $params['title'] = $params['text'];
  184. $row->save($params);
  185. $this->success();
  186. }
  187. $this->error();
  188. }
  189. $channel_id = $this->auth->agent_id ? $this->auth->agent_id : $this->auth->channel_id;
  190. $news = UrlService::instance()->replaceReferralHost($channel_id, $row['news_content'], false)->data;
  191. $text = UrlService::instance()->replaceReferralHost($channel_id, $row['text_content'], false)->data;
  192. if ($news) {
  193. $row['news_content'] = json_decode($news, true);
  194. }else{
  195. $row['news_content'] = [];
  196. }
  197. if ($text) {
  198. $row['text_content'] = json_decode($text, true);
  199. }else{
  200. $row['text_content'] = [];
  201. }
  202. if ($row['text_tip_word']) {
  203. $row['text_tip_word'] = json_decode($row['text_tip_word'], true);
  204. }else{
  205. $row['text_tip_word'] = [
  206. 'text_split' => '更多精彩内容,点击下方蓝字阅读',
  207. 'text_tip' => '$user_nickname,欢迎关注「$gzh_name」,点击下方继续阅读',
  208. ];
  209. }
  210. $this->assignconfig("row", $row);
  211. $this->assignconfig('kandian', \think\Config::get('site.kandian_sign'));
  212. return $this->view->fetch();
  213. }
  214. /**
  215. * 判断文本是否唯一
  216. * @internal
  217. */
  218. public function check_text_unique()
  219. {
  220. $row = $this->request->post("row/a");
  221. $except = $this->request->post("except");
  222. $text = isset($row['text']) ? $row['text'] : '';
  223. if ($this->model->where('text', $text)->where('admin_id', $this->auth->id)->where(function($query) use($except) {
  224. if ($except)
  225. {
  226. $query->where('text', '<>', $except);
  227. }
  228. })->count() == 0)
  229. {
  230. return $this->success();
  231. }
  232. else
  233. {
  234. return $this->error(__('Text already exists'));
  235. }
  236. }
  237. public function save()
  238. {
  239. $resourceTypeList = ['2' => 'text', '3' => 'news'];
  240. if ($this->request->isPost()) {
  241. $params = json_decode($this->request->post('data'), true);
  242. $newsContent = $params["news_content"];
  243. $textContent = $params["text_content"];
  244. $resource_type = $params["resource_type"];
  245. //文本每一二条之间的提示语
  246. $text_split = $params["text_split"];
  247. $text_tip = $params["text_tip"];
  248. $text_tip = str_replace('<br>',"\r\n", $text_tip);
  249. $keywords = $params["keywords"];
  250. $keyword_list = explode(',',$keywords);
  251. if (in_array('subscribe', $keyword_list)) {
  252. $this->error('请在新版被关注回复中设置关注回复内容');
  253. }
  254. $text_tip_word = json_encode([
  255. 'text_split' => $text_split,
  256. 'text_tip' => $text_tip,
  257. ], JSON_UNESCAPED_UNICODE);
  258. if (!isset($resourceTypeList[$resource_type])) {
  259. throw new Exception('模式错误');
  260. }
  261. $type = $resourceTypeList[$resource_type];
  262. $channel_id = $this->auth->agent_id ? $this->auth->agent_id : $this->auth->channel_id;
  263. if ($textContent) {
  264. foreach ($textContent as $index => $item) {
  265. if (empty($item['url']) && isset($item['chapter_id'])) {
  266. $textContent[$index]['url'] = getCurrentDomain($channel_id, '/index/book/chapter?book_id=' . $item['book_id'] . '&chapter_id=' . $item['chapter_id']);
  267. }
  268. }
  269. $textContent = UrlService::instance()->replaceReferralHost($this->getCurrentAccountChannelId(), json_encode($textContent, JSON_UNESCAPED_UNICODE))->data;
  270. }
  271. if ($newsContent) {
  272. foreach ($newsContent as $index => $item) {
  273. if (empty($item['url']) && isset($item['chapter_id'])) {
  274. $newsContent[$index]['url'] = getCurrentDomain($channel_id, '/index/book/chapter?book_id=' . $item['book_id'] . '&chapter_id=' . $item['chapter_id']);
  275. }
  276. }
  277. $newsContent = UrlService::instance()->replaceReferralHost($this->getCurrentAccountChannelId(), json_encode($newsContent, JSON_UNESCAPED_UNICODE))->data;
  278. }
  279. //替换域名
  280. $time = time();
  281. try {
  282. //编辑
  283. if ($params['id']??'') {
  284. $reply = $this->model
  285. ->where('id', $params['id'])
  286. ->find();
  287. if (!$reply) {
  288. throw new Exception('数据不存在');
  289. }
  290. }else{
  291. $reply = $this->model
  292. ->where('title', $keywords)
  293. ->where('admin_id', $this->auth->id)
  294. ->find();
  295. }
  296. if (!$reply) {
  297. //新增设置
  298. $data['admin_id'] = $this->auth->id;
  299. $data['type'] = $type;
  300. $data['status'] = 'normal';
  301. $data['title'] = $data['text'] = $keywords;
  302. $data['text_content'] = $textContent;
  303. $data['news_content'] = $newsContent;
  304. $data['text_tip_word'] = $text_tip_word;
  305. $data['createtime'] = $data['updatetime'] = $time;
  306. $data['event_keys'] = json_encode([
  307. 'text' => $this->auth->id . '_' . uniqid(),
  308. 'news' => $this->auth->id . '_' . uniqid(),
  309. ], JSON_UNESCAPED_UNICODE);
  310. $result = $this->model->insert($data);
  311. if ($result == false) {
  312. throw new Exception($this->model->getError());
  313. }
  314. } else {
  315. $update = [
  316. 'type' => $type,
  317. 'text_tip_word' => $text_tip_word,
  318. 'text_content' => $textContent,
  319. 'news_content' => $newsContent,
  320. 'createtime' => $time,
  321. 'updatetime' => $time,
  322. ];
  323. if (!$reply['event_keys']) {
  324. $update['event_keys'] = json_encode([
  325. 'text' => $this->auth->id . '_' . uniqid(),
  326. 'news' => $this->auth->id . '_' . uniqid(),
  327. ], JSON_UNESCAPED_UNICODE);
  328. }
  329. $update['title'] = $update['text'] = $keywords;
  330. $data = array_merge($reply->toArray(), $update);
  331. $this->model->update($update, ['id' => $reply['id']]);
  332. }
  333. $event_keys = json_decode($data['event_keys'], true);
  334. foreach ($event_keys as $type => $event_key) {
  335. $update = [];
  336. switch ($type) {
  337. case WechatSubscribeConstants::SUBCRIBE_TYPE_TEXT:
  338. if ($textContent) {
  339. //组织内容
  340. $resourceContent = $this->getTextContent($textContent, $text_split, $text_tip);
  341. $update = [
  342. 'type' => 'text',
  343. 'title' => '文字类型回复',
  344. 'content' => $resourceContent,
  345. 'updatetime' => $time,
  346. 'status' => 'normal',
  347. ];
  348. }
  349. break;
  350. default:
  351. if ($newsContent) {
  352. $update = [
  353. 'type' => 'news',
  354. 'title' => '图文类型回复',
  355. 'content' => $newsContent,
  356. 'updatetime' => $time,
  357. ];
  358. }
  359. break;
  360. }
  361. $where = [
  362. 'admin_id' => $this->auth->id,
  363. 'eventkey' => $event_key,
  364. ];
  365. if ($update) {
  366. $update['status'] = 'normal';
  367. if (!model("WechatResponse")->where($where)->find()) {
  368. $insert = array_merge($where, $update, ['createtime' => $time]);
  369. model("WechatResponse")->insert($insert);
  370. } else {
  371. model("WechatResponse")->update($update, $where);
  372. }
  373. }
  374. }
  375. } catch (\think\exception\PDOException $e) {
  376. $this->error($e->getMessage());
  377. } catch (Exception $e) {
  378. $this->error($e->getMessage().$e->getLine());
  379. }
  380. } else {
  381. $this->error(__('Parameter %s can not be empty', ''));
  382. }
  383. }
  384. public function getTextContent($content, $text_split, $text_tip)
  385. {
  386. $temp = WechatSubscribeConstants::AUTOREPLY_TEXT_TEMPLATE;
  387. $bookTemp = WechatSubscribeConstants::SUBCRIBE_TEXT_BOOK_TEMPLATE;
  388. $bookSets = json_decode($content, true);
  389. $contentStr = [];
  390. $resourceContent = '';
  391. if (is_array($bookSets)) {
  392. foreach ($bookSets as $index => $bookSet) {
  393. if ($index == 1 && $text_split) {
  394. $contentStr[] = $text_split;
  395. }
  396. $url = $bookSet['url'];
  397. $title = $bookSet['title'];
  398. $contentStr[] = str_replace(['{URL}', '{TITLE}'], [$url, $title], $bookTemp);
  399. }
  400. $wxJson = model("AdminConfig")
  401. ->where('admin_id', $this->auth->id)
  402. ->value('json');
  403. $gzhName = '';
  404. if (!empty($wxJson)) {
  405. $wxInfo = json_decode($wxJson, true);
  406. $gzhName = $wxInfo['authorizer_info']['nick_name'];
  407. }
  408. $resourceContent = str_replace(['{TEXT_TIP}', '$gzh_name', '{CONTENT}'], [$text_tip, $gzhName, implode("\r\n\r\n", $contentStr)], $temp);
  409. }
  410. return $resourceContent;
  411. }
  412. public function signreply()
  413. {
  414. $where = [
  415. 'admin_id' => $this->auth->id,
  416. 'title' => '签到',
  417. 'status' => 'normal',
  418. ];
  419. $find = model('wechat_autoreply')->where($where)->find();
  420. if ($this->request->isPost()) {
  421. $channelId = $this->getCurrentAccountChannelId();
  422. $data = json_decode($this->request->post('data'), true);
  423. $type = $data['type']??1;
  424. $text_split = $data['text_split']??'';
  425. $text = $data['text']??'{}';
  426. $textContent = UrlService::instance()->replaceReferralHost($channelId, json_encode($text, JSON_UNESCAPED_UNICODE))->data;
  427. if ($find) {
  428. if ($type == 2) {
  429. $update = [
  430. 'type' => 'text',
  431. 'text_content' => $textContent,
  432. 'text_tip_word' => $text_split
  433. ];
  434. } else {
  435. $update = [
  436. 'type' => 'news',
  437. 'news_content' => '[]',
  438. ];
  439. }
  440. model('wechat_autoreply')->update($update, $where);
  441. $cacheKey = CacheConstants::getSignReplyCache($this->auth->id);
  442. Redis::instance()->del($cacheKey.'OS:1');
  443. Redis::instance()->del($cacheKey.'OS:2');
  444. Redis::instance()->del($cacheKey.'OS:0');
  445. } else {
  446. $insert = $where;
  447. if ($type == 2) {
  448. $insert['type'] = 'text';
  449. } else {
  450. $insert['type'] = 'news';
  451. }
  452. //新增设置
  453. $insert['status'] = 'normal';
  454. $insert['title'] = $insert['text'] = '签到';
  455. $insert['text_content'] = $textContent;
  456. $insert['news_content'] = '[]';
  457. $insert['text_tip_word'] = $text_split;
  458. $insert['createtime'] = $insert['updatetime'] = time();
  459. $insert['event_keys'] = json_encode([
  460. 'text' => $this->auth->id . '_' . uniqid(),
  461. 'news' => $this->auth->id . '_' . uniqid(),
  462. ], JSON_UNESCAPED_UNICODE);
  463. model('wechat_autoreply')->insert($insert);
  464. }
  465. $cacheKey = CacheConstants::getSignReplyCache($channelId);
  466. Redis::instance()->del($cacheKey);
  467. $this->success();
  468. }
  469. if ($find && $find['text_content'] != '') {
  470. $data = [
  471. 'text' => UrlService::instance()->replaceReferralHost($this->getCurrentAccountChannelId(), $find['text_content'], false)->data,
  472. 'type' => $find['type'] == 'news' ? 1 : 2,
  473. 'text_split' => $find['text_tip_word'],
  474. ];
  475. } else {
  476. $data = [
  477. 'text' => '[]',
  478. 'type' => 1,
  479. 'text_split' => '',
  480. ];
  481. }
  482. $this->assignconfig($data);
  483. return $this->view->fetch();
  484. }
  485. }