index.vue
5.85 KB
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
<!--
* @Date: 2022-06-20 11:35:50
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2022-06-21 19:10:41
* @FilePath: /tswj/src/components/LocalismBox/index.vue
* @Description: 调整作品方言弹框组件
-->
<template>
<van-overlay :show="show" z-index="1000" :lock-scroll="false">
<div class="wrapper" @click.stop>
<div class="block">
<div class="close">
<van-icon name="close" color="#FFFFFF" @click="handleClose" />
</div>
<div class="localism-title">{{ title }}</div>
<div id="localism-list" class="localism-list">
<div v-for="(item, index) in localismList" :id="item.id" :key="index"
:class="[item.checked ? 'checked' : 'unchecked', 'van-hairline--top-bottom', 'localism-item']"
@click="setLocalism(item.id)">
{{ item.localism }}
</div>
<div v-show="show_localism" id="localism-bottom" class="van-hairline--top-bottom localism-item">
<input v-model="localism_name" placeholder="请输入新增的方言名称" style="border: 0; text-align: center;"
@blur="onBlur">
</div>
</div>
<div class="bar">
<div class="button">
<my-button type="plain" @on-click="addLocalism">新增方言</my-button>
</div>
<div class="button">
<my-button type="primary" @on-click="handleSubmit">确定</my-button>
</div>
</div>
</div>
</div>
</van-overlay>
</template>
<script setup>
import { ref, watch, nextTick } from 'vue'
import MyButton from '@/components/MyButton/index.vue'
import { localismListModiAPI, addLocalismAPI, modifyProdLocalismAPI } from '@/api/B/localism'
import { Toast, Dialog } from '@/utils/generatePackage.js'
const props = defineProps({
showLocalism: Boolean,
id: Number,
localism: String,
title: String,
})
const emit = defineEmits(['on-close', 'on-submit']);
/**
* 滚动到指定位置
* @param {*} id
*/
const scrollToDom = (id) => {
nextTick(() => {
document.getElementById(id)?.scrollIntoView({ block: 'center' });
})
}
// 处理语言列表数据
let localismList = ref([]);
const raw_id = ref('')
const getLocalismList = async () => {
const { data } = await localismListModiAPI();
localismList.value = data.map((item) => ({ id: item, localism: item, checked: false }))
localismList.value.forEach((item) => {
if (item.localism === props.localism) {
item.checked = true;
raw_id.value = item.id
}
});
scrollToDom(props.localism);
}
let show = ref(false);
watch(() => props.showLocalism, (v) => {
show.value = v;
if (v) {
getLocalismList()
}
})
// 点击行选择方言
const setLocalism = (v) => {
localismList.value.forEach((item) => {
item.checked = false;
});
localismList.value.forEach((item) => {
if (item.id === v) {
item.checked = true;
}
});
}
// 新增方言
const show_localism = ref(false)
const localism_name = ref('')
const addLocalism = () => { // 滚动到底部,显示新增输入框
show_localism.value = true;
const id = localismList.value[localismList.value.length - 1]['id'];
scrollToDom(id);
}
const onBlur = () => { // 失焦保存录入方言
if (!localism_name.value) return false;
Dialog.confirm({
title: '温馨提示',
message: `是否确认新增${localism_name.value}?`,
confirmButtonColor: '#11D2B1'
})
.then(async () => {
const { code } = await addLocalismAPI({ localism_name: localism_name.value });
if (code) {
Toast.success('新增成功!');
localismList.value.forEach((item) => {
item.checked = false;
});
localismList.value.push({ id: localism_name.value, localism: localism_name.value, checked: true });
scrollToDom(localism_name.value);
}
show_localism.value = false;
localism_name.value = '';
})
.catch(() => {
// on cancel
});
}
const clearAll = () => {
show.value = false
show_localism.value = false
localismList.value = []
localism_name.value = ''
}
const handleClose = () => { // 关闭提示框回调
clearAll();
emit('on-close', false)
}
const handleSubmit = () => { // 提交选择方言
const localism = localismList.value.filter((item) => item.checked === true);
// 原始和提交不一致请求接口提交
if (raw_id.value !== localism[0].id) {
Dialog.confirm({
title: '温馨提示',
message: `是否确认设置方言为${localism[0].id}?`,
confirmButtonColor: '#11D2B1'
})
.then(async () => {
const { code } = await modifyProdLocalismAPI({ prod_id: props.id, localism_name: localism[0].id });
if (code) {
Toast.success('更新成功!');
clearAll();
emit('on-submit', localism[0].id);
}
})
.catch(() => {
// on cancel
});
} else {
clearAll();
emit('on-submit', raw_id.value);
}
}
</script>
<style lang="less" scoped>
.wrapper {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
width: auto;
text-align: center;
}
.block {
width: 80%;
// height: 25rem;
background-color: #fff;
border-radius: 10px;
padding: 1rem 0;
position: relative;
margin-top: 1rem;
margin-bottom: 5rem;
.close {
position: absolute;
top: -2rem;
right: 1rem;
font-size: 1.5rem;
}
}
.localism-title {
font-size: 1.05rem;
}
.localism-list {
margin-top: 2rem;
height: 20rem;
overflow: scroll;
}
.bar {
display: flex;
align-items: center;
box-sizing: content-box;
background-color: white;
padding: 0.7rem;
.button {
display: flex;
flex-direction: column;
justify-content: center;
flex: 1;
padding: 0 0.5rem;
}
}
.localism-item {
padding: 1rem;
text-align: center;
}
.checked {
color: #11d2b1;
}
.unchecked {
color: gray;
}
input::-webkit-input-placeholder {
color: #B0B0B0;
}
</style>