addPage.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <template>
  2. <div class="page-container iotHardware-addPage">
  3. <div class="page-top-title-box">
  4. <p class="page-top-title-name-p">{{newData.id?'编辑':'新增'}}</p>
  5. <p class="page-top-title-out-p" @click="backPage">返回</p>
  6. <p class="page-top-title-submit-p" @click="submitForm">提交</p>
  7. </div>
  8. <div class="content-box">
  9. <el-form class="add-form-box" :model="newData" ref="form" :rules="rules" label-width="200px">
  10. <div class="left-max-box">
  11. <p class="left-title-p">基础信息</p>
  12. <div class="left-big-box scrollbar-box">
  13. <el-form-item label="名称:" prop="deviceName">
  14. <el-input v-model="newData.deviceName" placeholder="请输入名称" maxLength="20" style="width:500px;"></el-input>
  15. </el-form-item>
  16. <el-form-item label="编号:" prop="deviceNo">
  17. <el-input v-model="newData.deviceNo" placeholder="请输入名称" maxLength="20" style="width:500px;"></el-input>
  18. </el-form-item>
  19. <el-form-item label="设备类型:" prop="typeId">
  20. <el-select v-model="newData.typeId " @change="selectChange" placeholder="请选择类型" style="width: 500px">
  21. <el-option
  22. v-for="dict in options"
  23. :key="dict.typeId"
  24. :label="dict.typeName"
  25. :value="dict.typeId"/>
  26. </el-select>
  27. </el-form-item>
  28. <el-form-item label="设备属性:" prop="attributeId">
  29. <el-select v-model="newData.attributeId " @change="selectChangeStats" placeholder="请选择类型" style="width: 500px">
  30. <el-option
  31. v-for="dict in optionsStats"
  32. :key="dict.id"
  33. :label="dict.name"
  34. :value="dict.id"/>
  35. </el-select>
  36. </el-form-item>
  37. <el-form-item label="状态:" prop="state">
  38. <el-radio-group v-model="newData.state">
  39. <el-radio :label="true">启用</el-radio>
  40. <el-radio :label="false">禁用</el-radio>
  41. </el-radio-group>
  42. </el-form-item>
  43. <el-form-item label="楼栋楼层:" prop="address">
  44. <el-cascader
  45. style="width:500px;"
  46. v-model="newData.address"
  47. @change="cascaderChange"
  48. :props="{value: 'id', label: 'name',children:'buildFloorVoList'}"
  49. :options="treeOptions" :show-all-levels="false"></el-cascader>
  50. </el-form-item>
  51. <el-form-item label="实验室:" prop="subId">
  52. <el-select v-model="newData.subId" placeholder="请选择类型" style="width: 500px">
  53. <el-option v-for="item in subOptions" :key="item.subId" :label="item.subName" :value="item.subId"/>
  54. </el-select>
  55. </el-form-item>
  56. <el-form-item label="说明:" prop="remark">
  57. <el-input style="width:500px;" v-model="newData.remark" placeholder="请输入说明"
  58. maxLength='50' type="textarea" resize="none" show-word-limit :autosize="{ minRows: 2, maxRows: 4}">
  59. </el-input>
  60. </el-form-item>
  61. </div>
  62. </div>
  63. <div class="right-max-box">
  64. <p class="right-title-p">{{forPropsData.modelList[0]?'配置信息':''}}</p>
  65. <div class="right-big-box scrollbar-box">
  66. <forElFormItem ref="forElFormItem" :forPropsData="forPropsData"></forElFormItem>
  67. </div>
  68. </div>
  69. </el-form>
  70. </div>
  71. </div>
  72. </template>
  73. <script>
  74. import { systemBuildingGetTreeList,laboratorySubRelInfoGetListByFloor } from "@/api/commonality/permission";
  75. import { iotTypeGetAllTypes,iotTypeGetParamByTypeId,iotAttributeGetByTypeId,iotDeviceAdd,iotDeviceUpdate,iotDeviceDelete } from "@/api/iotDevice/index";
  76. import forElFormItem from "./forElFormItem.vue";
  77. export default {
  78. name: 'addPage',
  79. props:{
  80. propsData:{},
  81. },
  82. components: {
  83. forElFormItem
  84. },
  85. data(){
  86. return{
  87. options:[],
  88. optionsStats:[],
  89. subOptions:[],
  90. treeOptions:[],
  91. newData:{},
  92. rules:{
  93. deviceName: [
  94. { required: true, message: "请输入名称", trigger: "blur" },
  95. { required: true, message: "请输入名称", validator: this.spaceJudgment, trigger: "blur" }
  96. ],
  97. deviceNo: [
  98. { required: true, message: "请输入编号", trigger: "blur" },
  99. { required: true, message: "请输入编号", validator: this.spaceJudgment, trigger: "blur" }
  100. ],
  101. typeId: [
  102. { required: true, message: "请选择设备类型", trigger: "blur" },
  103. ],
  104. attributeId: [
  105. { required: true, message: "请选择设备属性", trigger: "blur" },
  106. ],
  107. state: [
  108. { required: true, message: "请选择状态", trigger: "blur" },
  109. ],
  110. address: [
  111. { required: true, message: "请选择楼栋楼层", trigger: "blur" },
  112. ],
  113. },
  114. noRules:{},
  115. modelList:[],
  116. forPropsData:{
  117. formData:{},
  118. rules:{},
  119. modelList:[]
  120. },
  121. }
  122. },
  123. created(){
  124. this.initialize();
  125. },
  126. mounted(){
  127. },
  128. methods:{
  129. //楼栋楼层选中
  130. cascaderChange(val){
  131. laboratorySubRelInfoGetListByFloor({floorId:val[val.length-1]}).then(response => {
  132. this.$set(this.newData,'subId','');
  133. this.$set(this,'subOptions',response.data);
  134. })
  135. },
  136. //设备属性选中
  137. selectChangeStats(val){
  138. let self = this;
  139. for(let i=0;i<self.optionsStats.length;i++){
  140. if(val == self.optionsStats[i].id){
  141. self.$set(self.newData,'code',self.optionsStats[i].code);
  142. }
  143. }
  144. },
  145. //设备类型选中
  146. selectChange(val){
  147. let self = this;
  148. for(let i=0;i<self.options.length;i++){
  149. if(val == self.options[i].typeId){
  150. self.$set(self.newData,'typeKey',self.options[i].typeKey);
  151. self.$set(self.newData,'typeName',self.options[i].typeName);
  152. }
  153. }
  154. this.$set(this.newData,'attributeId','');
  155. this.$set(this.newData,'code','');
  156. iotTypeGetParamByTypeId({typeId:val}).then(response => {
  157. //处理返回数据
  158. let formData = {};
  159. let rules = {};
  160. let modelList = [];
  161. for(let i=0;i<response.data.length;i++){
  162. //form数据
  163. if(response.data[i].type == 'checkbox' || response.data[i].type == 'datePickerRange'){
  164. formData[response.data[i].field] = [];
  165. }else if(response.data[i].type == 'timePickerRange'){
  166. formData[response.data[i].field] = null;
  167. }else if(response.data[i].type == 'number' || response.data[i].type == 'slider'){
  168. formData[response.data[i].field] = 1
  169. }else{
  170. formData[response.data[i].field] = '';
  171. }
  172. //验证数据处理
  173. if (response.data[i].required){
  174. if(response.data[i].type == 'text' || response.data[i].type == 'textarea' || response.data[i].type == 'password'){
  175. rules[response.data[i].field] = [
  176. { required: true, message: "请输入" + response.data[i].label, trigger: "blur" },
  177. { required: true, message: "请输入" + response.data[i].label, validator: this.spaceJudgment, trigger: "blur" }
  178. ];
  179. }else {
  180. rules[response.data[i].field] = [
  181. { required: true, message: "请选择" + response.data[i].label, trigger: "blur" },
  182. ];
  183. }
  184. }
  185. //model数据处理
  186. let obj = {
  187. label:response.data[i].label,
  188. field:response.data[i].field,
  189. modelType:response.data[i].type,
  190. required: response.data[i].required,
  191. models:response.data[i].iotParamValueModels
  192. }
  193. modelList.push(obj)
  194. }
  195. this.$set(this,'forPropsData',{formData:formData,rules:rules,modelList:modelList});
  196. if(this.$refs.forElFormItem.$refs["form"]){
  197. this.$refs.forElFormItem.$refs["form"].resetFields();
  198. }
  199. });
  200. iotAttributeGetByTypeId({typeId:val}).then(response => {
  201. this.$set(this,'optionsStats',response.data);
  202. });
  203. },
  204. //初始化
  205. initialize(){
  206. //设备类型列表
  207. iotTypeGetAllTypes({}).then(response => {
  208. this.$set(this,'options',response.data);
  209. });
  210. //楼栋楼层
  211. systemBuildingGetTreeList({}).then(response => {
  212. let list = JSON.parse(JSON.stringify(response.data));
  213. pushNode(list)
  214. function pushNode(list){
  215. for(let i=0;i<list.length;i++){
  216. if(list[i].type != 3 && !list[i].buildFloorVoList[0]){
  217. list.splice(i,1)
  218. i--
  219. }else if(list[i].type == 3){
  220. delete list[i].buildFloorVoList;
  221. }else{
  222. pushNode(list[i].buildFloorVoList)
  223. }
  224. }
  225. }
  226. this.$set(this,'treeOptions',list);
  227. })
  228. //编辑
  229. if(this.propsData.id){
  230. this.$set(this,'newData',{
  231. deviceName:this.propsData.deviceName,
  232. deviceNo:this.propsData.deviceNo,
  233. typeId:this.propsData.typeId,
  234. typeKey:this.propsData.typeKey,
  235. typeName:this.propsData.typeName,
  236. attributeId:this.propsData.attributeId,
  237. code:this.propsData.code,
  238. state:this.propsData.state,
  239. address:[this.propsData.schoolId,this.propsData.buildId,this.propsData.floorId],
  240. subId:this.propsData.subjectId,
  241. remark:this.propsData.remark,
  242. });
  243. let minData = this.propsData.reservedField?JSON.parse(this.propsData.reservedField):{};
  244. laboratorySubRelInfoGetListByFloor({floorId:this.propsData.floorId}).then(response => {
  245. this.$set(this,'subOptions',response.data);
  246. })
  247. iotTypeGetParamByTypeId({typeId:this.propsData.typeId}).then(response => {
  248. //处理返回数据
  249. let formData = {};
  250. let rules = {};
  251. let modelList = [];
  252. for(let i=0;i<response.data.length;i++){
  253. //form数据
  254. formData[response.data[i].field] = minData[response.data[i].field];
  255. //验证数据处理
  256. if (response.data[i].required){
  257. if(response.data[i].type == 'text' || response.data[i].type == 'textarea' || response.data[i].type == 'password'){
  258. rules[response.data[i].field] = [
  259. { required: true, message: "请输入" + response.data[i].label, trigger: "blur" },
  260. { required: true, message: "请输入" + response.data[i].label, validator: this.spaceJudgment, trigger: "blur" }
  261. ];
  262. }else {
  263. rules[response.data[i].field] = [
  264. { required: true, message: "请选择" + response.data[i].label, trigger: "blur" },
  265. ];
  266. }
  267. }
  268. //model数据处理
  269. let obj = {
  270. label:response.data[i].label,
  271. field:response.data[i].field,
  272. modelType:response.data[i].type,
  273. required: response.data[i].required,
  274. models:response.data[i].iotParamValueModels
  275. }
  276. modelList.push(obj)
  277. }
  278. this.$set(this,'forPropsData',{formData:formData,rules:rules,modelList:modelList});
  279. if(this.$refs.forElFormItem.$refs["form"]){
  280. this.$refs.forElFormItem.$refs["form"].resetFields();
  281. }
  282. })
  283. iotAttributeGetByTypeId({typeId:this.propsData.typeId}).then(response => {
  284. this.$set(this,'optionsStats',response.data);
  285. });
  286. }else{
  287. this.$set(this,'newData',{
  288. deviceName:'',
  289. deviceNo:'',
  290. typeId:'',
  291. typeKey:'',
  292. typeName:'',
  293. attributeId:'',
  294. code:'',
  295. state:true,
  296. address:[],
  297. subId:'',
  298. remark:'',
  299. });
  300. }
  301. },
  302. // 返回按钮
  303. backPage(){
  304. this.$parent.tableButton(6);
  305. },
  306. /** 提交按钮 */
  307. submitForm() {
  308. this.$refs["form"].validate(maxValid => {
  309. if (maxValid) {
  310. if(this.forPropsData.modelList[0]){
  311. this.$refs.forElFormItem.$refs["form"].validate(valid => {
  312. if (valid) {
  313. this.dataProcessing('1');
  314. }
  315. })
  316. }else{
  317. this.dataProcessing('2');
  318. }
  319. }
  320. })
  321. },
  322. //处理数据并提交
  323. dataProcessing(type){
  324. let self = this;
  325. let obj = {
  326. deviceName:this.newData.deviceName,
  327. deviceNo:this.newData.deviceNo,
  328. typeId:this.newData.typeId,
  329. typeKey:this.newData.typeKey,
  330. typeName:this.newData.typeName,
  331. attributeId:this.newData.attributeId,
  332. code:this.newData.code,
  333. state:this.newData.state,
  334. remark:this.newData.remark,
  335. reservedField:type==1?JSON.stringify(this.$refs.forElFormItem.formData):'',
  336. // reservedField:type==1 ? (this.$refs.forElFormItem.formData?JSON.stringify(this.$refs.forElFormItem.formData):''):'',
  337. };
  338. //获取校区-楼栋-楼层数据
  339. for(let i=0;i<self.treeOptions.length;i++){
  340. if(self.treeOptions[i].id == self.newData.address[0]){
  341. for(let o=0;o<self.treeOptions[i].buildFloorVoList.length;o++){
  342. if(self.treeOptions[i].buildFloorVoList[o].id == self.newData.address[1]){
  343. for(let x=0;x<self.treeOptions[i].buildFloorVoList[o].buildFloorVoList.length;x++){
  344. if(self.treeOptions[i].buildFloorVoList[o].buildFloorVoList[x].id == self.newData.address[2]){
  345. obj.schoolId = self.treeOptions[i].id;
  346. obj.schoolName = self.treeOptions[i].name;
  347. obj.buildId = self.treeOptions[i].buildFloorVoList[o].id;
  348. obj.buildName = self.treeOptions[i].buildFloorVoList[o].name;
  349. obj.floorId = self.treeOptions[i].buildFloorVoList[o].buildFloorVoList[x].id;
  350. obj.floorName = self.treeOptions[i].buildFloorVoList[o].buildFloorVoList[x].name;
  351. }
  352. }
  353. }
  354. }
  355. }
  356. }
  357. //获取实验室数据
  358. if(this.newData.subId){
  359. for(let i=0;i<self.subOptions.length;i++){
  360. if(self.subOptions[i].subId == self.newData.subId){
  361. obj.subjectId = self.subOptions[i].subId
  362. obj.subjectName = self.subOptions[i].subName
  363. obj.room = self.subOptions[i].roomNum
  364. }
  365. }
  366. }else{
  367. obj.subjectId = ''
  368. obj.subjectName = ''
  369. obj.room = ''
  370. }
  371. if(this.propsData.id){
  372. // 编辑
  373. obj.id = this.propsData.id;
  374. iotDeviceUpdate(obj).then(response => {
  375. this.msgSuccess(response.message)
  376. this.backPage();
  377. });
  378. }else{
  379. // 新增
  380. iotDeviceAdd(obj).then(response => {
  381. this.msgSuccess(response.message)
  382. this.backPage();
  383. });
  384. }
  385. },
  386. },
  387. }
  388. </script>
  389. <style scoped lang="scss">
  390. .iotHardware-addPage{
  391. .content-box{
  392. flex:1;
  393. display: flex;
  394. overflow: hidden;
  395. .add-form-box{
  396. flex:1;
  397. display: flex;
  398. overflow: hidden;
  399. .left-max-box{
  400. flex:1;
  401. display: flex;
  402. flex-direction: column;
  403. .left-title-p{
  404. line-height:40px;
  405. padding-left:40px;
  406. margin-top:10px;
  407. }
  408. .left-big-box{
  409. flex:1;
  410. }
  411. }
  412. .right-max-box{
  413. flex:1;
  414. display: flex;
  415. flex-direction: column;
  416. .right-title-p{
  417. line-height:40px;
  418. padding-left:40px;
  419. margin-top:10px;
  420. }
  421. .right-big-box{
  422. flex:1;
  423. }
  424. }
  425. }
  426. }
  427. }
  428. </style>