index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <view class="person-msg">
  3. <button class="head c-content" open-type="chooseAvatar" @chooseavatar="onChooseAvatar" @click="onClickAvatar">
  4. <image :src="form.avatarUrl || '/static/missing-face.png'"></image>
  5. <view>点击修改头像</view>
  6. </button>
  7. <u-form :model="form" class="form">
  8. <u-form-item label="昵称" label-width="150">
  9. <u-input type="nickname" v-model="form.nickName" placeholder="请输入昵称" :clearable="false" />
  10. </u-form-item>
  11. <u-form-item label="手机号" label-width="150">
  12. <u-input v-model="form.mobile" placeholder="未绑定手机号" disabled />
  13. </u-form-item>
  14. <view class="submit" @click="save">保存</view>
  15. </u-form>
  16. <u-top-tips ref="uTips" ></u-top-tips>
  17. <view ref="vvv"></view>
  18. </view>
  19. </template>
  20. <script>
  21. import { logout } from "@/api/login";
  22. import { mpUpdateUserProfile } from "@/api/connect.js";
  23. import { upload } from "@/api/common.js";
  24. import storage from "@/utils/storage.js";
  25. import uFormItem from "@/uview-ui/components/u-form-item/u-form-item.vue";
  26. export default {
  27. components: {
  28. uFormItem,
  29. },
  30. data() {
  31. return {
  32. avatarName: '',
  33. quitShow: false,
  34. lightColor: this.$lightColor, //高亮颜色
  35. statusBarHeight: uni.getSystemInfoSync().statusBarHeight,
  36. form: {
  37. nickName: storage.getUserInfo().nickName || "",
  38. mobile: storage.getUserInfo().mobile || "",
  39. avatarUrl: storage.getUserInfo().avatarUrl || "/static/missing-face.png", //默认头像
  40. sex: storage.getUserInfo().sex, //性别
  41. },
  42. photo: [
  43. { text: "立即拍照", color: this.$mainColor },
  44. { text: "从相册选择", color: this.$mainColor },
  45. ],
  46. };
  47. },
  48. methods: {
  49. /**
  50. * 显示退出登录对话框
  51. */
  52. showModalDialog() {
  53. this.quitShow = true;
  54. },
  55. clear() {
  56. storage.setAccessToken("");
  57. storage.setRefreshToken("");
  58. storage.setUserInfo({});
  59. this.$options.filters.navigateToLogin("redirectTo");
  60. },
  61. /**
  62. * 确认退出
  63. * 清除缓存重新登录
  64. */
  65. async confirm() {
  66. try {
  67. await logout();
  68. this.clear();
  69. } catch (e) {
  70. //TODO handle the exception
  71. this.clear();
  72. }
  73. },
  74. /**
  75. * 提交保存
  76. */
  77. save() {
  78. let params = JSON.parse(JSON.stringify(this.form));
  79. params.avatarUrl = this.avatarName;
  80. mpUpdateUserProfile(params).then((res) => {
  81. if (res.data.code == 200) {
  82. storage.setUserInfo(res.data.data);
  83. uni.navigateBack();
  84. } else {
  85. // 拿不到$refs.uTips?! 其他页面是可以的,待查 04-05
  86. this.$refs.uTips.show({ title: res.data.msg, type: "error" });
  87. }
  88. });
  89. },
  90. onClickAvatar(){
  91. // #ifndef MP-WEIXIN
  92. uni.chooseImage({
  93. success: (chooseImageRes) => {
  94. const avatarFilePath = chooseImageRes.tempFilePaths[0];
  95. this.form.avatarUrl = avatarFilePath;
  96. this.uploadAvatar(avatarFilePath)
  97. },
  98. });
  99. // #endif
  100. },
  101. onChooseAvatar(e) {
  102. const avatarFilePath = e.detail.avatarUrl;
  103. this.form.avatarUrl = avatarFilePath;
  104. this.uploadAvatar(avatarFilePath)
  105. },
  106. uploadAvatar(avatarFilePath){
  107. uni.uploadFile({
  108. url: upload,
  109. filePath: avatarFilePath,
  110. name: "file",
  111. header: {
  112. accessToken: storage.getAccessToken(),
  113. },
  114. success: (uploadFileRes) => {
  115. let data = JSON.parse(uploadFileRes.data)
  116. if (data.code == 200) {
  117. this.form.avatarUrl = data.data.url
  118. this.avatarName = data.data.name
  119. } else {
  120. uni.showToast({
  121. title: "上传失败",
  122. duration: 2000,
  123. icon: "error",
  124. });
  125. }
  126. },
  127. });
  128. },
  129. },
  130. };
  131. </script>
  132. <style lang="scss" scoped>
  133. .submit {
  134. height: 90rpx;
  135. line-height: 90rpx;
  136. text-align: center;
  137. margin-top: 90rpx;
  138. width: 100%;
  139. margin: 0 auto;
  140. color: $main-color;
  141. border-radius: 100px;
  142. }
  143. .head {
  144. height: 260rpx;
  145. color: $font-color-light;
  146. font-size: $font-sm;
  147. display: flex;
  148. flex-direction: column;
  149. justify-content: center;
  150. align-items: center;
  151. line-height: 2em;
  152. image {
  153. width: 144rpx;
  154. height: 144rpx;
  155. border-radius: 50%;
  156. }
  157. }
  158. /deep/ .u-form {
  159. padding: 0;
  160. margin-top: 30rpx;
  161. .u-form-item {
  162. padding: 0 20rpx;
  163. height: 110rpx;
  164. line-height: 110rpx;
  165. }
  166. }
  167. /deep/ .u-input__input {
  168. color: inherit !important;
  169. }
  170. .form {
  171. background-color: #ffffff;
  172. }
  173. </style>