public function actionCreate() { $model = new MWxMenu(); $gh = Yii::$app->user->gh; $model->gh_id = $gh->gh_id; if ($model->load(Yii::$app->request->post())) { if ($model->is_sub_button) { $model->type = ''; $model->parent_id = 0; $model->url = ''; } if ($model->save()) { return $this->redirect(['index']); } } else { $model->sort_order = MWxMenu::getNextSortOrder($model->gh_id); return $this->render('create', ['model' => $model, 'gh' => $gh]); } }
public static function importFromWechat($gh_id) { $n = MWxMenu::deleteAll('gh_id=:gh_id', array(':gh_id' => $gh_id)); Yii::$app->wx->setGhId($gh_id); $arr = Yii::$app->wx->WxMenuGet(); $buttons = $arr['menu']['button']; $sort_order = self::WX_MENU_SORT_ORDER_FROM; foreach ($buttons as $button) { if (!empty($button['sub_button'])) { $model = new MWxMenu(); $model->gh_id = $gh_id; $model->name = $button['name']; $model->parent_id = 0; $model->sort_order = $sort_order; $sort_order += self::WX_MENU_SORT_ORDER_STEP; $model->is_sub_button = 1; if ($model->save()) { $sub_buttons = $button['sub_button']; foreach ($sub_buttons as $sub_button) { $subModel = new MWxMenu(); $subModel->gh_id = $gh_id; $subModel->name = $sub_button['name']; $subModel->type = $sub_button['type']; $subModel->keyword = empty($sub_button['key']) ? '' : $sub_button['key']; $subModel->url = empty($sub_button['url']) ? '' : $sub_button['url']; $subModel->parent_id = $model->wx_menu_id; $subModel->sort_order = $sort_order; $sort_order += self::WX_MENU_SORT_ORDER_STEP; if (!$subModel->save()) { U::W('save sub button err'); } } } else { U::W('save button err'); } } else { $model = new MWxMenu(); $model->gh_id = $gh_id; $model->name = $button['name']; $model->type = $button['type']; $model->keyword = empty($button['key']) ? '' : $button['key']; $model->url = empty($button['url']) ? '' : $button['url']; $model->parent_id = 0; $model->is_sub_button = 0; $model->sort_order = $sort_order; $sort_order += self::WX_MENU_SORT_ORDER_STEP; $model->save(); } } }