ModelFormSample.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <!-- 添加采样 -->
  2. <template>
  3. <el-dialog
  4. :title="comonentVar.modelTitle"
  5. :visible.sync="comonentVar.dialogVisible"
  6. :destroy-on-close="true"
  7. :close-on-click-modal="true"
  8. @close="modelOnClose"
  9. >
  10. <div class="model-form">
  11. <!-- label-width 设置了:label 和 input 就不会换行 -->
  12. <el-form ref="modelForm" :model="modelForm" label-width="100px" :rules="rules">
  13. <el-form-item label="名称" prop="name">
  14. <el-input v-model="modelForm.name" autocomplete="off" maxlength="32" />
  15. </el-form-item>
  16. <!-- <el-form-item label="查询id">
  17. <el-input v-model="form.product" />
  18. </el-form-item> -->
  19. <el-form-item class="modelInput" label="寄存器范围" prop="range">
  20. <template slot-scope="scope">
  21. <el-select
  22. :key="toString(scope.row)"
  23. v-model="modelForm.range"
  24. :disabled="isupdata"
  25. clearable
  26. placeholder=""
  27. >
  28. <el-option
  29. v-for="item in checkPointFuctionOptions"
  30. :key="item.id"
  31. :label="item.name"
  32. :value="item.id"
  33. />
  34. </el-select>
  35. </template>
  36. </el-form-item>
  37. <el-form-item label="寄存器地址" prop="offset">
  38. <el-input v-model="modelForm.offset" :disabled="isupdata" type="number" maxlength="32" />
  39. </el-form-item>
  40. <el-form-item class="modelInput" label="值类型" prop="dataType">
  41. <template slot-scope="scope">
  42. <el-select
  43. :key="toString(scope.row)"
  44. v-model="modelForm.dataType"
  45. clearable
  46. placeholder=""
  47. >
  48. <el-option
  49. v-for="item in valueTypeOptions"
  50. :key="item.id"
  51. :label="item.name"
  52. :value="item.id"
  53. />
  54. </el-select>
  55. </template>
  56. </el-form-item>
  57. <el-form-item class="modelInput" label="字内交换">
  58. <el-switch
  59. v-model="modelForm.swapByte"
  60. active-text="交换"
  61. inactive-text="不交换"
  62. />
  63. </el-form-item>
  64. <el-form-item class="modelInput" label="字节交换">
  65. <el-switch
  66. v-model="modelForm.swapWord"
  67. active-text="交换"
  68. inactive-text="不交换"
  69. />
  70. </el-form-item>
  71. <el-form-item label="倍率">
  72. <el-input v-model="modelForm.scaling" maxlength="32" />
  73. </el-form-item>
  74. <el-form-item label="偏移量">
  75. <el-input v-model="modelForm.adjust" maxlength="32" />
  76. </el-form-item>
  77. </el-form>
  78. <div class="buttons">
  79. <div class="cancel">
  80. <el-button class="lightButton" icon="el-icon-circle-close" size="small" @click="cancelForm">取消</el-button>
  81. </div>
  82. <div class="submit">
  83. <el-button
  84. type="primary"
  85. icon="el-icon-circle-check"
  86. class="dark-button"
  87. size="small"
  88. @click="submitForm"
  89. >保存</el-button>
  90. </div>
  91. </div>
  92. </div>
  93. </el-dialog>
  94. </template>
  95. <script>
  96. import { mapGetters } from 'vuex'
  97. import {
  98. httpGet,
  99. putData
  100. } from '@/api/common-action'
  101. export default {
  102. name: 'ModelFormSample',
  103. // model-parames 模态框参数
  104. props: {
  105. modelParames: {
  106. type: Object,
  107. default: function() {
  108. return {}
  109. }
  110. },
  111. sampleData: {
  112. type: Array,
  113. default: function() {
  114. return []
  115. }
  116. }
  117. },
  118. data() {
  119. return {
  120. rules: {
  121. name: [{ required: true, message: '名称不能为空' }],
  122. range: [{ required: true, message: '寄存器范围不能为空' }],
  123. offset: [{ required: true, message: '寄存器地址不能为空' }],
  124. dataType: [{ required: true, message: '值类型不能为空' }]
  125. },
  126. comonentVar: {
  127. modelTitle: '加载中...',
  128. dialogVisible: true
  129. },
  130. // 表单默认数据,选项数组 一定要在这里先定义,否则无法正常获取
  131. modelForm: {
  132. name: ''
  133. },
  134. // 下拉框单独定义 避免错位
  135. valueTypeOptions: [],
  136. checkPointFuctionOptions: [],
  137. isupdata: false
  138. }
  139. },
  140. computed: {
  141. ...mapGetters([
  142. 'roles'
  143. ])
  144. },
  145. created() {
  146. },
  147. mounted() {
  148. // 初始化
  149. this.$nextTick(() => {
  150. this.initFunctions()
  151. // console.log('sampleData', this.sampleData)
  152. })
  153. },
  154. methods: {
  155. // 初始化
  156. async initFunctions() {
  157. // 初始化 值类型 下拉框选项
  158. this.valueTypeOptions = JSON.parse(localStorage.getItem('valueTypeOptions'))
  159. // 因为 this.modelForm 在前面已经赋值过了,所以这里必须使用 set 的方式赋值,否则无法更新选项
  160. // 初始化 功能码 下拉框选项
  161. this.checkPointFuctionOptions = JSON.parse(localStorage.getItem('checkPointFuctionOptions'))
  162. this.comonentVar.modelTitle = `[${this.modelParames.deviceName}]-添加采样`
  163. if (this.modelParames.modelFormData) {
  164. if (this.modelParames.modelFormData.id > 0) {
  165. this.comonentVar.modelTitle = `[${this.modelParames.deviceName}]-编辑采样`
  166. // #TODO: 编辑状态 锁住 寄存器范围和寄存器地址
  167. this.isupdata = true
  168. // 如果 寄存器范围 是 开关量 输出/输入 则 值类型 固定为 开关
  169. // 如果 寄存器范围 是 其它值类型 默认为 16位有符号数
  170. // this.$message({
  171. // message: `编辑功能还未准备好`,
  172. // type: 'error',
  173. // offset: window.screen.height / 3
  174. // })
  175. // 如果是编辑,从 接口 单测点查询 再获取一次数据
  176. // /product/model/:product/:tag
  177. // 例 :product 1100000645
  178. // 例 :tag 6386 tag id 为采样列表序号id
  179. const getUrl = `/product/model/${this.modelParames.productId}/${this.modelParames.modelFormData.iec104}`
  180. httpGet(getUrl).then(res => {
  181. this.modelForm = res
  182. if (res.swapByte === 1 || res.swapByte === true) {
  183. this.modelForm.swapByte = true
  184. } else {
  185. this.modelForm.swapByte = false
  186. }
  187. if (res.swapWord === 1 || res.swapWord === true) {
  188. this.modelForm.swapWord = true
  189. } else {
  190. this.modelForm.swapWord = false
  191. }
  192. })
  193. // this.modelForm = this.modelParames.modelFormData
  194. // console.log('mounted this.modelForm= ', this.modelForm)
  195. }
  196. }
  197. // 添加 采样 数据示例:
  198. // {
  199. // "id": 0,
  200. // "name": "my_tag",
  201. // "dataType": "BOOL",
  202. // "range": "COIL_STATUS",
  203. // "offset": 0,
  204. // "scaling": 1,
  205. // "adjust": 0,
  206. // "swapByte": false,
  207. // "swapWord": false,
  208. // "pollingCycle": 0
  209. // }
  210. // this.$forceUpdate()
  211. // httpGet(getUrl).then(res => {
  212. // delete res.code
  213. // console.log('获取测试规则 res= ', res)
  214. // this.modelForm.ruleOptions = res
  215. // })
  216. },
  217. // 关闭model时执行
  218. modelOnClose() {
  219. this.$emit('toggleModel', 'ModelFormSample', false)
  220. },
  221. // 表单提交按钮
  222. submitForm() {
  223. if (this.isupdata === false) {
  224. // console.log('this.modelForm', this.modelForm)
  225. // console.log('sampleData', this.sampleData)
  226. const hasThis = this.sampleData.some(item => item.range === this.modelForm.range && item.offset === parseInt(this.modelForm.offset))
  227. if (hasThis) {
  228. this.$message({
  229. type: 'error',
  230. message: '该采样值已存在',
  231. offset: window.screen.height / 3
  232. })
  233. return
  234. }
  235. }
  236. let checkForm = false
  237. this.$refs.modelForm.validate(f => { checkForm = f })
  238. if (!checkForm) {
  239. return
  240. }
  241. delete this.modelForm.code
  242. const putUrl = `/test/plan/${this.modelParames.curPlanId}/point`
  243. // const putUrl = `/test/plan/${this.modelForm.id}/point`
  244. putData(putUrl, this.modelForm).then(res => {
  245. // 保存表单
  246. this.comonentVar.dialogVisible = false
  247. })
  248. },
  249. // 表单取消按钮
  250. cancelForm() {
  251. this.modelForm = {}
  252. this.comonentVar.dialogVisible = false
  253. }
  254. }
  255. }
  256. </script>
  257. <style lang="scss">
  258. .steps-setting {
  259. padding: 10px 0;
  260. label {
  261. padding: 0;
  262. }
  263. }
  264. </style>
  265. <style lang="scss" scoped>
  266. // 表单按钮样式
  267. .buttons {
  268. display: flex;
  269. justify-content: right;
  270. padding-bottom: 20px;
  271. .submit {
  272. padding-left: 20px;
  273. }
  274. }
  275. .model-form {
  276. .setting-input {
  277. width: 100px;
  278. }
  279. }
  280. // 按钮公用样式
  281. .dark-button {
  282. background-color: #00706B;
  283. border: 1px #00706B solid;
  284. color: #fff;
  285. }
  286. .light-button {
  287. color: #00706B;
  288. border: 1px #00706B solid;
  289. }
  290. </style>