123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- <?php
- /**
- * Created by PhpStorm.
- * User: xiaofeng
- * Date: 2018/3/11
- * Time: 下午2:33
- */
- namespace backend\controllers;
- use backend\base\CommonController;
- use backend\base\Help;
- use common\models\CategoryCity;
- use Yii;
- use backend\server\UploadFile;
- use yii\helpers\ArrayHelper;
- class CategorycityController extends CommonController
- {
- public function actionTest()
- {
- $model = CategoryCity::find()->all();
- foreach ($model as $val)
- {
- $_m = clone $val;
- if($_m['pid'] == 0){
- $_m->level = 1;
- }
- if($_m['pid'] != 0){
- $_m->level = 2;
- }
- $_m->save(false);
- // $_m->save();
- }
- }
- /*
- * 区域列表页面
- * */
- public function actionCityhome()
- {
- return $this->render('cityhome');
- }
- /*
- * 区域数据
- * */
- public function actionCityhomeform()
- {
- $model = new CategoryCity();
- $input = Yii::$app->request->get();
- if(ArrayHelper::keyExists('id',$input))
- {
- $p = $model->FindById($input['id']);
- if(!empty($p))
- {
- if($p['pid'] == 0)
- {
- $model->pid = $p['id'];
- }
- if($p['pid'] != 0)
- {
- $model->id = $p['id'];
- $p = $model->FindById($p['pid']);
- }
- }
- }
- $model->state = [1,2];
- $row = $model->getList($input);
- if($row != null)
- {
- //重组数组,返回父类
- if(!empty($p))
- {
- array_push($row,$p->toArray());
- }
- foreach ($row as &$val)
- {
- $val['create_at'] = date('Y-m-d H:i',$val['create_at']);
- }
- return Help::JsonData(0,'成功',$model->Total(),$row);
- }
- }
- /*
- * 区域修改界面
- * */
- public function actionEdithome()
- {
- $model = new CategoryCity();
- $row = $model->FindById(Yii::$app->request->get('id'));
- if($row['level'] == 2){
- $model->pid = 0;
- }
- if($row['level'] == 3){
- $model->level = 2;
- }
- if($row)
- {
- $all = $model->getList(Yii::$app->request->post());
- return $this->render('edithome',['model'=>$row,'all'=>$all]);
- }
- }
- /*
- * 区域数据修改
- * */
- public function actionEdithomeform()
- {
- $input = Yii::$app->request->post();
- $model = new CategoryCity();
- $row = $model->FindById($input['id']);
- if(empty($row['a']))
- {
- $input['a'] = $input['pinyin'];
- }
- if(!empty($row))
- {
- if(isset($input['cityid']) && is_numeric($input['cityid']))
- {
- $path = $model->FindById($input['cityid']);
- $input['path'] = $path['path'].$path['id'].',';
- $input['pid'] = $path['id'];
- }
- if($row->load($input,'') && $row->save()) return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_success'));
- return Help::JsonCode(Help::ERROR,Yii::t('app','edit_error'),$row->errors);
- }
- }
- /*
- * 区域添加页面
- * */
- public function actionAddhome()
- {
- $model = new CategoryCity();
- $model->pid = 0;
- $result = $model->getList([],['city_name','id']);
- return $this->render('addhome',['model'=>$result,'city_info'=>Yii::$app->request->get()]);
- }
- /*
- * 添加区域数据
- * */
- public function actionAddcityform()
- {
- $model = new CategoryCity();
- $model->scenario = 'add';
- $input = Yii::$app->request->post('data');
- if(!isset($input['a']) || empty($input['a']))
- {
- $input['a'] = $input['pinyin'];
- }
- if(isset($input['pid']) && is_numeric($input['pid']))
- {
- $r = CategoryCity::findOne($input['pid']);
- if($r){
- $level = $r->level +1 ;
- $input['level'] = $level;
- }
- $input['path'] = '0,'.$input['pid'].',';
- }else{
- $input['level'] = 1;
- }
- if($model->load($input,'') && $model->save())
- {
- return Help::JsonCode(Help::SUCCESS,Yii::t('app','add_success'));
- }
- return Help::JsonCode(Help::ERROR,Yii::t('app','add_error'),$model->errors);
- }
- /*
- * 删除区域
- * */
- public function actionDelform()
- {
- $model = new CategoryCity();
- $row = $model->FindById(Yii::$app->request->post('id'));
- if($row != null)
- {
- $row->del = 2;
- if($row->update() == true) return Help::JsonCode(Help::SUCCESS,'删除成功');
- }
- return Help::JsonCode(Help::ERROR,'删除失败');
- }
- /**
- *2级联动数据
- */
- public function actionCitypid()
- {
- $model = new CategoryCity();
- $input = Yii::$app->request->post();
- $model->pid = $input['pid'];
- $rows = $model->getList([],['id','city_name']);
- if(!empty($rows)) return Help::JsonCode(Help::SUCCESS,Yii::t('app','get_success'),$rows);
- }
- public function actionEditall()
- {
- $model = new CategoryCity();
- $input = Yii::$app->request->post();
- $edit = $model->Edit($input);
- if($edit === true) return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_success'));
- return Help::JsonCode(Help::ERROR,Yii::t('app','edit_error'),$edit);
- // $row = $model->FindById($input['id']);
- // if(!empty($row))
- // {
- // if($row->load($input,'') && $row->save())
- // {
- // return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_success'));
- // }
- // else
- // {
- // return Help::JsonCode(Help::ERROR,Yii::t('app','edit_error'),$row->errors);
- // }
- // }
- // return Help::JsonCode(Help::ERROR,Yii::t('app','edit_error'));
- }
- public function actionDelcity(){
- $model = new CategoryCity();
- $input = Yii::$app->request->post();
- $row = $model->FindById($input['id']);
- if ($row){
- if ($row->level == 1 || $row->level == 2){
- $son = CategoryCity::find()->andWhere(['pid'=>$input['id']])->andWhere(['del'=>1])->exists();
- if ($son){
- return Help::JsonCode(Help::ERROR,'请先删除子区域');
- }
- $row->del = 2;
- }else{
- $row->del = 2;
- }
- if($row->save(false)){
- return Help::JsonCode(Help::SUCCESS,'删除成功');
- }
- return Help::JsonCode(Help::ERROR,'删除失败',$row->errors);
- }
- return Help::JsonCode(Help::ERROR,'删除失败');
- }
- }
|