当前位置:   article > 正文

uni-app内置组件camera在微信小程序中的扫码使用_uniapp 微信小程序:扫码

uniapp 微信小程序:扫码
  1. /*本文主要讲解uni-app内置组件camera在微信小程序中的扫码运用*/
  2. <template>
  3. <view>
  4. /*device-position:后置摄像头*/
  5. /*flash:关闭闪光灯*/
  6. /*mode:扫码模式*/
  7. /*@error:报错时触发*/
  8. /*@scancode:扫码识别成功时触发*/
  9. /*@initdone:初始化完成时触发*/
  10. /*@ready:初始化成功时触发*/
  11. /*@stop:非正常中止时触发,如后台退出*/
  12. <camera device-position="back" flash="off" @error="error" mode="scanCode" @scancode="scancode"
  13. @initdone="initdone" @ready="ready" @stop="stop">
  14. </camera>
  15. </view>
  16. </template>
  17. <script>
  18. export default {
  19. data() {
  20. return {
  21. loading: false,
  22. /*是否加载camera组件*/
  23. status: "load" /*扫码状态*/
  24. }
  25. },
  26. onLoad() {
  27. this.getAuth()
  28. },
  29. methods: {
  30. initdone() {
  31. console.log("camera初始化完成")
  32. },
  33. stop() {
  34. console.log("camera中止")
  35. },
  36. ready() {
  37. console.log("camera初始化成功")
  38. },
  39. error(e) {
  40. console.log("错误信息:" + e.detail.errMsg)
  41. },
  42. scancode(e) {
  43. if (this.status == "loading") {
  44. return
  45. }
  46. this.status = "loading"
  47. console.log("扫码结果:" + e.detail.result)
  48. setTimeout(() => {
  49. console.log("处理数据")
  50. this.status = "load"
  51. }, 2000)
  52. },
  53. getAuth() {
  54. uni.getSetting({
  55. success: (res => {
  56. if (res.authSetting["scope.camera"]) {
  57. console.log("已授权")
  58. this.loading = true
  59. } else {
  60. console.log("未授权")
  61. this.showModal()
  62. }
  63. })
  64. })
  65. },
  66. showModal() {
  67. uni.showModal({
  68. content: "当前页面功能需打开相机权限,请点击跳转设置",
  69. showCancel: false,
  70. confirmText: "点击跳转",
  71. success: (res => {
  72. this.goAuth()
  73. })
  74. })
  75. },
  76. goAuth() {
  77. uni.openSetting({
  78. success: (res => {
  79. if (res.authSetting["scope.camera"]) {
  80. console.log("已授权")
  81. this.loading = true
  82. } else {
  83. console.log("未授权")
  84. this.showModal()
  85. }
  86. })
  87. })
  88. }
  89. /*需要注意,发布正式版小程序时,需要在隐私协议添加相机权限,否则在设置中无法开启授权*/
  90. }
  91. }
  92. </script>
  93. <style lang="scss" scoped>
  94. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/575805
推荐阅读
相关标签
  

闽ICP备14008679号