set_avatar.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <view class="containar">
  3. <view class="avatarUrl">
  4. <button type="balanced" open-type="chooseAvatar" @chooseavatar="onChooseavatar">
  5. <image :src="avatarUrl?avatarUrl:'../../static/ucenter/big-avatar.png'" class="refreshIcon"></image>
  6. </button>
  7. </view>
  8. <view class="nickname">
  9. <text>昵称:</text>
  10. <input type="nickname" class="weui-input" maxlength="10" :value="nickName" @blur="bindblur" placeholder="请输入昵称"
  11. @input="bindinput" />
  12. </view>
  13. <view class="btn">
  14. <view class="btn-sub" @click="onSubmit">保存</view>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. import {pathToBase64} from 'image-tools'
  20. export default {
  21. data() {
  22. return {
  23. avatarUrl: '',
  24. nickName: '小狸',
  25. imgBase:'',
  26. };
  27. },
  28. onLoad(option) {},
  29. methods: {
  30. bindblur(e) {
  31. this.nickName = e.detail.value; // 获取微信昵称
  32. },
  33. bindinput(e) {
  34. this.nickName = e.detail.value; //这里要注意如果只用blur方法的话用户在输入玩昵称后直接点击保存按钮,会出现修改不成功的情况。
  35. },
  36. onChooseavatar(e) {
  37. this.avatarUrl=e.detail.avatarUrl
  38. pathToBase64(e.detail.avatarUrl).then(res=>{
  39. this.imgBase=res
  40. })
  41. },
  42. onSubmit() {
  43. if (this.nickName === '小狸' && this.avatarUrl === '') {
  44. uni.showToast({
  45. icon: 'none',
  46. title: '没有更改,勿提交'
  47. })
  48. return false;
  49. } else {
  50. let params = {
  51. nickName: this.nickName,
  52. avatar: this.imgBase
  53. }
  54. this.$store.state.users.avatar = this.avatarUrl
  55. this.$store.state.users.nickName = this.nickName
  56. this.$store.commit("UPDATEUSERS", this.$store.state.users)
  57. this.$http.wxLogin(params).then(res=>{
  58. if(res.code === 0){
  59. uni.navigateBack({delta:1})
  60. }else{
  61. this.$utils.msg(res.msg);
  62. }
  63. }).catch(error=>{
  64. this.$utils.msg(error);
  65. });
  66. }
  67. }
  68. }
  69. }
  70. </script>
  71. <style lang="scss" scoped>
  72. .containar {
  73. height: 100vh;
  74. background: #fff;
  75. .avatarUrl {
  76. display: flex;
  77. justify-content: center;
  78. justify-items: center;
  79. padding-top: 10vh;
  80. button {
  81. background: #fff;
  82. line-height: 80rpx;
  83. height: 150rpx;
  84. width: 160rpx;
  85. margin: 0;
  86. display: flex;
  87. justify-content: center;
  88. align-items: center;
  89. .refreshIcon {
  90. height: 110rpx;
  91. width: 110rpx;
  92. border-radius: 50%;
  93. }
  94. }
  95. }
  96. .nickname {
  97. background: #fff;
  98. padding: 30rpx;
  99. margin: 0rpx 20rpx;
  100. display: flex;
  101. align-items: center;
  102. justify-content: center;
  103. margin-top: 50rpx;
  104. border: #cccccc solid 1px;
  105. border-radius: 10rpx;
  106. .weui-input {
  107. padding-left: 60rpx;
  108. }
  109. }
  110. .btn {
  111. width: 100%;
  112. padding: 0 20rpx;
  113. box-sizing: border-box;
  114. .btn-sub {
  115. margin: 80rpx auto 0;
  116. height: 90rpx;
  117. background: #58be6b;
  118. border-radius: 45rpx;
  119. line-height: 90rpx;
  120. text-align: center;
  121. font-size: 36rpx;
  122. color: #fff;
  123. }
  124. }
  125. }
  126. </style>