upload-face.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <template>
  2. <view class="">
  3. <view class="pic" @click="takePhoto">
  4. <u-icon v-if="!pic" class="icon" name="plus" color="#2979ff" size="50"></u-icon>
  5. <img v-else :src="pic" alt="">
  6. </view>
  7. <view class="upload" style="width: 100%;">
  8. <u-button color="red" @click="deleteFace" type="primary" size="large" text="删除人脸特征值"></u-button>
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. // import UploadPhoto from "@/components/upload-photo/upload-photo.vue"
  14. import api from "@/utils/api.js"
  15. export default {
  16. // components: {
  17. // UploadPhoto
  18. // },
  19. data() {
  20. return {
  21. bgColor: '#3c9cff',
  22. pic: '',
  23. msg: {
  24. userId: uni.getStorageSync('userId'),
  25. facePic: ""
  26. },
  27. }
  28. },
  29. onLoad() {
  30. },
  31. methods: {
  32. takePhoto() {
  33. uni.chooseImage({
  34. count: 1, //选择图片的数量,默认9
  35. sizeType: ['compressed'], //原图、压缩图或者两者都有,可以指定原图或压缩图,默认两者都有
  36. sourceType: ['album', 'camera'], //从相册选择
  37. success: res => {
  38. const tempFilePaths = res.tempFilePaths; // 返回选定照片的本地文件路径列表
  39. console.log(tempFilePaths[0]);
  40. this.urlTobase64(tempFilePaths[0])
  41. }
  42. })
  43. },
  44. urlTobase64(url) {
  45. console.log('url', url);
  46. let that = this
  47. uni.getFileSystemManager().readFile({
  48. filePath: url, //照片的临时地址
  49. encoding: "base64", //编码格式
  50. success: async (res) => {
  51. let url = "data:image/jpg" + ";base64," + res.data //base64地址
  52. url = url.split(',')[1]
  53. that.msg.facePic = url
  54. const {
  55. data: result
  56. } = await uni.$http.post('/api/v1/test/user/face', that.msg)
  57. console.log(result);
  58. that.pic = 'data:image/jpeg;base64,' + url
  59. },
  60. fail: (res) => reject(res.errMsg),
  61. })
  62. },
  63. async textUpload() {
  64. // if (res.code === 200) {
  65. // } else {
  66. // this.$u.toast(`用户未上传人脸,请上传人脸`)
  67. // }
  68. },
  69. async deleteFace() {
  70. const {
  71. data: res
  72. } = await uni.$http.get(`/api/v1/test/user/${this.msg.userId}/hasUploadFace`)
  73. if (res.code === 200) {
  74. const {
  75. data: result
  76. } = await uni.$http.delete(`/api/v1/test/user/face/${this.msg.userId}`)
  77. if (result.code === 200) {
  78. uni.showToast({
  79. duration:1500,
  80. title:result.data,
  81. icon:'none'
  82. })
  83. this.pic = ''
  84. } else {
  85. uni.$showMsg('删除失败')
  86. }
  87. } else {
  88. uni.$showMsg('人脸未添加')
  89. }
  90. }
  91. }
  92. }
  93. </script>
  94. <style lang="scss">
  95. .camera {
  96. display: flex;
  97. justify-content: center;
  98. align-items: center;
  99. margin-top: 20px;
  100. }
  101. .pic {
  102. margin: 100px auto;
  103. border: 1px solid #3c9cff;
  104. width: 500rpx;
  105. height: 300px;
  106. display: flex;
  107. justify-content: center;
  108. align-items: center;
  109. .icon {}
  110. img {
  111. width: 100%;
  112. height: 100%;
  113. }
  114. }
  115. .upload {
  116. display: inline-block;
  117. position: fixed;
  118. bottom: 0;
  119. }
  120. </style>