Replay.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\wechat\shop\controller;
  11. use addon\wechat\model\Replay as ReplayModel;
  12. /**
  13. * 微信回复控制器
  14. */
  15. class Replay extends BaseWechat
  16. {
  17. /**
  18. * 回复设置--关键字自动回复
  19. */
  20. public function replay()
  21. {
  22. if (request()->isAjax()) {
  23. $page = input('page', 1);
  24. $limit = input('limit', PAGE_LIST_ROWS);
  25. $rule_type = input('rule_type', '');
  26. $search_text = input('search_text', '');
  27. $condition = array(
  28. ['rule_type', "=", $rule_type],
  29. ['site_id', '=', $this->site_id]
  30. );
  31. $condition[] = [
  32. 'rule_name', 'like', '%' . $search_text . '%'
  33. ];
  34. $order = 'create_time desc';
  35. $replay_model = new ReplayModel();
  36. $list = $replay_model->getReplayPageList($condition, $page, $limit, $order);
  37. foreach ($list['data']['list'] as $k => $v) {
  38. $list['data']['list'][$k]['key_list'] = $v['keywords_json'] != false ? json_decode($v['keywords_json']) : [];
  39. $list['data']['list'][$k]['replay_list'] = $v['replay_json'] != false ? json_decode($v['replay_json']) : [];
  40. }
  41. return $list;
  42. } else {
  43. $this->assign('link_list', []);//链接
  44. return $this->fetch('replay/replay', [], $this->replace);
  45. }
  46. }
  47. /**
  48. * 添加关键字回复
  49. */
  50. public function addOrEditRule()
  51. {
  52. $replay_model = new ReplayModel();
  53. if (request()->isAjax()) {
  54. $rule_name = input('rule_name', '');
  55. $rule_id = input('rule_id', 0);
  56. if ($rule_id > 0) {
  57. $data = [
  58. 'rule_name' => $rule_name,
  59. 'modify_time' => time()
  60. ];
  61. $res = $replay_model->editRule($data, [['rule_id', "=", $rule_id], ['site_id', "=", $this->site_id]]);
  62. return $res;
  63. } else {
  64. $data = [
  65. 'rule_name' => $rule_name,
  66. 'rule_type' => 'KEYWORDS',
  67. 'keywords_json' => '',
  68. 'replay_json' => '',
  69. 'create_time' => time(),
  70. 'site_id' => $this->site_id
  71. ];
  72. $res = $replay_model->addRule($data);
  73. return $res;
  74. }
  75. }
  76. }
  77. /**
  78. * 添加关键词回复
  79. */
  80. public function editKeywords()
  81. {
  82. $replay_model = new ReplayModel();
  83. if (request()->isAjax()) {
  84. $rule_id = input('rule_id', '');
  85. $info_result = $replay_model->getRuleInfo([['rule_id', "=", $rule_id]]);
  86. $info = $info_result["data"];
  87. $keywords_name = input('keywords_name', '');
  88. $keywords_type = input('keywords_type', 0);
  89. $key_id = input('key_id', -1);
  90. if ($info['keywords_json']) {
  91. $data = json_decode($info['keywords_json']);
  92. if ($key_id > -1) {
  93. $data[$key_id] = [
  94. 'keywords_name' => $keywords_name,
  95. 'keywords_type' => $keywords_type
  96. ];
  97. } else {
  98. array_push($data, [
  99. 'keywords_name' => $keywords_name,
  100. 'keywords_type' => $keywords_type
  101. ]);
  102. }
  103. $data = json_encode($data);
  104. } else {
  105. $data = [
  106. [
  107. 'keywords_name' => $keywords_name,
  108. 'keywords_type' => $keywords_type
  109. ]
  110. ];
  111. $data = json_encode($data);
  112. }
  113. $data = [
  114. 'keywords_json' => $data,
  115. 'modify_time' => time()
  116. ];
  117. $res = $replay_model->editRule($data, [['rule_id', "=", $rule_id], ['site_id', "=", $this->site_id]]);
  118. return $res;
  119. }
  120. }
  121. /**
  122. * 删除关键字回复
  123. */
  124. public function deleteRule()
  125. {
  126. if (request()->isAjax()) {
  127. $rule_id = input('rule_id', 0);
  128. $replay_model = new ReplayModel();
  129. $res = $replay_model->deleteRule([['rule_id', "=", $rule_id], ['site_id', "=", $this->site_id]]);
  130. return $res;
  131. }
  132. }
  133. /**
  134. * 删除关键词数据
  135. */
  136. public function deleteKeywords()
  137. {
  138. $replay_model = new ReplayModel();
  139. if (request()->isAjax()) {
  140. $rule_id = input('rule_id', '');
  141. $key_id = input('key_id', '');
  142. $info_result = $replay_model->getRuleInfo([['rule_id', "=", $rule_id], ['site_id', "=", $this->site_id]]);
  143. $info = $info_result["data"];
  144. if ($info['keywords_json']) {
  145. $data = json_decode($info['keywords_json']);
  146. array_splice($data, $key_id, 1);
  147. $data = json_encode($data);
  148. }
  149. $data = [
  150. 'keywords_json' => $data,
  151. ];
  152. $res = $replay_model->editRule($data, [['rule_id', "=", $rule_id], ['site_id', "=", $this->site_id]]);
  153. return $res;
  154. }
  155. }
  156. /**
  157. * 修改回复数据
  158. * @return mixed
  159. */
  160. public function editReplays()
  161. {
  162. $replay_model = new ReplayModel();
  163. if (request()->isAjax()) {
  164. $rule_id = input('rule_id', '');
  165. $reply_content = input('reply_content', '');
  166. $media_id = input('media_id', '');
  167. $key_id = input('key_id', -1);
  168. $type = input('type', '');
  169. $replay_type = input('replay_type','');
  170. $info_result = $replay_model->getRuleInfo([['rule_id', "=", $rule_id], ['site_id', "=", $this->site_id]]);
  171. $info = $info_result["data"];
  172. if(!empty($rule_id)){
  173. if ($info['replay_json']) {
  174. $data = json_decode($info['replay_json']);
  175. if ($key_id > -1) {
  176. $data[$key_id] = [
  177. 'reply_content' => $reply_content,
  178. 'type' => $type,
  179. ];
  180. if (!empty($media_id)) {
  181. $data[$key_id]['media_id'] = $media_id;
  182. }
  183. } else {
  184. if (!empty($media_id)) {
  185. array_push($data, [
  186. 'reply_content' => $reply_content,
  187. 'type' => $type,
  188. 'media_id' => $media_id
  189. ]);
  190. } else {
  191. array_push($data, [
  192. 'reply_content' => $reply_content,
  193. 'type' => $type,
  194. ]);
  195. }
  196. }
  197. } else {
  198. $data = [
  199. [
  200. 'reply_content' => $reply_content,
  201. 'type' => $type,
  202. ]
  203. ];
  204. if (!empty($media_id)) {
  205. $data[0]['media_id'] = $media_id;
  206. }
  207. }
  208. $data = json_encode($data);
  209. $data = [
  210. 'replay_json' => $data,
  211. 'modify_time' => time()
  212. ];
  213. $res = $replay_model->editRule($data, [['rule_id', "=", $rule_id], ['site_id', "=", $this->site_id]]);
  214. return $res;
  215. }else{
  216. $rule_type = '';
  217. $rule_name = '';
  218. if ($replay_type == "default"){
  219. $rule_type = 'DEFAULT';
  220. $rule_name = '默认回复';
  221. }else if($replay_type == "follow"){
  222. $rule_type = 'AFTER';
  223. $rule_name = '关注后回复';
  224. }
  225. $data = [
  226. [
  227. 'reply_content' => $reply_content,
  228. 'type' => $type,
  229. ]
  230. ];
  231. if (!empty($media_id)) {
  232. $data[0]['media_id'] = $media_id;
  233. }
  234. $data = json_encode($data);
  235. $data = [
  236. 'replay_json' => $data,
  237. 'modify_time' => time(),
  238. 'rule_name' => $rule_name,
  239. 'rule_type' => $rule_type,
  240. 'site_id' => $this->site_id,
  241. ];
  242. $res = $replay_model->addRule($data);
  243. return $res;
  244. }
  245. }
  246. }
  247. /**
  248. * 删除回复数据
  249. */
  250. public function deleteReply()
  251. {
  252. $replay_model = new ReplayModel();
  253. if (request()->isAjax()) {
  254. $rule_id = input('rule_id', '');
  255. $key_id = input('key_id', '');
  256. $info = $replay_model->getRuleInfo([['rule_id', "=", $rule_id], ['site_id', "=", $this->site_id]]);
  257. $info = $info['data'];
  258. if ($info['replay_json']) {
  259. $data = json_decode($info['replay_json']);
  260. array_splice($data, $key_id, 1);
  261. $data = json_encode($data);
  262. }
  263. $data = [
  264. 'replay_json' => $data,
  265. ];
  266. $res = $replay_model->editRule($data, [['rule_id', "=", $rule_id], ['site_id', "=", $this->site_id]]);
  267. return $res;
  268. }
  269. }
  270. /**
  271. * 关注后回复设置
  272. */
  273. public function afterAttention()
  274. {
  275. return $this->fetch('replay/aterAttention', [], $this->replace);
  276. }
  277. /**
  278. * 回复设置--关注后自动回复
  279. */
  280. public function follow()
  281. {
  282. if (request()->isAjax()) {
  283. $page = input('page', 1);
  284. $pagesize = input('limit', PAGE_LIST_ROWS);
  285. $rule_type = input('rule_type', '');
  286. $condition = array(
  287. 'rule_type' => $rule_type,
  288. 'site_id' => $this->site_id
  289. );
  290. $order = 'create_time desc';
  291. $replay_model = new ReplayModel();
  292. $list = $replay_model->getReplayPageList($condition, $page, $pagesize, $order);
  293. foreach ($list['data']['list'] as $k => $v) {
  294. $list['data']['list'][$k]['key_list'] = $v['keywords_json'] != false ? json_decode($v['keywords_json']) : [];
  295. $list['data']['list'][$k]['replay_list'] = $v['replay_json'] != false ? json_decode($v['replay_json']) : [];
  296. }
  297. return $list;
  298. } else {
  299. $this->assign('link_list', []);//链接
  300. return $this->fetch('replay/follow', [], $this->replace);
  301. }
  302. }
  303. public function default()
  304. {
  305. if (request()->isAjax()) {
  306. $page = input('page', 1);
  307. $limit = input('limit', PAGE_LIST_ROWS);
  308. // $rule_type = input('rule_type', '');
  309. $search_text = input('search_text', '');
  310. $condition = array(
  311. ['rule_type', "=",'DEFAULT'], //只获取默认的
  312. ['site_id', '=', $this->site_id]
  313. );
  314. $condition[] = [
  315. 'rule_name', 'like', '%' . $search_text . '%'
  316. ];
  317. $order = 'create_time desc';
  318. $replay_model = new ReplayModel();
  319. $list = $replay_model->getReplayPageList($condition, $page, $limit, $order);
  320. foreach ($list['data']['list'] as $k => $v) {
  321. $list['data']['list'][$k]['key_list'] = $v['keywords_json'] != false ? json_decode($v['keywords_json']) : [];
  322. $list['data']['list'][$k]['replay_list'] = $v['replay_json'] != false ? json_decode($v['replay_json']) : [];
  323. }
  324. return $list;
  325. } else {
  326. $this->assign('link_list', []);//链接
  327. return $this->fetch('replay/default', [], $this->replace);
  328. }
  329. }
  330. }