form-club-device.vue 17 KB

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