public function actionCreate() { $status = 1; $msg = '添加成功'; if ($_POST) { $parentId = intval($_POST['parentId']); $name = $_POST['name']; if ($parentId > 0) { if (mb_strlen($name, 'utf8') > 7) { $status = -1; $msg = '二级菜单名称不能超过7个汉字'; } //当前菜单的子菜单个数 $count = MenuModel::model()->count('wechatId = :wechatId and parentId=:parentId', array(':wechatId' => $this->wechatInfo->id, ':parentId' => $parentId)); if ($count >= 5) { $status = -1; $msg = '二级菜单不能超过5个'; } } else { if (mb_strlen($name, 'utf8') > 4) { $status = -1; $msg = '一级菜单名称不能超过4个汉字'; } //一级菜单个数 $count = MenuModel::model()->count('wechatId = :wechatId and parentId=:parentId', array(':wechatId' => $this->wechatInfo->id, ':parentId' => 0)); if ($count >= 3) { $status = -1; $msg = '一级菜单不能超过3个'; } } if ($status == 1) { $modelAction = new MenuactionModel(); $modelAction->action = $_POST['type'] == Globals::TYPE_URL ? $_POST['url'] : $_POST['action']; $model = new MenuModel(); $model->name = $name; $model->wechatId = $this->wechatInfo->id; $model->type = $_POST['type']; $model->parentId = $_POST['parentId'] ? $_POST['parentId'] : 0; //responseId 响应ID if ($_POST['type'] == Globals::TYPE_KEYWORDS || $_POST['type'] == Globals::TYPE_GIFT) { //如果是关键词则需要找到对应的responseId $keyword = KeywordsModel::model()->find("wechatId=:wechatId and name like concat('%',:name,'%') order by id desc", array(':wechatId' => $this->wechatInfo->id, ':name' => $_POST['action'])); if (!empty($keyword->responseId)) { $model->responseId = $keyword->responseId; } else { $msg = '不存在该关键词'; } } if ($model->validate() && $modelAction->validate()) { $model->save(); $modelAction->menuId = $model->id; $modelAction->save(); } else { $status = -1; $error = $model->getErrors(); if ($error) { foreach ($error as $e) { $msg .= $e[0]; } } } } } echo json_encode(array('status' => $status, 'msg' => $msg)); }