upload_avatar.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <template>
  2. <view>
  3. <okingtz-cropper @uploadSuccess="uploadSuccess"></okingtz-cropper>
  4. </view>
  5. </template>
  6. <script>
  7. // 1.引入项目
  8. import OkingtzCropper from '@/uni_modules/okingtz-cropper/components/okingtz-cropper/okingtz-cropper'
  9. export default {
  10. components:{
  11. //2.使用组件
  12. OkingtzCropper
  13. },
  14. data() {
  15. return {
  16. base_url: "",
  17. info:{}
  18. };
  19. },
  20. onLoad(option) {
  21. this.base_url = option.base_url
  22. // 获取个人信息
  23. this.$api.user_getUinfo({
  24. staff_num: uni.getStorageSync('user').staff_num
  25. }).then((res) => {
  26. this.info = res.data.data
  27. })
  28. },
  29. methods: {
  30. // 3.定义自己的回调函数
  31. uploadSuccess(tempFilePath){
  32. uni.showLoading({
  33. mask:true
  34. })
  35. // 4.根据自己的业务场景处理tempFilePath ;接口保存,或者上传至云空间
  36. console.log('tempFilePath_->',tempFilePath)
  37. uni.uploadFile({
  38. url: this.base_url + "/worksheet/design/up_images",
  39. header: {
  40. 'Authorization': uni.getStorageSync('token_type') +' '+uni.getStorageSync('Authorization')
  41. },
  42. filePath: tempFilePath,
  43. name: 'file',
  44. // formData只有H5存在
  45. formData: {
  46. image: tempFilePath
  47. },
  48. success: (uploadFileRes) => {
  49. console.log(JSON.parse(uploadFileRes.data));
  50. const data = JSON.parse(uploadFileRes.data).data
  51. if (!this.info.avatar) {
  52. this.info.avatar = data.path
  53. } else {
  54. this.info.avatar = data.path
  55. }
  56. console.log(this.info.avatar)
  57. this.$api.user_updateUserMessage({
  58. avatar: this.info.avatar
  59. }).then((res) => {
  60. console.log(res)
  61. uni.hideLoading()
  62. uni.showToast({
  63. icon: "none",
  64. title: "更换成功"
  65. })
  66. setTimeout(()=>{
  67. uni.navigateBack()
  68. },1000)
  69. })
  70. }
  71. })
  72. }
  73. }
  74. }
  75. </script>