set_avatar.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <template>
  2. <view class="containar">
  3. <view class="infoList">
  4. <text>头像</text>
  5. <view class="avatarUrl">
  6. <button type="balanced" open-type="chooseAvatar" @chooseavatar="onChooseavatar">
  7. <image :src="avatarUrl?avatarUrl:'../../static/ucenter/big-avatar.png'" class="refreshIcon"></image>
  8. </button>
  9. <view class="dayu" style="line-height: 110rpx;"><image src="../../static/ucenter/dayu.png"></image></view>
  10. </view>
  11. </view>
  12. <view class="infoList">
  13. <text style="width: 400rpx;">昵称</text>
  14. <input type="nickname" class="weui-input" maxlength="10" :value="nickName" @blur="bindblur" placeholder="请输入昵称"
  15. @input="bindinput" />
  16. <view class="dayu" style="padding-top: 10rpx;"><image src="../../static/ucenter/dayu.png"></image></view>
  17. </view>
  18. <view class="infoList">
  19. <text>手机号码</text>
  20. <view class="lastText">{{userinfo.telephone}} 不可修改</view>
  21. </view>
  22. <view class="infoList">
  23. <text>加入智小狸</text>
  24. <view class="lastText">{{days}}天</view>
  25. </view>
  26. <view class="btn">
  27. <view class="btn-sub" @click="onSubmit">保存信息</view>
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. import {pathToBase64} from 'image-tools'
  33. export default {
  34. data() {
  35. return {
  36. avatarUrl: '',
  37. nickName: '小狸',
  38. imgBase:'',
  39. days:null,
  40. userinfo: this.$store.state.users,
  41. };
  42. },
  43. onShow() {
  44. this.getUserDays()
  45. this.getUseravatar()
  46. },
  47. onLoad(option) {},
  48. methods: {
  49. getUseravatar(){
  50. if(this.$store.state.users.nickName){
  51. this.nickName=this.$store.state.users.nickName
  52. }
  53. if(this.$store.state.users.avatar){
  54. this.avatarUrl=this.$store.state.users.avatar
  55. }
  56. },
  57. //获取加入天数
  58. getUserDays() {
  59. this.$http.getDays().then(res => {
  60. if (res.code === 0) {
  61. this.days = res.data
  62. } else {
  63. this.$utils.msg(res.msg);
  64. }
  65. })
  66. },
  67. bindblur(e) {
  68. this.nickName = e.detail.value; // 获取微信昵称
  69. },
  70. bindinput(e) {
  71. this.nickName = e.detail.value; //这里要注意如果只用blur方法的话用户在输入玩昵称后直接点击保存按钮,会出现修改不成功的情况。
  72. },
  73. onChooseavatar(e) {
  74. this.avatarUrl=e.detail.avatarUrl
  75. pathToBase64(e.detail.avatarUrl).then(res=>{
  76. this.imgBase=res
  77. })
  78. },
  79. onSubmit() {
  80. if (this.nickName === '小狸' && this.avatarUrl === '') {
  81. uni.showToast({
  82. icon: 'none',
  83. title: '没有更改,勿提交'
  84. })
  85. return false;
  86. } else {
  87. let params = {
  88. nickName: this.nickName,
  89. avatar: this.imgBase
  90. }
  91. this.$store.state.users.avatar = this.avatarUrl
  92. this.$store.state.users.nickName = this.nickName
  93. this.$store.commit("UPDATEUSERS", this.$store.state.users)
  94. this.$http.wxLogin(params).then(res=>{
  95. if(res.code === 0){
  96. uni.switchTab({
  97. url:'/pages/ucenter/index'
  98. })
  99. }else{
  100. this.$utils.msg(res.msg);
  101. }
  102. }).catch(error=>{
  103. this.$utils.msg(error);
  104. });
  105. }
  106. }
  107. }
  108. }
  109. </script>
  110. <style lang="scss" scoped>
  111. .containar {
  112. height: 100vh;
  113. background: #fff;
  114. display: flex;
  115. flex-direction: column;
  116. padding: 0 30rpx;
  117. padding-top: 30rpx;
  118. .infoList{
  119. display: flex;
  120. width: 100%;
  121. justify-content: space-between;
  122. border-bottom: #F2F3F5 solid 1px;
  123. padding: 30rpx 0rpx;
  124. align-items: center;
  125. text{
  126. font-weight: 600;
  127. color: #1D2129;
  128. line-height: 35rpx;
  129. font-size: 30rpx;
  130. font-family: PingFang SC-Regular, PingFang SC;
  131. }
  132. .dayu image{
  133. width: 36rpx;
  134. height: 36rpx;
  135. }
  136. .lastText{
  137. color: #C9CDD3;
  138. padding-right: 35rpx;
  139. }
  140. }
  141. .avatarUrl {
  142. display: flex;
  143. justify-content: center;
  144. justify-items: center;
  145. button {
  146. background: #fff;
  147. line-height: 80rpx;
  148. height: 110rpx;
  149. margin: 0;
  150. display: flex;
  151. justify-content: center;
  152. align-items: center;
  153. border: none;
  154. .refreshIcon {
  155. height: 110rpx;
  156. width: 110rpx;
  157. border-radius: 50%;
  158. }
  159. }
  160. button::after{
  161. border: none;
  162. }
  163. }
  164. .weui-input {
  165. padding-left: 60rpx;
  166. color: #86909C;
  167. font-weight: 500;
  168. text-align: right;
  169. }
  170. .btn {
  171. width: 100%;
  172. padding: 0 20rpx;
  173. box-sizing: border-box;
  174. .btn-sub {
  175. margin: 80rpx auto 0;
  176. height: 90rpx;
  177. background: #58be6b;
  178. border-radius: 45rpx;
  179. line-height: 90rpx;
  180. text-align: center;
  181. font-size: 36rpx;
  182. color: #fff;
  183. }
  184. }
  185. }
  186. </style>