index.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <template>
  2. <view class="p-t-30">
  3. <u-cell-group :customStyle="{backgroundColor:'white'}">
  4. <u-cell title="头像">
  5. <button slot="value" open-type="chooseAvatar" @chooseavatar="chooseAvatar">
  6. <view class="flex-dir-row flex-y-center">
  7. <u-avatar :src="face" size="60"/>
  8. <view class="m-l-10">
  9. <u-icon name="arrow-right" color="#c8c9cc"/>
  10. </view>
  11. </view>
  12. </button>
  13. </u-cell>
  14. <u-cell title="昵称">
  15. <view slot="value">
  16. <u--input v-model="nickName" type="nickname" color="#c8c9cc" inputAlign="right" border="none"/>
  17. </view>
  18. </u-cell>
  19. <!-- <u-cell>
  20. <view slot="title">
  21. <u-icon name="reload" :color="$u.color['primary']"
  22. label="随机头像" :labelColor="$u.color['primary']" @click="randomFace"/>
  23. </view>
  24. <view slot="value">
  25. <u-icon name="reload" :color="$u.color['primary']"
  26. label="随机昵称" :labelColor="$u.color['primary']" @click="randomName"/>
  27. </view>
  28. </u-cell> -->
  29. </u-cell-group>
  30. <view class="width-100 k-btn">
  31. <view class="p-30">
  32. <u-button text="保存" :color="edited?'black':'#c8c9cc'" :disabled="!edited" shape="circle" @click="save"/>
  33. </view>
  34. </view>
  35. <bt-timer ref="timer"/><!-- 一定要加ref=timer,mixins里会用到 -->
  36. </view>
  37. </template>
  38. <script>
  39. export default {
  40. mixins: [],
  41. data() {
  42. return {
  43. face: '',
  44. nickName: '',
  45. edited: false, //是否更改
  46. }
  47. },
  48. watch: {//监听头像或昵称任何一个发生变动
  49. face(newValue, oldValue) {
  50. if (oldValue && newValue != oldValue) {
  51. this.edited = true
  52. }
  53. },
  54. nickName(newValue, oldValue) {
  55. if (oldValue && newValue != oldValue) {
  56. this.edited = true
  57. }
  58. },
  59. },
  60. methods: {
  61. chooseAvatar(e) {
  62. this.$u.api.putFile(e.detail.avatarUrl).then(res => {
  63. this.face = res.data.link
  64. })
  65. },
  66. randomName() {
  67. this.$u.api.getRandomName().then(res => {
  68. this.nickName = res.data
  69. })
  70. },
  71. randomFace() {
  72. this.$u.api.getRandomAvatar().then(res => {
  73. this.face = res.data
  74. })
  75. },
  76. save() {
  77. if (!this.edited) {
  78. return
  79. }
  80. this.$u.api.saveNicknameAndAvatar(this.nickName, this.face).then(res => {
  81. this.$u.vuex('userInfo.avatar', this.face) //写入缓存
  82. this.$u.vuex('userInfo.nick_name', this.nickName) //写入缓存
  83. uni.$u.toast('保存成功')
  84. this.edited = false
  85. uni.$emit('refreshUser', this.face, this.nickName)//要特别调用父页面的方法,不然父页面不同步
  86. })
  87. },
  88. },
  89. onLoad() {
  90. this.face = this.userInfo.avatar || '/static/images/face.png'
  91. this.nickName = this.userInfo.nick_name || ''
  92. },
  93. }
  94. </script>
  95. <style>
  96. .k-btn {
  97. position: absolute;
  98. bottom: 30%;
  99. }
  100. button[open-type=chooseAvatar] {
  101. background-color: white;
  102. padding: 0;
  103. }
  104. </style>