validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } } public static function getTemplateEdit() { $id = get_params('id', 0); $template = DiyConfig::getSetting($id); return [ 'code' => 0, 'msg' => '加载成功', 'data' => [ //'modules_list' => DiyConfig::getConfig(), 'template' => $template ? Json::decode($template->template) : [] ] ]; } public static function getTemplateSave() { $template = post_params('template'); $controls = post_params('controls'); $id = post_params('id', 0); $name = post_params('name', "DIY"); $type = post_params('type', 'index'); $form = DiyTemplate::findOne(['store_id' => get_store_id(), 'is_delete' => 0, 'id' => $id]) ?: new DiyTemplate(); $form->store_id = get_store_id(); $form->name = $name; $form->template = $template; $form->is_delete = 0; $form->addtime = time(); $form->type = $type; if ($form->save()) { // 保存控件 if ($controls) { self::saveControls($controls); } return [ 'code' => 0, 'msg' => '保存成功', 'data' => [ 'template' => $form, 'controls' => DiyTemplate::find() ->where([ 'store_id' => get_store_id(), 'is_delete' => 0, 'type' => 'controls' ])->asArray()->one() ] ]; } else { return [ 'code' => 0, 'msg' => '保存失败' ]; } } public static function saveControls($controls) { $controlsDiy = DiyTemplate::findOne(['store_id' => get_store_id(), 'is_delete' => 0, 'type' => 'controls']) ?: new DiyTemplate(); $controlsDiy->store_id = get_store_id(); $controlsDiy->name = 'controls'; $controlsDiy->template = $controls; $controlsDiy->is_delete = 0; $controlsDiy->addtime = time(); $controlsDiy->type = 'controls'; return $controlsDiy->save(); } public static function getTemplateList() { $list = DiyTemplate::find() ->where([ 'store_id' => get_store_id(), 'is_delete' => 0, 'type' => ['index', 'user', 'goods', 'cat', 'sign'] ])->orderBy(['id' => SORT_ASC])->asArray()->all(); $template_list = []; $index_list = []; foreach($list as $val) { if (in_array($val['type'],['user', 'goods', 'cat', 'sign'])) { $template_list[] = $val; } else { $index_list[] = $val; } } $new_list = array_merge($template_list, $index_list); return [ 'code' => 0, 'msg' => '加载成功', 'data' => [ 'list' => $new_list, 'controls' => DiyTemplate::find() ->where([ 'store_id' => get_store_id(), 'is_delete' => 0, 'type' => 'controls' ])->asArray()->one() ] ]; } public static function getPageList() { $query = DiyPage::find() ->with(['template']) ->where([ 'store_id' => get_store_id(), 'is_delete' => 0 ])->orderBy(['id' => SORT_ASC])->asArray(); $pagination = pagination_make($query); $pagination['data'] = $pagination['list']; unset($pagination['list']); return [ 'code' => 0, 'msg' => '加载成功', 'template_list' => DiyTemplate::find()->select(['id', 'name']) ->where(['store_id' => get_store_id(),'is_delete' => 0])->asArray()->all(), 'data' => $pagination ]; } public static function getPageSave() { $form = DiyPage::findOne(post_params('id')) ?: new DiyPage(); $form->template_id = post_params('template_id'); $form->title = post_params('title'); $form->store_id = get_store_id(); $form->addtime = time(); if ($form->save()) { $res = ['code' => 0, 'msg' => '保存成功']; }else { $res = ['code' => 1, 'msg' => '保存失败']; } return $res; } }