profile.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <view class="u-m-20">
  3. <u-navbar :border-bottom="false" :is-back="true" title="个人信息"></u-navbar>
  4. <view>
  5. <u-cell-group>
  6. <u-cell-item :arrow="true" hover-class="none" title="头像" @click="updateAvatar">
  7. <u-avatar :src="pic" size="100"></u-avatar>
  8. </u-cell-item>
  9. </u-cell-group>
  10. <u-cell-group>
  11. <u-cell-item :arrow="true" hover-class="none" title="昵称" @click="updateName">
  12. {{ vuex_user.user.nickName }}
  13. </u-cell-item>
  14. </u-cell-group>
  15. </view>
  16. <u-modal ref="uModal" v-model="showModel" :async-close="true" title="设置昵称"
  17. @confirm="confirmName">
  18. <view class="slot-content">
  19. <u-input v-model="nickName" :border="false" placeholder="请输入昵称" type="text"/>
  20. </view>
  21. </u-modal>
  22. <!-- <view class="u-m-t-20">
  23. <u-button type="primary" @click="subProfile">提交</u-button>
  24. </view> -->
  25. <!-- 如果是微信登录小程序,则获取用户的昵称与头像 -->
  26. <!-- #ifdef MP-WEIXIN -->
  27. <!-- <u-button type="default">使用微信头像与昵称</u-button> -->
  28. <!-- #endif -->
  29. </view>
  30. </template>
  31. <script>
  32. import config from "@/common/config.js" // 全局配置文件
  33. export default {
  34. data() {
  35. return {
  36. pic: uni.getStorageSync('lifeData').vuex_user.user.avatar.includes(config.staticUrl) ? uni.getStorageSync('lifeData').vuex_user.user.avatar : config.staticUrl + uni.getStorageSync('lifeData').vuex_user.user.avatar,
  37. nickName: null,
  38. showModel: false,
  39. }
  40. },
  41. methods: {
  42. updateName() {
  43. this.showModel = true;
  44. },
  45. confirmName() {
  46. if (!this.nickName) {
  47. this.showModel = false;
  48. return this.$mytip.toast('请输入昵称')
  49. }
  50. let url = "api/profile/updateProfile";
  51. let vuex_user = uni.getStorageSync('lifeData').vuex_user
  52. let user = vuex_user.user
  53. let userId = user.userId;
  54. this.$u.put(url, {
  55. userId: userId,
  56. nickName: this.nickName,
  57. }).then(data => {
  58. // 关闭
  59. user.nickName = this.nickName
  60. this.$u.vuex('vuex_user', vuex_user);
  61. this.showModel = false;
  62. this.$mytip.toast('昵称修改成功')
  63. });
  64. },
  65. updateAvatar() {
  66. this.$u.route('/pages/profile/avatar')
  67. },
  68. subProfile() {
  69. // 1.更新vuex中的用户信息
  70. this.$mytip.toast('修改成功')
  71. // 2.页面跳转
  72. uni.switchTab({
  73. url: '/pages/center/center'
  74. })
  75. },
  76. getUserProfile() {
  77. // 如果是微信登录小程序,则获取用户的昵称与头像
  78. // #ifdef MP-WEIXIN
  79. // 此处执行微信才执行
  80. // #endif
  81. var that = this;
  82. uni.getUserProfile({
  83. desc: '获取您的微信信息以授权小程序',
  84. lang: 'zh_CN',
  85. success: UserProfileRes => {
  86. console.log(UserProfileRes)
  87. uni.login({
  88. provider: 'weixin',
  89. success: function (loginRes) {
  90. let avatarUrl = UserProfileRes.userInfo.avatarUrl; //用户头像
  91. let nickName = UserProfileRes.userInfo.nickName; //用户微信名
  92. let gender = UserProfileRes.userInfo.gender;//性别
  93. console.log(avatarUrl);
  94. // 去修改用户的昵称与头像
  95. },
  96. fail(err) {
  97. console.log(err)
  98. }
  99. });
  100. },
  101. fail: err => {
  102. console.log(err)
  103. }
  104. })
  105. }
  106. }
  107. }
  108. </script>
  109. <style lang="scss" scoped>
  110. .slot-content {
  111. padding: 40 rpx;
  112. }
  113. </style>