cultivationApp/pages/addPlant/addPlant.vue

599 lines
15 KiB
Vue
Raw Normal View History

2023-12-20 18:13:01 +08:00
<template>
<view class="content">
<view class="card">
<up-form labelWidth='130' :model="data.formData" ref="form1">
2024-01-23 19:52:15 +08:00
<view class="c-title">养殖基地信息</view>
2023-12-20 18:13:01 +08:00
<view class="c-box">
2024-01-23 19:52:15 +08:00
<up-form-item label="养殖基地名称" prop="farm_name" borderBottom required>
<up-input input-align="right" placeholder="请输入养殖基地名称" border="none"
v-model="data.formData.farm_name"></up-input>
2023-12-20 18:13:01 +08:00
</up-form-item>
2024-01-23 19:52:15 +08:00
<!-- <up-form-item label="养殖基地类型" prop="farm_type" borderBottom required>
<up-input input-align="right" placeholder="请输入养殖基地类型" border="none"
v-model="data.formData.farm_type"></up-input>
</up-form-item> -->
<up-form-item label="养殖基地类型" prop="farm_type" borderBottom required @click="isshowFarm">
<up-input input-align="right" placeholder="请选择养殖基地类型" suffixIcon="arrow-down"
suffix-icon-style="color: #b6b6b6;" border="none" v-model="farm_type_name" readonly
style="pointer-events: none;"></up-input>
2023-12-20 18:13:01 +08:00
</up-form-item>
2024-01-23 19:52:15 +08:00
<!-- <up-form-item label="养殖种类" prop="breed_type" borderBottom required>
2023-12-20 18:13:01 +08:00
<up-input input-align="right" placeholder="请输入养殖种类" border="none"
2024-01-23 19:52:15 +08:00
v-model="data.formData.breed_type"></up-input>
</up-form-item> -->
<up-form-item label="养殖种类" prop="farm_type" borderBottom required @click="isshowBreed">
<up-input input-align="right" placeholder="请选择养殖种类" suffixIcon="arrow-down"
suffix-icon-style="color: #b6b6b6;" border="none" v-model="breed_type_name" readonly
style="pointer-events: none;"></up-input>
</up-form-item>
<up-form-item label="养殖基地规模(亩)" prop="form_scale" borderBottom required>
<up-input input-align="right" placeholder="请输入养殖基地规模" border="none"
v-model="data.formData.form_scale"></up-input>
2023-12-20 18:13:01 +08:00
</up-form-item>
2024-01-23 19:52:15 +08:00
<up-form-item label="负责人" prop="master" borderBottom required>
<up-input input-align="right" placeholder="请输入负责人姓名" border="none"
v-model="data.formData.master"></up-input>
2023-12-20 18:13:01 +08:00
</up-form-item>
2024-01-23 19:52:15 +08:00
<up-form-item label="负责人电话" prop="master_contact" borderBottom required>
2023-12-20 18:13:01 +08:00
<up-input input-align="right" placeholder="请输入负责人电话" type="number" border="none"
2024-01-23 19:52:15 +08:00
v-model="data.formData.master_contact"></up-input>
2023-12-20 18:13:01 +08:00
</up-form-item>
</view>
<view class="c-title">地址信息</view>
<view class="c-box">
2024-01-23 19:52:15 +08:00
<up-form-item label="所在省份" prop="province" borderBottom required @click="isshow1">
2023-12-20 18:13:01 +08:00
<up-input input-align="right" placeholder="请选择所在省份" suffixIcon="arrow-down"
suffix-icon-style="color: #b6b6b6;" border="none" v-model="province_name" readonly
style="pointer-events: none;"></up-input>
</up-form-item>
2024-01-23 19:52:15 +08:00
<up-form-item label="所在城市" prop="city" borderBottom required @click="isshow2">
2023-12-20 18:13:01 +08:00
<up-input input-align="right" placeholder="请选择所在城市" suffixIcon="arrow-down"
suffix-icon-style="color: #b6b6b6;" border="none" v-model="city_name" readonly
style="pointer-events: none;"></up-input>
</up-form-item>
2024-01-23 19:52:15 +08:00
<up-form-item label="所在区县" prop="area" borderBottom required @click="isshow3">
2023-12-20 18:13:01 +08:00
<up-input input-align="right" placeholder="请选择所在区县" suffixIcon="arrow-down"
2024-01-23 19:52:15 +08:00
suffix-icon-style="color: #b6b6b6;" border="none" v-model="area_name" readonly
2023-12-20 18:13:01 +08:00
style="pointer-events: none;"></up-input>
</up-form-item>
2024-01-23 19:52:15 +08:00
<up-form-item label="养殖基地详细地址" prop="address" style="position: relative;" borderBottom required>
<up-input input-align="right" placeholder="请输入养殖基地详细地址" suffixIcon="map"
2023-12-20 18:13:01 +08:00
suffix-icon-style="color: #b6b6b6;" border="none" v-model="data.formData.address">
</up-input>
<view style="position: absolute;right: 0;width: 50rpx; height: 100%; " @click="location">
</view>
</up-form-item>
</view>
<view class="card-li">
2024-01-23 19:52:15 +08:00
<view class="c-title">养殖基地图片</view>
2023-12-20 18:13:01 +08:00
<view class="c-box c-box-p">
<view class="code-img1">
<view class="" v-if="pic.length>0"
style="display: flex;flex-direction: row;flex-wrap: wrap;margin-bottom: 30rpx;">
<view class="" v-for="(item,i) in pic" style="margin-right: 30rpx;">
<view class="">
<u-icon name="close" size="15" style="margin-left:120.85rpx;"
@click="delimg(i)"></u-icon>
<u-image :src="item" width="150.85rpx" height="150.85rpx"></u-image>
</view>
</view>
</view>
</view>
<view class="code-img">
<view class="" @click="updateImgFn">
<view class="carime-icon">
<u-image src="/static/img/DJSC.png" width="91.12rpx" height="91.12rpx"></u-image>
<view class="">
点击上传图片
</view>
</view>
</view>
</view>
</view>
</view>
</up-form>
<u-action-sheet :show="show" keyName="name" closeOnClickOverlay :actions="columns" title="请选择"
@close="show = false" @select="leavaType1">
</u-action-sheet>
<u-action-sheet :show="show1" keyName="name" :actions="columns1" title="请选择" @close="show1 = false"
@select="leavaType2">
</u-action-sheet>
<u-action-sheet :show="show2" keyName="name" :actions="columns2" title="请选择" @close="show2 = false"
@select="leavaType3">
</u-action-sheet>
2024-01-23 19:52:15 +08:00
<u-action-sheet :show="showFarm" keyName="name" :actions="farm_tpye_list" title="请选择" @close="showFarm = false"
@select="selectFarmType">
</u-action-sheet>
<u-action-sheet :show="showBreed" keyName="name" :actions="breed_tpye_list" title="请选择" @close="showBreed = false"
@select="selectBreedType">
</u-action-sheet>
2023-12-20 18:13:01 +08:00
<u-button class="btn" @click="submit">提交</u-button>
</view>
</view>
</template>
<script setup>
import {
ref,
reactive,
onMounted,
unref,
computed,
watch
} from 'vue';
import {
onLoad,
onReady
} from '@dcloudio/uni-app';
import {
Uploads
} from "@/api/upload.js"
import {
province,
city,
county,
towns,
villages,
groups
2024-01-23 19:52:15 +08:00
} from '@/api/api.js';
import {
farmAdd
} from '@/api/index.js';
import {
breedTypeLists,
farmTypeLists
} from '@/api/dict.js';
2023-12-20 18:13:01 +08:00
const form1 = ref(null);
const data = reactive({
formData: {
2024-01-23 19:52:15 +08:00
farm_name: '',
farm_type: '',
breed_type: '',
form_scale: '',
master: '',
master_contact: '',
province: '',
city: '',
2023-12-20 18:13:01 +08:00
area: '',
address: '',
lat: "",
lon: "",
master_name: '',
2024-01-23 19:52:15 +08:00
pic: []
2023-12-20 18:13:01 +08:00
},
rules: {
2024-01-23 19:52:15 +08:00
farm_name: [{
2023-12-20 18:13:01 +08:00
type: 'string',
required: true,
2024-01-23 19:52:15 +08:00
message: '请填写养殖基地名称',
2023-12-20 18:13:01 +08:00
trigger: ['blur', 'change']
}],
2024-01-23 19:52:15 +08:00
farm_type: [{
type: 'string',
required: true,
message: '请填写养殖基地类型',
trigger: ['blur', 'change']
}],
breed_type: [{
type: 'string',
required: true,
message: '请填写养殖基地种类',
trigger: ['blur', 'change']
}],
form_scale: [{
2023-12-20 18:13:01 +08:00
type: 'string',
required: true,
2024-01-23 19:52:15 +08:00
message: '请填写养殖基地规模',
2023-12-20 18:13:01 +08:00
trigger: ['blur', 'change']
}],
2024-01-23 19:52:15 +08:00
province: [{
2023-12-20 18:13:01 +08:00
type: 'string',
required: true,
message: '请请选择所在省份',
trigger: 'change'
}],
2024-01-23 19:52:15 +08:00
city: [{
2023-12-20 18:13:01 +08:00
type: 'string',
required: true,
message: '请选择所在城市',
trigger: 'change'
}],
2024-01-23 19:52:15 +08:00
area: [{
2023-12-20 18:13:01 +08:00
type: 'string',
required: true,
message: '请选择所在区县',
trigger: 'change'
}],
address: [{
type: 'string',
required: true,
2024-01-23 19:52:15 +08:00
message: '请输入养殖基地详细地址',
2023-12-20 18:13:01 +08:00
trigger: ['blur', 'change']
}],
2024-01-23 19:52:15 +08:00
master: [{
2023-12-20 18:13:01 +08:00
type: 'string',
required: true,
2024-01-23 19:52:15 +08:00
message: '请输入养殖基地负责人姓名',
2023-12-20 18:13:01 +08:00
trigger: ['blur', 'change']
}],
2024-01-23 19:52:15 +08:00
master_contact: [{
2023-12-20 18:13:01 +08:00
required: true,
message: '请输入手机号',
trigger: ['change', 'blur'],
},
{
validator: (rule, value, callback) => {
// 上面有说返回true表示校验通过返回false表示不通过
// uni.$u.test.mobile()就是返回true或者false的
return uni.$u.test.mobile(value);
},
message: '手机号码不正确',
// 触发器可以同时用blur和change
trigger: ['change', 'blur'],
}
],
}
});
const pic = reactive([]);
const province_name = ref(null)
const city_name = ref(null)
2024-01-23 19:52:15 +08:00
const area_name = ref(null)
const farm_type_name = ref(null)
const breed_type_name = ref(null)
2023-12-20 18:13:01 +08:00
const show = ref(false)
const show1 = ref(false)
const show2 = ref(false)
2024-01-23 19:52:15 +08:00
const showFarm = ref(false)
const showBreed = ref(false)
2023-12-20 18:13:01 +08:00
const columns = reactive([])
const columns1 = reactive([])
const columns2 = reactive([])
2024-01-23 19:52:15 +08:00
const farm_tpye_list = reactive([])
const breed_tpye_list = reactive([])
2023-12-20 18:13:01 +08:00
const provincen = () => {
province().then((res) => {
columns.splice(0, columns.length, ...res.data.map((step, index) => {
return {
name: step.province_name,
id: step.province_code,
};
}));
});
}
const cityn = (id) => {
city({
province_code: id
}).then((res) => {
2024-01-23 19:52:15 +08:00
columns1.splice(0, columns1.length, ...res.data.map((step, index) => {
2023-12-20 18:13:01 +08:00
return {
name: step.city_name,
id: step.city_code,
};
}));
});
}
const countyn = (id) => {
county({
city_code: id
}).then((res) => {
2024-01-23 19:52:15 +08:00
columns2.splice(0, columns2.length, ...res.data.map((step, index) => {
2023-12-20 18:13:01 +08:00
return {
name: step.county_name,
id: step.county_code,
};
}));
});
}
2024-01-23 19:52:15 +08:00
const farmTypeListsn = (id) => {
farmTypeLists().then((res) => {
farm_tpye_list.splice(0, farm_tpye_list.length, ...res.data.map((step, index) => {
return {
name: step.name,
value: step.value,
id: step.id
};
}));
});
}
const selectFarmType = (e)=>{
farm_type_name.value = e.name;
data.formData.farm_type = e.value;
}
const breedTypeListsn = (id) => {
breedTypeLists().then((res) => {
breed_tpye_list.splice(0, breed_tpye_list.length, ...res.data.map((step, index) => {
return {
name: step.name,
value: step.value,
id: step.id
};
}));
});
}
const selectBreedType = (e)=>{
breed_type_name.value = e.name;
data.formData.breed_type = e.value;
}
2023-12-20 18:13:01 +08:00
//图片删除
const delimg = (i) => {
pic.splice(i, 1);
}
//图片上传
const updateImgFn = async () => {
uni.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: (res) => {
Uploads(res.tempFilePaths[0], 'img').then(res => {
// console.log(res)
if (res.code == 1) {
pic.push(res.data.url)
console.log(data.formData)
uni.$u.toast('上传成功')
}
}).catch(err => {
//console.log('err', err);
// uni.$u.toast('上传失败')
})
},
fail: function(err) {
//console.log('choose失败');
uni.$u.toast('添加失败')
}
});
// let res = await Uploads()
// data.formData.pic = res.data.image
}
// 地址 省 市 街道 乡村 小队
const isshow1 = () => {
show.value = true
}
const isshow2 = () => {
show1.value = true
}
const isshow3 = () => {
show2.value = true
}
2024-01-23 19:52:15 +08:00
const isshowFarm = () =>{
showFarm.value = true
}
const isshowBreed = () =>{
showBreed.value = true
}
2023-12-20 18:13:01 +08:00
const leavaType1 = (e) => {
2024-01-23 19:52:15 +08:00
console.log(e);
2023-12-20 18:13:01 +08:00
province_name.value = e.name
2024-01-23 19:52:15 +08:00
data.formData.province = e.id
2023-12-20 18:13:01 +08:00
cityn(e.id)
}
const leavaType2 = (e) => {
city_name.value = e.name
2024-01-23 19:52:15 +08:00
data.formData.city = e.id
2023-12-20 18:13:01 +08:00
countyn(e.id)
}
const leavaType3 = (e) => {
2024-01-23 19:52:15 +08:00
area_name.value = e.name
data.formData.area = e.id
2023-12-20 18:13:01 +08:00
}
2024-01-23 19:52:15 +08:00
2023-12-20 18:13:01 +08:00
const location = () => {
2024-01-23 19:52:15 +08:00
uni.navigateTo({
url: '/pages/addPlant/select_address_n',
success: (res)=>{
uni.$once('setData', (e)=>{
e = JSON.parse(e||'{}');
console.log('选择地址', e);
data.formData.address = e.address;
data.formData.lat = e.lat;
data.formData.lon = e.lon;
})
}
})
2023-12-20 18:13:01 +08:00
}
onReady(() => {
form1.value.setRules(data.rules);
});
const submit = async () => {
try {
const valid = await form1.value.validate();
if (valid) {
// console.log('表单通过', data.formData);
if (pic.length == 0) {
uni.$u.toast('请上传图片')
return
}
data.formData.pic = JSON.stringify(pic)
// 表单验证通过,执行提交操作
2024-01-23 19:52:15 +08:00
farmAdd(data.formData).then((res) => {
2023-12-20 18:13:01 +08:00
console.log(res)
if (res.code == 1) {
uni.navigateBack({
delta: 1
})
uni.$u.toast(res.msg)
// console.log('提交通过');
}
})
} else {
// 表单验证不通过
console.log('表单验证未通过');
}
} catch (error) {
// 捕获验证过程中的错误
console.error(error);
}
}
onLoad((options) => {
2024-01-23 19:52:15 +08:00
provincen();
farmTypeListsn();
breedTypeListsn();
2023-12-20 18:13:01 +08:00
})
</script>
<style lang="scss">
page {
background-color: #fffcf5;
}
.card {
background-color: #fffcf5;
padding: 0;
padding-bottom: 80rpx;
.c-title {
font-weight: bold;
display: flex;
align-items: center;
padding: 30rpx;
padding-bottom: 15rpx;
&:before {
content: '';
display: inline-block;
height: 0.8rem;
width: 6rpx;
margin-top: 0.2rem;
margin-right: 10rpx;
background-color: #feb048;
border-radius: 6rpx;
}
}
.c-box {
background-color: #fff;
padding-left: 30rpx;
padding-right: 20rpx;
padding-bottom: 20rpx;
box-shadow: 0px 2px 8px rgba(0, 0, 0, 0.1);
}
.c-box-p {
padding: 20rpx;
}
.btn {
margin: 20rpx auto;
width: 694rpx;
height: 80rpx;
border-radius: 80rpx;
background-color: #ffb049;
color: #fff;
}
}
.card-li-tit {
position: relative;
span {
position: absolute;
left: -9px;
color: #f56c6c;
line-height: 20px;
font-size: 20px;
top: 3px;
}
margin-bottom: 20rpx;
}
.confim-btn {
margin: 0 auto;
width: 196.26rpx;
height: 66.59rpx;
/* border: ; */
border: #00A15E 1px solid;
color: #00A15E;
display: flex;
align-items: center;
justify-content: center;
border-radius: 42.06rpx 42.06rpx 42.06rpx 42.06rpx;
}
.up-img {
width: 341.71rpx
}
.today-btn {
width: 588.79rpx;
background-color: #00A15E;
color: white;
position: fixed;
bottom: 40rpx;
/* transform: ; */
left: 50%;
transform: translateX(-50%);
background: linear-gradient(to right, #00A15E, #4CC593);
/* margin: 0 auto; */
}
.code-img {
background-color: #F4F4F4;
height: 350.47rpx;
position: relative;
.carime-icon {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: flex;
flex-direction: column;
align-items: center;
}
}
</style>