| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- <?php
- namespace app\admin\controller;
- use think\Config;
- use think\Request;
- use think\Db;
- class Keywords extends Admin
- {
-
- protected $_wechatDb = null;
- protected $model = null;
- protected function _initialize()
- {
- parent::_initialize();
- $this->_wechatDb = Config::get('db_config_wechat.database'); // 微信公众号
- $this->model = model('WxKeywordRule');
- }
- public function index()
- {
- // 查询条件
- $where = [];
- $where['is_del'] = 0;
- // 规则名
- if (input('request.rule_name') != '') {
- $where['rule_name'] =['like', '%' . input('request.rule_name') .'%'];
- }
- if (input('request.type') != '') {
- $where['type'] = input('request.type') == 2 ? 0 : 1 ;
- }
- if (input('request.keywords') != '') {
- $ids = model('WxKeywords')->getRuleID(input('request.keywords'));
- $where['id'] = ['in',$ids];
- }
- $start = $this->request->param('cr_start');
- $end = $this->request->param('cr_end');
- //开始时间和结束时间不为空时
- if ($start != '' && $end != '') {
- $where['create_time'] = [
- ['>=', strtotime($start)],
- ['<=', strtotime($end . ' 23:59:59')],
- ];
- } //开始时间不为空时
- elseif ($start != '') {
- $where['create_time'] = ['>=', strtotime($start)];
- } //结束时间不为空时
- elseif ($end != '') {
- $where['create_time'] = ['<=', strtotime($end . ' 23:59:59')];
- }
- //
- $infoList = $this->model
- ->where($where)
- ->order('create_time', 'desc')
- ->paginate(10, false, ['query' => input('get.')])
- ->each(function($item, $key){
- $item['keywords'] = model('WxKeywords')->getKeywords($item['id']);
- $item['replays'] = model('WxReplays')->getReplays($item['id']);
- return $item;
- });
-
- $this->assign('list', $infoList);
- $this->assign('page', $infoList->render());
- return $this->fetch();
- }
- public function add()
- {
- if (request()->isPost()){
- $post = input('post.');
- $keywords = $post['keyword'];
- $is_alls = $post['is_all'];
- $replays = $post['replays'];
- $type = $post['type'];
- $data = [
- 'rule_name' => input('post.rule_name') ,
- 'type' => input('post.type' , 0 , 'intval') ,
- 'status' => $type ? $post['gift_status'] : $post['status'],
- 'remark' => input('post.remark') ,
- 'create_time' => NOW_TIMESTAMP,
- 'update_time' => NOW_TIMESTAMP
- ];
- // 字段验证
- $verify = $this->verify($post);
- if ( !$verify['code']) {
- $this->error( $verify['msg']);
- }
- Db::startTrans();
- try {
- $ruleId = Db::table($this->_wechatDb . '.nw_wx_keyword_rule')->insertGetId($data);
- $keywordData = [];
- if ( $type) {
- if( model('WxKeywords')->existsKeyword($post['gift_keyword'])){
- throw new \Exception('关键词不能重复:' . $post['gift_keyword']);
- }
- $keywordData[] = [
- 'keyword' => $post['gift_keyword'],
- 'rule_id' => $ruleId,
- 'is_all' => 1
- ];
-
- }else{
- // 判断关键词是否重复
- foreach ($keywords as $key => $value) {
- if ( empty($value)) {
- throw new \Exception('关键词不能为空');
- }
- if( model('WxKeywords')->existsKeyword($value)){
- throw new \Exception('关键词不能重复:' . $value);
- }
- $keywordData[] = [
- 'keyword' => $value,
- 'rule_id' => $ruleId,
- 'is_all' => $is_alls[$key]
- ];
- }
- }
- $result = Db::table($this->_wechatDb . '.nw_wx_keywords')->insertAll($keywordData);
- // 回复
- $replaysData = [];
- if ( $type) {
- $replaysData[] = [
- 'content' => $post['gift_replays'],
- 'rule_id' => $ruleId,
- ];
- }else{
- foreach ($replays as $k => $v) {
- $replaysData[] = [
- 'content' => $v,
- 'rule_id' => $ruleId,
- ];
- }
- }
- $result2 = Db::table($this->_wechatDb . '.nw_wx_replays')->insertAll($replaysData);
- // 礼包
- if ( $type) {
- $codeStr = $post['gift_code_' . $post['gift_status']];
- $codeArray = explode("\n", $codeStr);
- $codeArray = array_map('filterAndTrimInput', $codeArray);
- $codeArray = array_filter($codeArray);
- $libaocodeData = [];
- foreach ($codeArray as $key => $value) {
- $libaocodeData[] = [
- 'rule_id' => $ruleId,
- 'code' => $value,
- 'status' => $post['gift_status'] ? 0 : 2,
- ];
- }
- $result3 = Db::table($this->_wechatDb . '.nw_wx_keyword_libaocode')->insertAll($libaocodeData);
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $this->error("添加失败: " . $e->getMessage());
- }
- $this->success('操作成功!', url('index'));
- }
- return $this->fetch();
- }
- public function edit()
- {
- $id = $this->request->param('id',0,'intval');
- if (!$id){
- $this->error('ID不能为空');
- }
- if (request()->isPost()){
- $post = input('post.');
- $type = $post['type'];
- $data = [
- 'id' => $id,
- 'rule_name' => input('post.rule_name') ,
- 'status' => $type ? $post['gift_status'] : $post['status'],
- 'remark' => input('post.remark') ,
- 'update_time' => NOW_TIMESTAMP
- ];
- // 字段验证
- $verify = $this->verify($post,$id);
- if ( !$verify['code']) {
- $this->error( $verify['msg']);
- }
- Db::startTrans();
- try {
- $result = Db::table($this->_wechatDb . '.nw_wx_keyword_rule')->update($data);
- $keywordData = [];
- if ( $type) {
- if( model('WxKeywords')->existsKeyword($post['gift_keyword'],$id)){
- throw new \Exception('关键词不能重复:' . $post['gift_keyword']);
- }
- $keywordRes = Db::table($this->_wechatDb . '.nw_wx_keywords')
- ->where('rule_id',$id)
- ->update(['keyword' => $post['gift_keyword']]);
-
- }else{
- $keywords = $post['keyword'];
- $is_alls = $post['is_all'];
-
- // 判断关键词是否重复
- foreach ($keywords as $key => $value) {
- if ( empty($value)) {
- throw new \Exception('关键词不能为空');
- }
- if( model('WxKeywords')->existsKeyword($value,$id)){
- throw new \Exception('关键词不能重复:' . $value);
- }
- $keywordData[] = [
- 'keyword' => $value,
- 'rule_id' => $id,
- 'is_all' => $is_alls[$key]
- ];
- }
- $delRes = Db::table($this->_wechatDb . '.nw_wx_keywords')
- ->where('rule_id',$id)
- ->delete();
- $keywordRes = Db::table($this->_wechatDb . '.nw_wx_keywords')->insertAll($keywordData);
- }
- // 回复
- $replaysData = [];
- if ( $type) {
- $replaysRes = Db::table($this->_wechatDb . '.nw_wx_replays')
- ->where('rule_id',$id)
- ->update(['content' => $post['gift_replays']]);
-
- }else{
- $replays = $post['replays'];
- foreach ($replays as $k => $v) {
- $replaysData[] = [
- 'content' => $v,
- 'rule_id' => $id,
- ];
- }
- $delRes = Db::table($this->_wechatDb . '.nw_wx_replays')
- ->where('rule_id',$id)
- ->delete();
- $replaysRes = Db::table($this->_wechatDb . '.nw_wx_replays')->insertAll($replaysData);
- }
- // 礼包
- if ( $type && $post['gift_status'] == 0) {
- $codeRes = Db::table($this->_wechatDb . '.nw_wx_keyword_libaocode')
- ->where(['rule_id' => $id,'status' => 2])
- ->update(['code' => $post['gift_code_0']]);
-
- }
- if ( $type && $post['gift_status'] == 1) {
- $codeStr = $post['gift_code_add'];
- $codeArray = explode("\n", $codeStr);
- $codeArray = array_map('filterAndTrimInput', $codeArray);
- $codeArray = array_filter($codeArray);
- $libaocodeData = [];
- foreach ($codeArray as $key => $value) {
- $libaocodeData[] = [
- 'rule_id' => $id,
- 'code' => $value,
- ];
- }
- $codeRes = Db::table($this->_wechatDb . '.nw_wx_keyword_libaocode')->insertAll($libaocodeData);
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $this->error("编辑失败: " . $e->getMessage());
- }
- $this->success('操作成功!', url('index'));
- }
- $info = $this->model->find($id);
- $keywords = model('WxKeywords')->getKeywordsData($id);
- $replays = model('WxReplays')->getReplaysData($id);
- if ( $info['type'] == 1) {
- $code = model('WxKeywordLibaocode')->where(['rule_id'=>$id,'status'=> ['in',[0,2]]])->column('code', 'id');
- $this->assign('code', implode("\r\n", $code));
- }
- $this->assign('keywords',$keywords);
- $this->assign('replays',$replays);
- $this->assign('info',$info);
- return $this->fetch();
- }
- // 删除
- public function del()
- {
- $id = $this->request->param('id',0,'intval');
- if (!$id){
- $this->error('ID不能为空');
- }
- $info = Db::table($this->_wechatDb . '.nw_wx_keyword_rule')->find($id);
- if ( empty($info)) {
- $this->error('ID不存在');
- }
- Db::startTrans();
- try {
- // 伪删除
- Db::table($this->_wechatDb . '.nw_wx_keyword_rule')->where('id',$id)->update(['is_del' => 1]);
- Db::table($this->_wechatDb . '.nw_wx_keywords')->where('rule_id',$id)->delete();
- Db::table($this->_wechatDb . '.nw_wx_replays')->where('rule_id',$id)->delete();
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $this->error("删除失败: " . $e->getMessage());
- }
- $this->success('删除成功!', url('index'));
- }
- // 数据验证
- public function verify($data,$id = 0)
- {
- $result = [
- 'code' => 0,
- 'msg' => ''
- ];
- if ( empty($data['rule_name'])) {
- $result['msg'] = '规则名不能为空';
- return $result;
- }
- if ( $this->model->existsRuleName($data['rule_name'],$id)) {
- $result['msg'] = '规则名不能重复';
- return $result;
- }
- if ( $data['type'] == 1) {
- if ( empty($data['gift_keyword'])) {
- $result['msg'] = '关键词不能为空';
- return $result;
- }
- if ( empty($data['gift_replays'])) {
- $result['msg'] = '回复内容不能为空';
- return $result;
- }
- if ( empty($data['gift_code_' . $data['gift_status']])) {
- $result['msg'] = '礼包序列号不能为空';
- return $result;
- }
-
- }else{
- foreach ($data['replays'] as $k => $v){
- if ( empty($v)) {
- $result['msg'] = '回复内容不能为空';
- return $result;
- }
- }
- }
- $result['code'] = 1;
- return $result;
- }
- }
|