verify.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <view class="container">
  3. <view class="text-box">
  4. <text v-if="scanResult == ''" class="scanResult">待扫码</text>
  5. <text v-else class="scanResult">扫码结果:{{ scanResult }}</text>
  6. </view>
  7. <view class="btn-box">
  8. <button class="btn-scan" type="primary" @click="handleScan()">扫一扫</button>
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. import {
  14. AssVerificationRequest
  15. } from "@/grpc/app_api_pb.js"
  16. import Base64 from 'base-64'
  17. export default {
  18. data() {
  19. return {
  20. userId: 0,
  21. uoId: 0,
  22. scanResult: ''
  23. }
  24. },
  25. onLoad() {},
  26. methods: {
  27. handleScan() {
  28. let that = this
  29. this.scanResult = ''
  30. // 允许从相机和相册扫码
  31. uni.scanCode({
  32. success: function(res) {
  33. console.log('条码类型:' + res.scanType)
  34. console.log('条码内容:' + res.result)
  35. try {
  36. let result = Base64.decode(res.result)
  37. // console.log(result)
  38. result = JSON.parse(result)
  39. console.log('条码内容 Base64.decode:' + JSON.stringify(result))
  40. that.userId = result.userId
  41. that.uoId = result.uoId
  42. // console.log('userId:' + that.userId)
  43. // console.log('uoId:' + that.uoId)
  44. that.verification()
  45. } catch(e) {
  46. console.log('扫码出现异常:' + e.message)
  47. that.scanResult = '扫码错误,请扫描正确的二维码'
  48. }
  49. }
  50. })
  51. },
  52. // 核销
  53. verification() {
  54. // 创建请求参数并赋值
  55. var request = new AssVerificationRequest()
  56. request.setUserid(this.userId)
  57. request.setUoid(this.uoId)
  58. // 调用客户端相应的grpc方法,发送grpc请求,并接受后台发送回来的返回值
  59. this.$client.assVerification(request, {}, (err, response) => {
  60. if (err) {
  61. console.log(`[assVerification] err: code = ${err.code}` +
  62. `, message = "${err.message}"`)
  63. uni.showToast({
  64. title: `${err.message} (err:${err.code})`,
  65. icon: 'none',
  66. duration: 2000
  67. })
  68. this.scanResult = `${err.message} (err:${err.code})`
  69. } else {
  70. // console.log(response)
  71. uni.showToast({
  72. title: `核销成功`,
  73. icon: 'success',
  74. duration: 2000
  75. })
  76. this.scanResult = '核销成功'
  77. }
  78. });
  79. }
  80. }
  81. }
  82. </script>
  83. <style scoped lang="scss">
  84. .container {
  85. padding: 15px;
  86. box-sizing: border-box;
  87. .text-box {
  88. display: flex;
  89. justify-content: center;
  90. align-items: center;
  91. height: 200rpx;
  92. .scanResult {}
  93. }
  94. .btn-box {
  95. display: flex;
  96. justify-content: space-between;
  97. align-items: center;
  98. margin-top: 20px;
  99. .btn-scan {
  100. width: 90%;
  101. }
  102. }
  103. }
  104. </style>