scancode.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <template>
  2. <view>
  3. <!-- 扫描框 -->
  4. <view class="scanBox" >
  5. <camera class="camera" mode="scanCode" @scancode="scancode" :flash='flashBtn'>
  6. <image class="coverImg" :src="iconScanBgGif"></image>
  7. </camera>
  8. <view style="text-align: center;margin: 80rpx 0 40rpx 0;">
  9. <button class="cu-btn scancodePic" bindtap="changeflashBtn">
  10. <!-- <image :src="flashBtn=='on'?scancodeOpen:scancodeClose"></image> -->
  11. </button>
  12. </view>
  13. <view class="scanTip">{{status==1?'请':'请扫描二维码'}}</view>
  14. <!-- 关闭扫码页面 -->
  15. <view class='scan-left' @click="scanClick(0)">扫码添加名片</view>
  16. <view class='scan' @click="scanClick(1)">名片识别</view>
  17. <div class="000"></div>
  18. </view>
  19. </view>
  20. </template>
  21. <script>
  22. // import { iconScanBgGif,scancodeOpen,scancodeClose } from "../../../utils/imgUrl/index";
  23. // const permisson = require("../../../utils/tools/permisson"); // 权限校验封装
  24. const userCameraName = "scope.camera"; // 摄像头权限
  25. const userCameraZhName = "手机摄像头"; // 摄像头权限对应的中文名称
  26. export default {
  27. data() {
  28. return {
  29. iconScanBgGif:'/static/imgs/code.gif',
  30. // iconScanBgGif,scancodeOpen,scancodeClose,
  31. canScan: false, // 是否显示自定义扫码界面
  32. flashBtn: 'off', // off关闭 on 打开手电筒
  33. scanResult: '', // 扫描结果
  34. isShow:false,
  35. status:0
  36. };
  37. },
  38. mounted() {
  39. var that = this
  40. //需要获得现在的授权信息
  41. uni.getSetting({
  42. success(res){
  43. //如果相机访问权限没有授权
  44. if (!res.authSetting['scope.camera']) {
  45. //访问授权
  46. uni.authorize({
  47. scope: 'scope.camera',
  48. success(){
  49. uni.showToast({
  50. title:'授权成功',
  51. })
  52. that.isShow = true
  53. },
  54. //拒绝后第二次访问授权默认进入fail,询问用户“检测到您没打开获取相机功能权限,是否去设置打开”
  55. fail(){
  56. uni.showModal({
  57. content: '检测到您没打开获取相机功能权限,是否去设置打开?',
  58. confirmText: "确认",
  59. cancelText: '取消',
  60. success: (res)=>{
  61. if (res.confirm) {
  62. //打开设置页面,让用户授权
  63. uni.openSetting({
  64. success: ()=>{
  65. uni.showModal({
  66. title: '授权后请重新打开此页面',
  67. icon: 'none',
  68. success: function(){
  69. // 刷新
  70. uni.navigateTo({
  71. url: '/pages/cardHolder/scancode',
  72. });
  73. }
  74. })
  75. }
  76. })
  77. }
  78. }
  79. })
  80. }
  81. })
  82. }else{
  83. that.isShow = true
  84. }
  85. }
  86. })
  87. },
  88. methods: {
  89. changeflashBtn(){
  90. this.flashBtn=this.data.flashBtn=='off'?'on':'off'
  91. },
  92. // 显示扫码界面(扫一扫)
  93. scanShowClick(){
  94. console.log('扫码',111111)
  95. // 校验权限, 必须开启摄像头权限
  96. // wx.getSetting({
  97. // success:async(res)=> {
  98. // if(!res.authSetting['scope.camera']) {
  99. // 权限封装
  100. // permisson.permission_request(userCameraName, userCameraZhName);
  101. // 也可自己写,通过 wx.authorize 打开有关授权=>官方链接https://developers.weixin.qq.com/miniprogram/dev/api/open-api/authorize/wx.authorize.html
  102. // } else {
  103. this.canScan=true
  104. // 顶部标题栏背景变黑
  105. uni.setNavigationBarColor({
  106. backgroundColor: '#000000',
  107. frontColor: '#ffffff',
  108. });
  109. // }
  110. // }
  111. // })
  112. },
  113. // 隐藏扫码界面
  114. scanClick(status){
  115. uni.scanCode({
  116. scanType: ['barCode', 'qrCode'],
  117. success: function(res) {
  118. }
  119. })
  120. // this.status=status
  121. },
  122. // 扫一扫返回数据
  123. scancode(e) {
  124. console.log(e.detail)
  125. uni.scanCode({
  126. scanType: ['barCode', 'qrCode'],
  127. success: function(res) {
  128. }
  129. })
  130. // const {result:scanResult} = e.detail;
  131. // if(result) {
  132. // this.scanResult,canScan=false
  133. // // 顶部标题栏背景变白
  134. // uni.setNavigationBarColor({
  135. // backgroundColor: '#ffffff',
  136. // frontColor: '#000000'
  137. // });
  138. // }
  139. }
  140. },
  141. };
  142. </script>
  143. <style lang="scss" scoped>
  144. .scanBox {
  145. position: fixed;
  146. top: 0;
  147. width: 100%;
  148. height: 100vh;
  149. background-color: #000000;
  150. }
  151. .camera {
  152. position: relative;
  153. width: 70vw;
  154. height: 70vw;
  155. margin: 20vh auto 0;
  156. }
  157. .coverImg{
  158. position: absolute;
  159. top: 0;
  160. left: 0;
  161. width: 100%;
  162. height: 100%;
  163. z-index: 99999;
  164. }
  165. .scanTip {
  166. padding: 20rpx 0 0 0;
  167. font-size: 32rpx;
  168. text-align: center;
  169. color: #fff;
  170. }
  171. /* 开关手电筒 */
  172. .scancodePic{
  173. background: transparent !important;
  174. width: 100rpx;
  175. height: 100rpx;
  176. padding: 0 !important;
  177. }
  178. .scancodePic>image{
  179. width: 100%;
  180. height: 100%;
  181. }
  182. /* 关闭按钮 */
  183. .scan,.scan-left{
  184. position: absolute;
  185. bottom: 0;
  186. padding: 20rpx 30rpx;
  187. z-index: 99999;
  188. background: #fff;
  189. }
  190. .scan{
  191. right:0;
  192. }
  193. .scan-left{
  194. left:0;
  195. }
  196. </style>