scancode.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. uni.showLoading({
  133. title: '数据加载中',
  134. mask:true
  135. })
  136. const ctx = uni.createCameraContext();
  137. console.log(ctx)
  138. ctx.takePhoto({
  139. quality: 'high',
  140. fail: (req) => {
  141. uni.hideLoading()
  142. },
  143. success: (res) => {
  144. this.src = res.tempImagePath
  145. this.uploadFilePromise(res.tempImagePath)
  146. },
  147. });
  148. },
  149. uploadFilePromise(url) {
  150. console.log(url)
  151. uploadImage(url, 'cardImages/',
  152. result => {
  153. console.log(result, 22222)
  154. }
  155. )
  156. },
  157. error(e) {
  158. console.log(e.detail);
  159. },
  160. // 显示扫码界面(扫一扫)
  161. scanShowClick(){
  162. console.log('扫码')
  163. // 校验权限, 必须开启摄像头权限
  164. // wx.getSetting({
  165. // success:async(res)=> {
  166. // if(!res.authSetting['scope.camera']) {
  167. // 权限封装
  168. // permisson.permission_request(userCameraName, userCameraZhName);
  169. // 也可自己写,通过 wx.authorize 打开有关授权=>官方链接https://developers.weixin.qq.com/miniprogram/dev/api/open-api/authorize/wx.authorize.html
  170. // } else {
  171. this.canScan=true
  172. // 顶部标题栏背景变黑
  173. uni.setNavigationBarColor({
  174. backgroundColor: '#000000',
  175. frontColor: '#ffffff',
  176. });
  177. // }
  178. // }
  179. // })
  180. },
  181. // 隐藏扫码界面
  182. scanClick(status){
  183. this.status=status
  184. uni.setStorageSync('scancodeStatus', this.status);
  185. uni.redirectTo({
  186. url: '/pages/cardHolder/scancode',
  187. });
  188. },
  189. // 扫一扫返回数据
  190. scancode(e) {
  191. if(this.flag){
  192. return
  193. }else{
  194. this.flag=true
  195. if(e){
  196. console.log(e)
  197. if(e.target.result){
  198. uni.navigateTo({
  199. url: "/pageA/cardHolder/scanCodeAddCard?id="+e.target.result
  200. })
  201. this.flag=false
  202. }else{
  203. uni.showModal({
  204. content: '二维码无效',
  205. showCancel: false
  206. });
  207. this.flag = false
  208. return
  209. }
  210. }
  211. }
  212. // const {result:scanResult} = e.detail;
  213. // if(result) {
  214. // this.scanResult,canScan=false
  215. // // 顶部标题栏背景变白
  216. // uni.setNavigationBarColor({
  217. // backgroundColor: '#ffffff',
  218. // frontColor: '#000000'
  219. // });
  220. // }
  221. }
  222. },
  223. };
  224. </script>
  225. <style lang="scss" scoped>
  226. .scanBox {
  227. position: fixed;
  228. top: 0;
  229. width: 100%;
  230. height: 100vh;
  231. background-color: #000000;
  232. }
  233. .camera {
  234. position: relative;
  235. width: 100vw;
  236. height: 80vh;
  237. }
  238. .coverImg{
  239. position: absolute;
  240. top: 0;
  241. left: 0;
  242. width: 100%;
  243. height: 100%;
  244. z-index: 99999;
  245. }
  246. .scanTip {
  247. padding: 20rpx 0 0 0;
  248. font-size: 32rpx;
  249. text-align: center;
  250. color: #fff;
  251. }
  252. /* 关闭按钮 */
  253. .scan,.scan-left{
  254. padding: 20rpx 30rpx;
  255. background: #fff;
  256. }
  257. .wrap{
  258. width:100vw;
  259. position:absolute;
  260. z-index: 99999;
  261. left:0;
  262. bottom:0;
  263. background-color: #000000;
  264. }
  265. .content{
  266. display: flex;
  267. justify-content: space-between;
  268. }
  269. .center-btn{
  270. background:#fff;
  271. }
  272. // .scan-border {
  273. // width: 100%;
  274. // height: 100%;
  275. // /* border: 6rpx solid #08FDFE; */
  276. // display: flex;
  277. // flex-direction: column;
  278. // align-items: center;
  279. .scan-camera {
  280. position: relative;
  281. width: 70vw;
  282. height: 70vw;
  283. margin: 20vh auto 0;
  284. border-radius: 6rpx;
  285. }
  286. // }
  287. .scan-animation {
  288. position: absolute;
  289. top: 10%;
  290. left:5%;
  291. width: 480rpx;
  292. height: 8rpx;
  293. background-color: #0091ff;
  294. border-radius: 50%;
  295. }
  296. .btns {
  297. margin: 10rpx;
  298. width: 100%;
  299. background-color: #0091ff;
  300. }
  301. .scan-pict {
  302. position: absolute;
  303. right: 1%;
  304. bottom: 1%;
  305. width: 70rpx;
  306. height: 70rpx;
  307. z-index: 20;
  308. border-radius: 50%;
  309. background-color: #c0c0c0;
  310. opacity: 0.8;
  311. .pic {
  312. z-index: 999;
  313. width: 50rpx;
  314. height: 50rpx;
  315. margin: 10rpx;
  316. }
  317. }
  318. </style>