CategorycityController.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/3/11
  6. * Time: 下午2:33
  7. */
  8. namespace backend\controllers;
  9. use backend\base\CommonController;
  10. use backend\base\Help;
  11. use common\models\CategoryCity;
  12. use Yii;
  13. use backend\server\UploadFile;
  14. use yii\helpers\ArrayHelper;
  15. class CategorycityController extends CommonController
  16. {
  17. public function actionTest()
  18. {
  19. $model = CategoryCity::find()->all();
  20. foreach ($model as $val)
  21. {
  22. $_m = clone $val;
  23. if($_m['pid'] == 0){
  24. $_m->level = 1;
  25. }
  26. if($_m['pid'] != 0){
  27. $_m->level = 2;
  28. }
  29. $_m->save(false);
  30. // $_m->save();
  31. }
  32. }
  33. /*
  34. * 区域列表页面
  35. * */
  36. public function actionCityhome()
  37. {
  38. return $this->render('cityhome');
  39. }
  40. /*
  41. * 区域数据
  42. * */
  43. public function actionCityhomeform()
  44. {
  45. $model = new CategoryCity();
  46. $input = Yii::$app->request->get();
  47. if(ArrayHelper::keyExists('id',$input))
  48. {
  49. $p = $model->FindById($input['id']);
  50. if(!empty($p))
  51. {
  52. if($p['pid'] == 0)
  53. {
  54. $model->pid = $p['id'];
  55. }
  56. if($p['pid'] != 0)
  57. {
  58. $model->id = $p['id'];
  59. $p = $model->FindById($p['pid']);
  60. }
  61. }
  62. }
  63. $model->state = [1,2];
  64. $row = $model->getList($input);
  65. if($row != null)
  66. {
  67. //重组数组,返回父类
  68. if(!empty($p))
  69. {
  70. array_push($row,$p->toArray());
  71. }
  72. foreach ($row as &$val)
  73. {
  74. $val['create_at'] = date('Y-m-d H:i',$val['create_at']);
  75. }
  76. return Help::JsonData(0,'成功',$model->Total(),$row);
  77. }
  78. }
  79. /*
  80. * 区域修改界面
  81. * */
  82. public function actionEdithome()
  83. {
  84. $model = new CategoryCity();
  85. $row = $model->FindById(Yii::$app->request->get('id'));
  86. if($row['level'] == 2){
  87. $model->pid = 0;
  88. }
  89. if($row['level'] == 3){
  90. $model->level = 2;
  91. }
  92. if($row)
  93. {
  94. $all = $model->getList(Yii::$app->request->post());
  95. return $this->render('edithome',['model'=>$row,'all'=>$all]);
  96. }
  97. }
  98. /*
  99. * 区域数据修改
  100. * */
  101. public function actionEdithomeform()
  102. {
  103. $input = Yii::$app->request->post();
  104. $model = new CategoryCity();
  105. $row = $model->FindById($input['id']);
  106. if(empty($row['a']))
  107. {
  108. $input['a'] = $input['pinyin'];
  109. }
  110. if(!empty($row))
  111. {
  112. if(isset($input['cityid']) && is_numeric($input['cityid']))
  113. {
  114. $path = $model->FindById($input['cityid']);
  115. $input['path'] = $path['path'].$path['id'].',';
  116. $input['pid'] = $path['id'];
  117. }
  118. if($row->load($input,'') && $row->save()) return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_success'));
  119. return Help::JsonCode(Help::ERROR,Yii::t('app','edit_error'),$row->errors);
  120. }
  121. }
  122. /*
  123. * 区域添加页面
  124. * */
  125. public function actionAddhome()
  126. {
  127. $model = new CategoryCity();
  128. $model->pid = 0;
  129. $result = $model->getList([],['city_name','id']);
  130. return $this->render('addhome',['model'=>$result,'city_info'=>Yii::$app->request->get()]);
  131. }
  132. /*
  133. * 添加区域数据
  134. * */
  135. public function actionAddcityform()
  136. {
  137. $model = new CategoryCity();
  138. $model->scenario = 'add';
  139. $input = Yii::$app->request->post('data');
  140. if(!isset($input['a']) || empty($input['a']))
  141. {
  142. $input['a'] = $input['pinyin'];
  143. }
  144. if(isset($input['pid']) && is_numeric($input['pid']))
  145. {
  146. $r = CategoryCity::findOne($input['pid']);
  147. if($r){
  148. $level = $r->level +1 ;
  149. $input['level'] = $level;
  150. }
  151. $input['path'] = '0,'.$input['pid'].',';
  152. }else{
  153. $input['level'] = 1;
  154. }
  155. if($model->load($input,'') && $model->save())
  156. {
  157. return Help::JsonCode(Help::SUCCESS,Yii::t('app','add_success'));
  158. }
  159. return Help::JsonCode(Help::ERROR,Yii::t('app','add_error'),$model->errors);
  160. }
  161. /*
  162. * 删除区域
  163. * */
  164. public function actionDelform()
  165. {
  166. $model = new CategoryCity();
  167. $row = $model->FindById(Yii::$app->request->post('id'));
  168. if($row != null)
  169. {
  170. $row->del = 2;
  171. if($row->update() == true) return Help::JsonCode(Help::SUCCESS,'删除成功');
  172. }
  173. return Help::JsonCode(Help::ERROR,'删除失败');
  174. }
  175. /**
  176. *2级联动数据
  177. */
  178. public function actionCitypid()
  179. {
  180. $model = new CategoryCity();
  181. $input = Yii::$app->request->post();
  182. $model->pid = $input['pid'];
  183. $rows = $model->getList([],['id','city_name']);
  184. if(!empty($rows)) return Help::JsonCode(Help::SUCCESS,Yii::t('app','get_success'),$rows);
  185. }
  186. public function actionEditall()
  187. {
  188. $model = new CategoryCity();
  189. $input = Yii::$app->request->post();
  190. $edit = $model->Edit($input);
  191. if($edit === true) return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_success'));
  192. return Help::JsonCode(Help::ERROR,Yii::t('app','edit_error'),$edit);
  193. // $row = $model->FindById($input['id']);
  194. // if(!empty($row))
  195. // {
  196. // if($row->load($input,'') && $row->save())
  197. // {
  198. // return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_success'));
  199. // }
  200. // else
  201. // {
  202. // return Help::JsonCode(Help::ERROR,Yii::t('app','edit_error'),$row->errors);
  203. // }
  204. // }
  205. // return Help::JsonCode(Help::ERROR,Yii::t('app','edit_error'));
  206. }
  207. public function actionDelcity(){
  208. $model = new CategoryCity();
  209. $input = Yii::$app->request->post();
  210. $row = $model->FindById($input['id']);
  211. if ($row){
  212. if ($row->level == 1 || $row->level == 2){
  213. $son = CategoryCity::find()->andWhere(['pid'=>$input['id']])->andWhere(['del'=>1])->exists();
  214. if ($son){
  215. return Help::JsonCode(Help::ERROR,'请先删除子区域');
  216. }
  217. $row->del = 2;
  218. }else{
  219. $row->del = 2;
  220. }
  221. if($row->save(false)){
  222. return Help::JsonCode(Help::SUCCESS,'删除成功');
  223. }
  224. return Help::JsonCode(Help::ERROR,'删除失败',$row->errors);
  225. }
  226. return Help::JsonCode(Help::ERROR,'删除失败');
  227. }
  228. }