Files
app-govern/pages/project/new.vue

473 lines
17 KiB
Vue
Raw Normal View History

2023-02-20 14:19:28 +08:00
<template>
2023-05-24 08:58:48 +08:00
<Cn-page :loading="loading">
<view slot="body">
<view class="new">
2023-02-20 14:19:28 +08:00
<view class="content">
<uni-forms :label-width="80">
2023-05-24 08:58:48 +08:00
<uni-forms-item label="工程名称">
2023-05-25 10:10:22 +08:00
<uni-easyinput
v-model="formData.engineeringName"
placeholder="请输入项目名称"
:clearable="false"
2023-08-10 09:18:17 +08:00
disabled
2023-05-25 10:10:22 +08:00
/>
2023-05-24 08:58:48 +08:00
</uni-forms-item>
2023-02-20 14:19:28 +08:00
<uni-forms-item label="项目名称">
2023-08-17 09:24:59 +08:00
<uni-easyinput v-model="formData.name" placeholder="请输入项目名称"/>
2023-02-20 14:19:28 +08:00
</uni-forms-item>
<uni-forms-item label="区域">
2023-05-24 08:58:48 +08:00
<view style="display: flex">
<uni-easyinput
:clearable="false"
type="textarea"
autoHeight
v-model="formData.area"
placeholder="请输入区域信息"
/>
2023-07-24 08:47:20 +08:00
<uni-icons
type="location"
color="#007aff"
size="26"
class="ml20"
@click="chooseLocation"
></uni-icons>
2023-02-20 14:19:28 +08:00
</view>
</uni-forms-item>
<uni-forms-item label="描述">
2023-07-24 08:47:20 +08:00
<uni-easyinput
type="textarea"
autoHeight
v-model="formData.description"
placeholder="请输入项目描述"
/>
2023-02-20 14:19:28 +08:00
</uni-forms-item>
2023-07-24 08:47:20 +08:00
<view class="temp-choose">
<view class="temp-choose-title">从模版库选择拓扑图</view>
<view class="temp-choose-content">
<view class="temp-choose-content-item" v-for="(item, index) in formData.tempFiles">
<image
class="temp-choose-content-item-img"
:src="item.filePath"
mode="aspectFill"
/>
<view class="temp-choose-content-item-close" @click="deleteImg(index)">
2023-08-17 09:24:59 +08:00
<uni-icons style="font-size: 18px" type="closeempty" color="#fff" size="20"/>
2023-07-24 08:47:20 +08:00
</view>
</view>
<view class="temp-choose-content-item" @click="openTemp">
<uni-icons type="plusempty" size="70" color="#f1f1f1"></uni-icons>
</view>
</view>
</view>
2023-05-24 08:58:48 +08:00
<uni-file-picker
v-model="formData.files"
2023-07-24 08:47:20 +08:00
title="从本地上传拓扑图"
2023-05-24 08:58:48 +08:00
:sourceType="['album']"
2023-08-17 09:24:59 +08:00
:before-remove="beforeRemove"
2023-05-24 08:58:48 +08:00
@select="select"
></uni-file-picker>
2023-02-20 14:19:28 +08:00
</uni-forms>
</view>
<view class="btn-wrap">
2023-08-17 09:24:59 +08:00
<view class="btn-wrap-item" @click="submit"> 提交</view>
2023-02-20 14:19:28 +08:00
</view>
2023-07-24 08:47:20 +08:00
<uni-drawer ref="showRight" mode="right" :mask-click="false">
<uni-indexed-list
:options="engineeringListFilter"
:showSelect="false"
@click="closeDrawer"
></uni-indexed-list>
2023-05-24 08:58:48 +08:00
</uni-drawer>
2023-07-24 08:47:20 +08:00
<uni-popup ref="showTemp" type="bottom" :mask-click="false">
<view class="popup-header">
<view class="popup-header-title">模版库</view>
<view class="popup-header-close" @click="closeTemp">
2023-08-17 09:24:59 +08:00
<uni-icons type="closeempty" color="#111" size="20"/>
2023-07-24 08:47:20 +08:00
</view>
</view>
<view class="temp-list">
<view
class="temp-list-item"
:class="{
2023-07-31 09:00:30 +08:00
'temp-list-item-active': formData.tempFiles.some(
2023-08-17 09:24:59 +08:00
(item2) => item2.id === item.id || item2.topoId === item.id,
2023-07-31 09:00:30 +08:00
),
2023-07-24 08:47:20 +08:00
}"
v-for="(item, index) in tempList"
@click="chooseTempItem(item)"
>
2023-08-17 09:24:59 +08:00
<image class="temp-list-item-img" :src="item.filePath" mode="aspectFill"/>
2023-07-24 08:47:20 +08:00
<view class="temp-list-item-name">{{ item.name }}</view>
</view>
</view>
</uni-popup>
2023-02-20 14:19:28 +08:00
</view>
</view>
</Cn-page>
</template>
<script>
2023-08-17 09:24:59 +08:00
import {pinyin} from 'pinyin-pro'
import {addAppProject, updateAppProject, checkCanDelete} from '../../common/api/project'
import {getTopoTemplate} from '../../common/api/device'
import {queryEngineering} from '@/common/api/engineering.js'
2023-02-20 14:19:28 +08:00
export default {
2023-05-24 08:58:48 +08:00
data() {
2023-02-20 14:19:28 +08:00
return {
loading: false,
formData: {
2023-05-24 08:58:48 +08:00
engineeringId: '',
engineeringName: '',
area: '',
2023-03-30 09:04:07 +08:00
files: [],
2023-07-24 08:47:20 +08:00
tempFiles: [],
topoIds: [],
2023-03-30 09:04:07 +08:00
description: '',
2023-02-20 14:19:28 +08:00
name: '',
2023-08-10 09:18:17 +08:00
lat: '',
lng: '',
2023-02-20 14:19:28 +08:00
},
2023-07-24 08:47:20 +08:00
engineeringList: [],
tempList: [],
2023-07-31 09:00:30 +08:00
project: null,
2023-08-17 15:37:19 +08:00
options: {}
2023-02-20 14:19:28 +08:00
}
},
2023-05-24 08:58:48 +08:00
computed: {
2023-07-24 08:47:20 +08:00
engineeringListFilter() {
2023-05-24 08:58:48 +08:00
let result = []
2023-07-24 08:47:20 +08:00
this.engineeringList.forEach((item) => {
2023-08-17 09:24:59 +08:00
let arr = pinyin(item.name[0], {toneType: 'none', type: 'array'})
2023-05-24 08:58:48 +08:00
let letter = arr[0][0].toUpperCase()
console.log(letter)
let index = result.findIndex((item) => item.letter === letter)
if (index === -1) {
result.push({
letter,
data: [item.name],
})
} else {
result[index].data.push(item.name)
}
})
return result
},
},
2023-07-24 08:47:20 +08:00
onLoad(options) {
2023-08-17 15:37:19 +08:00
this.options = options
2023-07-24 08:47:20 +08:00
if (options.project) {
2023-07-31 09:00:30 +08:00
uni.setNavigationBarTitle({
title: '编辑项目',
})
2023-07-24 08:47:20 +08:00
this.project = JSON.parse(decodeURIComponent(options.project))
console.log(this.project)
for (let key in this.formData) {
if (this.project[key]) {
this.formData[key] = this.project[key]
}
}
2023-08-10 09:18:17 +08:00
} else {
let engineering = uni.getStorageSync(this.$cacheKey.engineering)
this.formData.engineeringId = engineering.id
this.formData.engineeringName = engineering.name
2023-07-24 08:47:20 +08:00
}
2023-03-30 09:04:07 +08:00
uni.getLocation({
type: 'wgs84',
success: function (res) {
2023-05-24 08:58:48 +08:00
console.log(res)
console.log('当前位置的经度:' + res.longitude)
console.log('当前位置的纬度:' + res.latitude)
},
})
queryEngineering().then((res) => {
2023-07-24 08:47:20 +08:00
this.engineeringList = res.data
})
getTopoTemplate().then((res) => {
console.log(res)
this.tempList = res.data
2023-07-31 09:00:30 +08:00
if (this.project) {
this.project.topologyDiagramPaths.forEach((item) => {
if (this.tempList.some((item2) => item2.id === item.topoId)) {
this.formData.tempFiles.push(item)
} else {
this.formData.files.push({
url: item.filePath,
extname: item.name.split('.')[1],
name: item.name,
...item,
})
}
})
}
2023-05-24 08:58:48 +08:00
})
2023-03-30 09:04:07 +08:00
},
2023-02-20 14:19:28 +08:00
methods: {
2023-08-17 09:24:59 +08:00
beforeRemove(e) {
console.log(e)
if (!e.tempFile.id) {
return true
}
return new Promise((resolve, reject) => {
checkCanDelete(e.tempFile.id).then((res) => {
console.log(res)
if (res.data) {
resolve(true)
} else {
reject()
}
})
})
},
async deleteImg(index) {
await this.beforeRemove({
tempFile: this.formData.tempFiles[index],
})
2023-07-24 08:47:20 +08:00
this.formData.tempFiles.splice(index, 1)
},
2023-08-17 09:24:59 +08:00
async chooseTempItem(item) {
console.log(item)
console.log(this.formData.tempFiles)
// 编辑的时候如果已经存在就要验证topoId
let index = this.formData.tempFiles.findIndex((item2) => item2.id === item.id || item2.topoId === item.id)
if (index > -1) {
await this.beforeRemove({
tempFile: this.formData.tempFiles[index],
})
this.formData.tempFiles.splice(index, 1)
2023-07-24 08:47:20 +08:00
} else {
this.formData.tempFiles.push(item)
}
},
closeTemp() {
this.$refs.showTemp.close()
},
openTemp() {
this.$refs.showTemp.open()
},
2023-05-24 08:58:48 +08:00
showDrawer() {
this.$refs.showRight.open()
2023-03-30 09:04:07 +08:00
},
2023-05-24 08:58:48 +08:00
closeDrawer(e) {
console.log(e)
this.formData.engineeringName = e.item.name
2023-07-24 08:47:20 +08:00
this.formData.engineeringId = this.engineeringList.find((item) => item.name === e.item.name).id
2023-05-24 08:58:48 +08:00
this.$refs.showRight.close()
},
select(e) {
console.log(e)
2023-08-17 09:24:59 +08:00
this.formData.files.push(...e.tempFiles)
2023-05-24 08:58:48 +08:00
console.log(this.formData.files)
},
chooseLocation() {
2023-02-20 14:19:28 +08:00
uni.chooseLocation({
2023-08-11 11:03:31 +08:00
success: (res) => {
2023-08-10 09:18:17 +08:00
this.formData.area = res.name
this.formData.lat = res.latitudeame
this.formData.lng = res.longitude
2023-05-24 08:58:48 +08:00
console.log('位置名称:' + res.name)
console.log('详细地址:' + res.address)
console.log('纬度:' + res.latitude)
console.log('经度:' + res.longitude)
},
})
2023-02-20 14:19:28 +08:00
},
2023-05-24 08:58:48 +08:00
async submit() {
console.log(this.formData)
2023-03-30 09:04:07 +08:00
if (!this.formData.name) {
this.$util.toast('请输入项目名称')
return
}
if (!this.formData.area) {
this.$util.toast('请输入区域信息')
return
}
if (!this.formData.description) {
this.$util.toast('请输入项目描述')
return
}
2023-07-24 08:47:20 +08:00
if (!this.formData.files.length && !this.formData.tempFiles.length) {
this.$util.toast('请至少选择一张拓扑图')
2023-03-30 09:04:07 +08:00
return
}
let arr = []
for (let i = 0; i < this.formData.files.length; i++) {
let item = this.formData.files[i]
2023-07-24 08:47:20 +08:00
console.log(item)
2023-03-30 09:04:07 +08:00
arr.push({
2023-08-17 09:24:59 +08:00
...item,
2023-03-30 09:04:07 +08:00
name: 'files',
2023-05-25 10:10:22 +08:00
uri: item.url,
2023-03-30 09:04:07 +08:00
})
}
2023-08-17 09:24:59 +08:00
console.log(arr)
2023-08-17 15:37:19 +08:00
this.formData.topoIds = this.formData.tempFiles.map((item) => (item.id))
2023-03-30 09:04:07 +08:00
let data = JSON.parse(JSON.stringify(this.formData))
2023-07-31 09:00:30 +08:00
let res = {}
2023-08-10 09:18:17 +08:00
console.warn(data, arr)
2023-07-31 09:00:30 +08:00
if (this.project) {
data.id = this.project.id
2023-08-17 09:24:59 +08:00
data.newTopoIds = data.files.filter(item => item.id).map((item) => item.id)
data.newTopoIds.push(...data.tempFiles.filter(item => item.id && item.topoId).map((item) => item.id))
data.topoIds = data.tempFiles.filter(item => item.id && !item.topoId).map((item) => item.id)
delete data.tempFiles
delete data.files
arr = arr.filter(item => !item.id)
2023-07-31 09:00:30 +08:00
res = await updateAppProject(data, arr)
} else {
2023-08-17 09:24:59 +08:00
delete data.tempFiles
delete data.files
2023-07-31 09:00:30 +08:00
res = await addAppProject(data, arr)
}
2023-08-10 09:18:17 +08:00
let result = {}
if (res.code === 'A0000') {
result = res
} else {
console.warn(res)
if (res.length === 1) {
this.$util.toast(res[0].message)
return
}
result = JSON.parse(res[1].data)
2023-07-31 09:00:30 +08:00
}
if (result.code === 'A0000') {
if (this.project) {
this.$util.toast('项目修改成功')
this.$util.refreshPrePage(2)
2023-07-24 08:47:20 +08:00
} else {
2023-08-17 15:37:19 +08:00
if (this.options.from === 'newDevice') {
uni.navigateBack()
} else {
this.$util.toast('项目创建成功')
this.$util.refreshPrePage()
}
2023-07-24 08:47:20 +08:00
}
2023-07-31 09:00:30 +08:00
} else {
this.$util.toast(result.message)
}
2023-05-24 08:58:48 +08:00
},
},
2023-02-20 14:19:28 +08:00
}
</script>
2023-05-24 08:58:48 +08:00
<style lang="scss">
2023-02-20 14:19:28 +08:00
.new {
2023-02-28 14:03:38 +08:00
padding: 34rpx;
2023-02-20 14:19:28 +08:00
.content {
.content-des {
font-size: 28rpx;
color: #666;
margin-bottom: 20rpx;
}
margin-bottom: 20rpx;
2023-02-28 14:03:38 +08:00
padding: 34rpx;
2023-02-20 14:19:28 +08:00
background: $uni-theme-white;
border-radius: 12rpx;
}
.btn-wrap {
margin-top: 40rpx;
display: flex;
align-items: center;
justify-content: space-between;
.btn-wrap-item {
display: flex;
align-items: center;
justify-content: center;
flex: 1;
2023-08-03 14:51:36 +08:00
background: $uni-theme-color;
2023-02-20 14:19:28 +08:00
color: #fff;
height: 80rpx;
border-radius: 12rpx;
}
}
2023-08-17 09:24:59 +08:00
2023-07-24 08:47:20 +08:00
.temp-choose {
margin-bottom: 44rpx;
2023-08-17 09:24:59 +08:00
2023-07-24 08:47:20 +08:00
.temp-choose-title {
font-size: 14px;
color: #333;
}
2023-08-17 09:24:59 +08:00
2023-07-24 08:47:20 +08:00
.temp-choose-content {
margin-top: 20rpx;
display: grid;
grid-template-columns: repeat(3, 198rpx);
grid-gap: 10rpx;
2023-08-17 09:24:59 +08:00
2023-07-24 08:47:20 +08:00
.temp-choose-content-item {
box-sizing: border-box;
border: 1px #eee solid;
position: relative;
display: flex;
align-items: center;
justify-content: center;
height: 198rpx;
2023-08-17 09:24:59 +08:00
2023-07-24 08:47:20 +08:00
.temp-choose-content-item-img {
height: 100%;
width: 100%;
background: skyblue;
display: block;
}
2023-08-17 09:24:59 +08:00
2023-07-24 08:47:20 +08:00
.temp-choose-content-item-close {
display: flex;
align-items: center;
justify-content: center;
position: absolute;
top: 3px;
right: 3px;
border-radius: 50%;
background-color: rgba(0, 0, 0, 0.5);
z-index: 2;
height: 52rpx;
width: 52rpx;
font-size: 24rpx;
color: #666;
}
}
}
}
.temp-list {
box-sizing: border-box;
width: 750rpx;
height: 80vh;
padding: 20rpx;
overflow-y: scroll;
background: #fff;
.temp-list-item {
margin-bottom: 10rpx;
position: relative;
box-sizing: border-box;
border: 4px solid #f1f1f1;
2023-08-17 09:24:59 +08:00
2023-07-24 08:47:20 +08:00
.temp-list-item-img {
height: 280rpx;
width: 100%;
}
2023-08-17 09:24:59 +08:00
2023-07-24 08:47:20 +08:00
.temp-list-item-name {
position: absolute;
right: 0;
top: 0;
background: #fff;
padding: 4rpx 8rpx;
}
2023-08-17 09:24:59 +08:00
2023-07-24 08:47:20 +08:00
&-active {
2023-08-03 14:51:36 +08:00
border: 4rpx solid $uni-theme-color;
2023-07-24 08:47:20 +08:00
}
}
}
2023-02-20 14:19:28 +08:00
}
/deep/ .uni-drawer__content {
width: 100vw !important;
}
2023-05-24 08:58:48 +08:00
</style>