scancode.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <template>
  2. <view>
  3. <!-- 扫描框 -->
  4. <view class="scanBox" >
  5. <camera :class="status==0?'scancode':'camera'" :mode="status==0?'scanCode':'normal'" @scancode="scancode" :flash='flashBtn'>
  6. <image v-if='status==0' class="coverImg" :src="iconScanBgGif"></image>
  7. </camera>
  8. <view class="scanTip">{{status==1?'请':'请扫描二维码'}}</view>
  9. <!-- 关闭扫码页面 -->
  10. <view class='wrap'>
  11. <view class='content'>
  12. <view class='scan-left' @click="scanClick(0)">扫码添加名片</view>
  13. <view v-if='status==0'></view>
  14. <view @click='takePhoto' v-if='status==1' class='center-btn'><u-icon size="40" name="camera"></u-icon></view>
  15. <view class='scan' @click="scanClick(1)">名片识别</view>
  16. </view>
  17. </view>
  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. flag:false,
  37. src:''
  38. };
  39. },
  40. mounted() {
  41. var that = this
  42. //需要获得现在的授权信息
  43. uni.getSetting({
  44. success(res){
  45. //如果相机访问权限没有授权
  46. if (!res.authSetting['scope.camera']) {
  47. //访问授权
  48. uni.authorize({
  49. scope: 'scope.camera',
  50. success(){
  51. uni.showToast({
  52. title:'授权成功',
  53. })
  54. that.isShow = true
  55. },
  56. //拒绝后第二次访问授权默认进入fail,询问用户“检测到您没打开获取相机功能权限,是否去设置打开”
  57. fail(){
  58. uni.showModal({
  59. content: '检测到您没打开获取相机功能权限,是否去设置打开?',
  60. confirmText: "确认",
  61. cancelText: '取消',
  62. success: (res)=>{
  63. if (res.confirm) {
  64. //打开设置页面,让用户授权
  65. uni.openSetting({
  66. success: ()=>{
  67. uni.showModal({
  68. title: '授权后请重新打开此页面',
  69. icon: 'none',
  70. success: function(){
  71. // 刷新
  72. uni.navigateTo({
  73. url: '/pages/cardHolder/scancode',
  74. });
  75. }
  76. })
  77. }
  78. })
  79. }
  80. }
  81. })
  82. }
  83. })
  84. }else{
  85. that.isShow = true
  86. }
  87. }
  88. })
  89. },
  90. methods: {
  91. takePhoto() {
  92. const ctx = uni.createCameraContext();
  93. ctx.takePhoto({
  94. quality: 'high',
  95. success: (res) => {
  96. this.src = res.tempImagePath
  97. }
  98. });
  99. },
  100. // 显示扫码界面(扫一扫)
  101. scanShowClick(){
  102. console.log('扫码',111111)
  103. // 校验权限, 必须开启摄像头权限
  104. // wx.getSetting({
  105. // success:async(res)=> {
  106. // if(!res.authSetting['scope.camera']) {
  107. // 权限封装
  108. // permisson.permission_request(userCameraName, userCameraZhName);
  109. // 也可自己写,通过 wx.authorize 打开有关授权=>官方链接https://developers.weixin.qq.com/miniprogram/dev/api/open-api/authorize/wx.authorize.html
  110. // } else {
  111. this.canScan=true
  112. // 顶部标题栏背景变黑
  113. uni.setNavigationBarColor({
  114. backgroundColor: '#000000',
  115. frontColor: '#ffffff',
  116. });
  117. // }
  118. // }
  119. // })
  120. },
  121. // 隐藏扫码界面
  122. scanClick(status){
  123. this.status=status
  124. },
  125. // 扫一扫返回数据
  126. scancode(e) {
  127. if(this.flag){
  128. return
  129. }else{
  130. this.flag=true
  131. if(e){
  132. console.log(e,1111111)
  133. if(e.target.result){
  134. uni.navigateTo({
  135. url: "/pages/cardHolder/scanCodeAddCard?id="+e.target.result
  136. })
  137. this.flag=false
  138. }else{
  139. uni.showModal({
  140. content: '二维码无效',
  141. showCancel: false
  142. });
  143. this.flag = false
  144. return
  145. }
  146. }
  147. }
  148. // const {result:scanResult} = e.detail;
  149. // if(result) {
  150. // this.scanResult,canScan=false
  151. // // 顶部标题栏背景变白
  152. // uni.setNavigationBarColor({
  153. // backgroundColor: '#ffffff',
  154. // frontColor: '#000000'
  155. // });
  156. // }
  157. }
  158. },
  159. };
  160. </script>
  161. <style lang="scss" scoped>
  162. .scanBox {
  163. position: fixed;
  164. top: 0;
  165. width: 100%;
  166. height: 100vh;
  167. background-color: #000000;
  168. }
  169. .scancode {
  170. position: relative;
  171. width: 70vw;
  172. height: 70vw;
  173. margin: 20vh auto 0;
  174. }
  175. .camera {
  176. position: relative;
  177. width: 100vw;
  178. height: 80vh;
  179. }
  180. .coverImg{
  181. position: absolute;
  182. top: 0;
  183. left: 0;
  184. width: 100%;
  185. height: 100%;
  186. z-index: 99999;
  187. }
  188. .scanTip {
  189. padding: 20rpx 0 0 0;
  190. font-size: 32rpx;
  191. text-align: center;
  192. color: #fff;
  193. }
  194. /* 开关手电筒 */
  195. .scancodePic{
  196. background: transparent !important;
  197. width: 100rpx;
  198. height: 100rpx;
  199. padding: 0 !important;
  200. }
  201. .scancodePic>image{
  202. width: 100%;
  203. height: 100%;
  204. }
  205. /* 关闭按钮 */
  206. .scan,.scan-left{
  207. padding: 20rpx 30rpx;
  208. background: #fff;
  209. }
  210. .wrap{
  211. width:100vw;
  212. position:absolute;
  213. z-index: 99999;
  214. left:0;
  215. bottom:0;
  216. background-color: #000000;
  217. }
  218. .content{
  219. display: flex;
  220. justify-content: space-between;
  221. }
  222. .center-btn{
  223. background:#fff;
  224. }
  225. </style>