Sanakey
3 天以前 b5c1614fe473330ceca8b7cff0f1802e19bd5039
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<template>
  <div class="p-2">
    <a-alert message="设置标签后,可以在「线索、客户、商机」三个模块应用" type="info" show-icon />
    <vxe-toolbar>
      <template #buttons>
        <div class="">
          <a-input-search
            v-model:value="tagsKeyword"
            placeholder="输入搜索标签"
            style="width: 400px"
            @search="onTagsSearch"
          />
        </div>
      </template>
      <template #tools>
        <a-button type="primary" shape="round" @click="showModal('add')">添加标签</a-button>
      </template>
    </vxe-toolbar>
 
    <vxe-table
      ref="xTable"
      style="margin: 10px 0"
      :data="demo.tableData"
      @mounted="onMounted"
      height="660"
    >
      <vxe-column width="40">
        <template #default>
          <span class="drag-btn">
            <HolderOutlined />
          </span>
        </template>
      </vxe-column>
      <vxe-column field="TagName" title="标签名称" minWidth="250">
        <template #default="{ row }">
          <div class="my-d-f">
            <div
              v-if="row.systemFlag"
              class="bookmark"
              :style="{ backgroundColor: row.tagColor }"
            ></div>
            <div v-else>
              <ColorPicker
                v-model="row.tagColor"
                :type="2"
                @change="fnRowColorChange($event, row)"
              ></ColorPicker>
            </div>
            <a-tag class="ml-5" :color="row.tagColor">{{ row.tagName }}</a-tag>
          </div>
        </template>
      </vxe-column>
      <vxe-column field="age" title="操作" width="150">
        <template #default="{ row }">
          <a style="margin-right: 10px" @click="showModal('edit', row)">编辑</a>
          <a style="margin-right: 10px" @click="fnDeleteRow(row)">删除</a>
        </template>
      </vxe-column>
    </vxe-table>
    <a-modal :width="300" v-model:open="open" :title="`${title}标签`" @ok="handleOk">
      <a-form ref="formRef" :model="form" style="margin-top: 20px">
        <a-form-item label="标签颜色" name="tagColor">
          <ColorPicker v-model="form.tagColor" :type="2"></ColorPicker>
        </a-form-item>
        <a-form-item
          label="标签名称"
          name="tagName"
          :rules="[{ required: true, message: '请输入标签名称', trigger: 'blur' }]"
        >
          <a-input v-model:value="form.tagName" placeholder="请输入标签名称" />
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>
 
<script lang="ts" setup>
import { ref, computed, onMounted, nextTick, onUnmounted, reactive } from 'vue';
import ColorPicker from '@/components/ColorPicker/index.vue';
import { addTagApi, deleteTagApi, updateTagApi, getTagApi } from '@/api/email/userList';
 
// 排序
import { HolderOutlined } from '@ant-design/icons-vue';
import Sortable from 'sortablejs';
let tagsKeyword = ref('');
function onTagsSearch(e) {
  console.log(e);
}
let sortable: any;
const demo = reactive({
  showHelpTip: false,
  tableData: [],
});
const xTable = ref();
const rowDrop = () => {
  const $table = xTable.value;
  sortable = Sortable.create($table.$el.querySelector('.body--wrapper>.vxe-table--body tbody'), {
    handle: '.drag-btn',
    onEnd: (sortableEvent) => {
      const newIndex = sortableEvent.newIndex as number;
      const oldIndex = sortableEvent.oldIndex as number;
      const currRow: Record<string, any> = demo.tableData.splice(oldIndex, 1)[0];
      // demo.tableData.splice(newIndex, 0, currRow);
      updateTagApi({
        tagColor: currRow.tagColor,
        tagName: currRow.tagName,
        sortId: newIndex,
        tagId: currRow.tagId,
      })
        .then(() => {
          fnGetList();
        })
        .catch(() => {});
    },
  });
};
 
let initTime: any;
nextTick(() => {
  // 加载完成之后在绑定拖动事件
  initTime = setTimeout(() => {
    rowDrop();
  }, 500);
});
 
