form-club-device.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. <template>
  2. <div class="club-device">
  3. <template v-for="(formItem, index) in formList">
  4. <div :key="formItem.uid" class="device-section">
  5. <span
  6. class="remove-btn"
  7. @click="removeOne(formItem)"
  8. v-if="formList.length > 1"
  9. >删除这台设备</span
  10. >
  11. <el-form :model="formItem" :rules="rules" ref="form">
  12. <el-form-item label="认证方式:">
  13. <el-radio-group
  14. v-model="formItem.authType"
  15. @change="onAuthTypeChange(formItem, index)"
  16. >
  17. <el-radio :label="1">新设备认证</el-radio>
  18. <el-radio :label="2">关联已认证设备</el-radio>
  19. </el-radio-group>
  20. </el-form-item>
  21. <template
  22. v-if="
  23. formItem.authType === 1 ||
  24. (formItem.authType === 2 && formItem.snCode)
  25. "
  26. >
  27. <el-form-item
  28. prop="productName"
  29. :label="`设备名称${formItem.uuid}:`"
  30. >
  31. <el-select
  32. v-model="formItem.productName"
  33. filterable
  34. allow-create
  35. placeholder="请输入新设备名称或选择已有设备"
  36. @change="onProductNameChange(formItem, $event)"
  37. clearable
  38. :disabled="formItem.authType === 2"
  39. >
  40. <el-option
  41. v-for="item in deviceList"
  42. :key="item.productTypeId"
  43. :label="item.name"
  44. :value="item.productTypeId"
  45. />
  46. </el-select>
  47. </el-form-item>
  48. <el-form-item prop="productImage" label="设备图片:">
  49. <br />
  50. <el-input
  51. v-show="false"
  52. v-model="formItem.productImage"
  53. ></el-input>
  54. <SimpleUploadImage
  55. :disabled="Boolean(formItem.productTypeId)"
  56. :limit="1"
  57. :image-list="formItem.productImageList"
  58. :before-upload="beforeProductImageUpload"
  59. @success="uploadProductImageSuccess(formItem, $event)"
  60. @remove="handleProductImageRemove(formItem, $event)"
  61. />
  62. </el-form-item>
  63. <el-form-item label="所属品牌:" prop="infoId" v-if="editPrams">
  64. <el-select
  65. v-model="formItem.infoId"
  66. placeholder="请选择品牌"
  67. :disabled="formItem.authType === 2"
  68. >
  69. <el-option
  70. v-for="item in brandList"
  71. :key="item.infoId"
  72. :label="item.brandName"
  73. :value="item.infoId"
  74. />
  75. </el-select>
  76. </el-form-item>
  77. <el-form-item prop="purchaseWay" label="购买渠道:">
  78. <el-input
  79. placeholder="请输入购买渠道"
  80. v-model="formItem.purchaseWay"
  81. :disabled="formItem.authType === 2"
  82. ></el-input>
  83. </el-form-item>
  84. <el-form-item prop="invoiceImage" label="发票:">
  85. <br />
  86. <el-input
  87. v-show="false"
  88. v-model="formItem.invoiceImage"
  89. ></el-input>
  90. <SimpleUploadImage
  91. :disabled="formItem.authType === 2"
  92. :limit="1"
  93. :image-list="formItem.invoiceImageList"
  94. :before-upload="beforeInvoiceImageUpload"
  95. @success="uploadInvoiceImageSuccess(formItem, $event)"
  96. @remove="handleInvoiceImageRemove(formItem, $event)"
  97. />
  98. </el-form-item>
  99. </template>
  100. <el-form-item prop="snCode" :label="`设备SN码${formItem.uuid}:`">
  101. <el-input
  102. :placeholder="
  103. formItem.authType === 1
  104. ? '请输入设备SN码'
  105. : '请输入已认证过的设备SN码'
  106. "
  107. @blur="onSnCodeBlur(formItem)"
  108. v-model.lazy="formItem.snCode"
  109. ></el-input>
  110. </el-form-item>
  111. <template v-if="formItem.authType === 1">
  112. <el-form-item prop="paramList" label="设备参数:" v-if="editPrams">
  113. <br />
  114. <div class="device-param-list">
  115. <span class="add-param" @click="insertParam(formItem)"
  116. >添加参数</span
  117. >
  118. <template v-for="(param, index) in formItem.paramList">
  119. <div :key="index">
  120. <div class="param flex justify-between mb-4">
  121. <el-input
  122. style="width: 40%"
  123. placeholder="例如:品牌"
  124. class="mr-2"
  125. v-model="param.paramName"
  126. ></el-input>
  127. <el-input
  128. placeholder="请输入参数信息"
  129. v-model="param.paramContent"
  130. ></el-input>
  131. <span
  132. class="remove el-icon-close"
  133. @click="removeParam(formItem, index)"
  134. v-if="formItem.paramList.length > 4"
  135. ></span>
  136. </div>
  137. </div>
  138. </template>
  139. </div>
  140. </el-form-item>
  141. </template>
  142. </el-form>
  143. <el-divider></el-divider>
  144. </div>
  145. </template>
  146. <div class="add-device" @click="insertOne" v-if="formType !== 'edit'">
  147. <div class="add-icon"></div>
  148. 添加设备
  149. </div>
  150. <SimpleDialog
  151. v-if="formType !== 'edit'"
  152. v-model="active"
  153. @confirm="active = false"
  154. confirmText="好的"
  155. :cancel="false"
  156. description="请慎重填写设备信息,认证通过后将无法更改!"
  157. :center="true"
  158. />
  159. </div>
  160. </template>
  161. <script>
  162. import SimpleUploadImage from '@/components/SimpleUploadImage'
  163. import { mapGetters } from 'vuex'
  164. export default {
  165. components: {
  166. SimpleUploadImage,
  167. },
  168. props: {
  169. formType: {
  170. type: String,
  171. default: 'add',
  172. },
  173. },
  174. data() {
  175. const productNameValidate = (rule, value, callback) => {
  176. if (value.toString().length > 50) {
  177. callback(new Error('设备名称长度需要在50个字符内'))
  178. } else {
  179. callback()
  180. }
  181. }
  182. const paramListValidate = (rule, value, callback) => {
  183. const notEmptyList = value.filter(
  184. (item) => item.paramName.trim() && item.paramContent.trim()
  185. )
  186. if (notEmptyList.length === 0) {
  187. callback(new Error('参数列表不能为空'))
  188. } else if (notEmptyList.length < 4) {
  189. callback(new Error('请填写至少4项参数'))
  190. } else {
  191. callback()
  192. }
  193. }
  194. return {
  195. active: true,
  196. uuid: 0, // 表单id
  197. productImageList: [],
  198. rules: {
  199. productName: [
  200. { required: true, message: '设备名称不能为空', trigger: ['change'] },
  201. { validator: productNameValidate, trigger: ['change'] },
  202. ],
  203. productImage: [
  204. { required: true, message: '设备图片不能为空', trigger: ['change'] },
  205. ],
  206. infoId: [
  207. { required: true, message: '所属品牌不能为空', trigger: ['change'] },
  208. ],
  209. snCode: [
  210. { required: true, message: '设备SN码不能为空', trigger: ['blur'] },
  211. ],
  212. paramList: [
  213. { required: true, message: '参数不能为空', trigger: ['blur'] },
  214. { validator: paramListValidate, trigger: ['change'] },
  215. ],
  216. purchaseWay: [
  217. {
  218. required: true,
  219. message: '请输入购买渠道不能为空',
  220. trigger: ['blur'],
  221. },
  222. {
  223. max: 50,
  224. message: '最大长度为50个字符',
  225. trigger: ['blur'],
  226. },
  227. ],
  228. invoiceImage: [
  229. { required: true, message: '请上传发票', trigger: ['change'] },
  230. ],
  231. authType: [
  232. { required: true, message: '请选择认证方式', trigger: ['change'] },
  233. ],
  234. },
  235. formList: [],
  236. brandList: [],
  237. deviceList: [],
  238. editPrams: true,
  239. }
  240. },
  241. computed: {
  242. ...mapGetters(['authUserId']),
  243. },
  244. created() {
  245. this.fetchBrandList()
  246. this.fetchDeviceList()
  247. this.initFormList()
  248. },
  249. methods: {
  250. // sn码输入框是去焦点
  251. async onSnCodeBlur(formItem) {
  252. if (!formItem.authType === 1) return
  253. if (!formItem.snCode) return
  254. try {
  255. const res = await this.$http.api.fetchDetialBySnCode({
  256. snCode: formItem.snCode,
  257. })
  258. formItem.invoiceImage = res.data.invoiceImage
  259. formItem.purchaseWay = res.data.purchaseWay
  260. formItem.productTypeId = res.data.productTypeId
  261. formItem.productName = res.data.productName
  262. formItem.infoId = res.data.infoId
  263. formItem.productImage = res.data.productImage
  264. if (res.data.invoiceImage) {
  265. formItem.invoiceImageList = [
  266. { name: '发票', url: res.data.invoiceImage },
  267. ]
  268. }
  269. if (res.data.productImage) {
  270. formItem.productImageList = [
  271. { name: '产品图片', url: res.data.productImage },
  272. ]
  273. }
  274. } catch (error) {
  275. console.log(error)
  276. }
  277. },
  278. // 认证方式切换
  279. onAuthTypeChange(formItem, index) {
  280. formItem.purchaseWay = ''
  281. formItem.invoiceImage = ''
  282. formItem.productName = ''
  283. formItem.productTypeId = ''
  284. formItem.productImageList = []
  285. formItem.invoiceImageList = []
  286. formItem.paramList = this.initParams()
  287. this.$refs.form[index]?.resetFields()
  288. },
  289. // 表单验证
  290. validate() {
  291. this.$emit('step', this.formatFormList())
  292. return Promise.all(this.$refs.form.map((item) => item.validate()))
  293. },
  294. async init(formData) {
  295. console.log('formData', formData)
  296. const obj = {}
  297. const productImageList = [
  298. {
  299. name: '',
  300. url: formData?.productImage,
  301. },
  302. ]
  303. const invoiceImageList = [
  304. {
  305. name: '',
  306. url: formData.invoiceImage,
  307. },
  308. ]
  309. obj.uuid = ++this.uuid
  310. obj.authType = formData.authType
  311. obj.productImageList = productImageList
  312. obj.invoiceImageList = invoiceImageList
  313. obj.productImage = formData.productImage
  314. obj.productName = formData.productName
  315. obj.snCode = formData.snCode
  316. obj.infoId = formData.infoId
  317. obj.productId = formData.productId
  318. obj.productTypeId = formData.productTypeId
  319. obj.purchaseWay = formData.purchaseWay
  320. obj.invoiceImage = formData.invoiceImage
  321. obj.paramList = formData.paramList
  322. this.formList.splice(0, 1, obj)
  323. console.log('formList', this.formList)
  324. },
  325. formatFormList() {
  326. const list = []
  327. this.formList.forEach((formItem) => {
  328. const obj = {}
  329. obj.productImage = formItem.productImage
  330. obj.authType = formItem.authType
  331. obj.productName = formItem.productName
  332. obj.snCode = formItem.snCode
  333. obj.infoId = formItem.infoId
  334. obj.productId = formItem.productId
  335. obj.source = 2
  336. obj.productTypeId = formItem.productTypeId
  337. obj.purchaseWay = formItem.purchaseWay
  338. obj.invoiceImage = formItem.invoiceImage
  339. obj.paramList = formItem.paramList
  340. list.push(obj)
  341. })
  342. return list
  343. },
  344. generateFormData() {
  345. return {
  346. uuid: ++this.uuid,
  347. authUserId: '',
  348. authId: '', // 授权id
  349. createBy: '', // 创建人id
  350. // 设备参数列表
  351. paramList: this.initParams(),
  352. productId: '', // 授权设备id
  353. productImage: '', // 设备图片
  354. productName: '', // 设备名称
  355. snCode: '', // 设备SN码
  356. infoId: '',
  357. productTypeId: '',
  358. purchaseWay: '', // 购买渠道
  359. invoiceImage: '', // 发票
  360. productImageList: [],
  361. invoiceImageList: [],
  362. authType: 1,
  363. }
  364. },
  365. generageProductParam() {
  366. return {
  367. paramContent: '',
  368. paramName: '',
  369. }
  370. },
  371. initParams() {
  372. const list = []
  373. for (let i = 0; i < 4; i++) {
  374. list.push(this.generageProductParam())
  375. }
  376. return list
  377. },
  378. insertParam(formItem) {
  379. formItem.paramList.push(this.generateFormData())
  380. },
  381. removeParam(formItem, index) {
  382. formItem.paramList.splice(index, 1)
  383. },
  384. initFormList() {
  385. this.formList.push(this.generateFormData())
  386. console.log(this.formList)
  387. },
  388. insertOne() {
  389. this.formList.push(this.generateFormData())
  390. },
  391. removeOne(formItem) {
  392. const index = this.formList.findIndex(
  393. (item) => item.uuid === formItem.uuid
  394. )
  395. this.formList.splice(index, 1)
  396. },
  397. onProductNameChange(formItem, value) {
  398. if (typeof value === 'number') {
  399. formItem.productTypeId = value
  400. const deviceInfo = this.deviceList.find(
  401. (item) => item.productTypeId === value
  402. )
  403. formItem.productImage = deviceInfo.image
  404. formItem.productImageList = [{ name: '', url: deviceInfo.image }]
  405. this.editPrams = false
  406. } else {
  407. formItem.productTypeId = ''
  408. formItem.productImage = ''
  409. formItem.productImageList = []
  410. this.editPrams = true
  411. }
  412. },
  413. // 获取品牌列表
  414. async fetchBrandList() {
  415. try {
  416. const res = await this.$http.api.fetchBrandList({
  417. type: 3,
  418. authUserId: this.authUserId,
  419. })
  420. this.brandList = res.data
  421. } catch (error) {
  422. console.log(error)
  423. }
  424. },
  425. // 获取设备列表
  426. async fetchDeviceList() {
  427. try {
  428. const res = await this.$http.api.fetchProductSelectList({
  429. authUserId: this.authUserId,
  430. })
  431. this.deviceList = res.data
  432. } catch (error) {
  433. console.log(error)
  434. }
  435. },
  436. // 产品图片上传
  437. beforeProductImageUpload(file) {
  438. const flag = file.size / 1024 / 1024 < 5
  439. if (!flag) {
  440. this.$message.error('上传产品图片大小不能超过 5MB!')
  441. }
  442. return flag
  443. },
  444. uploadProductImageSuccess(formItem, { response, file, fileList }) {
  445. formItem.productImageList = fileList
  446. formItem.productImage = response.data
  447. },
  448. handleProductImageRemove(formItem, { file, fileList }) {
  449. formItem.productImageList = fileList
  450. formItem.productImage = ''
  451. },
  452. // 发票上传
  453. beforeInvoiceImageUpload(file) {
  454. const flag = file.size / 1024 / 1024 < 5
  455. if (!flag) {
  456. this.$message.error('发票图片大小不能超过 5MB!')
  457. }
  458. return flag
  459. },
  460. uploadInvoiceImageSuccess(formItem, { response, file, fileList }) {
  461. formItem.invoiceImageList = fileList
  462. formItem.invoiceImage = response.data
  463. },
  464. handleInvoiceImageRemove(formItem, { file, fileList }) {
  465. formItem.invoiceImageList = fileList
  466. formItem.invoiceImage = ''
  467. },
  468. },
  469. }
  470. </script>
  471. <style lang="scss" scoped>
  472. .club-device {
  473. ::v-deep {
  474. .el-input.is-active .el-input__inner,
  475. .el-input__inner:focus {
  476. @include themify($themes) {
  477. border-color: themed('color');
  478. }
  479. }
  480. .el-radio__input.is-checked + .el-radio__label {
  481. @include themify($themes) {
  482. color: themed('color');
  483. }
  484. }
  485. .el-radio__input.is-checked .el-radio__inner {
  486. @include themify($themes) {
  487. background-color: themed('color');
  488. border-color: themed('color');
  489. }
  490. }
  491. }
  492. }
  493. // pc端
  494. @media screen and (min-width: 768px) {
  495. .el-select {
  496. width: 100%;
  497. }
  498. .device-section {
  499. position: relative;
  500. .el-form {
  501. padding-bottom: 10px;
  502. }
  503. .remove-btn {
  504. position: absolute;
  505. right: 0;
  506. bottom: 24px;
  507. font-size: 16px;
  508. color: #f94b4b;
  509. text-decoration: underline;
  510. cursor: pointer;
  511. }
  512. }
  513. .device-param-list {
  514. position: relative;
  515. .add-param {
  516. position: absolute;
  517. cursor: pointer;
  518. top: -40px;
  519. right: 0;
  520. text-decoration: underline;
  521. font-size: 14px;
  522. @include themify($themes) {
  523. color: themed('color');
  524. }
  525. }
  526. .param {
  527. position: relative;
  528. .remove {
  529. position: absolute;
  530. right: 0;
  531. top: 0;
  532. width: 20px;
  533. height: 20px;
  534. background: #f94b4b;
  535. border-radius: 2px;
  536. cursor: pointer;
  537. color: #fff;
  538. font-size: 14px;
  539. text-align: center;
  540. line-height: 20px;
  541. }
  542. }
  543. }
  544. .add-device {
  545. display: flex;
  546. justify-content: center;
  547. align-items: center;
  548. width: 162px;
  549. height: 46px;
  550. border-radius: 4px;
  551. box-sizing: border-box;
  552. font-size: 18px;
  553. margin: 0 auto;
  554. cursor: pointer;
  555. @include themify($themes) {
  556. border: 1px solid themed('color');
  557. color: themed('color');
  558. }
  559. .add-icon {
  560. width: 20px;
  561. height: 20px;
  562. position: relative;
  563. margin-right: 16px;
  564. &::before,
  565. &::after {
  566. position: absolute;
  567. width: 3px;
  568. height: 20px;
  569. left: 50%;
  570. top: 50%;
  571. transform: translate(-50%, -50%);
  572. border-radius: 1px;
  573. content: '';
  574. display: block;
  575. @include themify($themes) {
  576. background: themed('color');
  577. }
  578. }
  579. &::after {
  580. transform: translate(-50%, -50%) rotateZ(90deg);
  581. }
  582. }
  583. }
  584. }
  585. // 移动端
  586. @media screen and (max-width: 768px) {
  587. ::v-deep {
  588. .el-form-item__label {
  589. font-size: 3.4vw;
  590. }
  591. }
  592. .el-select {
  593. width: 100%;
  594. }
  595. .device-param-list {
  596. position: relative;
  597. .add-param {
  598. position: absolute;
  599. cursor: pointer;
  600. top: -40px;
  601. right: 0;
  602. font-size: 3.4vw;
  603. @include themify($themes) {
  604. color: themed('color');
  605. }
  606. }
  607. .param {
  608. position: relative;
  609. .remove {
  610. position: absolute;
  611. right: 0;
  612. top: 0;
  613. width: 4.4vw;
  614. height: 4.4vw;
  615. background: #f94b4b;
  616. border-radius: 0.2vw;
  617. cursor: pointer;
  618. color: #fff;
  619. font-size: 3.4vw;
  620. text-align: center;
  621. line-height: 4.4vw;
  622. }
  623. }
  624. }
  625. .add-device {
  626. display: flex;
  627. justify-content: center;
  628. align-items: center;
  629. width: 31vw;
  630. height: 8.8vw;
  631. border-radius: 0.4vw;
  632. box-sizing: border-box;
  633. font-size: 3.4vw;
  634. margin: 0 auto;
  635. cursor: pointer;
  636. @include themify($themes) {
  637. border: 1px solid themed('color');
  638. color: themed('color');
  639. }
  640. .add-icon {
  641. width: 20px;
  642. height: 20px;
  643. position: relative;
  644. margin-right: 16px;
  645. &::before,
  646. &::after {
  647. position: absolute;
  648. width: 0.6vw;
  649. height: 4.1vw;
  650. left: 50%;
  651. top: 50%;
  652. transform: translate(-50%, -50%);
  653. border-radius: 1px;
  654. content: '';
  655. display: block;
  656. @include themify($themes) {
  657. background: themed('color');
  658. }
  659. }
  660. &::after {
  661. transform: translate(-50%, -50%) rotateZ(90deg);
  662. }
  663. }
  664. }
  665. }
  666. </style>