From aff152e40d04637faacfafd988a6b23720b43cbf Mon Sep 17 00:00:00 2001 From: "HESU\\HESU" <1696136552@qq.com> Date: Thu, 27 Mar 2025 11:13:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=87=E7=AD=BE=E5=85=B3=E7=B3=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/CustomerController.php | 6 ++-- app/controller/TagGroupController.php | 36 ++++++++++++++++++++++-- app/model/Customer.php | 9 ++++++ app/model/CustomerTag.php | 36 ++++++++++++++++++++++++ app/model/Tag.php | 4 +++ resource/translations/en/messages.php | 2 ++ resource/translations/zh_CN/messages.php | 2 ++ 7 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 app/model/CustomerTag.php diff --git a/app/controller/CustomerController.php b/app/controller/CustomerController.php index b032c9b..f7da53f 100644 --- a/app/controller/CustomerController.php +++ b/app/controller/CustomerController.php @@ -144,8 +144,8 @@ class CustomerController } - // 执行分页查询 - $query = Customer::where($where) + // 执行分页查询 //关联标签tags //关联标签及分组tags.group + $query = Customer::with('tags')->where($where) ->orderBy('id', 'desc'); if (!Util::isEmpty($keyword)) { $query->where(function ($query) use ($keyword) { @@ -210,7 +210,7 @@ class CustomerController return Util::fail([], 'id' . trans('not_null')); } - $res = Customer::select('id', 'user_id', 'grade', 'channel_id', 'car_type_id', 'country_id', 'magnitude', 'email', 'phone', 'WhatsApp', 'contacts', 'describe', 'address', 'lat', 'lng', 'follow_next', 'follow_remark', 'sex') + $res = Customer::with('tags')->select('id', 'user_id', 'grade', 'channel_id', 'car_type_id', 'country_id', 'magnitude', 'email', 'phone', 'WhatsApp', 'contacts', 'describe', 'address', 'lat', 'lng', 'follow_next', 'follow_remark', 'sex') ->where('id', $id) ->first(); if ($res) { diff --git a/app/controller/TagGroupController.php b/app/controller/TagGroupController.php index 04a7d9d..7edb77a 100644 --- a/app/controller/TagGroupController.php +++ b/app/controller/TagGroupController.php @@ -2,6 +2,7 @@ namespace app\controller; +use app\model\CustomerTag; use app\model\TagGroup; use app\util\Util; use support\Request; @@ -17,12 +18,12 @@ class TagGroupController $language = $request->user['language']; foreach ($list as &$item) { $item['names'] = unserialize($item['names']); - if(in_array($language, $languageArr)){ + if (in_array($language, $languageArr)) { $item['name'] = $item['names'][$language]; } foreach ($item['tags'] as &$tag) { $tag['names'] = unserialize($tag['names']); - if(in_array($language, $languageArr)){ + if (in_array($language, $languageArr)) { $item['name'] = $item['names'][$language]; } } @@ -30,4 +31,35 @@ class TagGroupController return Util::success($list); } + + + /** + * 更新客户标签关系 + * @param Request $request + * @return \support\Response + */ + public function updatePivot(Request $request) + { + $customerId = Util::getParam($request, 'customer_id'); + $groupId = Util::getParam($request, 'group_id'); + $tagId = Util::getParam($request, 'tag_id'); + if (Util::isEmpty($customerId)) { + return Util::fail([], trans('customer_id') . trans('not_null')); + } + if (Util::isEmpty($groupId)) { + return Util::fail([], trans('tag_group_id') . trans('not_null')); + } + + if(empty($tagId)){ + $res = CustomerTag::where(['customer_id' => $customerId, 'group_id' => $groupId])->delete(); + }else{ + $res = CustomerTag::updateOrCreate(['customer_id' => $customerId, 'group_id' => $groupId], ['tag_id' => $tagId]); + } + + if($res){ + return Util::success($res, trans('success')); + }else{ + return Util::fail($res, trans('fail')); + } + } } \ No newline at end of file diff --git a/app/model/Customer.php b/app/model/Customer.php index a166ee8..09a9f0e 100644 --- a/app/model/Customer.php +++ b/app/model/Customer.php @@ -96,4 +96,13 @@ class Customer extends Model return $customer; } + + /** + * 客户的标签 + */ + public function tags() + { + return $this->belongsToMany(Tag::class, 'customer_tag', 'customer_id', 'tag_id') + ->withTimestamps(); // 如果需要记录关联时间 + } } \ No newline at end of file diff --git a/app/model/CustomerTag.php b/app/model/CustomerTag.php new file mode 100644 index 0000000..f3f96a3 --- /dev/null +++ b/app/model/CustomerTag.php @@ -0,0 +1,36 @@ +belongsTo(TagGroup::class, 'group_id', 'id'); } + public function customers() + { + return $this->belongsToMany(Customer::class, 'customer_tag', 'tag_id', 'customer_id'); + } } \ No newline at end of file diff --git a/resource/translations/en/messages.php b/resource/translations/en/messages.php index 9a7b4f9..2a20ae4 100644 --- a/resource/translations/en/messages.php +++ b/resource/translations/en/messages.php @@ -28,4 +28,6 @@ return [ 'sign_not_null' => ' No affiliated company has been set', 'password_short' => ' Password cannot be less than 6 characters', 'id_empty' => ' ID cannot be empty', + 'customer_id' => ' customer ID', + 'tag_group_id' => ' tag group ID', ]; \ No newline at end of file diff --git a/resource/translations/zh_CN/messages.php b/resource/translations/zh_CN/messages.php index 5647b34..1a6428c 100644 --- a/resource/translations/zh_CN/messages.php +++ b/resource/translations/zh_CN/messages.php @@ -28,4 +28,6 @@ return [ 'sign_not_null' => ' 未设置所属公司', 'password_short' => ' 密码不能少于6位', 'id_empty' => ' id不能为空', + 'customer_id' => ' 客户ID', + 'tag_group_id' => ' 标签组ID', ]; \ No newline at end of file