onUnmounted(() => {
  clearTimeout(initTime);
  if (sortable) {
    sortable.destroy();
  }
});
 
function fnGetList() {
  getTagApi({}).then((res) => {
    console.log(res);
    demo.tableData = res.data;
  });
}
 
import { useMessage } from '@/hooks/web/useMessage';
 
const { createMessage } = useMessage();
 
const open = ref(false);
const formRef = ref();
 
interface formType {
  tagColor: string;
  tagName: string;
  tagType: number;
  systemFlag: boolean;
  tagId?: number;
}
 
const defaultForm: formType = {
  tagColor: '#000000',
  tagName: '',
  tagType: 1,
  systemFlag: false,
  tagId: Math.random()*10000000,
};
 
const form = ref<formType>({ ...defaultForm });
 
const title = ref('新建');
const signType = ref('add');
 
// let currentIndex = ref(0);
const showModal = (type: string, row) => {
  // Logger.log(row,index);
  signType.value = type;
  open.value = true;
 
  if (type == 'add') {
    form.value = { ...defaultForm };
  } else {
    title.value = '编辑';
    nextTick(() => {
      // formRef.value.resetFields();
      form.value = {
        tagColor: row.tagColor,
        tagName: row.tagName,
        tagType: row.tagType,
        systemFlag: row.systemFlag,
        tagId: row.tagId,
      };
    });
  }
};
 
function fnDeleteRow(row) {
  // deleteTagApi({ tagId: row.tagId }).then((res) => {
  //   if (res.code === 0) {
  //     createMessage.success(res.msg);
  //     fnGetList();
  //   }
  // });
  demo.tableData.map((item) => {
    if (item.tagId == row.tagId) {
      demo.tableData.splice(item, 1);
    }
  });
}
 
function handleOk() {
  nextTick(() => {
    formRef.value
      .validate()
      .then((res) => {
        // const api = signType.value == 'add' ? addTagApi : updateTagApi;
        // api(form.value).then((res) => {
        //   if (res.code === 0) {
        //     createMessage.success(res.msg);
        //     fnGetList();
        //     open.value = false;
        //   }
        // });
        // form.value.tagId = Math.random()*10000000;
        Logger.log('formRef.value res',res);
        let isEdit = false;
        demo.tableData.map((item) => {
          if (item.tagId == form.value.tagId) {
            isEdit = true;
            item.tagColor = res.tagColor;
            item.tagName = res.tagName;
          }
          // else {
          //   demo.tableData.push(form.value);
          // }
        });
        if (!isEdit){demo.tableData.push(form.value);}
 
        Logger.log(form.value);
        open.value = false;
      })
      .catch(() => {});
  });
}
 
function fnRowColorChange(color, row) {
  console.log(color, row);
  const data = {
    tagColor: row.tagColor,
    tagName: row.tagName,
    tagType: row.tagType,
    systemFlag: row.systemFlag,
    tagId: row.tagId,
  };
  updateTagApi(data).then((res) => {
    if (res.code === 0) {
      createMessage.success(res.msg);
      fnGetList();
    }
  });
}
 
onMounted(() => {
  fnGetList();
});
</script>
 
<style scoped>
.sortable-row-demo .drag-btn {
  font-size: 12px;
  cursor: move;
}
 
.sortable-row-demo .vxe-body--row.sortable-ghost,
.sortable-row-demo .vxe-body--row.sortable-chosen {
  background-color: #dfecfb;
}
 
.bookmark {
  display: inline-block;
  position: relative;
  width: 14px;
  height: 18px;
  margin-right: 5px;
  border-radius: 2px 2px 0 0; /* Rounded top corners */
}
 
.bookmark::after {
  content: '';
  position: absolute;
  right: 0;
  bottom: 0;
  left: 0;
  height: 8px;
  background-color: #fff;
  clip-path: polygon(50% 60%, 0% 100%, 100% 100%); /* Triangle at the bottom */
}
</style>