| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\common\diy\DiyConfig;
- use app\models\DiyPage;
- use app\models\DiyTemplate;
- use yii\base\BaseObject;
- use yii\base\Model;
- use yii\helpers\Json;
- class DiyForm extends Model
- {
- /**
- * @return array
- */
- public function rules()
- {
- return [
- [[]]
- ];
- }
- public function attributeLabels()
- {
- return [
- ];
- }
- public function save()
- {
- if(!$this->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;
- }
- }
|