setImg.vue 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <template>
  2. <view class="content">
  3. <view @click="editImg" class="headbox dffd">
  4. <image :src="userInfo.userInfo.avatar" mode=""></image>
  5. <text class="promain">更改头像</text>
  6. </view>
  7. </view>
  8. </template>
  9. <script>
  10. import { mapState, mapActions } from 'vuex';
  11. import { pathToBase64 } from '@/common/tool/pathToBase64.js';
  12. export default {
  13. data() {
  14. return {
  15. head_img: ''
  16. };
  17. },
  18. computed: {
  19. ...mapState({ userInfo: state => state.userInfo })
  20. },
  21. onLoad() {},
  22. methods: {
  23. //上传头像
  24. upload(id) {
  25. this.$api
  26. .request('user/changeAvatar', {
  27. avatar: id || ''
  28. })
  29. .then(data => {
  30. this.$api.toast(data.msg);
  31. if (data.code == 200) {
  32. setTimeout(res => {
  33. this.$store.dispatch('getUserInfo');
  34. }, 800);
  35. } else {
  36. }
  37. });
  38. },
  39. //修改头像
  40. editImg() {
  41. let that = this;
  42. uni.chooseImage({
  43. count: 1, //默认9
  44. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  45. success: function(res) {
  46. console.log(res);
  47. var imageSrc = res.tempFilePaths[0];
  48. pathToBase64(imageSrc).then(base64 => {
  49. that.$api
  50. .request('common/doupload', {
  51. imgdata: base64
  52. })
  53. .then(res => {
  54. if (res.code == 200) {
  55. that.upload(res.data.img_id);
  56. } else {
  57. that.$api.toast(res.msg);
  58. }
  59. });
  60. });
  61. }
  62. });
  63. }
  64. }
  65. };
  66. </script>
  67. <style lang="scss">
  68. .content {
  69. min-height: 100vh;
  70. padding-top: 100rpx;
  71. background-color: #fff;
  72. .headbox {
  73. align-items: center;
  74. image {
  75. width: 421rpx;
  76. height: 421rpx;
  77. background-color: #fefacb;
  78. border-radius: 50%;
  79. margin-bottom: 167rpx;
  80. }
  81. .promain {
  82. width: 307rpx;
  83. height: 65rpx;
  84. line-height: 65rpx;
  85. text-align: center;
  86. background: #0bb291;
  87. border-radius: 33rpx;
  88. font-size: 30rpx;
  89. color: #fff;
  90. }
  91. }
  92. }
  93. </style>