information.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. success: res => {
  66. let data = JSON.parse(res.data)
  67. this.saveSub(data.data.path)
  68. },
  69. fail: err => {
  70. uni.showToast({
  71. title: '上传头像失败'
  72. })
  73. }
  74. })
  75. },
  76. saveSub(avatar) {
  77. let _this = this;
  78. _this.$ajax.get(
  79. `${SaveUserInfo}?nickname=${this.nickname}&avatar=${avatar}`
  80. ).then(([, {
  81. data: res
  82. }]) => {
  83. if (res.code == 200) {
  84. uni.navigateTo({
  85. url: '../index/index'
  86. })
  87. uni.setStorageSync('nickName', this.nickname)
  88. uni.setStorageSync('avatar', this.avatarUrl)
  89. }
  90. })
  91. }
  92. }
  93. }
  94. </script>
  95. <style lang="scss">
  96. .info {
  97. width: 100%;
  98. height: 100%;
  99. margin: 0 auto;
  100. }
  101. .avatar_box {
  102. margin: 100rpx 0 40rpx 0;
  103. button {
  104. margin: 0;
  105. padding: 0;
  106. outline: none;
  107. border-radius: 0;
  108. background-color: transparent;
  109. }
  110. button::after {
  111. border: none;
  112. }
  113. .avatar_btn {
  114. .avatar {
  115. width: 240rpx;
  116. height: 240rpx;
  117. border-radius: 16rpx;
  118. }
  119. }
  120. .avatar_btn:active {
  121. outline: none;
  122. box-shadow: none;
  123. }
  124. }
  125. .name_box {
  126. width: 100%;
  127. display: flex;
  128. just-content: flex-start;
  129. align-items: center;
  130. border: 2rpx solid #efefef;
  131. height: 120rpx;
  132. padding: 0 24rpx;
  133. box-sizing: border-box;
  134. line-height: 120rpx;
  135. view {
  136. font-size: 34rpx;
  137. font-weight: bold;
  138. margin-right: 24rpx;
  139. }
  140. input {
  141. width: 80%;
  142. height: 100%;
  143. font-size: 34rpx;
  144. }
  145. }
  146. .btn_box {
  147. width: 560rpx;
  148. height: 88rpx;
  149. line-height: 88rpx;
  150. text-align: center;
  151. font-size: 34rpx;
  152. font-weight: bold;
  153. background: linear-gradient(93deg, #F30000 0%, #FE4815 100%);
  154. opacity: 1;
  155. border-radius: 44rpx;
  156. color: #fff;
  157. margin: 60rpx auto 0;
  158. }
  159. </style>