upload-face.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. <u-toast ref="uToast"></u-toast>
  11. </view>
  12. </template>
  13. <script>
  14. export default {
  15. data() {
  16. return {
  17. bgColor: '#3c9cff',
  18. pic: '',
  19. msg: {
  20. userId: uni.getStorageSync('userId'),
  21. facePic: ""
  22. },
  23. }
  24. },
  25. methods: {
  26. takePhoto() {
  27. uni.chooseImage({
  28. count: 1, //选择图片的数量,默认9
  29. sizeType: ['compressed'], //原图、压缩图或者两者都有,可以指定原图或压缩图,默认两者都有
  30. sourceType: ['album', 'camera'], //从相册选择
  31. success: res => {
  32. const tempFilePaths = res.tempFilePaths; // 返回选定照片的本地文件路径列表
  33. console.log(tempFilePaths[0]);
  34. this.urlTobase64(tempFilePaths[0])
  35. }
  36. })
  37. },
  38. urlTobase64(url) {
  39. console.log('url', url);
  40. let that = this
  41. uni.getFileSystemManager().readFile({
  42. filePath: url, //照片的临时地址
  43. encoding: "base64", //编码格式
  44. success: async (res) => {
  45. let url = "data:image/jpg" + ";base64," + res.data //base64地址
  46. url = url.split(',')[1]
  47. that.msg.facePic = url
  48. uni.showLoading({
  49. mask: true,
  50. title: '图片上传中'
  51. })
  52. const {
  53. data: result
  54. } = await uni.$http.post('/api/v1/test/user/face', that.msg)
  55. uni.hideLoading()
  56. console.log(11, result);
  57. if (result.code == 200) {
  58. this.$u.toast(`人脸上传成功`)
  59. that.pic = 'data:image/jpeg;base64,' + url
  60. } else {
  61. // this.$u.toast(result.msg)
  62. this.$refs.uToast.show({
  63. type: 'error',
  64. icon: false,
  65. title: '失败主题',
  66. message: result.msg
  67. })
  68. }
  69. },
  70. fail: (res) => reject(res.errMsg),
  71. })
  72. },
  73. async getFacePic() {
  74. uni.showLoading({
  75. mask: true,
  76. title: '正在加载',
  77. icon: "none"
  78. })
  79. let {
  80. data: res
  81. } = await uni.$http.get(`/api/v1/test/user/facePic/${this.msg.userId}`)
  82. console.log(res);
  83. uni.hideLoading()
  84. if (res.code == 200) {
  85. this.pic = 'data:image/jpg;base64,' + res.data
  86. console.log(this.pic);
  87. } else {
  88. this.$u.toast(`人脸未上传,请上传照片`)
  89. }
  90. },
  91. async deleteFace() {
  92. const {
  93. data: result
  94. } = await uni.$http.delete(`/api/v1/test/user/face/${this.msg.userId}`)
  95. console.log(this.msg.userId);
  96. if (result.code === 200) {
  97. uni.showToast({
  98. duration: 1500,
  99. title: result.data,
  100. icon: 'none'
  101. })
  102. this.pic = ''
  103. } else {
  104. uni.$showMsg('删除失败')
  105. }
  106. }
  107. },
  108. onLoad() {
  109. this.getFacePic()
  110. }
  111. }
  112. </script>
  113. <style lang="scss">
  114. .camera {
  115. display: flex;
  116. justify-content: center;
  117. align-items: center;
  118. margin-top: 20px;
  119. }
  120. .pic {
  121. margin: 100px auto;
  122. border: 1px solid #3c9cff;
  123. width: 500rpx;
  124. height: 300px;
  125. display: flex;
  126. justify-content: center;
  127. align-items: center;
  128. .icon {}
  129. img {
  130. width: 100%;
  131. height: 100%;
  132. }
  133. }
  134. .upload {
  135. display: inline-block;
  136. position: fixed;
  137. bottom: 0;
  138. }
  139. </style>