DeviceStep1.vue
7.78 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
267
268
269
270
<template>
<div class="step1">
<div class="step1-form">
<BasicForm @register="register">
<template #iconSelect>
<Upload
name="avatar"
list-type="picture-card"
class="avatar-uploader"
:show-upload-list="false"
:customRequest="customUpload"
:before-upload="beforeUpload"
>
<img v-if="devicePic" :src="devicePic" alt="avatar" />
<div v-else>
<!-- <LoadingOutlined v-if="loading" /> -->
<PlusOutlined />
<div class="ant-upload-text">图片上传</div>
</div>
</Upload>
</template>
<template #deviceAddress>
<Input disabled v-model:value="positionState.address">
<template #addonAfter>
<EnvironmentTwoTone @click="selectPosition" />
</template>
</Input>
</template>
</BasicForm>
</div>
<Modal
v-model:visible="visible"
title="设备位置"
@ok="handleOk"
width="800px"
@cancel="handleCancel"
centered
>
<div>
<a-form :label-col="labelCol">
<a-row :gutter="20" class="pt-4 pl-6">
<a-col :span="8">
<a-form-item label="经度">
<Input type="input" v-model:value="positionState.lng" disabled />
</a-form-item>
</a-col>
<a-col :span="8">
<a-form-item label="纬度">
<Input type="input" v-model:value="positionState.lat" disabled />
</a-form-item>
</a-col>
</a-row>
</a-form>
<div ref="wrapRef" style="height: 300px; width: 90%" class="ml-6"></div>
</div>
</Modal>
</div>
</template>
<script lang="ts">
import { defineComponent, ref, nextTick, unref, reactive } from 'vue';
import { BasicForm, useForm } from '/@/components/Form';
import { step1Schemas } from './data';
import { useScript } from '/@/hooks/web/useScript';
import { ScrollActionType } from '/@/components/Container/index';
import { Input, Divider, Upload, message, Modal, Form, Row, Col } from 'ant-design-vue';
import { EnvironmentTwoTone, PlusOutlined } from '@ant-design/icons-vue';
import { upload } from '/@/api/oss/ossFileUploader';
import { FileItem } from '/@/components/Upload/src/typing';
export default defineComponent({
components: {
BasicForm,
Input,
[Input.Group.name]: Input.Group,
[Divider.name]: Divider,
Upload,
EnvironmentTwoTone,
// LoadingOutlined,
PlusOutlined,
Modal,
[Form.name]: Form,
[Form.Item.name]: Form.Item,
[Row.name]: Row,
[Col.name]: Col,
},
emits: ['next'],
setup(_, { emit }) {
const devicePic = ref('');
let positionState = reactive({
lng: '',
lat: '',
address: '',
});
const [register, { validate, resetFields, setFieldsValue, getFieldsValue }] = useForm({
labelWidth: 100,
schemas: step1Schemas,
actionColOptions: {
span: 14,
},
labelAlign: 'left',
showResetButton: false,
submitButtonOptions: {
text: '下一步',
},
submitFunc: customSubmitFunc,
});
async function customSubmitFunc() {
try {
let values = await validate();
values = { devicePic: devicePic.value, ...positionState, ...values };
delete values.icon;
delete values.deviceAddress;
emit('next', values);
// 获取输入的数据
} catch (e) {}
}
// 图片上传
async function customUpload({ file }) {
if (beforeUpload(file)) {
const formData = new FormData();
formData.append('file', file);
const response = await upload(formData);
if (response.fileStaticUri) {
devicePic.value = response.fileStaticUri;
}
}
}
const beforeUpload = (file: FileItem) => {
const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
if (!isJpgOrPng) {
message.error('只能上传图片文件!');
}
const isLt2M = (file.size as number) / 1024 / 1024 < 2;
if (!isLt2M) {
message.error('图片大小不能超过2MB!');
}
return isJpgOrPng && isLt2M;
};
// 地图的弹框
const visible = ref(false);
const selectPosition = () => {
visible.value = true;
initMap();
};
// 地图
const BAI_DU_MAP_URL =
'https://api.map.baidu.com/getscript?v=3.0&ak=7uOPPyAHn2Y2ZryeQqHtcRqtIY374vKa';
const wrapRef = ref<HTMLDivElement | null>(null);
const scrollRef = ref<Nullable<ScrollActionType>>(null);
const { toPromise } = useScript({ src: BAI_DU_MAP_URL });
async function initMap() {
await toPromise();
await nextTick();
const wrapEl = unref(wrapRef);
const BMap = (window as any).BMap;
if (!wrapEl) return;
const map = new BMap.Map(wrapEl);
const point = new BMap.Point(104.04813399999999, 30.54348986021446);
map.centerAndZoom(point, 15);
map.enableScrollWheelZoom(true);
let preMarker = null;
map.addEventListener('click', (e) => {
const { lat, lng } = map.he;
positionState.lat = lat + '';
positionState.lng = lng + '';
let gc = new BMap.Geocoder();
let newPoint = new BMap.Point(lng, lat);
if (!e.overlay) {
let myIcon = new BMap.Icon(
'http://api.map.baidu.com/img/markers.png',
new BMap.Size(40, 25),
{
offset: new BMap.Size(0, 0), // 指定定位位置
imageOffset: new BMap.Size(20, -260), // 设置图片偏移
}
);
let marker = new BMap.Marker(e.point, { icon: myIcon });
map.removeOverlay(preMarker);
map.addOverlay(marker);
preMarker = marker;
}
gc.getLocation(newPoint, (rs) => {
let addComp = rs.addressComponents;
//获取详细的地址,精确到街道的名称
let addrname = addComp.city + addComp.district + addComp.street + addComp.streetNumber;
positionState.address = addrname;
});
});
}
// 确定选择的位置
const handleOk = () => {
visible.value = false;
};
// 取消选择位置
const handleCancel = () => {
for (let key in positionState) {
positionState[key] = '';
}
};
// 父组件调用更新字段值的方法
function parentSetFieldsValue(data) {
setFieldsValue(data);
}
function parentGetFieldsValue() {
return getFieldsValue();
}
async function parentValidate() {
const valid = await validate();
return valid;
}
return {
resetFields,
positionState,
register,
beforeUpload,
customUpload,
selectPosition,
devicePic,
visible,
scrollRef,
handleOk,
handleCancel,
wrapRef,
labelCol: { style: { width: '40px' } },
parentSetFieldsValue,
parentGetFieldsValue,
parentValidate,
};
},
});
</script>
<style lang="less" scoped>
.step1 {
&-form {
width: 450px;
margin: 0 auto;
}
h3 {
margin: 0 0 12px;
font-size: 16px;
line-height: 32px;
color: @text-color;
}
h4 {
margin: 0 0 4px;
font-size: 14px;
line-height: 22px;
color: @text-color;
}
p {
color: @text-color;
}
}
.pay-select {
width: 20%;
}
.pay-input {
width: 70%;
}
</style>