upload-face.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. // uni.showToast({
  69. // duration: 2000,
  70. // title: result.msg,
  71. // icon: 'error'
  72. // })
  73. }
  74. },
  75. fail: (res) => reject(res.errMsg),
  76. })
  77. },
  78. async getFacePic() {
  79. uni.showLoading({
  80. mask: true,
  81. title: '正在加载',
  82. icon: "none"
  83. })
  84. let {
  85. data: res
  86. } = await uni.$http.get(`/api/v1/test/user/facePic/${this.msg.userId}`)
  87. console.log(res);
  88. uni.hideLoading()
  89. if (res.code == 200) {
  90. this.pic = 'data:image/jpg;base64,' + res.data
  91. console.log(this.pic);
  92. } else {
  93. this.$u.toast(`人脸未上传,请上传照片`)
  94. }
  95. },
  96. async deleteFace() {
  97. const {
  98. data: result
  99. } = await uni.$http.delete(`/api/v1/test/user/face/${this.msg.userId}`)
  100. console.log(this.msg.userId);
  101. if (result.code === 200) {
  102. uni.showToast({
  103. duration: 1500,
  104. title: result.data,
  105. icon: 'none'
  106. })
  107. this.pic = ''
  108. } else {
  109. uni.$showMsg('删除失败')
  110. }
  111. }
  112. },
  113. onLoad() {
  114. this.getFacePic()
  115. }
  116. }
  117. </script>
  118. <style lang="scss">
  119. .camera {
  120. display: flex;
  121. justify-content: center;
  122. align-items: center;
  123. margin-top: 20px;
  124. }
  125. .pic {
  126. margin: 100px auto;
  127. border: 1px solid #3c9cff;
  128. width: 500rpx;
  129. height: 300px;
  130. display: flex;
  131. justify-content: center;
  132. align-items: center;
  133. .icon {}
  134. img {
  135. width: 100%;
  136. height: 100%;
  137. }
  138. }
  139. .upload {
  140. display: inline-block;
  141. position: fixed;
  142. bottom: 0;
  143. }
  144. </style>