['规则1','规则2'...] * * @var array */ protected $rule = [ 'id' => 'require|integer', 'link' => 'url|max:255', 'product_id' => 'require|integer', 'platform_id' => 'sameSpuPlatformCheck|integer' ]; /** * 定义错误信息 * 格式:'字段名.规则名' => '错误信息' * * @var array */ protected $message = [ 'id.require' => 'id不能为空', 'id.integer' => 'id字段类型错误', 'link.url' => '链接格式不正确', 'link.max' => '链接不能超过255个字符', 'product_id.require' => '产品id不能为空', 'product_id.integer' => '产品id类型错误', 'platform_id.sameSpuPlatformCheck' => '同型号平台不能重复', 'platform_id.integer' => '平台id类型错误' ]; /** * 同型号平台不能重复校验 */ protected function sameSpuPlatformCheck($value, $rule, $data = []) { $product = 0; if (!empty($data['id'])) { $sql = ProductPurchaseLinkModel::alias('s') ->field(['s.product_id']) ->where('s.id', '=', $data['id']) ->limit(1) ->buildSql(); $product = \think\facade\Db::raw($sql); } else { if (empty($data['product_id'])) { return false; } $product = $data['product_id']; } $platforms = ProductPurchaseLinkModel::alias('p') ->where(function ($query) use ($data, $product) { $query->where('p.product_id', '=', $product)->where('p.language_id', '=', $data['language_id']); if (!empty($data['id'])) { $query->where('p.id', '<>', $data['id']); } }) ->column(['p.platform_id']); if (!empty($platforms) && in_array($value, $platforms)) { return false; } return true; } /** * 添加场景 */ public function sceneAdd() { return $this->only(['link', 'product_id', 'platform_id']); } /** * 更新场景 */ public function sceneEdit() { return $this->only(['id', 'link', 'platform_id']); } }