form-club-device.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. <template>
  2. <div class="club-device">
  3. <template v-for="formItem in formList">
  4. <div :key="formItem.uid">
  5. <el-form :model="formItem" :rules="rules" ref="form">
  6. <el-form-item prop="productName" :label="`设备名称${formItem.uuid}:`">
  7. <el-select
  8. v-model="formItem.productName"
  9. filterable
  10. allow-create
  11. placeholder="请输入新设备名称或选择已有设备"
  12. @change="onProductNameChange(formItem, $event)"
  13. >
  14. <el-option
  15. v-for="item in deviceList"
  16. :key="item.productTypeId"
  17. :label="item.name"
  18. :value="item.productTypeId"
  19. />
  20. </el-select>
  21. </el-form-item>
  22. <el-form-item prop="productImage" label="设备图片:">
  23. <br />
  24. <el-input v-show="false" v-model="formItem.productImage"></el-input>
  25. <SimpleUploadImage
  26. :disabled="Boolean(formItem.productTypeId)"
  27. :limit="1"
  28. :image-list="formItem.productImageList"
  29. :before-upload="beforeProductImageUpload"
  30. @success="uploadProductImageSuccess(formItem, $event)"
  31. @remove="handleProductImageRemove(formItem, $event)"
  32. />
  33. </el-form-item>
  34. <el-form-item label="所属品牌:" prop="brandId">
  35. <el-select v-model="formItem.brandId" placeholder="请选择品牌">
  36. <el-option
  37. v-for="item in brandList"
  38. :key="item.id"
  39. :label="item.name"
  40. :value="item.id"
  41. />
  42. </el-select>
  43. </el-form-item>
  44. <el-form-item prop="purchaseWay" label="购买渠道:">
  45. <el-input
  46. placeholder="请输入购买渠道"
  47. v-model="formItem.purchaseWay"
  48. ></el-input>
  49. </el-form-item>
  50. <el-form-item prop="invoiceImage" label="发票:">
  51. <br />
  52. <el-input v-show="false" v-model="formItem.invoiceImage"></el-input>
  53. <SimpleUploadImage
  54. :limit="1"
  55. :image-list="formItem.invoiceImageList"
  56. :before-upload="beforeInvoiceImageUpload"
  57. @success="uploadInvoiceImageSuccess(formItem, $event)"
  58. @remove="handleInvoiceImageRemove(formItem, $event)"
  59. />
  60. </el-form-item>
  61. <el-form-item prop="snCode" label="设备SN码:">
  62. <el-input
  63. placeholder="请输入设备SN码"
  64. v-model="formItem.snCode"
  65. ></el-input>
  66. </el-form-item>
  67. <el-form-item prop="paramList" label="设备参数:">
  68. <br />
  69. <div class="device-param-list">
  70. <span class="add-param" @click="insertParam(formItem)"
  71. >添加参数</span
  72. >
  73. <template v-for="(param, index) in formItem.paramList">
  74. <div :key="index">
  75. <div class="param flex justify-between mb-4">
  76. <el-input
  77. style="width: 40%"
  78. placeholder="例如:品牌"
  79. class="mr-2"
  80. v-model="param.paramName"
  81. ></el-input>
  82. <el-input
  83. placeholder="请输入参数信息"
  84. v-model="param.paramContent"
  85. ></el-input>
  86. <span
  87. class="remove el-icon-close"
  88. @click="removeParam(formItem, index)"
  89. v-if="formItem.paramList.length > 4"
  90. ></span>
  91. </div>
  92. </div>
  93. </template>
  94. </div>
  95. </el-form-item>
  96. </el-form>
  97. <el-divider></el-divider>
  98. </div>
  99. </template>
  100. <div class="add-device" @click="insertOne" v-if="formType != 'edit'">
  101. <div class="add-icon"></div>
  102. 添加设备
  103. </div>
  104. <SimpleDialog
  105. v-if="formType != 'edit'"
  106. v-model="active"
  107. @confirm="active = false"
  108. :cancel="false"
  109. description="请慎重填写机构信息,认证通过后将无法更改!"
  110. :center="true"
  111. />
  112. </div>
  113. </template>
  114. <script>
  115. import SimpleUploadImage from '@/components/SimpleUploadImage'
  116. import { mapGetters } from 'vuex'
  117. export default {
  118. components: {
  119. SimpleUploadImage,
  120. },
  121. props:{
  122. formType:{
  123. type:String,
  124. default:1
  125. }
  126. },
  127. data() {
  128. const productNameValidate = (rule, value, callback) => {
  129. if (value.toString().length > 50) {
  130. callback(new Error('设备名称长度需要在50个字符内'))
  131. } else {
  132. callback()
  133. }
  134. }
  135. return {
  136. active: true,
  137. uuid: 0, // 表单id
  138. productImageList: [],
  139. rules: {
  140. productName: [
  141. { required: true, message: '设备名称不能为空', trigger: ['change'] },
  142. { validator: productNameValidate, trigger: ['change'] },
  143. ],
  144. productImage: [
  145. { required: true, message: '设备图片不能为空', trigger: ['change'] },
  146. ],
  147. brandId: [
  148. { required: true, message: '所属品牌不能为空', trigger: ['change'] },
  149. ],
  150. snCode: [
  151. { required: true, message: '设备SN码不能为空', trigger: ['blur'] },
  152. ],
  153. paramList: [{ required: true, message: '参数不能为空' }],
  154. },
  155. formList: [],
  156. brandList: [],
  157. deviceList: [],
  158. }
  159. },
  160. computed: {
  161. ...mapGetters(['authUserId']),
  162. },
  163. created() {
  164. this.fetchBrandList()
  165. this.fetchDeviceList()
  166. this.initFormList()
  167. this.init()
  168. },
  169. methods: {
  170. // 表单验证
  171. validate() {
  172. this.$emit('step', this.formatFormList())
  173. return Promise.all(this.$refs.form.map((item) => item.validate()))
  174. },
  175. async init(formData) {
  176. console.log('formData',formData)
  177. const obj = {}
  178. const productImageList = {
  179. name:'',
  180. url:formData.productImage
  181. }
  182. const invoiceImageList = {
  183. name:'',
  184. url:formData.invoiceImage
  185. }
  186. obj.uuid = ++this.uuid
  187. obj.productImageList = productImageList
  188. obj.invoiceImageList = invoiceImageList
  189. obj.productImage = formData.productImage
  190. obj.productName = formData.productName
  191. obj.snCode = formData.snCode
  192. obj.brandId = formData.brandId
  193. obj.productTypeId = formData.productTypeId
  194. obj.purchaseWay = formData.purchaseWay
  195. obj.invoiceImage = formData.invoiceImage
  196. obj.paramList = formData.paramList
  197. this.formList.push(obj)
  198. console.log('formList',this.formList)
  199. },
  200. formatFormList() {
  201. const list = []
  202. this.formList.forEach((formItem) => {
  203. const obj = {}
  204. obj.productImage = formItem.productImage
  205. obj.productName = formItem.productName
  206. obj.snCode = formItem.snCode
  207. obj.brandId = formItem.brandId
  208. obj.productTypeId = formItem.productTypeId
  209. obj.purchaseWay = formItem.purchaseWay
  210. obj.invoiceImage = formItem.invoiceImage
  211. obj.paramList = formItem.paramList
  212. list.push(obj)
  213. })
  214. return list
  215. },
  216. generateFormData() {
  217. return {
  218. uuid: ++this.uuid,
  219. authUserId: '',
  220. authId: '', // 授权id
  221. createBy: '', // 创建人id
  222. // 设备参数列表
  223. paramList: this.initParams(),
  224. productId: '', // 授权设备id
  225. productImage: '', // 设备图片
  226. productName: '', // 设备名称
  227. snCode: '', // 设备SN码
  228. brandId: '',
  229. productTypeId: '',
  230. purchaseWay: '', // 购买渠道
  231. invoiceImage: '', // 发票
  232. productImageList: [],
  233. invoiceImageList: [],
  234. }
  235. },
  236. generageProductParam() {
  237. return {
  238. paramContent: '',
  239. paramName: '',
  240. }
  241. },
  242. initParams() {
  243. const list = []
  244. for (let i = 0; i < 4; i++) {
  245. list.push(this.generageProductParam())
  246. }
  247. return list
  248. },
  249. insertParam(formItem) {
  250. formItem.paramList.push(this.generateFormData())
  251. },
  252. removeParam(formItem, index) {
  253. formItem.paramList.splice(index, 1)
  254. },
  255. initFormList() {
  256. this.formList.push(this.generateFormData())
  257. console.log(this.formList)
  258. },
  259. insertOne() {
  260. this.formList.push(this.generateFormData())
  261. },
  262. removeOne() {},
  263. onProductNameChange(formItem, value) {
  264. if (typeof value === 'number') {
  265. formItem.productTypeId = value
  266. const deviceInfo = this.deviceList.find(
  267. (item) => item.productTypeId === value
  268. )
  269. formItem.productImage = deviceInfo.image
  270. formItem.productImageList = [{ name: '', url: deviceInfo.image }]
  271. } else {
  272. formItem.productTypeId = ''
  273. formItem.productImage = ''
  274. formItem.productImageList = []
  275. }
  276. },
  277. // 获取品牌列表
  278. async fetchBrandList() {
  279. try {
  280. const res = await this.$http.api.fetchBrandList({
  281. type: 3,
  282. authUserId: this.authUserId,
  283. })
  284. this.brandList = res.data
  285. } catch (error) {
  286. console.log(error)
  287. }
  288. },
  289. // 获取设备列表
  290. async fetchDeviceList() {
  291. try {
  292. const res = await this.$http.api.fetchProductSelectList({
  293. authUserId: this.authUserId,
  294. })
  295. this.deviceList = res.data
  296. } catch (error) {
  297. console.log(error)
  298. }
  299. },
  300. // 产品图片上传
  301. beforeProductImageUpload(file) {
  302. const flag = file.size / 1024 / 1024 < 1
  303. if (!flag) {
  304. this.$message.error('上传产品图片大小不能超过 1MB!')
  305. }
  306. return flag
  307. },
  308. uploadProductImageSuccess(formItem, { response, file, fileList }) {
  309. formItem.productImageList = fileList
  310. formItem.productImage = response.data
  311. },
  312. handleProductImageRemove(formItem, { file, fileList }) {
  313. formItem.productImageList = fileList
  314. formItem.productImage = ''
  315. },
  316. // 发票上传
  317. beforeInvoiceImageUpload(file) {
  318. const flag = file.size / 1024 / 1024 < 1
  319. if (!flag) {
  320. this.$message.error('发票图片大小不能超过 1MB!')
  321. }
  322. return flag
  323. },
  324. uploadInvoiceImageSuccess(formItem, { response, file, fileList }) {
  325. formItem.invoiceImageList = fileList
  326. formItem.invoiceImage = response.data
  327. },
  328. handleInvoiceImageRemove(formItem, { file, fileList }) {
  329. formItem.invoiceImageList = fileList
  330. formItem.invoiceImage = ''
  331. },
  332. },
  333. }
  334. </script>
  335. <style lang="scss" scoped>
  336. // pc端
  337. @media screen and (min-width: 768px) {
  338. .el-select {
  339. width: 100%;
  340. }
  341. .device-param-list {
  342. position: relative;
  343. .add-param {
  344. position: absolute;
  345. cursor: pointer;
  346. top: -40px;
  347. right: 0;
  348. text-decoration: underline;
  349. font-size: 14px;
  350. @include themify($themes) {
  351. color: themed('color');
  352. }
  353. }
  354. .param {
  355. position: relative;
  356. .remove {
  357. position: absolute;
  358. right: 0;
  359. top: 0;
  360. width: 20px;
  361. height: 20px;
  362. background: #f94b4b;
  363. border-radius: 2px;
  364. cursor: pointer;
  365. color: #fff;
  366. font-size: 14px;
  367. text-align: center;
  368. line-height: 20px;
  369. }
  370. }
  371. }
  372. .add-device {
  373. display: flex;
  374. justify-content: center;
  375. align-items: center;
  376. width: 162px;
  377. height: 46px;
  378. border-radius: 4px;
  379. box-sizing: border-box;
  380. font-size: 18px;
  381. margin: 0 auto;
  382. cursor: pointer;
  383. @include themify($themes) {
  384. border: 1px solid themed('color');
  385. color: themed('color');
  386. }
  387. .add-icon {
  388. width: 20px;
  389. height: 20px;
  390. position: relative;
  391. margin-right: 16px;
  392. &::before,
  393. &::after {
  394. position: absolute;
  395. width: 3px;
  396. height: 20px;
  397. left: 50%;
  398. top: 50%;
  399. transform: translate(-50%, -50%);
  400. border-radius: 1px;
  401. content: '';
  402. display: block;
  403. @include themify($themes) {
  404. background: themed('color');
  405. }
  406. }
  407. &::after {
  408. transform: translate(-50%, -50%) rotateZ(90deg);
  409. }
  410. }
  411. }
  412. }
  413. // 移动端
  414. @media screen and (max-width: 768px) {
  415. ::v-deep {
  416. .el-form-item__label {
  417. font-size: 3.4vw;
  418. }
  419. }
  420. .el-select {
  421. width: 100%;
  422. }
  423. .device-param-list {
  424. position: relative;
  425. .add-param {
  426. position: absolute;
  427. cursor: pointer;
  428. top: -40px;
  429. right: 0;
  430. font-size: 3.4vw;
  431. @include themify($themes) {
  432. color: themed('color');
  433. }
  434. }
  435. .param {
  436. position: relative;
  437. .remove {
  438. position: absolute;
  439. right: 0;
  440. top: 0;
  441. width: 4.4vw;
  442. height: 4.4vw;
  443. background: #f94b4b;
  444. border-radius: 0.2vw;
  445. cursor: pointer;
  446. color: #fff;
  447. font-size: 3.4vw;
  448. text-align: center;
  449. line-height: 4.4vw;
  450. }
  451. }
  452. }
  453. .add-device {
  454. display: flex;
  455. justify-content: center;
  456. align-items: center;
  457. width: 31vw;
  458. height: 8.8vw;
  459. border-radius: 0.4vw;
  460. box-sizing: border-box;
  461. font-size: 3.4vw;
  462. margin: 0 auto;
  463. cursor: pointer;
  464. @include themify($themes) {
  465. border: 1px solid themed('color');
  466. color: themed('color');
  467. }
  468. .add-icon {
  469. width: 20px;
  470. height: 20px;
  471. position: relative;
  472. margin-right: 16px;
  473. &::before,
  474. &::after {
  475. position: absolute;
  476. width: 0.6vw;
  477. height: 4.1vw;
  478. left: 50%;
  479. top: 50%;
  480. transform: translate(-50%, -50%);
  481. border-radius: 1px;
  482. content: '';
  483. display: block;
  484. @include themify($themes) {
  485. background: themed('color');
  486. }
  487. }
  488. &::after {
  489. transform: translate(-50%, -50%) rotateZ(90deg);
  490. }
  491. }
  492. }
  493. }
  494. </style>