123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662 |
- <?php
- namespace app\controllers;
- use app\common\helpers\Identify;
- use app\common\widget\LinkmenuWidget;
- use app\models\Linkmenu;
- use app\modules\admin\models\Sysconfig;
- use app\modules\car\models\FCarInfo;
- use app\modules\cms\models\Category;
- use app\common\components\CacheId;
- use app\modules\cms\models\CategoryConfig;
- use app\common\components\FormElements;
- use app\common\helpers\Cookie;
- use app\modules\doc\models\DocDownlog;
- use app\modules\doc\models\DocReal;
- use app\modules\doc\models\DocRealData;
- use app\models\VisitStatisticsByDay;
- use app\modules\ucenter\models\UserHistory;
- use Yii;
- use yii\web\Controller;
- class AjaxController extends Controller
- {
- public $enableCsrfValidation = false;
- public $userInfo;
- public function init()
- {
- $this->userInfo = Identify::getUserInfo();
- }
- //处理文档下载请求
- public function actionDowndoc()
- {
- $docConfigResult = Yii::$app->db->createCommand("select * from {{%config}} where name='doc'")->queryOne();
- $docConfig = string2array($docConfigResult['value']);
- $mimes = getMimes();
- $direct = Yii::$app->request->get('direct');
- $downcode = Yii::$app->request->get('downcode');
- $downcodeModel = \app\modules\doc\models\DocDowncode::find()->where("downcode='".$downcode."'")->one();
- if($downcodeModel&&$downcodeModel->doc_id)
- {
- $doc = DocReal::findOne($downcodeModel->doc_id);
- $docData = DocRealData::findOne($downcodeModel->doc_id);
- if($doc&&$docData)
- {
- $downFlag = false;
- //普通下载场景需要校验用户
- if((!empty($this->userInfo['user_id'])&&$downcodeModel->user_id == $this->userInfo['user_id'])||$direct==1)
- {
- $downFlag = true;
- }
- if($downFlag)
- {
- //生成下载日志
- $downlog = DocDownlog::find()->where("user_id=".intval($this->userInfo['user_id'])." and doc_id=".$doc->id)->one();
- if($downlog)
- {
- $downlog->updateCounters(['count'=>1]);
- }
- else
- {
- $downlog = new DocDownlog();
- $downlog->user_id = intval($this->userInfo['user_id'])?intval($this->userInfo['user_id']):intval($downcodeModel->user_id);
- $downlog->doc_id = $doc->id;
- $downlog->cat_id = $doc->cat_id;
- $downlog->doc_type = $doc->doc_type;
- $downlog->count = 1;
- $downlog->create_time = TIMESTAMP;
- $downlog->save();
- }
- //更新文档被下载次数
- $doc->updateCounters(['downs'=>1]);
- //输出文件下载请求
- $file = sys_auth($docData->file,'DECODE',$docData->hash);
- //对于大文件,生成种子文件
- if($doc->filesize>=$docConfig['bigfilesize']*1024*1024)
- {
- $tempFile = locateFile(getFileUrl($file),'download');
- $file = initBigfileTorent($tempFile,$doc,$docConfig);
- $bigFileTip = '【下载说明】';
- }
- else if($doc->doc_type==3)//网盘资源
- {
- $file = initPanfile($doc,$docConfig);
- $bigFileTip = '【提取说明】';
- }
- else if($doc->doc_type==2)//合辑文档
- {
- $allsize = 0;
- $docIds = Yii::$app->db->createCommand("SELECT doc_id FROM {{%doc_col}} where col_id=".$doc->id)->queryColumn();
- $resultList = DocReal::findAll($docIds);
- foreach($resultList as $result)
- {
- $allsize += $result->filesize;
- $contentList[] = array('title'=>$result->title,'ext'=>$result->ext,'file'=>sys_auth($result->data->file,'DECODE',$result->data->hash));
- }
- $tempFile = locateColFile($doc,$contentList);
- $file = initBigfileTorent($tempFile,$doc,$docConfig);
- $bigFileTip = '【下载说明】';
- }
- $ext = fileext($file);
- $mime = $mimes[$ext];
- if(is_array($mime))$mime = $mime[0];
- $fileContents = file_get_contents(getFileUrl($file));
- $downcodeModel->used = 1;
- $downcodeModel->save();
- ob_clean();
- header("Content-Type: $mime; charset=UTF-8");
- if(preg_match("/Safari/", $_SERVER["HTTP_USER_AGENT"]))
- {
- $filename = rawurlencode($doc->title.$bigFileTip);
- $filename = iconv('utf-8', 'gbk', $filename);
- header( 'Content-Disposition: attachment; filename*=UTF-8\'\''. $filename.'.'.$ext);
- }
- else
- {
- header("Content-Disposition:attachment;filename=".rawurlencode(rtrim($doc->title.$bigFileTip,".")).'.'.$ext);
- }
- echo $fileContents;
- exit;
- }
- }
- }
- }
- //判断下载请求处理状态
- public function actionCheckdown()
- {
- $downcode = Yii::$app->request->get('downcode');
- $downcodeModel = \app\modules\doc\models\DocDowncode::find()->where("downcode='".$downcode."'")->one();
- if($downcodeModel->used==1)
- {
- $result = [
- 'error' => 0,
- 'msg' => '处理完毕',
- 'data' => [],
- 'code'=>200
- ];
- echo_json($result);
- }
- }
- //联动菜单下级数据获取
- public function actionNextlinkmenu()
- {
- $parent_id = isset($_GET['parent_id'])?intval($_GET['parent_id']):0;
- if(empty($parent_id))
- {
- $result = array('error'=>1,'msg'=>'获取失败');
- }
- else
- {
- $parentMenu = Linkmenu::findOne($parent_id);
- if(empty($parentMenu))
- {
- $result = array('error'=>1,'msg'=>'获取失败');
- }
- else
- {
- $query = Linkmenu::find();
- if($parentMenu->key_id==0){
- $params =['key_id'=>$parent_id,'parent_id'=>0,'disabled'=>0];
- }
- else
- {
- $params =['parent_id'=>$parent_id,'disabled'=>0];
- }
- $query = mergeParams($query,$params);
- $menuList = $query->orderBy(['list_order'=>SORT_ASC])->all();
- if(is_array($menuList))foreach($menuList as $menu)
- {
- $data[] = array('menu_id'=>$menu->menu_id,'name'=>$menu->name);
- }
- $result = array('error'=>0,'msg'=>'获取成功','data'=>$data);
- echo_json($result);
- }
- }
- }
- //获取下级栏目
- public function actionNextlinkcat()
- {
- $parent_id = isset($_GET['parent_id'])?intval($_GET['parent_id']):0;
- $content_model_id = isset($_GET['content_model_id'])?intval($_GET['content_model_id']):0;
- if(empty($parent_id)&&empty($content_model_id))
- {
- $result = array('error'=>1,'msg'=>'获取失败');
- }
- else
- {
- $catList = json_decode(Yii::$app->cache->get(CacheId::categoryItemsCacheId($content_model_id)),true);
- $data = [];
- if($content_model_id&&empty($parent_id))
- {
- if(is_array($catList))foreach($catList as $cat)
- {
- if($cat['parent_id']==0)
- {
- $data[] = array('cat_id'=>$cat['cat_id'],'cat_name'=>$cat['cat_name']);
- }
- }
- }
- else if($content_model_id&&$parent_id)
- {
- if(is_array($catList))foreach($catList as $cat)
- {
- if($cat['parent_id']==$parent_id)
- {
- $data[] = array('cat_id'=>$cat['cat_id'],'cat_name'=>$cat['cat_name']);
- }
- }
- }
- $result = array('error'=>0,'msg'=>'获取成功','data'=>$data);
- }
- echo_json($result);
- }
- //获取栏目扩展配置
- public function actionGetextconfig()
- {
- $cat_id = isset($_GET['cat_id'])?intval($_GET['cat_id']):0;
- $id = isset($_GET['id'])?intval($_GET['id']):0;
- $realStr = !empty($_GET['real'])?'_real':'';
- if(empty($cat_id))
- {
- $result = array('error'=>1,'msg'=>'获取失败');
- }
- else
- {
- $category = Category::findOne($cat_id);
- //栏目面包屑
- $catTitTree = [];
- $catBreadInfos = Category::getParentCatRs($cat_id);
- if(is_array($catBreadInfos))foreach($catBreadInfos as $catBread)
- {
- $catTitTree[] = $catBread['cat_name'];
- }
- $catTitTree = join(" > ",$catTitTree);
- $model_list = json_decode(Yii::$app->cache->get(\app\common\components\CacheId::modelListCacheId()),true);
- $model = $model_list[$category->content_model_id];
- $table_1_name = $model['table_name'].$realStr;
- $table_2_name = $model['table_name'].$realStr.'_data';
- $model_name = getModelName($table_1_name);
- if($category->arr_parent_ids)
- {
- $extTypeList = CategoryConfig::find()->where("cat_id=".$cat_id." or cat_id in(".trim($category->arr_parent_ids,',').")")->orderBy(['cat_id'=>SORT_ASC,'list_order'=>SORT_ASC])->limit(5)->asArray()->all();
- }
- else
- {
- $extTypeList = CategoryConfig::find()->where("cat_id=".$cat_id)->orderBy(['cat_id'=>SORT_ASC,'list_order'=>SORT_ASC])->limit(5)->asArray()->all();
- }
- if($id)
- {
- $connection = Yii::$app->db;
- $sql = "select * from {{%".$table_1_name."}} a left join {{%".$table_2_name."}} b on a.id=b.id where a.id = ".$id."";
- $command=$connection->createCommand($sql);
- $row=$command->queryOne();
- }
- else
- {
- $row = [];
- }
- if(is_array($extTypeList))foreach($extTypeList as $k=>$ext_type)
- {
- $simpleField = 'ext_type_'.($k+1);
- $ext_type['var_name'] = $simpleField;
- $optionList = [];
- $field = $model_name?$model_name.'['.$ext_type['var_name'].']':$ext_type['var_name'];
- $fieldId = str_replace(array('[',']'),array('-',''),strtolower($field));
- $value = $row[$simpleField];
- if($ext_type['type']==1)
- {
- $optionList = string2array($ext_type['value']);
- $formlist[] = array(
- 'label'=>$ext_type['bak'],
- 'html'=> FormElements::select($optionList,$value,"请选择","name='$field' id='$fieldId' lay-ignore class='form-select ' "),
- 'optionList'=>$optionList,
- 'type'=>$ext_type['type'],
- );
- }
- else if($ext_type['type']==2)
- {
- $options = string2array($ext_type['value']);
- foreach($options as $v)
- {
- $optionList[$v] = $v;
- }
- $formlist[] = array(
- 'label'=>$ext_type['bak'],
- 'html'=> FormElements::select($optionList,$value,"请选择","name='$field' id='$fieldId' lay-ignore class='form-select '"),
- 'optionList'=>$optionList,
- 'type'=>$ext_type['type'],
- );
- }
- else if($ext_type['type']==3)
- {
- $sysconfig = Sysconfig::find()->where("name='".$ext_type['value']."'")->one();
- if($sysconfig->type==1) $optionList = json_decode($sysconfig->value,true);
- if($sysconfig->type==2)
- {
- $options = json_decode($sysconfig->value,true);
- foreach($options as $v)
- {
- $optionList[$v] = $v;
- }
- }
- $formlist[] = array(
- 'label'=>$ext_type['bak'],
- 'html'=> FormElements::select($optionList,$value,"请选择","name='$field' id='$fieldId' lay-ignore class='form-select '"),
- 'optionList'=>$optionList,
- 'type'=>$ext_type['type'],
- );
- }
- else if($ext_type['type']==4)
- {
- $array = array();
- $array['key_id']= $ext_type['value'];
- $array['id']= $fieldId;
- $array['name']= $field;
- $array['level']= $ext_type['level']?$ext_type['level']:2;
- $array['emptyTxts']= ['请选择','请选择','请选择'];
- if(isset($value))$array['value']= $value;
- $array['selectClass']= 'form-select ';
- $array['blockClass']= 'mb-3 row';
- $array['unitClass']= 'col-md-'.(12/$ext_type['level']);
- $array['unitStyle'] = 'padding-right:10px;';
- $html = LinkmenuWidget::widget($array);
- $formlist[] = array(
- 'label'=>$ext_type['bak'],
- 'html'=> $html,
- 'optionList'=>[],
- 'type'=>$ext_type['type'],
- );
- }
- }
- $result = array('error'=>0,'msg'=>'','data'=>['formlist'=>$formlist,'catTitTree'=>$catTitTree]);
- }
- echo_json($result);
- }
- //生成二维码
- public function actionQrcode()
- {
- ob_clean();
- $url = $_GET['url'];
- $qrcode = new \app\components\qrcode\qrcode($url);
- $qrcode->create();
- }
- //生成含入口脚本的URL地址
- public function createRealUrl($params)
- {
- return urldecode(Yii::$app->urlManager->createAbsoluteUrl($params)) ;
- }
- //记录显示模式
- public function actionSetshowmode()
- {
- $showmode = $_GET['showmode'];
- $pageflag = $_GET['pageflag'];
- if(!empty($showmode)&&!empty($pageflag))
- {
- Cookie::setCookie($pageflag,$showmode);
- }
- }
- //处理前台加载文档页面
- public function actionLoaddocpage()
- {
- ini_set('pcre.backtrack_limit', 999999999);
- $page = Yii::$app->request->get('page');
- $type = Yii::$app->request->get('type');
- $id = Yii::$app->request->get('id');
- if(!empty($page)&&!empty($type)&&!empty($id))
- {
- $docData = DocRealData::findOne($id);
- //文档内容
- $imgcode = string2array($docData->imgcode);
- $h5code = string2array($docData->h5code);
- if($type=='h5')
- {
- $result = https_request(getFileUrl($h5code[$page]));
- //过滤时段
- $badwordConfigResult = Yii::$app->db->createCommand("select * from {{%config}} where name='badword'")->queryOne();
- $badwordConfig = string2array($badwordConfigResult['value']);
- $filter = true;
- $hournow = get_date(TIMESTAMP,'H');
- if($badwordConfig['hours'])
- {
- $hours = explode(",",$badwordConfig['hours']);
- if(!in_array($hournow,$hours))
- {
- $filter = false;
- }
- }
- //爬虫过滤
- if(isSpider()&&!$badwordConfig['open_spider'])
- {
- $filter = false;
- }
- if($filter)
- {
- $sesResult = sensitive($result);
- if(!empty($sesResult['after']))
- {
- $result = $sesResult['after'];
- }
- }
- }
- else
- {
- $result = "<img class='imgmode_unit' src='".getFileUrl($imgcode[$page-1])."'>";
- }
- echo_json(array('error'=>'0','msg'=>'获取成功','data'=>$result));
- }
- }
- //处理前台加载文档页面
- public function actionLoaddocpagedirect()
- {
- ini_set('pcre.backtrack_limit', 999999999);
- $page = Yii::$app->request->get('page');
- $type = Yii::$app->request->get('type');
- $id = Yii::$app->request->get('id');
- if(!empty($page)&&!empty($type)&&!empty($id))
- {
- $docData = DocRealData::findOne($id);
- //文档内容
- $imgcode = string2array($docData->imgcode);
- $h5code = string2array($docData->h5code);
- if($type=='h5')
- {
- $result = https_request(getFileUrl($h5code[$page]));
- //过滤时段设置
- $badwordConfigResult = Yii::$app->db->createCommand("select * from {{%config}} where name='badword'")->queryOne();
- $badwordConfig = string2array($badwordConfigResult['value']);
- $filter = true;
- $hournow = get_date(TIMESTAMP,'H');
- if($badwordConfig['hours'])
- {
- $hours = explode(",",$badwordConfig['hours']);
- if(!in_array($hournow,$hours))
- {
- $filter = false;
- }
- }
- //爬虫过滤
- if(isSpider()&&!$badwordConfig['open_spider'])
- {
- $filter = false;
- }
- if($filter)
- {
- $sesResult = sensitive($result);
- if(!empty($sesResult['after']))
- {
- $result = $sesResult['after'];
- }
- }
- }
- else
- {
- $result = "<img class='imgmode_unit' src='".getFileUrl($imgcode[$page-1])."'>";
- }
- echo $result;
- exit;
- }
- }
- //处理前台加载多媒体文档数据
- public function actionLoaddocblob()
- {
- $id = Yii::$app->request->get('id');
- if(!empty($id))
- {
- $docData = DocRealData::findOne($id);
- //文档内容
- $h5code = string2array($docData->h5code);
- $media_file = getFileUrl($h5code[0]);
- ob_clean();
- echo https_request($media_file);
- }
- }
- //访问记录
- public function actionVisitrecord()
- {
- $table_name = Yii::$app->request->get('table_name');
- $data_id = Yii::$app->request->get('data_id');
- $cat_id = Yii::$app->request->get('cat_id');
- if(!empty($table_name)&&!empty($data_id))
- {
- $tableLogDate = Yii::$app->db->tablePrefix."visit_record_".date('Ymd');
- $preLogTable = Yii::$app->db->tablePrefix."visit_record_".date('Ymd',TIMESTAMP-24*3600);
- $sqlbuf = "show tables like '".$tableLogDate."';";
- $result = Yii::$app->db->createCommand($sqlbuf)->queryOne();
- if(!$result)
- {
- $sqlbuf = "create table if not exists $tableLogDate like {{%visit_record_t}};";
- Yii::$app->db->createCommand($sqlbuf)->execute();
- $sqlbuf = "show tables like '".$preLogTable."';";
- $exist = Yii::$app->db->createCommand($sqlbuf)->queryAll();
- if($exist)
- {
- $sqlbuf = "drop table $preLogTable;";
- Yii::$app->db->createCommand($sqlbuf)->execute();
- }
- }
- $visit_ip = MYIP;
- $visit_time = TIMESTAMP;
- $request_from = REQUEST_FROM;
- $sqlbuf="insert into $tableLogDate(table_name,data_id,cat_id,request_from,visit_ip,visit_time)values('".$table_name."',$data_id,$cat_id,$request_from,$visit_ip,$visit_time)";
- Yii::$app->db->createCommand($sqlbuf)->execute();
- $date = str_to_time(get_date(TIMESTAMP,'Y-m-d'));
- $stModel = VisitStatisticsByDay::find()->where("table_name='".$table_name."' and data_id = $data_id and date='".$date."'")->one();
- if(empty($stModel))
- {
- $stModel = new VisitStatisticsByDay();
- $stModel->table_name = $table_name;
- $stModel->data_id = $data_id;
- $stModel->cat_id = $cat_id;
- $stModel->date = $date;
- $stModel->pv=1;
- $stModel->uv=1;
- $stModel->save();
- }
- else
- {
- $sqlbuf = "select count(distinct(visit_ip)) as uv from $tableLogDate where table_name='".$table_name."' and data_id = $data_id";
- $result = Yii::$app->db->createCommand($sqlbuf)->queryOne();
- $uv = $result['uv'];
- $stModel->uv = $uv;
- $stModel->pv = $stModel->pv+1;
- $stModel->save();
- }
- //查询数据表主键
- $primaryKey = getPrikeyByTableName($table_name);
- if($primaryKey)
- {
- $sqlbuf = "update {{%$table_name}} set views=views+1 where $primaryKey = $data_id";
- Yii::$app->db->createCommand($sqlbuf)->execute();
- }
- //写历史记录
- if($table_name&&Identify::hasLogined())
- {
- $history = new UserHistory();
- $history->table_name = $table_name;
- $history->data_id = $data_id;
- $history->user_id = Identify::getUserInfo(NULL,'user_id');
- $history->create_time = TIMESTAMP;
- $history->save();
- }
- }
- }
- //根据车牌搜索车辆
- public function actionSearchcar()
- {
- $kw = Yii::$app->request->get('kw');
- $city1 = Yii::$app->request->get('city1');
- $city2 = Yii::$app->request->get('city2');
- $city3 = Yii::$app->request->get('city3');
- $params = [];
- $sql = "select card_num from wz_f_car_info ";
- if(!empty($kw)) $params[] = "card_num like '%".$kw."%'";
- if(!empty($city1) && $city1!='全省') $params[] = "city = '".$city1."'";
- if(!empty($city2) && $city2!='地市') $params[] = "dpt_sec = '".$city2."'";
- if(!empty($city3) && $city3!='网格') $params[] = "grid = '".$city3."'";
- if($params) $sql .= " where ".implode(' and ',$params);
- $sql .= "group by card_num limit 20";
- //if(!empty($kw))
- //{
- $resultList = Yii::$app->db->createCommand($sql)->queryAll();
- foreach($resultList as $result)
- {
- $carList[] = $result['card_num'];
- }
- $result = [
- 'error' => 0,
- 'msg' => '搜索成功',
- 'data' => ['carList'=>$carList],
- 'code'=>200
- ];
- echo_json($result);
- //}
- }
- //下拉车牌搜索
- public function actionSelectcar()
- {
- $kw = Yii::$app->request->post('card_num');
- $city1 = Yii::$app->request->post('city1');
- $city2 = Yii::$app->request->post('city2');
- $city3 = Yii::$app->request->post('city3');
- $page = Yii::$app->request->post('pageNumber')?:1;
- $pagesize = Yii::$app->request->post('pageSize')?:10;
- $params = [];
- if(empty($kw)) $kw = Yii::$app->request->post('searchValue');
- //$sql = "select card_num from wz_f_car_info ";
- if(!empty($kw)) $params[] = "card_num like '%".strtoupper($kw)."%'";
- $ci = ['全省','地市','网格','单位','一级单位','二级单位','三级单位'];
- if(!empty($city1) && !in_array($city1,$ci)) $params[] = "city = '".$city1."'";
- if(!empty($city2) && !in_array($city2,$ci)) $params[] = "dpt_sec = '".$city2."'";
- if(!empty($city3) && !in_array($city3,$ci)) $params[] = "grid = '".$city3."'";
- //if($params) $sql .= " where ".implode(' and ',$params);
- $query = FCarInfo::find()->select('card_num')->where(join(' and ',$params))->groupBy('card_num');
- $countQuery = clone $query;
- $resultList = $query->limit(intval($pagesize))->offset(intval(($page-1)*$pagesize))->orderBy(['card_num' => SORT_DESC])->asArray()->all();
- $result = [
- 'error' => 0,
- 'msg' => '搜索成功',
- 'totalRow' => $countQuery->count(),
- 'list' => $resultList,
- 'code'=>200
- ];
- echo_json($result);
- //}
- }
- }
|