<?php namespace App\Http\Validators; use App\Model\SupplierAccountModel; use App\Model\SupplierChannelModel; use Validator; class SupplierAccountValidator { //保存相关的验证,别问我为什么不用laravel自带的form-request类 //因为控制器那边已经被之前的人魔改的难用的一比,而且控制器那边还接收了一大堆统一变量 public function checkSave($request) { //整理下请求数据 $account = $request->all(); $rules = [ "supplier_code" => "required", "mobile" => "required|regex:/^1[0-9][0-9]{9}$/", "password_raw" => "required", "a_type" => "required", ]; $messages = $this->messages(); $validator = Validator::make($account, $rules, $messages); //判断联系方式的表单验证 if ($validator->fails()) { return $validator->errors()->first(); } if (empty($account['id'])) { $supplierModel = new SupplierChannelModel(); $yunxinChannelUid = $supplierModel->where('supplier_code', $account['supplier_code'])->value('yunxin_channel_uid'); if (empty($yunxinChannelUid)) { return '该供应商没有绑定云芯采购,请完善相关信息'; } $model = new SupplierAccountModel(); $supplierCount = $model->where('supplier_code', $account['supplier_code']) ->count(); if ($supplierCount > 0) { return '该供应商已经存在账号'; } $mobileCount = $model->where('mobile', $account['mobile']) ->count(); if ($mobileCount > 0) { return '该手机号已经被注册'; } } return true; } private function messages() { return [ 'supplier_code.required' => '请选择一个供应商', 'a_type.required' => '类型必须勾选为云芯商家', 'mobile.required' => '登录账号不能为空', 'mobile.regex' => '账号格式必须为手机号', 'password_raw.required' => '账号密码不能为空', ]; } }