personInfo.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <template>
  2. <view class="root">
  3. <view class="content-box">
  4. <u-cell-group>
  5. <u-cell title="头像" @click="handlerUploadImg" isLink>
  6. <template slot="value">
  7. <u-avatar shape="square" size="60" :src="userInfo.avatar" @click.stop="viewAvatar(userInfo.avatar)"></u-avatar>
  8. </template>
  9. </u-cell>
  10. <u-cell title="昵称" isLink>
  11. <template slot="value" >
  12. <u-input inputAlign="right" :border="false" v-model="userInfo.nickname" />
  13. </template>
  14. </u-cell>
  15. <u-cell title="性别" isLink>
  16. <template slot="value">
  17. <u-radio-group v-model="userInfo.gender">
  18. <u-radio v-for="item in sexList" :key="item.type" :label="item.text" :name="item.type" />
  19. </u-radio-group>
  20. </template>
  21. </u-cell>
  22. </u-cell-group>
  23. </view>
  24. <view class="btn-box">
  25. <u-button type="primary" shape="circle" @click="handlerSubmitBtn">确认</u-button>
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. import { uploadFile } from "@/utils/upload"
  31. export default {
  32. data() {
  33. return {
  34. userInfo:{
  35. userId:'',
  36. avatar:'',
  37. gender:'',
  38. nickname:''
  39. },
  40. time_value: '',
  41. timeShow: false,
  42. sexList: [{
  43. id: 0,
  44. text: '男',
  45. type:'MALE'
  46. },
  47. {
  48. id: 1,
  49. text: '女',
  50. type:'FEMALE'
  51. }
  52. ],
  53. current: 0,
  54. }
  55. },
  56. mounted(){
  57. this.userInfo.userId = this.$store.getters.userId
  58. this.userInfo.avatar = this.$store.getters.avatar
  59. this.userInfo.gender = this.$store.getters.gender
  60. this.userInfo.nickname = this.$store.getters.nickname
  61. },
  62. methods: {
  63. viewAvatar(url){
  64. uni.previewImage({
  65. urls:[url]
  66. })
  67. },
  68. // 选择性别
  69. handlerSelectGender(item) {
  70. this.current = item.id
  71. },
  72. // 上传头像
  73. async handlerUploadImg() {
  74. const res = await uni.showActionSheet({
  75. itemList: ['拍照', '从相册选择'],
  76. });
  77. if (res.tapIndex === 0) {
  78. // 用户选择拍照
  79. this.takePhoto();
  80. } else if (res.tapIndex === 1) {
  81. // 用户选择从相册选择
  82. this.chooseImage();
  83. }
  84. },
  85. // 拍照
  86. takePhoto() {
  87. uni.chooseImage({
  88. sourceType: ['camera'],
  89. count: 1,
  90. success: res => {
  91. const tempFilePaths = res.tempFilePaths;
  92. this.uploadAvatar(tempFilePaths[0]);
  93. },
  94. fail: error => {
  95. console.error(error);
  96. },
  97. });
  98. },
  99. //从相册中选择
  100. chooseImage() {
  101. uni.chooseImage({
  102. sourceType: ['album'],
  103. count: 1,
  104. success: res => {
  105. const tempFilePaths = res.tempFilePaths;
  106. this.uploadAvatar(tempFilePaths[0]);
  107. },
  108. fail: error => {
  109. console.error(error);
  110. },
  111. });
  112. },
  113. // 上传头像
  114. async uploadAvatar(filePath) {
  115. // 在这里实现上传头像的逻辑,将filePath作为参数传入
  116. const res = await uploadFile(filePath)
  117. if(res.statusCode === 200 && res.data){
  118. const resp = JSON.parse(res.data)
  119. this.userInfo.avatar = resp.data.url
  120. }
  121. },
  122. // 点击提交按钮
  123. handlerSubmitBtn() {
  124. this.$store.dispatch('UpdateUserInfo',this.userInfo).then(res=>{
  125. setTimeout(()=>{
  126. uni.navigateBack(-1)
  127. },1500)
  128. })
  129. }
  130. }
  131. }
  132. </script>
  133. <style lang="scss" scoped>
  134. .root{
  135. background-color: #f3f3f3;
  136. height: calc(100vh - env(safe-area-inset-top));
  137. position: relative;
  138. .content-box{
  139. background-color: #fff;
  140. }
  141. .btn-box{
  142. position: absolute;
  143. bottom: 0;
  144. padding: 32rpx 16rpx;
  145. box-sizing: border-box;
  146. width: 100%;
  147. }
  148. }
  149. </style>