From 4c89b3999cb5137a88c050ccc1e6ff766f195631 Mon Sep 17 00:00:00 2001 From: "WIN-C27C5MSVTPO\\16961" <1696136552@qq.com> Date: Thu, 14 Aug 2025 15:50:57 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=9F=E6=88=90code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/service/UserService.php | 4 ++++ app/crm/model/Customer.php | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/app/common/service/UserService.php b/app/common/service/UserService.php index 3712f20..f3c28d1 100644 --- a/app/common/service/UserService.php +++ b/app/common/service/UserService.php @@ -140,6 +140,10 @@ class UserService foreach ($result as &$data){ $data['customer_count'] = Customer::where('user_id', $data['id'])->count(); + unset($data['password']); + unset($data['open_id']); + unset($data['tenant_key']); + unset($data['union_id']); } return $this->postProcess('List', $result); diff --git a/app/crm/model/Customer.php b/app/crm/model/Customer.php index af5c571..ff0287d 100644 --- a/app/crm/model/Customer.php +++ b/app/crm/model/Customer.php @@ -79,4 +79,31 @@ class Customer extends Model { return $this->belongsTo(User::class, 'user_id', 'id'); } + + protected static function booted() + { + static::saved(function ($customer) { + if (empty($customer->code)) { + $customer->code = self::generateCustomerCode($customer); + // 使用saveQuietly避免递归调用 + $customer->saveQuietly(); + } + }); + } + + public static function generateCustomerCode($customer) + { + // 生成唯一16位code,CM+14位 + $unique = strtoupper(substr(md5($customer->id . microtime(true) . mt_rand(1000, 9999)), 8, 14)); + $code = 'CM' . date('Ymd') .$unique; + return $code; + } + + // 静默保存方法,不触发事件 + public function saveQuietly(array $options = []) + { + return static::withoutEvents(function () use ($options) { + return $this->save($options); + }); + } } \ No newline at end of file