コード例 #1
0
 public function actionCreate()
 {
     $post = Yii::$app->request->post();
     //判断名称
     if (empty($post['title'])) {
         throw new NotFoundHttpException(Yii::t('yii', 'Missing required parameters: {params}', ['params' => '菜单名称']));
     }
     $post['type'] = trim($post['type']);
     if (!empty($post['type']) && !in_array($post['type'], $this->menuTypes)) {
         throw new NotFoundHttpException(Yii::t('yii', '请填写正确的类型,必须在[' . implode(',', $this->menuTypes) . ']中选择一种'));
     }
     //获取传递过来的所属的父级ID
     $fid = !empty($post['fid']) ? intval($post['fid']) : 0;
     //获取最大允许的菜单数(自定义菜单最多包括3个一级菜单,每个一级菜单最多包含5个二级菜单)
     $maxMenuNum = !empty($fid) ? 5 : 3;
     //获取当前fid下的菜单个数
     $menuNum = CustomMenus::find()->where(['public_id' => $this->pid, 'fid' => intval($fid)])->count();
     if (intval($menuNum) >= $maxMenuNum) {
         throw new NotFoundHttpException(Yii::t('yii', '你创建的菜单在该级别菜单下已经达到上线,不能再创建'));
     }
     //把当前的公众号ID加入进去
     $post['public_id'] = $this->pid;
     $post['create_time'] = time();
     $post['update_time'] = time();
     $model = new CustomMenus();
     if ($model->load(['CustomMenus' => $post]) && $model->save()) {
         $model->order_id = $model->id;
         $model->save();
         //跳到对应级别的菜单列表下
         return $this->redirect(['custom-menus/index', 'pid' => $this->pid, 'fid' => $fid]);
     } else {
         throw new NotFoundHttpException(Yii::t('yii', 'An internal server error occurred.'));
     }
 }