form-club-device.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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.relationId = formData.relationId || ''
  312. obj.productImageList = productImageList
  313. obj.invoiceImageList = invoiceImageList
  314. obj.productImage = formData.productImage
  315. obj.productName = formData.productName
  316. obj.snCode = formData.snCode
  317. obj.infoId = formData.infoId
  318. obj.productId = formData.productId
  319. obj.productTypeId = formData.productTypeId
  320. obj.purchaseWay = formData.purchaseWay
  321. obj.invoiceImage = formData.invoiceImage
  322. obj.paramList = formData.paramList
  323. this.formList.splice(0, 1, obj)
  324. console.log('formList', this.formList)
  325. },
  326. formatFormList() {
  327. const list = []
  328. this.formList.forEach((formItem) => {
  329. const obj = {}
  330. obj.productImage = formItem.productImage
  331. obj.authType = formItem.authType
  332. obj.productName = formItem.productName
  333. obj.snCode = formItem.snCode
  334. obj.infoId = formItem.infoId
  335. obj.productId = formItem.productId
  336. obj.source = 2
  337. obj.productTypeId = formItem.productTypeId
  338. obj.purchaseWay = formItem.purchaseWay
  339. obj.invoiceImage = formItem.invoiceImage
  340. obj.paramList = formItem.paramList
  341. list.push(obj)
  342. })
  343. return list
  344. },
  345. generateFormData() {
  346. return {
  347. uuid: ++this.uuid,
  348. authUserId: '',
  349. authId: '', // 授权id
  350. createBy: '', // 创建人id
  351. // 设备参数列表
  352. paramList: this.initParams(),
  353. productId: '', // 授权设备id
  354. productImage: '', // 设备图片
  355. productName: '', // 设备名称
  356. snCode: '', // 设备SN码
  357. infoId: '',
  358. productTypeId: '',
  359. purchaseWay: '', // 购买渠道
  360. invoiceImage: '', // 发票
  361. productImageList: [],
  362. invoiceImageList: [],
  363. authType: 1,
  364. }
  365. },
  366. generageProductParam() {
  367. return {
  368. paramContent: '',
  369. paramName: '',
  370. }
  371. },
  372. initParams() {
  373. const list = []
  374. for (let i = 0; i < 4; i++) {
  375. list.push(this.generageProductParam())
  376. }
  377. return list
  378. },
  379. insertParam(formItem) {
  380. formItem.paramList.push(this.generateFormData())
  381. },
  382. removeParam(formItem, index) {
  383. formItem.paramList.splice(index, 1)
  384. },
  385. initFormList() {
  386. this.formList.push(this.generateFormData())
  387. console.log(this.formList)
  388. },
  389. insertOne() {
  390. this.formList.push(this.generateFormData())
  391. },
  392. removeOne(formItem) {
  393. const index = this.formList.findIndex(
  394. (item) => item.uuid === formItem.uuid
  395. )
  396. this.formList.splice(index, 1)
  397. },
  398. onProductNameChange(formItem, value) {
  399. if (typeof value === 'number') {
  400. formItem.productTypeId = value
  401. const deviceInfo = this.deviceList.find(
  402. (item) => item.productTypeId === value
  403. )
  404. formItem.productImage = deviceInfo.image
  405. formItem.productImageList = [{ name: '', url: deviceInfo.image }]
  406. this.editPrams = false
  407. } else {
  408. formItem.productTypeId = ''
  409. formItem.productImage = ''
  410. formItem.productImageList = []
  411. this.editPrams = true
  412. }
  413. },
  414. // 获取品牌列表
  415. async fetchBrandList() {
  416. try {
  417. const res = await this.$http.api.fetchBrandList({
  418. type: 3,
  419. authUserId: this.authUserId,
  420. })
  421. this.brandList = res.data
  422. } catch (error) {
  423. console.log(error)
  424. }
  425. },
  426. // 获取设备列表
  427. async fetchDeviceList() {
  428. try {
  429. const res = await this.$http.api.fetchProductSelectList({
  430. authUserId: this.authUserId,
  431. })
  432. this.deviceList = res.data
  433. } catch (error) {
  434. console.log(error)
  435. }
  436. },
  437. // 产品图片上传
  438. beforeProductImageUpload(file) {
  439. const flag = file.size / 1024 / 1024 < 5
  440. if (!flag) {
  441. this.$message.error('上传产品图片大小不能超过 5MB!')
  442. }
  443. return flag
  444. },
  445. uploadProductImageSuccess(formItem, { response, file, fileList }) {
  446. formItem.productImageList = fileList
  447. formItem.productImage = response.data
  448. },
  449. handleProductImageRemove(formItem, { file, fileList }) {
  450. formItem.productImageList = fileList
  451. formItem.productImage = ''
  452. },
  453. // 发票上传
  454. beforeInvoiceImageUpload(file) {
  455. const flag = file.size / 1024 / 1024 < 5
  456. if (!flag) {
  457. this.$message.error('发票图片大小不能超过 5MB!')
  458. }
  459. return flag
  460. },
  461. uploadInvoiceImageSuccess(formItem, { response, file, fileList }) {
  462. formItem.invoiceImageList = fileList
  463. formItem.invoiceImage = response.data
  464. },
  465. handleInvoiceImageRemove(formItem, { file, fileList }) {
  466. formItem.invoiceImageList = fileList
  467. formItem.invoiceImage = ''
  468. },
  469. },
  470. }
  471. </script>
  472. <style lang="scss" scoped>
  473. .club-device {
  474. ::v-deep {
  475. .el-input.is-active .el-input__inner,
  476. .el-input__inner:focus {
  477. @include themify($themes) {
  478. border-color: themed('color');
  479. }
  480. }
  481. .el-radio__input.is-checked + .el-radio__label {
  482. @include themify($themes) {
  483. color: themed('color');
  484. }
  485. }
  486. .el-radio__input.is-checked .el-radio__inner {
  487. @include themify($themes) {
  488. background-color: themed('color');
  489. border-color: themed('color');
  490. }
  491. }
  492. }
  493. }
  494. // pc端
  495. @media screen and (min-width: 768px) {
  496. .el-select {
  497. width: 100%;
  498. }
  499. .device-section {
  500. position: relative;
  501. .el-form {
  502. padding-bottom: 10px;
  503. }
  504. .remove-btn {
  505. position: absolute;
  506. right: 0;
  507. bottom: 24px;
  508. font-size: 16px;
  509. color: #f94b4b;
  510. text-decoration: underline;
  511. cursor: pointer;
  512. }
  513. }
  514. .device-param-list {
  515. position: relative;
  516. .add-param {
  517. position: absolute;
  518. cursor: pointer;
  519. top: -40px;
  520. right: 0;
  521. text-decoration: underline;
  522. font-size: 14px;
  523. @include themify($themes) {
  524. color: themed('color');
  525. }
  526. }
  527. .param {
  528. position: relative;
  529. .remove {
  530. position: absolute;
  531. right: 0;
  532. top: 0;
  533. width: 20px;
  534. height: 20px;
  535. background: #f94b4b;
  536. border-radius: 2px;
  537. cursor: pointer;
  538. color: #fff;
  539. font-size: 14px;
  540. text-align: center;
  541. line-height: 20px;
  542. }
  543. }
  544. }
  545. .add-device {
  546. display: flex;
  547. justify-content: center;
  548. align-items: center;
  549. width: 162px;
  550. height: 46px;
  551. border-radius: 4px;
  552. box-sizing: border-box;
  553. font-size: 18px;
  554. margin: 0 auto;
  555. cursor: pointer;
  556. @include themify($themes) {
  557. border: 1px solid themed('color');
  558. color: themed('color');
  559. }
  560. .add-icon {
  561. width: 20px;
  562. height: 20px;
  563. position: relative;
  564. margin-right: 16px;
  565. &::before,
  566. &::after {
  567. position: absolute;
  568. width: 3px;
  569. height: 20px;
  570. left: 50%;
  571. top: 50%;
  572. transform: translate(-50%, -50%);
  573. border-radius: 1px;
  574. content: '';
  575. display: block;
  576. @include themify($themes) {
  577. background: themed('color');
  578. }
  579. }
  580. &::after {
  581. transform: translate(-50%, -50%) rotateZ(90deg);
  582. }
  583. }
  584. }
  585. }
  586. // 移动端
  587. @media screen and (max-width: 768px) {
  588. ::v-deep {
  589. .el-form-item__label {
  590. font-size: 3.4vw;
  591. }
  592. }
  593. .el-select {
  594. width: 100%;
  595. }
  596. .device-param-list {
  597. position: relative;
  598. .add-param {
  599. position: absolute;
  600. cursor: pointer;
  601. top: -40px;
  602. right: 0;
  603. font-size: 3.4vw;
  604. @include themify($themes) {
  605. color: themed('color');
  606. }
  607. }
  608. .param {
  609. position: relative;
  610. .remove {
  611. position: absolute;
  612. right: 0;
  613. top: 0;
  614. width: 4.4vw;
  615. height: 4.4vw;
  616. background: #f94b4b;
  617. border-radius: 0.2vw;
  618. cursor: pointer;
  619. color: #fff;
  620. font-size: 3.4vw;
  621. text-align: center;
  622. line-height: 4.4vw;
  623. }
  624. }
  625. }
  626. .add-device {
  627. display: flex;
  628. justify-content: center;
  629. align-items: center;
  630. width: 31vw;
  631. height: 8.8vw;
  632. border-radius: 0.4vw;
  633. box-sizing: border-box;
  634. font-size: 3.4vw;
  635. margin: 0 auto;
  636. cursor: pointer;
  637. @include themify($themes) {
  638. border: 1px solid themed('color');
  639. color: themed('color');
  640. }
  641. .add-icon {
  642. width: 20px;
  643. height: 20px;
  644. position: relative;
  645. margin-right: 16px;
  646. &::before,
  647. &::after {
  648. position: absolute;
  649. width: 0.6vw;
  650. height: 4.1vw;
  651. left: 50%;
  652. top: 50%;
  653. transform: translate(-50%, -50%);
  654. border-radius: 1px;
  655. content: '';
  656. display: block;
  657. @include themify($themes) {
  658. background: themed('color');
  659. }
  660. }
  661. &::after {
  662. transform: translate(-50%, -50%) rotateZ(90deg);
  663. }
  664. }
  665. }
  666. }
  667. </style>