add.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <div class="hum-page-container">
  3. <div class="extra">
  4. <div class="extra-name">当前站点:{{ site.siteName }}</div>
  5. <div class="extra-actions">
  6. <el-upload :action="uploadAction" :on-success="importSection" name="file" :show-file-list="false">
  7. <el-button type="primary">导入</el-button>
  8. </el-upload>
  9. <el-button :disabled="!this.id" class="extra-action-export" @click="download">导出</el-button>
  10. </div>
  11. </div>
  12. <div class="hum-page-title" style="margin-top: 30px">断面图</div>
  13. <div class="hum-page-section">
  14. <SectionChart :sections="sections" />
  15. </div>
  16. <div class="hum-page-title" style="margin-top: 30px">断面表</div>
  17. <div class="hum-page-section">
  18. <el-form :model="form" :rules="rules" size="large" ref="form" label-position="top">
  19. <el-row :gutter="30">
  20. <el-col :span="6">
  21. <el-form-item label="断面名称" prop="berthingName">
  22. <el-input
  23. v-model="form.berthingName"
  24. placeholder="请输入断面名称"
  25. clearable
  26. />
  27. </el-form-item>
  28. </el-col>
  29. </el-row>
  30. </el-form>
  31. <el-table v-loading="loading" :data="tableSections" border height="400">
  32. <el-table-column label="起点距">
  33. <template slot-scope="scope">
  34. <el-input v-if="scope.row.isEdit" size="mini" type="text" v-model="form.x" />
  35. <span v-else>{{ scope.row.x }}</span>
  36. </template>
  37. </el-table-column>
  38. <el-table-column label="高程">
  39. <template slot-scope="scope">
  40. <el-input v-if="scope.row.isEdit" size="mini" type="text" v-model="form.y" />
  41. <span v-else>{{ scope.row.y }}</span>
  42. </template>
  43. </el-table-column>
  44. <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
  45. <template slot-scope="scope">
  46. <el-button v-if="scope.row.isEdit" size="mini" type="text" icon="el-icon-circle-check" @click="saveSection(scope.row)">保存</el-button>
  47. <el-button v-else size="mini" type="text" icon="el-icon-edit" @click="editSection(scope.row)">编辑</el-button>
  48. <el-button size="mini" type="text" icon="el-icon-top" @click="insertSection(scope.$index - 1)">向上插入</el-button>
  49. <el-button size="mini" type="text" icon="el-icon-bottom" @click="insertSection(scope.$index + 1)">向下插入</el-button>
  50. <el-button size="mini" type="text" icon="el-icon-delete" @click="deleteSection(scope.$index)">删除</el-button>
  51. </template>
  52. </el-table-column>
  53. </el-table>
  54. </div>
  55. <div class="hum-page-form-action">
  56. <el-button @click="$router.back()">取消</el-button>
  57. <el-button type="primary" @click="submitForm" :loading="loading">保存</el-button>
  58. </div>
  59. </div>
  60. </template>
  61. <script>
  62. import { getSite, updateSite } from '@/api/site/site'
  63. import { addSection, getSection } from '@/api/site/berthing'
  64. import SectionChart from './chart';
  65. export default {
  66. components: {
  67. SectionChart,
  68. },
  69. data() {
  70. return {
  71. id: 0,
  72. siteId: 0,
  73. site: 0,
  74. loading: false,
  75. sections: [],
  76. tableSections: [],
  77. form: {
  78. x: 0,
  79. y: 0,
  80. berthingName: '',
  81. },
  82. rules: {
  83. berthingName: [
  84. {required: true, message: "断面名称不能为空", trigger: "blur"}
  85. ],
  86. },
  87. uploadAction: `${process.env.VUE_APP_BASE_API}/berthing/uploadFile`
  88. }
  89. },
  90. computed: {
  91. title() {
  92. return this.id ? '编辑断面' : '新增断面';
  93. }
  94. },
  95. methods: {
  96. loadSection() {
  97. getSection(this.id).then((res) => {
  98. const positions = JSON.parse(res.data.positions) || [];
  99. this.sections = positions;
  100. this.form.berthingName = res.data.berthingName;
  101. this.tableSections = positions.map(({ x, y }) => ({ x, y, isEdit: false }))
  102. })
  103. },
  104. loadSite() {
  105. getSite(this.siteId).then((res) => {
  106. this.site = { ...res.data };
  107. })
  108. },
  109. importSection(res) {
  110. this.sections = res.data || [];
  111. this.tableSections = (res.data || []).map(({ x, y }) => ({ x, y, isEdit: false }))
  112. },
  113. saveSection(s) {
  114. if (!/^-?\d+(\.\d+)?$/.test(this.form.x)) {
  115. return this.$message.error("起点距必须是数字")
  116. }
  117. if (!/^-?\d+(\.\d+)?$/.test(this.form.y)) {
  118. return this.$message.error("高程必须是数字")
  119. }
  120. s.x = this.form.x;
  121. s.y = this.form.y;
  122. s.isEdit = false;
  123. this.sections = this.tableSections.map(({ x, y }) => ({ x, y }));
  124. },
  125. editSection(s) {
  126. this.tableSections.forEach((r) => r.isEdit = false)
  127. this.form.x = s.x;
  128. this.form.y = s.y;
  129. s.isEdit = true;
  130. },
  131. insertSection(index) {
  132. this.form.x = 0;
  133. this.form.y = 0;
  134. if (index < 0) {
  135. this.sections = [
  136. { x: 0, y: 0 },
  137. ...this.sections,
  138. ]
  139. this.tableSections = [
  140. { x: 0, y: 0, isEdit: true },
  141. ...this.tableSections,
  142. ]
  143. } else {
  144. this.sections = [
  145. ...this.sections.slice(0, index),
  146. { x: 0, y: 0 },
  147. ...this.sections.slice(index),
  148. ]
  149. this.tableSections = [
  150. ...this.tableSections.slice(0, index),
  151. { x: 0, y: 0, isEdit: true },
  152. ...this.tableSections.slice(index),
  153. ]
  154. }
  155. },
  156. deleteSection(index) {
  157. this.tableSections.splice(index, 1);
  158. this.sections = this.tableSections.map(({ x, y }) => ({ x, y }));
  159. },
  160. submitForm() {
  161. this.$refs["form"].validate(valid => {
  162. if (!valid) {
  163. return;
  164. }
  165. this.loading = true;
  166. addSection({ ...this.form, siteId: this.siteId, positions: this.sections }).then(response => {
  167. this.$modal.msgSuccess(this.id ? "编辑成功" : "新增成功");
  168. this.$router.back();
  169. }).finally(() => {
  170. this.loading = false;
  171. });
  172. });
  173. },
  174. download() {
  175. window.open(`${process.env.VUE_APP_BASE_API}/berthing/downFile/${this.id}`, '_blank')
  176. },
  177. },
  178. mounted() {
  179. this.id = this.$route.params.id;
  180. this.siteId = this.$route.query.siteId;
  181. if (this.siteId) {
  182. this.loadSite();
  183. }
  184. if (this.id) {
  185. this.loadSection();
  186. }
  187. },
  188. }
  189. </script>
  190. <style lang="scss" scoped>
  191. .extra {
  192. display: flex;
  193. align-items: center;
  194. justify-content: space-between;
  195. background: #fff;
  196. padding: 20px;
  197. border-radius: 4px;
  198. &-name {
  199. font-size: 14px;
  200. font-weight: 500;
  201. color: #1D2738;
  202. }
  203. &-actions {
  204. display: flex;
  205. }
  206. &-action-export {
  207. margin-left: 16px;
  208. }
  209. }
  210. </style>