فهرست منبع

客户日志 设定标签

Zhouzhendong 5 سال پیش
والد
کامیت
09d73fa5bf

+ 16 - 7
app/Http/Controllers/CustomerBaseController.php

@@ -11,6 +11,7 @@ use App\Services\LogService;
 use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Http\Request;
 use Illuminate\Http\Response;
+use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Gate;
 use Illuminate\Support\Facades\Validator;
 
@@ -154,14 +155,22 @@ class CustomerBaseController extends Controller
 
     public function addTag(Request $request)
     {
-        $tags = $request->input("tags");
+        $tag = $request->input("tag");
         $id = $request->input("id");
-        /** @var Customer $customer */
-        $customer = app("CustomerService")->find($id);
-        if (!$customer)return ["success"=>false,"data"=>"客户不存在"];
-        $customer->tags()->sync($tags);
-        $customer->load("tags");
-        return ["success"=>true,"data"=>$customer->tags];
+        DB::transaction(function ()use ($id,$tag){
+            DB::insert(DB::raw("INSERT INTO customer_tag_customer VALUES (?,?)"),[$id,$tag]);
+            CustomerTag::query()->where("id",$tag)->increment("count");
+            DB::commit();
+        });
+        return ["success"=>true];
+    }
+
+    public function delTag(Request $request)
+    {
+        $tag = $request->input("tag");
+        $id = $request->input("id");
+        DB::delete(DB::raw("DELETE FROM customer_tag_customer WHERE customer_id = ? AND customer_tag_id = ?"),[$id,$tag]);
+        return ["success"=>true];
     }
 
     public function destroyLog(Request $request)

+ 32 - 0
database/migrations/2021_01_06_170342_add_column_count_table_customer_tags.php

@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class AddColumnCountTableCustomerTags extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('customer_tags', function (Blueprint $table) {
+            $table->integer("count")->default(0);
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('customer_tags', function (Blueprint $table) {
+            $table->dropColumn("count");
+        });
+    }
+}

+ 100 - 53
resources/views/customer/customer/index.blade.php

@@ -18,10 +18,10 @@
             <table class="table table-striped table-hover text-nowrap">
                 <tr>
                     <th>序号</th>
-                    <th>代码</th>
                     <th>名称</th>
+                    <th>标签</th>
+                    <th></th>
                     <th>客户全称</th>
-                    <th>发票地址</th>
                     <th>联系人</th>
                     <th>联系电话</th>
                     <th>公司备注</th>
@@ -40,16 +40,42 @@
                         </div>
                     </th>
                     <th>合同</th>
+                    <th>发票地址</th>
                     <th>创建时间</th>
-                    <th>标签</th>
                     <th>操作</th>
                 </tr>
                 <tr v-for="(customer,i) in customers" :id="'model-'+customer.id">
                     <td>@{{ i+1 }}</td>
-                    <td>@{{ customer.code }}</td>
                     <td>@{{ customer.name }}</td>
+                    <td>
+                        <div class="text-overflow-warp-200 row">
+                            <span class="badge badge-pill badge-primary ml-1" v-for="(tag,i) in customer.tags">@{{ tag.name }}</span>
+                        </div>
+                    </td>
+                    <td class="cursor-pointer text-dark font-weight-bold">
+                        <div class="dropdown">
+                            <span class="fa fa-plus" :id="'tag-'+i" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></span>
+                            <div class="dropdown-menu" :aria-labelledby="'tag-'+i">
+                                <div @click="$event.stopPropagation();" style="min-width: 250px">
+                                    <label class="w-75 ml-2"><input class="form-control form-control-sm" @keydown="submitTag(i,$event)" type="text" @input="search($event)"></label>
+                                    <span v-if="searchTag.length>0">
+                                        <a class="dropdown-item text-nowrap" v-for="tag in searchTag" @click="selected(i,tag)">
+                                            @{{ tag.value }}
+                                            <span class="fa fa-check text-info pull-right" style="top: 0.3rem" v-if="selectedTag[customer.id][tag.name]"></span>
+                                        </a>
+                                    </span>
+                                    <span v-else>
+                                        <a class="dropdown-item text-nowrap" v-for="tag in defaultTag" @click="selected(i,tag)">
+                                            <span class="fa fa-fire text-danger"></span>
+                                            &nbsp;@{{ tag.value }}
+                                            <span class="fa fa-check text-info pull-right" style="top: 0.3rem" v-if="selectedTag[customer.id][tag.name]"></span>
+                                        </a>
+                                    </span>
+                                </div>
+                            </div>
+                        </div>
+                    </td>
                     <td>@{{ customer.company_name }}</td>
