material_request.vue
8.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
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
<!--
* @Date: 2024-07-23 12:53:15
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2024-07-25 10:01:06
* @FilePath: /temple_material_request/src/views/material_request.vue
* @Description: 申领物资页面
-->
<template>
<div class="material-request-page">
<van-tabs v-model:active="active" @change="onChange" sticky :color="styleColor.baseColor">
<van-tab v-for="index in 8" :title="'标签 ' + index">
<div class="list-wrapper">
<van-sticky :offset-top="44">
<van-row justify="space-between" class="select-all-item">
<van-col span="8">
<van-icon v-if="is_all_checked" @click="onCheckAll()" name="checked" size="1.25rem" :color="styleColor.baseColor" />
<van-icon v-else name="passed" @click="onCheckAll()" size="1.25rem" />
<span @click="onCheckAll()" :style="{ color: styleColor.baseColor }"> 全选</span>
</van-col>
<van-col span="16" style="text-align: right; font-size: 0.85rem; color: #666666;">参考上次同类活动的领用情况</van-col>
</van-row>
</van-sticky>
<van-list
v-model:loading="loading"
:finished="finished"
finished-text="没有更多了"
@load="onLoad"
>
<div v-for="item in list" :id="item.id" :key="item.id" class="list-boxer">
<van-row align="center" justify="space-between">
<van-col span="16" style="display: flex;">
<van-icon v-if="item.checked" @click="onCheck(item)" name="checked" size="1.25rem" :color="styleColor.baseColor" />
<van-icon v-else name="passed" @click="onCheck(item)" size="1.25rem" />
<div class="van-ellipsis" :style="{ color: styleColor.baseColor, textDecoration: 'underline' }" @click="onClickTitle(item)">
{{ item.name }}
</div>
</van-col>
<van-col span="8" style="display: flex; align-items: center;">
<van-field
v-model="item.num"
style="border: 1px solid #f0f0f0; padding: 0; border-radius: 5px;"
label=""
label-width="0"
input-align="center"
placeholder="数量"
type="number"
@blur="onBlur(item)"
>
<template #left-icon></template>
</van-field> <span style="font-size: 0.9rem; color: #666;">个</span>
</van-col>
</van-row>
<div v-if="item.error" style="padding: 0.5rem 2rem 0 0; font-size: 0.85rem; color: red; text-align: right;">输入值有误</div>
</div>
</van-list>
<div style="height: 6rem;"></div>
</div>
</van-tab>
</van-tabs>
<div class="control-bar">
<div>
<van-button icon="plus" type="primary" :color="styleColor.baseColor" @click="addMore">添加更多</van-button>
</div>
<div style="display: flex; align-items: center;">
<van-button plain type="primary" :color="styleColor.baseColor" @click="addShoppingCart">加入购物车</van-button>
<van-badge :content="cart_count">
<van-icon name="shopping-cart-o" size="2.5rem" :color="styleColor.baseColor" @click="goShoppingCart" />
</van-badge>
</div>
</div>
</div>
<choose-material :show="show_choose_material" @close="onCloseChoose"></choose-material>
<material-detail :show="show_material_detail" @close="onCloseDetail"></material-detail>
</template>
<script setup>
import { ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { showToast } from 'vant';
import { Cookies, $, _, axios, storeToRefs, mainStore, Toast, useTitle } from '@/utils/generatePackage.js'
//import { } from '@/utils/generateModules.js'
//import { } from '@/utils/generateIcons.js'
//import { } from '@/composables'
import { styleColor } from "@/constant.js";
import chooseMaterial from '@/components/chooseMaterial/index.vue';
import materialDetail from '@/components/materialDetail/index.vue';
import { getActivityDeptsAPI } from "@/api/material";
const $route = useRoute();
const $router = useRouter();
useTitle($route.meta.title);
// TODO: getActivityDeptsAPI,is_previous=1,only_my_dept=1
const tabList = ref([]); // 组别列表
const is_all_checked = ref(false);
const num_value = ref('');
const onCheckAll = () => {
is_all_checked.value = !is_all_checked.value;
if (is_all_checked.value) {
list.value.forEach(item => {
item.checked = true;
})
} else {
list.value.forEach(item => {
item.checked = false;
item.error = false;
})
}
}
const onCheck = (item) => {
item.checked = !item.checked;
// 全部为选中
let all_checked = list.value.every((item) => item.checked === true);
if (all_checked) {
is_all_checked.value = true;
} else {
is_all_checked.value = false;
}
// 如果取消选中,隐藏错误提示
if (!item.checked) {
item.error = false;
}
}
const onBlur = (item) => { // 输入框失去焦点回调
if (item.checked) {
if (item.num === null || item.num === '') {
item.error = true;
} else {
item.error = false;
}
}
}
const active = ref(0);
const onChange = (index) => { // 切换标签回调
console.warn(index);
// 取消全选
is_all_checked.value = false;
// TODO:重新查询列表数据, 取消选中
list.value.forEach(item => {
item.checked = false;
item.error = false;
});
}
const list = ref([]);
const loading = ref(false);
const finished = ref(false);
const onLoad = () => {
// 异步更新数据
// setTimeout 仅做示例,真实场景中一般为 ajax 请求
setTimeout(() => {
for (let i = 0; i < 10; i++) {
list.value.push({ id: list.value.length + 1, name: '标题 床垫 1.2m*2m', num: null, checked: false, error: false });
}
// 加载状态结束
loading.value = false;
// 数据全部加载完成
if (list.value.length >= 40) {
finished.value = true;
}
}, 1000);
// TODO: 如果查询到的数据大于0,需要取消全选
is_all_checked.value = false;
};
const onClickTitle = (item) => { // 点击物资标题回调
console.warn(item);
show_material_detail.value = true;
}
const addMore = () => { // 添加更多
console.warn('addMore');
show_choose_material.value = true;
}
const cart_count = ref(0);
const shop_cart_list = ref([]); // 购物车列表
const addShoppingCart = () => { // 加入购物车
shop_cart_list.value = [];
// 把选中的值集合
list.value
.filter(item => item.checked)
.forEach(item => {
shop_cart_list.value = shop_cart_list.value.concat(item);
});
// 购物车为空提示
if (!shop_cart_list.value.length) {
showToast('请选择物资');
return;
}
// 校验购物车值
let error_list = shop_cart_list.value.filter((item) => { if (item.num === null || item.num === '') { return item;} })
if (error_list.length) {
// 跳到第一个错误框
scrollToSection(error_list[0].id);
// 标记所有错误
error_list.forEach(item => {
item.error = true;
})
return;
}
// 购物车数量
cart_count.value = shop_cart_list.value.length;
}
const goShoppingCart = () => { // 跳转购物车
console.warn('goShoppingCart');
$router.push({ path: '/material_pre_request' });
}
const show_choose_material = ref(false);
const onCloseChoose = () => { // 关闭选择物资弹窗回调
show_choose_material.value = false;
}
const show_material_detail = ref(false);
const onCloseDetail = () => {
show_material_detail.value = false;
}
const scrollToSection = (id) => { // 滚动到指定位置
const current_index = list.value.findIndex((item) => item.id === id);
// 因为布局问题,需要滚动到上一个节点 查询上一个节点ID
const previous_index = current_index - 2 >= 0 ? current_index - 2 : 0;
nextTick(() => {
let element = $('.list-wrapper').find(`#${list.value[previous_index]['id']}`);
if (element[0]) {
element[0].scrollIntoView({ behavior: 'smooth' });
} else {
console.log('Element with ID ' + id + ' not found.');
}
})
}
</script>
<style lang="less" scoped>
.material-request-page {
.list-wrapper {
width: 100%;
.select-all-item {
padding: 0.5rem 1rem;
border-top: 1px solid #f0f0f0;
background-color: white;
}
.list-boxer {
margin: 0; padding: 1rem; border-top: 1px solid #f0f0f0;
}
}
.control-bar {
display: flex; position: fixed; padding: 1rem 1.5rem; left: 0; right: 0; bottom: 0; background-color: white; width: calc(100% - 3rem); justify-content: space-between; align-items: center; box-shadow: 0rem -0.33rem 0.33rem 0.08rem rgba(0,0,0,0.06);
}
}
</style>