Files
fdesk/app/common/validate/InquiryLogValidate.php
T
mzl@hnxinyouhe.com 3537d2fc3c edit inquiry
2025-08-06 18:57:09 +08:00

176 lines
6.5 KiB
PHP

<?php
namespace app\common\validate;
use think\Validate;
use app\common\model\InquiryLog;
class InquiryLogValidate extends Validate
{
// 规则
protected $rule = [
'id' => 'require|integer|gt:0',
'order_no' => 'require|alphaDash|length:36',
'enter_user_id'=> 'integer|egt:0',
'recipient' => 'integer|egt:0',
'quality' => 'require|in:1,2,3',
'channel' => 'integer|between:0,99',
'email' => 'email|max:255|checkEmailContactUnique',
'tel' => 'max:255|checkTelContactUnique',
'whatsapp' => 'max:255|checkWhatsappContactUnique',
'country' => 'integer|egt:0|requireWith:contacts',
'contacts' => 'require|max:255|requireWithout:email,tel,whatsapp|checkContactUnique',
'content' => 'string',
'status' => 'in:0,1,2',
'r_time' => 'integer|egt:0',
't_time' => 'integer|egt:0'
];
// 提示
protected $message = [
'id.require' => 'ID不能为空',
'id.integer' => 'ID必须是整数',
'id.gt' => 'ID必须大于0',
'order_no.require' => '订单号不能为空',
'order_no.alphaDash'=> '订单号只能包含字母、数字和下划线',
'order_no.length' => '订单号长度必须为36位',
'enter_user_id.integer' => '录入人ID必须是整数',
'enter_user_id.egt' => '录入人ID不能为负数',
'recipient.integer' => '接单人ID必须是整数',
'recipient.egt' => '接单人ID不能为负数',
'quality.require' => '询单等级不能为空',
'quality.in' => '询单等级只能是1、2或3',
'channel.integer' => '渠道编号必须是整数',
'channel.between' => '渠道编号范围0-99',
'email.email' => '邮箱格式不正确',
'email.max' => '邮箱长度不能超过255个字符',
'tel.max' => '电话长度不能超过255个字符',
'whatsapp.max' => 'WhatsApp长度不能超过255个字符',
'country.integer' => '国家代码必须是整数',
'country.egt' => '国家代码不能为负数',
'contacts.require' => '联系人不能为空',
'contacts.max' => '联系人长度不能超过255个字符',
'content.string' => '询单内容必须是字符串',
'status.in' => '状态值只能是0、1或2',
'r_time.integer' => '发布时间必须是整数',
'r_time.egt' => '发布时间不能为负数',
't_time.integer' => '跟踪时间必须是整数',
't_time.egt' => '跟踪时间不能为负数',
'email.checkEmailContactUnique' => '邮箱联系方式已存在',
'tel.checkTelContactUnique' => '电话联系方式已存在',
'whatsapp.checkWhatsappContactUnique' => 'WhatsApp联系方式已存在',
'contacts.requireWithout' => '至少需要提供一种联系方式',
'contacts.checkContactUnique' => '联系人已存在',
'country.requireWith' => '国家必须与联系人一起提供'
];
// 场景
protected $scene = [
'store' => [
'enter_user_id', 'quality', 'channel', 'email', 'tel', 'whatsapp', 'country', 'contacts', 'content', 'recipient'
],
'update' => [
'quality', 'channel', 'email', 'tel', 'whatsapp', 'country', 'contacts', 'content', 'recipient'
],
'release' => [
'status', 'r_time'
],
'allocation' => [
'status', 't_time'
]
];
/**
* 验证联系方式唯一性
*/
protected function checkContactUnique($value, $rule, $data, $field, $title)
{
// 如果是更新操作且联系人未修改,跳过检查
if ($this->currentScene == 'update') {
$original = InquiryLog::find($data['id']);
if ($original && $original->contacts == $value && $original->country == ($data['country'] ?? 0)) {
return true;
}
}
if (empty($data['email']) && empty($data['tel']) && empty($data['whatsapp'])) {
$query = InquiryLog::where('contacts', trim($value))
->where('country', $data['country']);
if (!empty($data['id'])) {
$query->where('id', '<>', $data['id']);
}
return !$query->exists();
}
return true;
}
/**
* 验证邮箱联系方式唯一性
*/
protected function checkEmailContactUnique($value, $rule, $data, $field, $title)
{
if (empty($value)) return true;
// 如果是更新操作且邮箱未修改,跳过检查
if ($this->currentScene == 'update' && !empty($data['id'])) {
$original = InquiryLog::find($data['id']);
if ($original && $original->email == $value) {
return true;
}
}
$query = InquiryLog::where('email', trim($value));
if (!empty($data['id'])) {
$query->where('id', '<>', $data['id']);
}
return !$query->exists();
}
/**
* 验证电话联系方式唯一性
*/
protected function checkTelContactUnique($value, $rule, $data, $field, $title)
{
if (empty($value)) return true;
// 如果是更新操作且电话未修改,跳过检查
if ($this->currentScene == 'update' && !empty($data['id'])) {
$original = InquiryLog::find($data['id']);
if ($original && $original->tel == $value) {
return true;
}
}
$query = InquiryLog::where('tel', trim($value));
if (!empty($data['id'])) {
$query->where('id', '<>', $data['id']);
}
return !$query->exists();
}
/**
* 验证WhatsApp联系方式唯一性
*/
protected function checkWhatsappContactUnique($value, $rule, $data, $field, $title)
{
if (empty($value)) return true;
// 如果是更新操作且WhatsApp未修改,跳过检查
if ($this->currentScene == 'update' && !empty($data['id'])) {
$original = InquiryLog::find($data['id']);
if ($original && $original->whatsapp == $value) {
return true;
}
}
$query = InquiryLog::where('whatsapp', trim($value));
if (!empty($data['id'])) {
$query->where('id', '<>', $data['id']);
}
return !$query->exists();
}
}