15], [['template'], 'string', 'max' => 20], [['cover'], 'string', 'max' => 255], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'cate_id' => '分类id', 'title' => '名称', 'template' => '页面模板', 'content' => '内容', 'cover' => '预览图片', 'setting' => '价格设置', 'sort' => '排序,越小排越前', 'is_on_sale' => '上架状态【1是 0否】', 'status' => '状态[-1:删除;0:禁用;1启用]', 'created_at' => '添加时间', 'updated_at' => 'Updated At', ]; } /** * 保存数据 * @Author: lun * @DateTime: 2021/12/14 0014 16:59 * @Copyright: copyright (c) 2021 广东七件事集团 * @param array $params * @return bool|DiyTemplate */ public static function setData(array $params){ try{ if(!empty($params['id'])){ $model = self::findOne(['id' => $params['id']]); if(empty($model)) throw new Exception('页面不存在或已删除'); }else{ $model = new self(); $model->loadDefaultValues(); $model->template = DiyEnum::CUSTOM_PAGE; } if(!$model->load($params,'') || !$model->save()){ throw new Exception($model->getErrorMessage()); } // 模板禁用,会将所有商城使用该模板的数据禁用。已经购买,能正常使用,不禁用了 /*if ($model->status == StatusEnum::DISABLED) { DiyMallTemplate::updateAll(['status' => StatusEnum::DELETE], ['template_id' => $model->id]); DiyPage::updateAll(['status' => StatusEnum::DELETE], ['template_id' => $model->id]); }*/ return $model; }catch(Exception $e){ self::setStaticError($e->getMessage()); return false; } } /** * 获取所选的购买规则设置 * * @param integer $index * @return array|null */ public function getCurrentItem(int $index) { $setting = json_decode($this->setting, true); return $setting[$index] ?? null; } /** * 通过ID获取模板 * * @param integer $id * @return static|null */ public static function getTemplateById(int $id) { return static::find() ->where(['id' => $id, 'status' => StatusEnum::ENABLED]) ->one(); } }