alone.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <div class="nfc-popup">
  3. <van-popup v-model="visible" get-container="nfc-popup" lock-scroll>
  4. <div class="nfc-panel">
  5. <div>
  6. <p class="top-text">将手机背部靠近NFC标签扫描</p>
  7. <div class="image-box">
  8. <img class="nfc-image" v-show="nfcImage" :src="nfcImage" alt="" />
  9. </div>
  10. <div class="bottom-box">
  11. <div @click="submitNFC">
  12. <img :src="require('../../assets/img/icon/NFC扫描-01.png')" />
  13. <span>NFC</span>
  14. </div>
  15. <div @click="nfcTakePhotos">
  16. <img :src="require('../../assets/img/icon/camera.png')" />
  17. <span>拍照</span>
  18. </div>
  19. </div>
  20. </div>
  21. </div>
  22. <input v-show="false" ref="upload" type="file" accept="image/*" @change="fileChange" />
  23. <!-- <uploader v-show="false" ref="imageUpload" v-model="nfcImage" multiple :maxCount="1" :deletable="false"/>-->
  24. </van-popup>
  25. </div>
  26. </template>
  27. <script>
  28. import { baseImg } from '@/views/menu/LZRegister/api'
  29. import { upload } from '@/api/public'
  30. import ImageCompressor from 'js-image-compressor'
  31. import { Toast } from 'vant'
  32. export default {
  33. name: 'index',
  34. data() {
  35. return {
  36. //nfc弹窗
  37. visible: false,
  38. //nfc图片(base64)
  39. nfcImage: null,
  40. //是否显示底部按钮
  41. showBottomBox: true,
  42. objNfc: null
  43. }
  44. },
  45. watch: {
  46. visible(val) {
  47. if (val) {
  48. this.nfcImage = null
  49. }
  50. }
  51. },
  52. mounted() {
  53. window.openNFCScanCallBack = this.openNFCScanCallBack
  54. },
  55. methods: {
  56. //上传前
  57. async fileChange(e) {
  58. let file = e.target.files[0]
  59. let fileName = e.target.files[0].name
  60. let imgFile = await this.imageCompress(file)
  61. console.log(imgFile, 'imgFile')
  62. let formData = new FormData()
  63. formData.append('file', imgFile)
  64. upload(formData, 'image').then(res => {
  65. console.log(res, 'resssss')
  66. // this.nfcImage = baseImg.base;
  67. this.$emit('change', baseImg)
  68. this.visible = false
  69. // this.showBottomBox = false;
  70. })
  71. },
  72. //压缩图片
  73. imageCompress(file) {
  74. let imageCom = new Promise((resolve, reject) => {
  75. new ImageCompressor({
  76. file,
  77. quality: 0.6,
  78. success: result => {
  79. console.log(result, '图片压缩后')
  80. let img = new File([result], result.name, {
  81. width: result.width,
  82. height: result.height,
  83. type: result.type
  84. })
  85. resolve(img)
  86. },
  87. error: e => {
  88. console.log('imageError:' + e)
  89. this.$toast('图片压缩失败')
  90. reject(e)
  91. }
  92. })
  93. })
  94. return imageCom
  95. },
  96. nfcTakePhotos() {
  97. this.$refs.upload.click()
  98. },
  99. submitNFC() {
  100. this.useNFC()
  101. // Toast.success(this.openNFCScanCallBack())
  102. // alert(this.objNfc)
  103. },
  104. openNFCScanCallBack(data) {
  105. this.objNfc = JSON.parse(data)
  106. if (this.objNfc.errorCode == 0) {
  107. this.$emit('changeNFC', this.objNfc.content)
  108. Toast.success('扫描成功')
  109. this.visible = false
  110. } else {
  111. Toast.error(this.objNfc.errorString)
  112. }
  113. }
  114. }
  115. }
  116. </script>
  117. <style lang="scss">
  118. #app {
  119. .nfc-popup {
  120. .van-popup--center {
  121. border-radius: 20px;
  122. }
  123. }
  124. }
  125. </style>
  126. <style scoped lang="scss">
  127. .nfc-panel {
  128. width: 400px;
  129. background: url('../../assets/img/NFCphone.png') center no-repeat;
  130. background-size: 50%;
  131. > div {
  132. padding: 30px;
  133. }
  134. }
  135. .top-text {
  136. text-align: center;
  137. font-size: 26px;
  138. }
  139. .image-box {
  140. width: 100%;
  141. height: 350px;
  142. display: flex;
  143. align-items: center;
  144. justify-content: center;
  145. }
  146. .nfc-image {
  147. width: 300px;
  148. height: 300px;
  149. }
  150. .bottom-box {
  151. display: flex;
  152. justify-content: space-around;
  153. align-items: center;
  154. > div {
  155. display: flex;
  156. flex-direction: column;
  157. &:active {
  158. opacity: 0.5;
  159. }
  160. > img {
  161. width: 70px;
  162. height: 70px;
  163. }
  164. > span {
  165. text-align: center;
  166. font-size: 25px;
  167. }
  168. }
  169. }
  170. </style>