scancode.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <template>
  2. <view>
  3. <!-- 扫描框 -->
  4. <view class="scanBox" >
  5. <view class="scan-view">
  6. <view class="scan-border">
  7. <camera :class="status==0?'scan-camera':'camera'" @error="error" :mode="status==0?'scanCode':'normal'" @scancode="scancode" :flash='flashBtn'>
  8. <cover-view v-if='status==0'
  9. class="scan-animation"
  10. :animation="animation"
  11. ></cover-view>
  12. <!-- <cover-view class="scan-pict"
  13. ><cover-image
  14. class="pic"
  15. src="../../static/images/selectpic.png"
  16. ></cover-image>
  17. </cover-view> -->
  18. </camera>
  19. </view>
  20. </view>
  21. <view class="scanTip">{{status==1?'请':'请扫描二维码'}}</view>
  22. <!-- 关闭扫码页面 -->
  23. <view class='wrap'>
  24. <view class='content'>
  25. <view class='scan-left' @click="scanClick(0)">扫码添加名片</view>
  26. <view v-if='status==0'></view>
  27. <view @click='takePhoto' v-if='status==1' class='center-btn'><u-icon size="40" name="camera"></u-icon></view>
  28. <view class='scan' @click="scanClick(1)">名片识别</view>
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. </template>
  34. <script>
  35. // import { iconScanBgGif,scancodeOpen,scancodeClose } from "../../../utils/imgUrl/index";
  36. // const permisson = require("../../../utils/tools/permisson"); // 权限校验封装
  37. import uploadImage from '@/components/ossutil/uploadFile.js';
  38. const userCameraName = "scope.camera"; // 摄像头权限
  39. const userCameraZhName = "手机摄像头"; // 摄像头权限对应的中文名称
  40. let animation = uni.createAnimation({});
  41. export default {
  42. data() {
  43. return {
  44. // iconScanBgGif:'/static/imgs/code1.gif',
  45. // iconScanBgGif,scancodeOpen,scancodeClose,
  46. canScan: false, // 是否显示自定义扫码界面
  47. flashBtn: 'off', // off关闭 on 打开手电筒
  48. scanResult: '', // 扫描结果
  49. isShow:false,
  50. status:uni.getStorageSync('scancodeStatus')?uni.getStorageSync('scancodeStatus'):0,
  51. flag:false,
  52. src:'',
  53. animation,
  54. };
  55. },
  56. onShow() {
  57. this.donghua()
  58. },
  59. mounted() {
  60. var that = this
  61. //需要获得现在的授权信息
  62. uni.getSetting({
  63. success(res){
  64. //如果相机访问权限没有授权
  65. if (!res.authSetting['scope.camera']) {
  66. //访问授权
  67. uni.authorize({
  68. scope: 'scope.camera',
  69. success(){
  70. uni.showToast({
  71. title:'授权成功',
  72. })
  73. that.isShow = true
  74. },
  75. //拒绝后第二次访问授权默认进入fail,询问用户“检测到您没打开获取相机功能权限,是否去设置打开”
  76. fail(){
  77. uni.showModal({
  78. content: '检测到您没打开获取相机功能权限,是否去设置打开?',
  79. confirmText: "确认",
  80. cancelText: '取消',
  81. success: (res)=>{
  82. if (res.confirm) {
  83. //打开设置页面,让用户授权
  84. uni.openSetting({
  85. success: ()=>{
  86. uni.showModal({
  87. title: '授权后请重新打开此页面',
  88. icon: 'none',
  89. success: function(){
  90. // 刷新
  91. uni.redirectTo({
  92. url: '/pages/cardHolder/scancode',
  93. });
  94. }
  95. })
  96. }
  97. })
  98. }
  99. }
  100. })
  101. }
  102. })
  103. }else{
  104. that.isShow = true
  105. }
  106. }
  107. })
  108. },
  109. methods: {
  110. donghua() {
  111. let that = this;
  112. let scode = true;
  113. setInterval(
  114. function () {
  115. if (scode) {
  116. animation.translateY(240).step({
  117. duration: 1500,
  118. });
  119. scode = !scode;
  120. } else {
  121. animation.translateY(-10).step({
  122. duration: 1500,
  123. });
  124. scode = !scode;
  125. }
  126. that.animation = animation.export();
  127. }.bind(this),
  128. 1500
  129. );
  130. },
  131. takePhoto() {
  132. console.log(1111111)
  133. uni.showLoading({
  134. title: '数据加载中',
  135. mask:true
  136. })
  137. const ctx = uni.createCameraContext();
  138. console.log(ctx)
  139. ctx.takePhoto({
  140. quality: 'high',
  141. fail: (req) => {
  142. console.log(req,22222)
  143. uni.hideLoading()
  144. },
  145. success: (res) => {
  146. this.src = res.tempImagePath
  147. console.log(res.tempImagePath,33333333)
  148. this.uploadFilePromise(res.tempImagePath)
  149. },
  150. });
  151. },
  152. uploadFilePromise(url) {
  153. console.log(url)
  154. uploadImage(url, 'cardImages/',
  155. result => {
  156. console.log(result, 22222)
  157. }
  158. )
  159. },
  160. error(e) {
  161. console.log(e.detail);
  162. },
  163. // 显示扫码界面(扫一扫)
  164. scanShowClick(){
  165. console.log('扫码',111111)
  166. // 校验权限, 必须开启摄像头权限
  167. // wx.getSetting({
  168. // success:async(res)=> {
  169. // if(!res.authSetting['scope.camera']) {
  170. // 权限封装
  171. // permisson.permission_request(userCameraName, userCameraZhName);
  172. // 也可自己写,通过 wx.authorize 打开有关授权=>官方链接https://developers.weixin.qq.com/miniprogram/dev/api/open-api/authorize/wx.authorize.html
  173. // } else {
  174. this.canScan=true
  175. // 顶部标题栏背景变黑
  176. uni.setNavigationBarColor({
  177. backgroundColor: '#000000',
  178. frontColor: '#ffffff',
  179. });
  180. // }
  181. // }
  182. // })
  183. },
  184. // 隐藏扫码界面
  185. scanClick(status){
  186. this.status=status
  187. uni.setStorageSync('scancodeStatus', this.status);
  188. uni.redirectTo({
  189. url: '/pages/cardHolder/scancode',
  190. });
  191. },
  192. // 扫一扫返回数据
  193. scancode(e) {
  194. if(this.flag){
  195. return
  196. }else{
  197. this.flag=true
  198. if(e){
  199. console.log(e,1111111)
  200. if(e.target.result){
  201. uni.navigateTo({
  202. url: "/pages/cardHolder/scanCodeAddCard?id="+e.target.result
  203. })
  204. this.flag=false
  205. }else{
  206. uni.showModal({
  207. content: '二维码无效',
  208. showCancel: false
  209. });
  210. this.flag = false
  211. return
  212. }
  213. }
  214. }
  215. // const {result:scanResult} = e.detail;
  216. // if(result) {
  217. // this.scanResult,canScan=false
  218. // // 顶部标题栏背景变白
  219. // uni.setNavigationBarColor({
  220. // backgroundColor: '#ffffff',
  221. // frontColor: '#000000'
  222. // });
  223. // }
  224. }
  225. },
  226. };
  227. </script>
  228. <style lang="scss" scoped>
  229. .scanBox {
  230. position: fixed;
  231. top: 0;
  232. width: 100%;
  233. height: 100vh;
  234. background-color: #000000;
  235. }
  236. .camera {
  237. position: relative;
  238. width: 100vw;
  239. height: 80vh;
  240. }
  241. .coverImg{
  242. position: absolute;
  243. top: 0;
  244. left: 0;
  245. width: 100%;
  246. height: 100%;
  247. z-index: 99999;
  248. }
  249. .scanTip {
  250. padding: 20rpx 0 0 0;
  251. font-size: 32rpx;
  252. text-align: center;
  253. color: #fff;
  254. }
  255. /* 关闭按钮 */
  256. .scan,.scan-left{
  257. padding: 20rpx 30rpx;
  258. background: #fff;
  259. }
  260. .wrap{
  261. width:100vw;
  262. position:absolute;
  263. z-index: 99999;
  264. left:0;
  265. bottom:0;
  266. background-color: #000000;
  267. }
  268. .content{
  269. display: flex;
  270. justify-content: space-between;
  271. }
  272. .center-btn{
  273. background:#fff;
  274. }
  275. // .scan-border {
  276. // width: 100%;
  277. // height: 100%;
  278. // /* border: 6rpx solid #08FDFE; */
  279. // display: flex;
  280. // flex-direction: column;
  281. // align-items: center;
  282. .scan-camera {
  283. position: relative;
  284. width: 70vw;
  285. height: 70vw;
  286. margin: 20vh auto 0;
  287. border-radius: 6rpx;
  288. }
  289. // }
  290. .scan-animation {
  291. position: absolute;
  292. top: 10%;
  293. left:5%;
  294. width: 480rpx;
  295. height: 8rpx;
  296. background-color: #0091ff;
  297. border-radius: 50%;
  298. }
  299. .btns {
  300. margin: 10rpx;
  301. width: 100%;
  302. background-color: #0091ff;
  303. }
  304. .scan-pict {
  305. position: absolute;
  306. right: 1%;
  307. bottom: 1%;
  308. width: 70rpx;
  309. height: 70rpx;
  310. z-index: 20;
  311. border-radius: 50%;
  312. background-color: #c0c0c0;
  313. opacity: 0.8;
  314. .pic {
  315. z-index: 999;
  316. width: 50rpx;
  317. height: 50rpx;
  318. margin: 10rpx;
  319. }
  320. }
  321. </style>