information.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <template>
  2. <view class="info">
  3. <custom-nav :title="title" :to-back="toBack"></custom-nav>
  4. <view class="avatar_box">
  5. <button class="avatar_btn" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
  6. <image class="avatar" :src="avatarUrl"></image>
  7. </button>
  8. </view>
  9. <view class="name_box">
  10. <view>昵称</view>
  11. <input type="nickname" class="weui-input" placeholder="请输入昵称" v-model="nickname" @change="getVal" />
  12. </view>
  13. <view class="btn_box" @click="save">
  14. 保存
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. const defaultAvatarUrl =
  20. 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
  21. import { //引入 api
  22. SaveUserInfo,
  23. SaveUserAvatar
  24. } from '../../api.js'
  25. export default {
  26. data() {
  27. return {
  28. avatarUrl: defaultAvatarUrl, //用户头像
  29. nickname: '', //用户昵称
  30. title: '授权头像昵称'
  31. }
  32. },
  33. onLoad(ops) {
  34. if (ops.from) {
  35. this.title = '修改头像昵称'
  36. }
  37. },
  38. methods: {
  39. onChooseAvatar(e) {
  40. this.avatarUrl = e.detail.avatarUrl
  41. },
  42. getVal(e) {
  43. this.nickname = e.detail.value
  44. },
  45. save() {
  46. if (!this.avatarUrl) {
  47. uni.showToast({
  48. title: '头像不能为空',
  49. icon: 'none'
  50. })
  51. return
  52. }
  53. if (!this.nickname) {
  54. uni.showToast({
  55. title: '昵称不能为空',
  56. icon: 'none'
  57. })
  58. return
  59. }
  60. let _this = this
  61. uni.uploadFile({
  62. filePath: this.avatarUrl,
  63. name: 'file',
  64. url: `https://api.jiuweiyun.cn/api/user/SaveUserAvatar`, //服务器端接收图片的路径
  65. header: {
  66. type: 2
  67. },
  68. success: res => {
  69. let data = JSON.parse(res.data)
  70. this.saveSub(data.data.path)
  71. },
  72. fail: err => {
  73. uni.showToast({
  74. title: '上传头像失败'
  75. })
  76. }
  77. })
  78. },
  79. saveSub(avatar) {
  80. let _this = this;
  81. _this.$ajax.get(
  82. `${SaveUserInfo}?nickname=${this.nickname}&avatar=${avatar}`
  83. ).then(([, {
  84. data: res
  85. }]) => {
  86. if (res.code == 200) {
  87. uni.navigateTo({
  88. url: '../index/index'
  89. })
  90. uni.setStorageSync('nickName', this.nickname)
  91. uni.setStorageSync('avatar', this.avatarUrl)
  92. }
  93. })
  94. }
  95. }
  96. }
  97. </script>
  98. <style lang="scss">
  99. .info {
  100. width: 100%;
  101. height: 100%;
  102. margin: 0 auto;
  103. }
  104. .avatar_box {
  105. margin: 100rpx 0 40rpx 0;
  106. button {
  107. margin: 0;
  108. padding: 0;
  109. outline: none;
  110. border-radius: 0;
  111. background-color: transparent;
  112. }
  113. button::after {
  114. border: none;
  115. }
  116. .avatar_btn {
  117. .avatar {
  118. width: 240rpx;
  119. height: 240rpx;
  120. border-radius: 16rpx;
  121. }
  122. }
  123. .avatar_btn:active {
  124. outline: none;
  125. box-shadow: none;
  126. }
  127. }
  128. .name_box {
  129. width: 100%;
  130. display: flex;
  131. just-content: flex-start;
  132. align-items: center;
  133. border: 2rpx solid #efefef;
  134. height: 120rpx;
  135. padding: 0 24rpx;
  136. box-sizing: border-box;
  137. line-height: 120rpx;
  138. view {
  139. font-size: 34rpx;
  140. font-weight: bold;
  141. margin-right: 24rpx;
  142. }
  143. input {
  144. width: 80%;
  145. height: 100%;
  146. font-size: 34rpx;
  147. }
  148. }
  149. .btn_box {
  150. width: 560rpx;
  151. height: 88rpx;
  152. line-height: 88rpx;
  153. text-align: center;
  154. font-size: 34rpx;
  155. font-weight: bold;
  156. background: linear-gradient(93deg, #F30000 0%, #FE4815 100%);
  157. opacity: 1;
  158. border-radius: 44rpx;
  159. color: #fff;
  160. margin: 60rpx auto 0;
  161. }
  162. </style>