bind.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <template>
  2. <div class="app-container">
  3. <el-form ref="form" label-width="110px" :model="formData" :rules="rules">
  4. <el-form-item label="认证方式:">关联已认证设备</el-form-item>
  5. <el-form-item label="SN码列表:" prop="snList">
  6. <el-checkbox-group v-model="formData.snList" />
  7. <el-button size="mini" type="primary" @click="onChooseCode">选择SN码</el-button>
  8. </el-form-item>
  9. <el-form-item>
  10. <sncode-list class="snCode-list" :selection="false" :control="true" :list="selectedCodeList">
  11. <template #control="{ row }">
  12. <el-button size="mini" type="danger" @click="onSelectCodeRemove(row)">删除</el-button>
  13. </template>
  14. </sncode-list>
  15. </el-form-item>
  16. </el-form>
  17. <!-- 表单提交 返回 -->
  18. <div class="control-box">
  19. <el-button type="primary" @click="submit">保存</el-button>
  20. <el-button type="warning" @click="navigateBack">返回</el-button>
  21. </div>
  22. <!-- 选择sn码 -->
  23. <el-dialog title="选择关联设备" :visible.sync="dialogCodeListVisible" width="70%" :show-close="false">
  24. <sncode-list
  25. :selection="true"
  26. :control="false"
  27. :list="unselectedCodeList"
  28. height="380"
  29. @selected="onCodeSelected"
  30. />
  31. <div slot="footer" class="dialog-footer">
  32. <el-button type="primary" size="mini" @click="onCodeCancel">取 消</el-button>
  33. <el-button type="primary" size="mini" @click="onCodeConfirm">确 定</el-button>
  34. </div>
  35. </el-dialog>
  36. </div>
  37. </template>
  38. <script>
  39. import { SncodeList } from '@/views/components'
  40. import { fetchClubDeviceSnList, saveProductRelation } from '@/api/product'
  41. import { getStorage } from '@/utils/storage'
  42. import { uniqueArr } from '@/utils'
  43. export default {
  44. components: {
  45. SncodeList
  46. },
  47. data() {
  48. return {
  49. dialogCodeListVisible: false,
  50. formData: {
  51. authType: 2,
  52. authId: '',
  53. snList: []
  54. },
  55. rules: {
  56. snList: [{ type: 'array', required: true, message: 'SN码列表不能为空', trigger: ['change'] }]
  57. },
  58. listQuery: {
  59. authId: '',
  60. downStatus: 0,
  61. productName: '',
  62. snCode: '',
  63. authParty: ''
  64. },
  65. codeList: [], // sn码列表
  66. preSelectedCodeList: [] // table中已选中的sn码列表
  67. }
  68. },
  69. computed: {
  70. // 选中的sn码列表
  71. selectedCodeList() {
  72. return this.codeList.filter((item) => this.formData.snList.includes(item.snCode))
  73. },
  74. // 剩余未选中的sn码列表
  75. unselectedCodeList() {
  76. return this.codeList.filter((item) => !this.formData.snList.includes(item.snCode))
  77. }
  78. },
  79. watch: {
  80. selectedCodeListAll(nval) {
  81. this.formData.snList = nval
  82. }
  83. },
  84. created() {
  85. // 获取当前设备所属机构的id
  86. this.formData.authId = getStorage('device-setting-authId')
  87. this.listQuery.authId = this.formData.authId
  88. this.fetchSnCodeList()
  89. },
  90. methods: {
  91. // 选择sn码
  92. onChooseCode() {
  93. this.dialogCodeListVisible = true
  94. },
  95. // 获取全部sn码列表
  96. async fetchSnCodeList() {
  97. try {
  98. // 获取全部sn码列表
  99. this.listQuery.downStatus = 0
  100. const res1 = await fetchClubDeviceSnList(this.listQuery)
  101. res1.data = res1.data.map((item) => {
  102. item.downStatus = 0
  103. return item
  104. })
  105. // 获取与该机构有关联sn码列表
  106. this.listQuery.downStatus = 1
  107. const res2 = await fetchClubDeviceSnList(this.listQuery)
  108. res2.data = res2.data.map((item) => {
  109. item.downStatus = 0
  110. return item
  111. })
  112. this.codeList = [...res1.data, ...res2.data]
  113. console.log('🚀 ~ file: bind.vue:108 ~ fetchSnCodeList ~ this.codeList:', this.codeList)
  114. } catch (error) {
  115. console.log(error)
  116. }
  117. },
  118. // 保存
  119. async onSave() {
  120. try {
  121. await saveProductRelation(this.formData)
  122. this.$message.success('关联已认证设备成功')
  123. this.$store.dispatch('tagsView/delView', this.$route)
  124. this.$router.push(`/club/device-list?id=${this.formData.authId}`)
  125. } catch (error) {
  126. console.log(error)
  127. }
  128. },
  129. // 提交表单
  130. async submit() {
  131. try {
  132. await this.$refs.form.validate()
  133. this.onSave()
  134. } catch (error) {
  135. console.log(error)
  136. }
  137. },
  138. // 选中sn码
  139. onCodeSelected(list) {
  140. this.preSelectedCodeList = list
  141. },
  142. // 取消选择sn码
  143. onCodeCancel() {
  144. this.preSelectedCodeList = []
  145. this.dialogCodeListVisible = false
  146. },
  147. // 确认选择sn码
  148. onCodeConfirm() {
  149. const sncodeList = this.preSelectedCodeList.map((item) => item.snCode)
  150. const snList = [...this.formData.snList, ...sncodeList]
  151. this.formData.snList = uniqueArr(snList)
  152. this.dialogCodeListVisible = false
  153. },
  154. // 已选中sn码删除
  155. onSelectCodeRemove(row) {
  156. this.formData.snList = this.formData.snList.filter((code) => code !== row.snCode)
  157. }
  158. }
  159. }
  160. </script>
  161. <style lang="scss" scoped>
  162. .app-container {
  163. width: 80%;
  164. }
  165. .filter-container {
  166. background: #eee;
  167. padding: 6px 10px;
  168. .filter-control {
  169. margin-bottom: 0;
  170. }
  171. }
  172. .snCode-cate {
  173. display: flex;
  174. align-items: center;
  175. margin-bottom: 16px;
  176. }
  177. </style>