0], ['price', 'number', 'min' => 0], ['detail', 'safe'], ['name', 'required'], ]; } public function save() { if (!$this->validate()) { return ['error'=>$this->model->getErrors()]; } if ($this->model->isNewRecord) { $this->model->is_delete = 0; } $conditionList = []; foreach ($this->detail as &$item) { if (isset($item['condition'])) { if (in_array($item['condition'], $conditionList)) { throw new ApiException("同一条规则下,包邮条件不能相同"); } $conditionList[] = $item['condition']; $item['condition'] = trim($item['condition']); if (!is_numeric($item['condition']) || $item['condition'] < 0 || $item['condition'] > 99999999) { throw new ApiException("包邮条件必须大于等于0,小于99999999"); } if (empty($this->type)) { throw new ApiException("请选择包邮类型"); } if (in_array($this->type, [2, 4])) { $item['condition'] = (int)$item['condition']; } } else { throw new ApiException("请设置包邮条件"); } } unset($item); $this->model->detail = json_encode($this->detail); $this->model->price = $this->price; $this->model->name = $this->name; $this->model->type = $this->type; $this->model->status = $this->status; if ($this->model->save()) { return []; } else { return ['error'=>$this->model->getErrors()]; } } }