github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/ui/src/views/mock/components/Edit.vue (about) 1 <template> 2 <div class="mock-edit-modal"> 3 <a-modal 4 :closable=false 5 :footer="null" 6 :title="model.id == undefined ? $t('msg.mock.create') : $t('msg.mock.edit')" 7 :visible="visible" 8 dialogClass="full-screen-modal" 9 width="100%" 10 > 11 <div class="mock-edit-main"> 12 <div class="buttons"> 13 <a-button :disabled="!readyToSave" type="primary" @click="save"> 14 {{ $t('form.save') }} 15 </a-button> 16 <a-button @click="cancel"> 17 {{ $t('form.close') }} 18 </a-button> 19 </div> 20 21 <a-row :gutter="10" class="content-row"> 22 <a-col v-if="model.id == undefined" :span="11" class="content-col"> 23 <div class="upload-bar"> 24 <a-row> 25 <a-col :span="5"> 26 <a-upload :before-upload="beforeUpload" 27 :showUploadList="false" 28 accept=".yaml,.yml,.json"> 29 <a-button> 30 <a-icon type="upload"/> 31 <span>{{ $t('upload.spec') }}</span> 32 </a-button> 33 </a-upload> 34 </a-col> 35 <a-col :span="9"> 36 <span class="title">{{ model.name }}</span> 37 </a-col> 38 <a-col :span="10"> 39 <span class="label-path"></span> 40 <a-input v-model="model.path" :placeholder="$t('msg.mock.input.path')"/> 41 </a-col> 42 </a-row> 43 44 </div> 45 <div class="upload-content"> 46 <pre>{{ model.specContent }}</pre> 47 </div> 48 </a-col> 49 50 <a-col :span="model.id === undefined ? 13 : 24" class="content-col"> 51 <a-tabs :activeKey="currentTab" :animated="false" @change="tabChange"> 52 <a-tab-pane key="data" :tab="$t('msg.mock.data')"> 53 <pre v-show="model.id === undefined">{{ model.dataContent }}</pre> 54 <design-in-component 55 v-show="model.id !== undefined" 56 ref="designPage" 57 :modelProp="dataModel" 58 :time="time" 59 :type="resType" 60 @save="onModelDataSave"> 61 :visible="true"> 62 </design-in-component> 63 </a-tab-pane> 64 <a-tab-pane key="mock" :tab="$t('msg.mock.mock')"> 65 <!-- <pre>{{ model.mockContent }}</pre>--> 66 <div class="yaml-editor"> 67 <yaml-editor v-model="model.mockContent"/> 68 </div> 69 </a-tab-pane> 70 </a-tabs> 71 </a-col> 72 </a-row> 73 74 </div> 75 </a-modal> 76 77 </div> 78 </template> 79 80 <script> 81 import {uploadMock} from "@/api/mock"; 82 import mockMixin from "@/store/mockMixin"; 83 import {DesignInComponent} from '../../../components' 84 import {ResTypeDef} from "../../../api/utils"; 85 import YamlEditor from './Yaml.vue'; 86 import {getMock} from "@/api/mock"; 87 88 89 export default { 90 name: 'MockEditComp', 91 components: { 92 DesignInComponent, 93 YamlEditor, 94 }, 95 data() { 96 return { 97 model: {mockContent:''}, 98 resType: ResTypeDef, 99 specReady: false, 100 currentTab: this.current, 101 cmOptions: { 102 lineNumbers: true, // 显示行号 103 mode: 'text/x-yaml', // 语法model 104 gutters: ['CodeMirror-lint-markers'], // 语法检查器 105 theme: 'monokai', // 编辑器主题 106 lint: true // 开启语法检查 107 }, 108 }; 109 }, 110 props: { 111 type: { 112 type: String, 113 required: true 114 }, 115 visible: { 116 type: Boolean, 117 required: true 118 }, 119 mock: { 120 type: Object, 121 default: () => null 122 }, 123 time: { 124 type: Number, 125 default: () => 0 126 }, 127 current: { 128 type: String, 129 default: () => '' 130 }, 131 }, 132 mixins: [mockMixin], 133 computed: { 134 readyToSave() { 135 return this.specReady && this.model.path?.trim() 136 }, 137 codemirror() { 138 return this.$refs.cmEditor.codemirror 139 }, 140 dataModel() { 141 return {id: this.model.defId} 142 }, 143 }, 144 created() { 145 console.log('created') 146 }, 147 mounted: function () { 148 console.log('mounted') 149 }, 150 beforeDestroy() { 151 console.log('beforeDestroy') 152 }, 153 watch: { 154 mock(val) { 155 if(val == undefined){ 156 val = {mockContent:''}; 157 } 158 this.model = val 159 if(val.id !== undefined){ 160 this.specReady = true; 161 } 162 console.log("watch mock :", val) 163 }, 164 current(val) { 165 this.currentTab = val 166 } 167 }, 168 169 methods: { 170 save() { 171 console.log('save') 172 this.saveMockItem(this.model).then((json) => { 173 console.log('saveMockItem', json) 174 if (json.code === 0) { 175 this.model = {mockContent:''} 176 this.specReady = false 177 this.$emit('ok') 178 } else { 179 this.$notification['warning']({ 180 message: this.$i18n.t('upload.spec.failed'), 181 duration: 3, 182 }); 183 } 184 }) 185 }, 186 cancel() { 187 console.log('cancel') 188 this.model = {mockContent:''} 189 this.specReady = false 190 this.$emit('cancel') 191 }, 192 tabChange(key) { 193 this.currentTab = key 194 }, 195 196 getModel(id) { 197 console.log('getModel', id) 198 }, 199 200 onModelDataSave() { 201 getMock(this.model.id).then(json =>{ 202 this.model.dataContent = json.data.dataContent; 203 }) 204 }, 205 206 beforeUpload(file) { 207 console.log('beforeUpload', file) 208 209 const formData = new FormData() 210 formData.append('file', file) 211 212 uploadMock(formData).then((json) => { 213 console.log('uploadMock', json) 214 if (json.code === 0) { 215 this.model = { 216 name: json.data.name, 217 specContent: json.data.spec, 218 mockContent: json.data.mock, 219 dataContent: json.data.data, 220 dataPath: json.data.dataPath, 221 } 222 223 this.specReady = true 224 225 } else { 226 this.$notification['warning']({ 227 message: this.$i18n.t('upload.spec.failed'), 228 duration: 3, 229 }); 230 } 231 }) 232 233 return false 234 }, 235 handleEditSave() { 236 }, 237 } 238 } 239 </script> 240 241 <style lang="less" scoped> 242 .mock-edit-main { 243 244 .buttons { 245 position: absolute; 246 top: 6px; 247 right: 6px; 248 padding: 5px; 249 } 250 251 } 252 253 </style> 254 255 <style lang="less"> 256 .ant-modal-content { 257 overflow: hidden; 258 259 .ant-modal-body { 260 height: calc(~ "100% - 55px"); 261 262 .mock-edit-main { 263 height: 100%; 264 265 .content-row { 266 height: 100%; 267 268 .content-col { 269 height: 100%; 270 .yaml-editor { 271 height: 100%; 272 } 273 274 pre { 275 padding: 10px; 276 height: 100%; 277 } 278 279 .upload-bar { 280 padding: 10px; 281 282 .title { 283 display: inline-block; 284 padding: 3px 16px; 285 font-size: larger; 286 font-weight: bolder; 287 } 288 289 .label-path:before { 290 display: inline-block; 291 margin-right: 4px; 292 color: #f5222d; 293 font-size: 14px; 294 font-family: SimSun, sans-serif; 295 line-height: 1; 296 content: "*"; 297 } 298 299 input { 300 width: calc(~ "100% - 20px"); 301 } 302 303 } 304 305 .upload-content { 306 height: calc(~ "100% - 50px"); 307 } 308 309 .ant-tabs { 310 height: 100%; 311 312 .ant-tabs-bar { 313 margin-bottom: 10px; 314 } 315 316 .ant-tabs-content { 317 height: calc(~ "100% - 40px"); 318 overflow-y: auto; 319 320 .ant-tabs-tabpane-active { 321 height: calc(~ "100% - 10px"); 322 } 323 324 } 325 } 326 } 327 } 328 } 329 } 330 } 331 </style> 332 333 <style lang="less"> 334 .CodeMirror pre.CodeMirror-line { 335 padding: 3px !important; 336 } 337 </style>