form-club-device.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. @include useTheme() {
  474. ::v-deep {
  475. .el-input.is-active .el-input__inner,
  476. .el-input__inner:focus {
  477. border-color: fetch('color');
  478. }
  479. .el-radio__input.is-checked + .el-radio__label {
  480. color: fetch('color');
  481. }
  482. .el-radio__input.is-checked .el-radio__inner {
  483. background-color: fetch('color');
  484. border-color: fetch('color');
  485. }
  486. }
  487. }
  488. }
  489. // pc端
  490. @media screen and (min-width: 768px) {
  491. .club-device {
  492. @include useTheme() {
  493. .el-select {
  494. width: 100%;
  495. }
  496. .device-section {
  497. position: relative;
  498. .el-form {
  499. padding-bottom: 10px;
  500. }
  501. .remove-btn {
  502. position: absolute;
  503. right: 0;
  504. bottom: 24px;
  505. font-size: 16px;
  506. color: #f94b4b;
  507. text-decoration: underline;
  508. cursor: pointer;
  509. }
  510. }
  511. .device-param-list {
  512. position: relative;
  513. .add-param {
  514. position: absolute;
  515. cursor: pointer;
  516. top: -40px;
  517. right: 0;
  518. text-decoration: underline;
  519. font-size: 14px;
  520. color: fetch('color');
  521. }
  522. .param {
  523. position: relative;
  524. .remove {
  525. position: absolute;
  526. right: 0;
  527. top: 0;
  528. width: 20px;
  529. height: 20px;
  530. background: #f94b4b;
  531. border-radius: 2px;
  532. cursor: pointer;
  533. color: #fff;
  534. font-size: 14px;
  535. text-align: center;
  536. line-height: 20px;
  537. }
  538. }
  539. }
  540. .add-device {
  541. display: flex;
  542. justify-content: center;
  543. align-items: center;
  544. width: 162px;
  545. height: 46px;
  546. border-radius: 4px;
  547. box-sizing: border-box;
  548. font-size: 18px;
  549. margin: 0 auto;
  550. cursor: pointer;
  551. border: 1px solid fetch('color');
  552. color: fetch('color');
  553. .add-icon {
  554. width: 20px;
  555. height: 20px;
  556. position: relative;
  557. margin-right: 16px;
  558. &::before,
  559. &::after {
  560. position: absolute;
  561. width: 3px;
  562. height: 20px;
  563. left: 50%;
  564. top: 50%;
  565. transform: translate(-50%, -50%);
  566. border-radius: 1px;
  567. content: '';
  568. display: block;
  569. background: fetch('color');
  570. }
  571. &::after {
  572. transform: translate(-50%, -50%) rotateZ(90deg);
  573. }
  574. }
  575. }
  576. }
  577. }
  578. }
  579. // 移动端
  580. @media screen and (max-width: 768px) {
  581. .club-device {
  582. @include useTheme() {
  583. ::v-deep {
  584. .el-form-item__label {
  585. font-size: 3.4vw;
  586. }
  587. }
  588. .el-select {
  589. width: 100%;
  590. }
  591. .device-param-list {
  592. position: relative;
  593. .add-param {
  594. position: absolute;
  595. cursor: pointer;
  596. top: -40px;
  597. right: 0;
  598. font-size: 3.4vw;
  599. color: fetch('color');
  600. }
  601. .param {
  602. position: relative;
  603. .remove {
  604. position: absolute;
  605. right: 0;
  606. top: 0;
  607. width: 4.4vw;
  608. height: 4.4vw;
  609. background: #f94b4b;
  610. border-radius: 0.2vw;
  611. cursor: pointer;
  612. color: #fff;
  613. font-size: 3.4vw;
  614. text-align: center;
  615. line-height: 4.4vw;
  616. }
  617. }
  618. }
  619. .add-device {
  620. display: flex;
  621. justify-content: center;
  622. align-items: center;
  623. width: 31vw;
  624. height: 8.8vw;
  625. border-radius: 0.4vw;
  626. box-sizing: border-box;
  627. font-size: 3.4vw;
  628. margin: 0 auto;
  629. cursor: pointer;
  630. border: 1px solid fetch('color');
  631. color: fetch('color');
  632. .add-icon {
  633. width: 20px;
  634. height: 20px;
  635. position: relative;
  636. margin-right: 16px;
  637. &::before,
  638. &::after {
  639. position: absolute;
  640. width: 0.6vw;
  641. height: 4.1vw;
  642. left: 50%;
  643. top: 50%;
  644. transform: translate(-50%, -50%);
  645. border-radius: 1px;
  646. content: '';
  647. display: block;
  648. background: fetch('color');
  649. }
  650. &::after {
  651. transform: translate(-50%, -50%) rotateZ(90deg);
  652. }
  653. }
  654. }
  655. }
  656. }
  657. }
  658. </style>