-                    <td>@{{ customer.invoice_address }}</td>
                     <td>@{{ customer.contact_man }}</td>
                     <td>@{{ customer.phone }}</td>
                     <td>@{{ customer.remark }}</td>
@@ -106,25 +132,11 @@
                     </td>
                     <td v-if="customer.contractQuantity>0" class="cursor-pointer" @click="showContract(customer.id)"><span class="fa" :class="contractIds['_'+customer.id] === true ? 'fa-angle-double-down' : 'fa-angle-double-right'"></span>@{{ customer.contractQuantity }}份合同</td>
                     <td v-else>#</td>
+                    <td>@{{ customer.invoice_address }}</td>
                     <td>@{{ customer.created_at }}</td>
-                    <td>
-                        <div v-show="tagIndexes[i]">
-                            <select :id="'sel-'+i" class="selectpicker" data-none-results-text="无此标签,输入空格提交为新标签" multiple data-live-search="true" title="标签(多选)">
-                                <option v-for="tag in tags" :value="tag.name">@{{ tag.value }}</option>
-                            </select><br>
-                            <button class="btn btn-sm btn-success" @click="addTag(i)">确定</button>
-                            <button class="btn btn-sm btn-danger" @click="cancelTag(i)">取消</button>
-                        </div>
-                        <div v-if="!tagIndexes[i]">
-                            <span v-for="(tag,i) in customer.tags">
-                                <span class="badge badge-primary m-1" >@{{ tag.name }}</span><br v-if="(i+1)%2 === 0">
-                            </span>
-                        </div>
-                    </td>
                     <td>
                         @can("客户-编辑")
                             <button class="btn btn-sm btn-outline-success" @click="addContract(i)">新建合同</button>
-                            <button v-if="!tagIndexes[i]" class="btn btn-sm btn-info text-white" @click="setTag(i)">设定标签</button>
                             <button class="btn btn-sm btn-outline-primary" @click="relatedOwner(i)">关联项目</button>
                             <a :href="'{{url('customer/customer')}}/'+customer.id+'/edit'"><button class="btn btn-sm btn-outline-info">改</button></a>
                         @endcan
@@ -155,12 +167,14 @@
                 ],
                 ownerIds:[],
                 ownerNames:[],
-                tagIndexes : [],
                 tags:[
                     @foreach($tags as $tag)
                     {name:"{{$tag->id}}",value:"{{$tag->name}}"},
                     @endforeach
                 ],
+                defaultTag:[],
+                selectedTag:[],
+                searchTag:[],
                 editCustomer:"",
                 addCustomerLogIndex:"",
             },
@@ -182,8 +196,73 @@
                     el:"#form_div",
                     condition:data,
                 }).init();
