Keywords.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Config;
  4. use think\Request;
  5. use think\Db;
  6. class Keywords extends Admin
  7. {
  8. protected $_wechatDb = null;
  9. protected $model = null;
  10. protected function _initialize()
  11. {
  12. parent::_initialize();
  13. $this->_wechatDb = Config::get('db_config_wechat.database'); // 微信公众号
  14. $this->model = model('WxKeywordRule');
  15. }
  16. public function index()
  17. {
  18. // 查询条件
  19. $where = [];
  20. $where['is_del'] = 0;
  21. // 规则名
  22. if (input('request.rule_name') != '') {
  23. $where['rule_name'] =['like', '%' . input('request.rule_name') .'%'];
  24. }
  25. if (input('request.type') != '') {
  26. $where['type'] = input('request.type') == 2 ? 0 : 1 ;
  27. }
  28. if (input('request.keywords') != '') {
  29. $ids = model('WxKeywords')->getRuleID(input('request.keywords'));
  30. $where['id'] = ['in',$ids];
  31. }
  32. $start = $this->request->param('cr_start');
  33. $end = $this->request->param('cr_end');
  34. //开始时间和结束时间不为空时
  35. if ($start != '' && $end != '') {
  36. $where['create_time'] = [
  37. ['>=', strtotime($start)],
  38. ['<=', strtotime($end . ' 23:59:59')],
  39. ];
  40. } //开始时间不为空时
  41. elseif ($start != '') {
  42. $where['create_time'] = ['>=', strtotime($start)];
  43. } //结束时间不为空时
  44. elseif ($end != '') {
  45. $where['create_time'] = ['<=', strtotime($end . ' 23:59:59')];
  46. }
  47. //
  48. $infoList = $this->model
  49. ->where($where)
  50. ->order('create_time', 'desc')
  51. ->paginate(10, false, ['query' => input('get.')])
  52. ->each(function($item, $key){
  53. $item['keywords'] = model('WxKeywords')->getKeywords($item['id']);
  54. $item['replays'] = model('WxReplays')->getReplays($item['id']);
  55. return $item;
  56. });
  57. $this->assign('list', $infoList);
  58. $this->assign('page', $infoList->render());
  59. return $this->fetch();
  60. }
  61. public function add()
  62. {
  63. if (request()->isPost()){
  64. $post = input('post.');
  65. $keywords = $post['keyword'];
  66. $is_alls = $post['is_all'];
  67. $replays = $post['replays'];
  68. $type = $post['type'];
  69. $data = [
  70. 'rule_name' => input('post.rule_name') ,
  71. 'type' => input('post.type' , 0 , 'intval') ,
  72. 'status' => $type ? $post['gift_status'] : $post['status'],
  73. 'remark' => input('post.remark') ,
  74. 'create_time' => NOW_TIMESTAMP,
  75. 'update_time' => NOW_TIMESTAMP
  76. ];
  77. // 字段验证
  78. $verify = $this->verify($post);
  79. if ( !$verify['code']) {
  80. $this->error( $verify['msg']);
  81. }
  82. Db::startTrans();
  83. try {
  84. $ruleId = Db::table($this->_wechatDb . '.nw_wx_keyword_rule')->insertGetId($data);
  85. $keywordData = [];
  86. if ( $type) {
  87. if( model('WxKeywords')->existsKeyword($post['gift_keyword'])){
  88. throw new \Exception('关键词不能重复:' . $post['gift_keyword']);
  89. }
  90. $keywordData[] = [
  91. 'keyword' => $post['gift_keyword'],
  92. 'rule_id' => $ruleId,
  93. 'is_all' => 1
  94. ];
  95. }else{
  96. // 判断关键词是否重复
  97. foreach ($keywords as $key => $value) {
  98. if ( empty($value)) {
  99. throw new \Exception('关键词不能为空');
  100. }
  101. if( model('WxKeywords')->existsKeyword($value)){
  102. throw new \Exception('关键词不能重复:' . $value);
  103. }
  104. $keywordData[] = [
  105. 'keyword' => $value,
  106. 'rule_id' => $ruleId,
  107. 'is_all' => $is_alls[$key]
  108. ];
  109. }
  110. }
  111. $result = Db::table($this->_wechatDb . '.nw_wx_keywords')->insertAll($keywordData);
  112. // 回复
  113. $replaysData = [];
  114. if ( $type) {
  115. $replaysData[] = [
  116. 'content' => $post['gift_replays'],
  117. 'rule_id' => $ruleId,
  118. ];
  119. }else{
  120. foreach ($replays as $k => $v) {
  121. $replaysData[] = [
  122. 'content' => $v,
  123. 'rule_id' => $ruleId,
  124. ];
  125. }
  126. }
  127. $result2 = Db::table($this->_wechatDb . '.nw_wx_replays')->insertAll($replaysData);
  128. // 礼包
  129. if ( $type) {
  130. $codeStr = $post['gift_code_' . $post['gift_status']];
  131. $codeArray = explode("\n", $codeStr);
  132. $codeArray = array_map('filterAndTrimInput', $codeArray);
  133. $codeArray = array_filter($codeArray);
  134. $libaocodeData = [];
  135. foreach ($codeArray as $key => $value) {
  136. $libaocodeData[] = [
  137. 'rule_id' => $ruleId,
  138. 'code' => $value,
  139. 'status' => $post['gift_status'] ? 0 : 2,
  140. ];
  141. }
  142. $result3 = Db::table($this->_wechatDb . '.nw_wx_keyword_libaocode')->insertAll($libaocodeData);
  143. }
  144. Db::commit();
  145. } catch (\Exception $e) {
  146. Db::rollback();
  147. $this->error("添加失败: " . $e->getMessage());
  148. }
  149. $this->success('操作成功!', url('index'));
  150. }
  151. return $this->fetch();
  152. }
  153. public function edit()
  154. {
  155. $id = $this->request->param('id',0,'intval');
  156. if (!$id){
  157. $this->error('ID不能为空');
  158. }
  159. if (request()->isPost()){
  160. $post = input('post.');
  161. $type = $post['type'];
  162. $data = [
  163. 'id' => $id,
  164. 'rule_name' => input('post.rule_name') ,
  165. 'status' => $type ? $post['gift_status'] : $post['status'],
  166. 'remark' => input('post.remark') ,
  167. 'update_time' => NOW_TIMESTAMP
  168. ];
  169. // 字段验证
  170. $verify = $this->verify($post,$id);
  171. if ( !$verify['code']) {
  172. $this->error( $verify['msg']);
  173. }
  174. Db::startTrans();
  175. try {
  176. $result = Db::table($this->_wechatDb . '.nw_wx_keyword_rule')->update($data);
  177. $keywordData = [];
  178. if ( $type) {
  179. if( model('WxKeywords')->existsKeyword($post['gift_keyword'],$id)){
  180. throw new \Exception('关键词不能重复:' . $post['gift_keyword']);
  181. }
  182. $keywordRes = Db::table($this->_wechatDb . '.nw_wx_keywords')
  183. ->where('rule_id',$id)
  184. ->update(['keyword' => $post['gift_keyword']]);
  185. }else{
  186. $keywords = $post['keyword'];
  187. $is_alls = $post['is_all'];
  188. // 判断关键词是否重复
  189. foreach ($keywords as $key => $value) {
  190. if ( empty($value)) {
  191. throw new \Exception('关键词不能为空');
  192. }
  193. if( model('WxKeywords')->existsKeyword($value,$id)){
  194. throw new \Exception('关键词不能重复:' . $value);
  195. }
  196. $keywordData[] = [
  197. 'keyword' => $value,
  198. 'rule_id' => $id,
  199. 'is_all' => $is_alls[$key]
  200. ];
  201. }
  202. $delRes = Db::table($this->_wechatDb . '.nw_wx_keywords')
  203. ->where('rule_id',$id)
  204. ->delete();
  205. $keywordRes = Db::table($this->_wechatDb . '.nw_wx_keywords')->insertAll($keywordData);
  206. }
  207. // 回复
  208. $replaysData = [];
  209. if ( $type) {
  210. $replaysRes = Db::table($this->_wechatDb . '.nw_wx_replays')
  211. ->where('rule_id',$id)
  212. ->update(['content' => $post['gift_replays']]);
  213. }else{
  214. $replays = $post['replays'];
  215. foreach ($replays as $k => $v) {
  216. $replaysData[] = [
  217. 'content' => $v,
  218. 'rule_id' => $id,
  219. ];
  220. }
  221. $delRes = Db::table($this->_wechatDb . '.nw_wx_replays')
  222. ->where('rule_id',$id)
  223. ->delete();
  224. $replaysRes = Db::table($this->_wechatDb . '.nw_wx_replays')->insertAll($replaysData);
  225. }
  226. // 礼包
  227. if ( $type && $post['gift_status'] == 0) {
  228. $codeRes = Db::table($this->_wechatDb . '.nw_wx_keyword_libaocode')
  229. ->where(['rule_id' => $id,'status' => 2])
  230. ->update(['code' => $post['gift_code_0']]);
  231. }
  232. if ( $type && $post['gift_status'] == 1) {
  233. $codeStr = $post['gift_code_add'];
  234. $codeArray = explode("\n", $codeStr);
  235. $codeArray = array_map('filterAndTrimInput', $codeArray);
  236. $codeArray = array_filter($codeArray);
  237. $libaocodeData = [];
  238. foreach ($codeArray as $key => $value) {
  239. $libaocodeData[] = [
  240. 'rule_id' => $id,
  241. 'code' => $value,
  242. ];
  243. }
  244. $codeRes = Db::table($this->_wechatDb . '.nw_wx_keyword_libaocode')->insertAll($libaocodeData);
  245. }
  246. Db::commit();
  247. } catch (\Exception $e) {
  248. Db::rollback();
  249. $this->error("编辑失败: " . $e->getMessage());
  250. }
  251. $this->success('操作成功!', url('index'));
  252. }
  253. $info = $this->model->find($id);
  254. $keywords = model('WxKeywords')->getKeywordsData($id);
  255. $replays = model('WxReplays')->getReplaysData($id);
  256. if ( $info['type'] == 1) {
  257. $code = model('WxKeywordLibaocode')->where(['rule_id'=>$id,'status'=> ['in',[0,2]]])->column('code', 'id');
  258. $this->assign('code', implode("\r\n", $code));
  259. }
  260. $this->assign('keywords',$keywords);
  261. $this->assign('replays',$replays);
  262. $this->assign('info',$info);
  263. return $this->fetch();
  264. }
  265. // 删除
  266. public function del()
  267. {
  268. $id = $this->request->param('id',0,'intval');
  269. if (!$id){
  270. $this->error('ID不能为空');
  271. }
  272. $info = Db::table($this->_wechatDb . '.nw_wx_keyword_rule')->find($id);
  273. if ( empty($info)) {
  274. $this->error('ID不存在');
  275. }
  276. Db::startTrans();
  277. try {
  278. // 伪删除
  279. Db::table($this->_wechatDb . '.nw_wx_keyword_rule')->where('id',$id)->update(['is_del' => 1]);
  280. Db::table($this->_wechatDb . '.nw_wx_keywords')->where('rule_id',$id)->delete();
  281. Db::table($this->_wechatDb . '.nw_wx_replays')->where('rule_id',$id)->delete();
  282. Db::commit();
  283. } catch (\Exception $e) {
  284. Db::rollback();
  285. $this->error("删除失败: " . $e->getMessage());
  286. }
  287. $this->success('删除成功!', url('index'));
  288. }
  289. // 数据验证
  290. public function verify($data,$id = 0)
  291. {
  292. $result = [
  293. 'code' => 0,
  294. 'msg' => ''
  295. ];
  296. if ( empty($data['rule_name'])) {
  297. $result['msg'] = '规则名不能为空';
  298. return $result;
  299. }
  300. if ( $this->model->existsRuleName($data['rule_name'],$id)) {
  301. $result['msg'] = '规则名不能重复';
  302. return $result;
  303. }
  304. if ( $data['type'] == 1) {
  305. if ( empty($data['gift_keyword'])) {
  306. $result['msg'] = '关键词不能为空';
  307. return $result;
  308. }
  309. if ( empty($data['gift_replays'])) {
  310. $result['msg'] = '回复内容不能为空';
  311. return $result;
  312. }
  313. if ( empty($data['gift_code_' . $data['gift_status']])) {
  314. $result['msg'] = '礼包序列号不能为空';
  315. return $result;
  316. }
  317. }else{
  318. foreach ($data['replays'] as $k => $v){
  319. if ( empty($v)) {
  320. $result['msg'] = '回复内容不能为空';
  321. return $result;
  322. }
  323. }
  324. }
  325. $result['code'] = 1;
  326. return $result;
  327. }
  328. }