SysconfigController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace app\modules\admin\controllers;
  3. use app\common\controllers\BController;
  4. use app\modules\admin\models\Sysconfig;
  5. use Yii;
  6. class SysconfigController extends BController
  7. {
  8. public $layout = 'main';
  9. public function actionList()
  10. {
  11. $query = Sysconfig::find();
  12. if (Yii::$app->request->isAjax) {
  13. $data = [];
  14. $params = Yii::$app->request->get('Sysconfig');
  15. $query = mergeParams($query,$params);
  16. $countQuery = clone $query;
  17. //分页
  18. if(isset($_GET['limit'])){ $query->limit(intval($_GET['limit']));}
  19. if(isset($_GET['offset'])){ $query->offset(intval($_GET['offset']));}
  20. //排序
  21. if(isset($_GET['sort'])&&isset($_GET['sortOrder']))
  22. {
  23. $sortWay = $_GET['sortOrder']=='asc'?SORT_ASC:SORT_DESC;
  24. $resultList = $query->orderBy([$_GET['sort']=>$sortWay])->all();
  25. }
  26. else
  27. {
  28. $resultList = $query->orderBy(['id'=>SORT_ASC])->all();
  29. }
  30. foreach($resultList as $result)
  31. {
  32. $data[] = array('id'=>$result->id,'name'=>$result->name,'bak'=>$result->bak,'type'=>$result->type,'typeTxt'=>Sysconfig::typeOptions($result->type),'is_system'=>$result->is_system, 'list_order'=>$result->list_order,'disabled'=>$result->disabled);
  33. }
  34. $result = ["total"=>$countQuery->count(),"totalNotFiltered"=>$countQuery->count(),"rows"=>$data];
  35. echo_json($result);
  36. }
  37. $this->tableTitle = array(
  38. array('field'=>'id','title'=>Sysconfig::getAttributeName('id'),'align'=>'center','sortable'=>true,'class'=>'col-md-2'),
  39. array('field'=>'bak','title'=>Sysconfig::getAttributeName('bak'),'align'=>'center','class'=>'col-md-2'),
  40. array('field'=>'name','title'=>Sysconfig::getAttributeName('name'),'align'=>'center','sortable'=>true,'class'=>'col-md-2'),
  41. array('field'=>'typeTxt','title'=>Sysconfig::getAttributeName('type'),'align'=>'center','class'=>'col-md-2'),
  42. array('field'=>'disabled','title'=>Sysconfig::getAttributeName('disabled'),'align'=>'center','formatter'=>'switchFormatter','class'=>'col-md-2'),
  43. array('field'=>'operate','title'=>Sysconfig::getAttributeName('operate'),'align'=>'center','events'=>'window.operateEvents','formatter'=>'operateFormatter','class'=>'col-md-2'),
  44. );
  45. $this->tableConfig = array('table'=>Sysconfig::shortTableName(),'url'=>$this->createRealUrl(['admin/sysconfig/list']),'setFieldUrl'=>$this->createRealUrl(['admin/sysconfig/setfield']),'idField'=>Sysconfig::modelPrimaryKey(),'checkbox'=>0,'dropmenu'=>1,'pagination'=>false,'pagesize'=>2,'refresh'=>true);
  46. return $this->render('list',array('model'=>new Sysconfig()));
  47. }
  48. //添加
  49. function actionAdd()
  50. {
  51. $model = new Sysconfig();
  52. if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){
  53. if(!$model->validate())
  54. {
  55. $msgdata = ['error' => 1,'msg' => $model->returnFirstError()];
  56. }
  57. else
  58. {
  59. if($model->save())
  60. {
  61. $msgdata = ['error' => 0,'msg' => '操作成功'];
  62. }
  63. else
  64. {
  65. $msgdata = ['error' => 1,'msg' => '操作失败'];
  66. }
  67. }
  68. echo_json($msgdata);
  69. }
  70. return $this->renderAjax('add',array('model'=>$model));
  71. }
  72. //编辑
  73. function actionEdit()
  74. {
  75. $id = $this->getKeyId();
  76. $model = Sysconfig::findOne($id);
  77. check_record_exists($model);
  78. if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){
  79. $post = Yii::$app->request->post();
  80. $model->is_system = $post['Sysconfig']['is_system']?$post['Sysconfig']['is_system']:0;
  81. $model->disabled = $post['Sysconfig']['disabled']?$post['Sysconfig']['disabled']:0;
  82. if(!$model->validate())
  83. {
  84. $msgdata = ['error' => 1,'msg' => $model->returnFirstError()];
  85. }
  86. else
  87. {
  88. if($model->save())
  89. {
  90. $msgdata = ['error' => 0,'msg' => '成功'];
  91. }
  92. else
  93. {
  94. $msgdata = ['error' => 1,'msg' => '失败'];
  95. }
  96. }
  97. echo_json($msgdata);
  98. }
  99. return $this->renderAjax('add',array('model'=>$model));
  100. }
  101. //配置
  102. function actionConfig()
  103. {
  104. $id = $this->getKeyId();
  105. $model = Sysconfig::findOne($id);
  106. check_record_exists($model);
  107. if(Yii::$app->request->isAjax&&Yii::$app->request->post()){
  108. $data = Yii::$app->request->post();
  109. if($model->type==1)
  110. {
  111. foreach($data['group-a'] as $uintData)
  112. {
  113. $value[$uintData['k']] = $uintData['v'];
  114. }
  115. if(!empty($value))$model->value = json_encode($value);
  116. }
  117. else if($model->type==2)
  118. {
  119. foreach($data['group-a'] as $uintData)
  120. {
  121. $value[] = $uintData['v'];
  122. }
  123. if(!empty($value))$model->value = json_encode($value);
  124. }
  125. else if($model->type==3)
  126. {
  127. $model->value = $data['v'];
  128. }
  129. if($model->save())
  130. {
  131. $msgdata = ['error' => 0,'msg' => '操作成功'];
  132. }
  133. else
  134. {
  135. $msgdata = ['error' => 1,'msg' => '操作失败'];
  136. }
  137. echo_json($msgdata);
  138. }
  139. if($model->type==1)
  140. {
  141. $view = 'config1';
  142. }
  143. else if($model->type==2)
  144. {
  145. $view = 'config2';
  146. }
  147. else if($model->type==3)
  148. {
  149. $view = 'config3';
  150. }
  151. if($model->type==3)
  152. {
  153. $args = $model->value;
  154. }
  155. else
  156. {
  157. if(!empty($model->value))
  158. {
  159. $args = json_decode($model->value,true);
  160. }
  161. else
  162. {
  163. $args = [];
  164. }
  165. }
  166. return $this->renderAjax($view,array('model'=>$model,'args'=>$args));
  167. }
  168. //删除
  169. public function actionDel()
  170. {
  171. $id = $this->getKeyId();
  172. $model = Sysconfig::findOne($id);
  173. check_record_exists($model);
  174. if($model->delete())
  175. {
  176. $msgdata = ['error' => 0,'msg' => '操作成功!'];
  177. }
  178. else
  179. {
  180. $msgdata = ['error' => 1,'msg' => '操作失败!'];
  181. }
  182. echo_json($msgdata);
  183. }
  184. }