nfc.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <template>
  2. <view class="container">
  3. <uni-easyinput v-model="nfcWriteData" placeholder="请输入要写入NFC的数据" @input="input" />
  4. <view class="btn-box">
  5. <button class="btn" type="primary" @click="readData()">读取NFC</button>
  6. <button class="btn" type="primary" @click="writeData()">写入NFC</button>
  7. </view>
  8. <view class="btn-box">
  9. <button class="btn" type="primary" @click="getNfcState()">NFC状态</button>
  10. <button class="btn" type="warn" @click="writeAndLock()">写入并锁定</button>
  11. </view>
  12. <view class="btn-box">
  13. <button class="btn" type="warn" @click="setPassword()">设置密码</button>
  14. <button class="btn" type="warn" @click="delPassword()">清除密码</button>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. // import nfc from '../../utils/ouu-nfc.js'
  20. export default {
  21. data() {
  22. return {
  23. nfcWriteData: 'http://www.beswell.com'
  24. }
  25. },
  26. mounted() {
  27. console.log('[nfc] mounted')
  28. // nfc.listenNFCStatus();
  29. },
  30. onLoad() {
  31. console.log('[nfc] onLoad')
  32. this.$nfc.inputChanage(this.nfcWriteData)
  33. },
  34. onShow() {
  35. console.log('[nfc] onShow')
  36. uni.$off('NfcRead')
  37. uni.$on('NfcRead', this.onNfcRead)
  38. },
  39. onHide() {
  40. console.log('[nfc] onHide')
  41. },
  42. beforeDestroy() {
  43. console.log('[nfc] beforeDestroy')
  44. uni.$off('NfcRead')
  45. },
  46. methods: {
  47. input(data) {
  48. // console.log('input, data: ' + JSON.stringify(data))
  49. this.$nfc.inputChanage(data)
  50. },
  51. onNfcRead(data) {
  52. console.log('监听到事件来自 nfcRead, data: ' + JSON.stringify(data))
  53. // console.log(data)
  54. uni.showToast({
  55. title: `[${data.id}] '${data.data}'`,
  56. icon: 'none',
  57. duration: 2000
  58. })
  59. },
  60. writeData() {
  61. this.$nfc.writeData()
  62. },
  63. readData() {
  64. this.$nfc.readData()
  65. },
  66. setPassword() {
  67. this.$nfc.setPwdData()
  68. },
  69. delPassword() {
  70. this.$nfc.delPwdData()
  71. },
  72. getNfcState() {
  73. this.$nfc.getNfcState()
  74. },
  75. writeAndLock() {
  76. this.$nfc.writeAndLock()
  77. }
  78. }
  79. }
  80. </script>
  81. <style scoped lang="scss">
  82. .container {
  83. padding: 15px;
  84. box-sizing: border-box;
  85. .btn-box {
  86. display: flex;
  87. justify-content: center;
  88. align-items: center;
  89. margin-top: 20px;
  90. .btn {
  91. width: 35%;
  92. }
  93. }
  94. }
  95. </style>