index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <template>
  2. <view>
  3. <!-- #ifdef MP-ALIPAY -->
  4. <u-navbar :placeholder="true" bgColor="#fff" :autoBack="true" :border="true" title="个人信息"
  5. leftIconSize="0"></u-navbar>
  6. <!-- #endif -->
  7. <!-- #ifdef MP-WEIXIN -->
  8. <u-navbar :placeholder="true" bgColor="#fff" :autoBack="true" :border="true" title="个人信息"></u-navbar>
  9. <!-- #endif -->
  10. <!-- #ifndef MP-WEIXIN || MP-ALIPAY -->
  11. <view v-if="pagesNum > 1">
  12. <u-navbar :placeholder="true" bgColor="#fff" :autoBack="true" :border="true" title="个人信息" />
  13. </view>
  14. <view v-else>
  15. <u-navbar leftIconSize="0" :placeholder="true" bgColor="#fff" :border="true" title="个人信息">
  16. <view class="nav-left flex" slot="left" @click="$toIndex()">
  17. <u-icon name="arrow-left" size="20" color="#333"></u-icon>
  18. </view>
  19. </u-navbar>
  20. </view>
  21. <!-- #endif -->
  22. <view class="wrap">
  23. <view class="wrap-avatar flex">
  24. <view class="title">头像</view>
  25. <!-- <view><u-avatar :src="avatarUrl"></u-avatar></view> -->
  26. <!-- #ifdef H5 -->
  27. <button type="button" class="button" @click="onChooseImage">
  28. <u-avatar :src="avatarUrl+ '?imageView2/2/w/80'" mode="aspectFill"></u-avatar>
  29. </button>
  30. <!-- #endif -->
  31. <!-- #ifndef H5 -->
  32. <button type="balanced" class="button" open-type="chooseAvatar" @chooseavatar="onChooseavatar">
  33. <u-avatar :src="avatarUrl" mode="aspectFill"></u-avatar>
  34. </button>
  35. <!-- #endif -->
  36. </view>
  37. <view class="wrap-avatar flex">
  38. <view class="title">昵称</view>
  39. <view class="setName">
  40. <!-- <u-input v-model="userInfo.nickName" placeholder="请设置昵称" inputAlign="right" border="none" /> -->
  41. <input v-model="userInfo.nickName" class="input" type="nickname" placeholder="请设置昵称" />
  42. </view>
  43. </view>
  44. <!-- #ifdef MP-WEIXIN -->
  45. <view class="tip">建议使用您的微信头像与昵称</view>
  46. <!-- #endif -->
  47. <view class="btn">
  48. <u-button type="primary" text="确定" @click="setAvatar" shape="circle"></u-button>
  49. </view>
  50. </view>
  51. </view>
  52. </template>
  53. <script>
  54. import env from '../../config/env.js'
  55. import $http from '@/utils/request.js'
  56. import appId from '@/config/appId.js'
  57. export default {
  58. data() {
  59. return {
  60. avatarUrl: '', //展示用
  61. avatar: '', //上传用
  62. userInfo: {},
  63. pagesNum: '',
  64. }
  65. },
  66. onShow() {
  67. this.pagesNum = getCurrentPages().length
  68. this.getInfo()
  69. // this.userInfo = uni.getStorageSync('userInfo')
  70. // this.avatarUrl = env.filePublic + this.userInfo.avatar
  71. // this.avatar = this.userInfo.avatar
  72. },
  73. methods: {
  74. // 我的信息
  75. getInfo() {
  76. uni.showLoading({
  77. title: '加载中'
  78. });
  79. $http.post('/api/v1/mp/user/getLoginUserinfo', { appSource: appId }).then(res => {
  80. uni.hideLoading();
  81. if (res.code == 0) {
  82. this.userInfo = res.data
  83. this.avatarUrl = env.filePublic + res.data.avatar
  84. this.avatar = res.data.avatar
  85. }
  86. }).catch(() => {
  87. uni.hideLoading();
  88. })
  89. },
  90. setAvatar() {
  91. uni.showLoading({
  92. title: '加载中'
  93. });
  94. let data = {
  95. nickName: this.userInfo.nickName, //昵称
  96. userId: this.userInfo.userId, //用户id
  97. avatar: this.avatar, //文件地址
  98. }
  99. $http.post('/api/v1/mp/user/mine/updateUserInfo', data).then(res => {
  100. uni.hideLoading();
  101. if (res.code == 0) {
  102. $http.post('/api/v1/mp/user/getLoginUserinfo', { appSource: appId }).then(res => {
  103. if (res.code == 0) {
  104. uni.setStorageSync('userInfo', res.data)
  105. this.toUserIndex()
  106. }
  107. })
  108. }
  109. }).catch(() => {
  110. uni.hideLoading();
  111. })
  112. },
  113. onChooseImage() {
  114. let _this = this
  115. uni.chooseImage({ //选择本地图片
  116. count: 1, //默认9
  117. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  118. sourceType: ['album'], //从相册选择
  119. success: (res) => {
  120. _this.avatarUrl = res.tempFilePaths[0] // 将选择的图片路径预览
  121. $http.upload(res.tempFilePaths[0]).then(res2 => {
  122. uni.hideLoading();
  123. if (res2.code == 0) {
  124. this.avatarUrl = res.tempFilePaths[0]
  125. this.avatar = res2.data.fileName
  126. }
  127. }).catch(() => {
  128. uni.hideLoading();
  129. })
  130. }
  131. });
  132. },
  133. onChooseavatar(e) {
  134. uni.showLoading({
  135. title: '加载中'
  136. });
  137. $http.upload(e.detail.avatarUrl).then(res => {
  138. uni.hideLoading();
  139. if (res.code == 0) {
  140. this.avatarUrl = e.detail.avatarUrl
  141. this.avatar = res.data.fileName
  142. }
  143. }).catch(() => {
  144. uni.hideLoading();
  145. })
  146. },
  147. toUserIndex() {
  148. uni.switchTab({
  149. url: `/pages/user/index`
  150. })
  151. },
  152. }
  153. }
  154. </script>
  155. <style lang="scss" scoped>
  156. .wrap {
  157. min-height: calc(100vh - 64px - var(--status-bar-height));
  158. background-color: #FFFFFF;
  159. padding: 15px;
  160. &-avatar {
  161. justify-content: space-between;
  162. border-bottom: 1px solid rgba(102, 102, 102, 0.12);
  163. line-height: 50px;
  164. .title {
  165. color: #666;
  166. font-size: 30rpx
  167. }
  168. .setName {
  169. .input {
  170. text-align: right;
  171. width: 600rpx;
  172. line-height: 100rpx;
  173. height: 100rpx;
  174. }
  175. }
  176. .button {
  177. border: none;
  178. border-radius: 50%;
  179. background-color: #fff;
  180. padding: 0;
  181. margin: 0;
  182. }
  183. }
  184. &-avatar:first-child {
  185. margin-bottom: 10px;
  186. }
  187. .tip {
  188. margin: 5px 0;
  189. font-size: 24rpx;
  190. color: #999;
  191. }
  192. .btn {
  193. ::v-deep .u-button {
  194. margin-top: 80rpx;
  195. border: none;
  196. color: #fff;
  197. background: $uni-text-color;
  198. }
  199. }
  200. }
  201. </style>