faceLogin.nvue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <template>
  2. <view class="main" id="main">
  3. <epii-camera ref="camera" class="epiiCamer" :style="{width:width,height:height}"></epii-camera>
  4. <cover-view>
  5. <image :style="{width:width,height:height}" src="../../static/sl.gif" mode="scaleToFill"></image>
  6. </cover-view>
  7. </view>
  8. </template>
  9. <script>
  10. import http from '../../common/http.js'
  11. export default {
  12. data() {
  13. return {
  14. cameraId: 0,
  15. height: "500rpx",
  16. width: "750rpx",
  17. userId: '',
  18. }
  19. },
  20. onLoad(options) {
  21. this.userId = options.id;
  22. this.height = uni.getSystemInfoSync().screenHeight * 2 + 'rpx';
  23. this.$nextTick(() => {
  24. //这里可以做打开摄像头等功能
  25. this.openCamera(1)
  26. })
  27. },
  28. beforeDestroy() {
  29. this.release();
  30. },
  31. mounted() {
  32. console.log(this.$refs.camera)
  33. //this.common.hidingLoading();
  34. },
  35. methods: {
  36. takePicture(e) {
  37. const that = this;
  38. that.$refs.camera.takePicture({
  39. base64: false,
  40. quailty: 100,
  41. }, function(data) {
  42. let img = data.img;
  43. that.registerFace(img);
  44. return
  45. });
  46. },
  47. registerFace(img) {
  48. const that = this;
  49. uni.uploadFile({
  50. url: http.ip + '/sp-admin/app/AppUser/faceLogin',
  51. filePath: 'file://' + img,
  52. name: 'file',
  53. formData: {
  54. id: that.userId
  55. },
  56. header: {
  57. satoken: uni.getStorageSync('token')
  58. },
  59. success(resp) {
  60. let res = JSON.parse(resp.data);
  61. if (res.code == 200) {
  62. that.release();
  63. setTimeout(()=>{
  64. let data=res.data;
  65. uni.setStorageSync('token', data.tokenInfo.tokenValue);
  66. uni.setStorageSync('info', data.appUser);
  67. uni.setStorageSync('menu', data.per_list);
  68. uni.showToast({ title: '登录成功' });
  69. setTimeout(() => {
  70. uni.$emit('getMenu');
  71. uni.switchTab({ url: '/pages/index/index' });
  72. }, 1000);
  73. },500)
  74. } else {
  75. uni.showToast({
  76. icon:'none',
  77. title: res.msg
  78. })
  79. setTimeout(function() {
  80. that.takePicture();
  81. }, 1000);
  82. }
  83. }
  84. })
  85. },
  86. release() {
  87. this.$refs.camera.release();
  88. },
  89. openCamera(id) {
  90. if (uni.getSystemInfoSync().platform === "ios") {
  91. let that = this;
  92. this.$refs.camera.openCamera({
  93. cameraId: id,
  94. }, function(data) {
  95. setTimeout(function() {
  96. that.takePicture();
  97. }, 200);
  98. });
  99. } else {
  100. //android 系统需要考虑 组件的宽高 和摄像头的宽高比例,如果不一致会导致拉伸
  101. this.openCamera_1(id);
  102. }
  103. },
  104. openCamera_1(id) {
  105. //安卓解决拉伸问题,一般有两种思路。
  106. //1. 组件宽高不变,找出最适合的分辨比率
  107. //2. 根据摄像头的分辨率,来重新修改组件的宽和高。
  108. this.cid = id;
  109. let type = 2; //1 组件宽高不变,找出最适合的分辨比率 2根据摄像头的分辨率,来重新修改组件的宽和高。
  110. this.$refs.camera.getSupportedPreviewSizesByCameraId({
  111. cameraId: id
  112. }, (data) => {
  113. let bestHeight_index = 0;
  114. //方法1 组件宽高不变,找出最适合的分辨比率,这样只能减少拉伸,不能解决
  115. if (type == 1) {
  116. let r0 = (this.height * 1.0) / this.width;
  117. let diff = 1000;
  118. for (var i = 0; i < data.sizes.length; i++) {
  119. console.log(data.sizes[i]);
  120. //找出最合适的分辨率,宽高比例尽量接近组件的(摄像头宽高是反着呢)
  121. let tmp = Math.abs(r0 - (data.sizes[i].width * 1.0) / data.sizes[bestHeight_index]
  122. .width);
  123. if (tmp < diff) {
  124. diff = tmp;
  125. bestHeight_index = i;
  126. }
  127. }
  128. //方法2 先获取摄像头 支持的分辨率,然后修改组件的大小(一般保持宽不变),这样肯定不拉伸
  129. } else if (type == 2) {
  130. for (var i = 0; i < data.sizes.length; i++) {
  131. //找出最大高度对应的分辨比率(宽高是反着呢)
  132. if (data.sizes[i].width > data.sizes[bestHeight_index].width) {
  133. bestHeight_index = i;
  134. }
  135. }
  136. //this.width = data.sizes[max_index].height;
  137. //计算出当前宽度下最适合的高度,然后修改组件的高
  138. this.height = ((data.sizes[bestHeight_index].width / (data.sizes[bestHeight_index].height *
  139. 1.0)) * 750) + "rpx";
  140. // 3 用最大分辨比率,不考虑拉伸
  141. } else if (type == 3) {
  142. bestHeight_index = 0; //最大的一般是第一个,你也可以遍历后找出最大分辨比率
  143. }
  144. //延迟打开摄像头,组件大小修改需要时间
  145. const that = this;
  146. setTimeout(() => {
  147. this.$refs.camera.openCamera({
  148. cameraId: id,
  149. previewWidth: data.sizes[bestHeight_index].width,
  150. previewHeight: data.sizes[bestHeight_index].height
  151. }, function(data) {
  152. setTimeout(function() {
  153. that.takePicture();
  154. }, 800);
  155. });
  156. }, 500);
  157. });
  158. return;
  159. },
  160. switchCamera() {
  161. this.cameraId == 0 ? this.cameraId = 1 : this.cameraId = 0
  162. this.$refs.camera.switchCamera({
  163. cameraId: this.cameraId
  164. }, function(data) {
  165. uni.showModal({
  166. content: JSON.stringify(data)
  167. })
  168. });
  169. },
  170. start() {
  171. this.$refs.camera.startRecord({
  172. videoEncodingBitRate: 3 * 1920 * 1080
  173. });
  174. },
  175. stop() {
  176. this.$refs.camera.stopRecord(function(data) {
  177. //可以通过 uni.compressVideo 压缩视频 记得前缀 file://
  178. uni.showModal({
  179. content: JSON.stringify(data)
  180. })
  181. });
  182. },
  183. release() {
  184. this.$refs.camera.release();
  185. },
  186. flashOpen() {
  187. this.$refs.camera.flashOpen();
  188. },
  189. flashClose() {
  190. this.$refs.camera.flashClose();
  191. },
  192. changeSize() {
  193. this.height = "1100rpx";
  194. },
  195. getSupportedPreviewSizes() {
  196. this.$refs.camera.getSupportedPreviewSizes((ret) => {
  197. uni.showModal({
  198. content: JSON.stringify(ret)
  199. })
  200. });
  201. }
  202. }
  203. }
  204. </script>
  205. <style scoped>
  206. .main {
  207. position: relative;
  208. width: 750rpx;
  209. flex: 1;
  210. overflow: hidden;
  211. }
  212. .operate-bg {
  213. width: 750rpx;
  214. background-color: #000000;
  215. position: absolute;
  216. left: 0;
  217. bottom: 0;
  218. justify-content: center;
  219. align-items: center;
  220. padding-top: 60rpx;
  221. padding-bottom: 60rpx;
  222. }
  223. .operate {
  224. width: 140rpx;
  225. height: 140rpx;
  226. background-color: #fff;
  227. border-radius: 50%;
  228. border-width: 10rpx;
  229. border-style: solid;
  230. border-color: #453f3f;
  231. }
  232. .change {
  233. width: 96rpx;
  234. height: 96rpx;
  235. background-color: #453f3f;
  236. position: absolute;
  237. right: 60rpx;
  238. bottom: 82rpx;
  239. border-radius: 48rpx;
  240. align-items: center;
  241. justify-content: center;
  242. }
  243. .change-img {
  244. width: 56rpx;
  245. height: 56rpx;
  246. }
  247. .portrait-frame {
  248. width: 750rpx;
  249. flex: 1;
  250. position: absolute;
  251. top: 0;
  252. left: 0;
  253. right: 0;
  254. bottom: 0;
  255. align-items: center;
  256. justify-content: center;
  257. background-color: rgba(0, 0, 0, 0);
  258. }
  259. .pf-img {
  260. width: 633.6rpx;
  261. height: 582rpx;
  262. margin-top: -280rpx;
  263. }
  264. .epiiCamer {
  265. position: absolute;
  266. top: 0;
  267. right: 0;
  268. bottom: 0;
  269. left: 0;
  270. }
  271. </style>