+                this.defaultTag = this.tags.slice(0,3);
+                this.customers.forEach(customer=>{
+                    let selectedTag = {};
+                    if (customer.tags)
+                        customer.tags.forEach(tag=>{
+                            selectedTag[tag.id] = tag.name;
+                        });
+                    this.selectedTag[customer.id] = selectedTag;
+                });
             },
             methods:{
+                submitTag(index,e){
+                    if (e.keyCode === 9)window.event.returnValue = false;
+                    if (e.keyCode === 13 || e.keyCode === 9){
+                        let val = e.target.value;
+                        if (this.searchTag.length>0){
+                            let sign = this.searchTag.every(tag=> {
+                                if (tag.value === val){
+                                    this.selected(index,tag);
+                                    return false;
+                                }
+                                return true;
+                            });
+                            if (!sign)return;
+                        }
+                        window.tempTip.postBasicRequest("{{url('customer/customer/customerTag/save')}}",{name:val},res=>{
+                            let tag = {name:res.id,value:res.name};
+                            this.tags.unshift(tag);
+                            this.selected(index,tag);
+                            e.target.value = "";
+                        });
+                    }
+                },
+                selected(index,tag){
+                    let id = this.customers[index].id;
+                    if (!this.selectedTag[id])this.selectedTag[id] = {};
+                    if (this.selectedTag[id][tag.name]){
+                        window.tempTip.postBasicRequest("{{url('customer/customer/delTag')}}",{id:id,tag:tag.name},res=>{
+                            this.$delete(this.selectedTag[id],tag.name);
+                            this.customers[index].tags.some((t,i)=>{
+                                if (t.id == tag.name){
+                                    this.$delete(this.customers[index].tags,i);
+                                    return true;
+                                }
+                            });
+                            this.$forceUpdate();
+                        });
+                    }else window.tempTip.postBasicRequest("{{url('customer/customer/addTag')}}",{id:id,tag:tag.name},res=>{
+                        this.selectedTag[id][tag.name] = tag.value;
+                        let obj = {id:tag.name,name:tag.value};
+                        if (this.customers[index].tags) this.customers[index].tags.unshift(obj);
+                        else this.customers[index].tags = [obj];
+                        this.$forceUpdate();
+                    });
+                },
+                search(e){
+                    let val = e.target.value;
+                    if (!val){
+                        this.searchTag = [];
+                        return;
+                    }
+                    let arr = [];
+                    this.tags.forEach(tag=>{
+                        if (tag.value.indexOf(val) !== -1)arr.push(tag);
+                    });
+                    this.searchTag = arr;
+                },
                 editLogEvent(log){
                     log.isEdit = true;
                 },
@@ -250,38 +329,6 @@
                         });
                     })
                 },
-                addTag(index){
-                    let val = $("#sel-"+index).selectpicker('val');
-                    let url = "{{url('customer/customer/addTag')}}";
-                    let params = {id:this.customers[index]['id'],tags:val};
-                    window.tempTip.postBasicRequest(url,params,(res)=>{
-                        this.customers[index].tags = res;
-                        this.tagIndexes[index] = false;
-                        this.$forceUpdate();
-                        return "设定标签成功";
-                    });
-                },
-                cancelTag(index){
-                    this.tagIndexes[index] = false;
-                    this.$forceUpdate();
-                },
-                setTag(index) {
-                    this.tagIndexes[index] = true;
-                    let defaults = [];
-                    if (this.customers[index].tags){
-                        this.customers[index].tags.forEach(tag=>{
-                            defaults.push(tag.id);
-                        });
-                    }
-                    this.$forceUpdate();
-                    $(".selectpicker").selectpicker('refresh').selectpicker('val',defaults);
-                    $($(".bs-searchbox").children()[1]).bind('keypress', event=>{
-                        if(event.keyCode === 32){
-                            this.tags.unshift({name:event.target.value});
-                            event.target.value = "<a>"+event.target.value+"</a>";
-                        }
-                    });
-                },
                 _formatOwner(){
                     let ownerIds=[];
                     let ownerNames=[];

+ 1 - 0
routes/web.php

@@ -611,6 +611,7 @@ Route::group(['prefix'=>'customer'],function(){
         Route::post('storeLog', 'CustomerLogController@store');
         Route::post('relatedOwner', 'CustomerBaseController@relatedOwner');
         Route::post('addTag', 'CustomerBaseController@addTag');
+        Route::post('delTag', 'CustomerBaseController@delTag');
         Route::post('destroyLog', 'CustomerBaseController@destroyLog');
         Route::group(['prefix' => 'customerLogStatus'], function () {
             Route::post('save', 'CustomerLogStatusController@save');