personInformation.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <template>
  2. <view class="ccontainer">
  3. <view class="item-box">
  4. <view class="item-left"> 头像 : </view>
  5. <view class="item-right flex-end">
  6. <image @click="handlerUploadImg" class="img" :src="uoloadImg"></image>
  7. </view>
  8. </view>
  9. <view class="item-box">
  10. <view class="item-left"> 昵称 : </view>
  11. <view class="item-right">
  12. <u--input placeholder="请输入内容" border="none" v-model="queryParams.nickname"></u--input>
  13. </view>
  14. </view>
  15. <view class="item-box">
  16. <view class="item-left"> 性别 : </view>
  17. <view class="item-right">
  18. <view class="sex-item">
  19. <view class="item" v-for=" (item,index) of sexList" :class="current == index ? 'act-sex' : ''" :key="index"
  20. @click="handlerSelectGender(item)"> {{ item.name }} </view>
  21. </view>
  22. </view>
  23. </view>
  24. <view class="item-box">
  25. <view class="item-left"> 生日 : </view>
  26. <view class="item-right right-time-box" @click="timeShow = true">
  27. <view class="time-left" :class="queryParams.birthday ? '' : 'gray-color'">
  28. {{ queryParams.birthday ? queryParams.birthday : '请选择您的生日' }}
  29. </view>
  30. <view class="time-right">
  31. <u-icon name="arrow-right"></u-icon>
  32. </view>
  33. </view>
  34. </view>
  35. <u-datetime-picker :show="timeShow" v-model="time_value" mode="date" @cancel='timeShow = false'
  36. @confirm='confirmTime' :minDate="0" :maxDate='maxDataTime'></u-datetime-picker>
  37. <view class="btn-box">
  38. <button class="btn" @click="handlerSubmitBtn">提交</button>
  39. </view>
  40. </view>
  41. </template>
  42. <script>
  43. import {
  44. getUserInfo
  45. } from '@/api/user';
  46. import {
  47. uploadFile
  48. } from "@/utils/upload"
  49. export default {
  50. data() {
  51. return {
  52. queryParams: {
  53. avatar: '', // 头像
  54. gender: '', //性别
  55. birthday: '', //生日
  56. nickname: '', //昵称
  57. },
  58. time_value: '',
  59. timeShow: false,
  60. sexList: [{
  61. id: 0,
  62. name: '男',
  63. type: 'MALE'
  64. },
  65. {
  66. id: 1,
  67. name: '女',
  68. type: 'FEMALE'
  69. }
  70. ],
  71. current: 0,
  72. uoloadImg: '',
  73. maxDataTime: 0
  74. }
  75. },
  76. mounted() {
  77. this.maxDataTime = new Date().getTime()
  78. getUserInfo().then(res => {
  79. let {
  80. avatar,
  81. nickname,
  82. gender,
  83. birthday
  84. } = res.data
  85. this.uoloadImg = avatar
  86. this.queryParams.avatar = avatar
  87. this.queryParams.nickname = nickname
  88. this.queryParams.birthday = uni.$u.timeFormat(birthday, 'yyyy-mm-dd');
  89. if (gender) {
  90. for (let key in gender) {
  91. this.sexList.map(rs => {
  92. if (key == rs.type) {
  93. this.current = rs.id
  94. }
  95. })
  96. }
  97. }
  98. })
  99. },
  100. methods: {
  101. // 选择性别
  102. handlerSelectGender(item) {
  103. this.current = item.id
  104. },
  105. // 选择时间
  106. confirmTime(e) {
  107. this.queryParams.birthday = uni.$u.timeFormat(e.value, 'yyyy-mm-dd');
  108. this.timeShow = false
  109. },
  110. // 上传头像
  111. async handlerUploadImg() {
  112. try {
  113. const res = await uni.showActionSheet({
  114. itemList: ['拍照', '从相册选择'],
  115. });
  116. if (res.tapIndex === 0) {
  117. // 用户选择拍照
  118. this.takeOrChoosePhoto(0);
  119. } else if (res.tapIndex === 1) {
  120. // 用户选择从相册选择
  121. this.takeOrChoosePhoto(1);
  122. }
  123. } catch (error) {}
  124. },
  125. // 拍照
  126. takeOrChoosePhoto(idx) {
  127. uni.chooseImage({
  128. sourceType: idx == 0 ? ['camera'] : ['album'],
  129. count: 1,
  130. success: res => {
  131. const tempFilePaths = res.tempFilePaths;
  132. // 调用上传图片的方法
  133. this.uploadAvatar(tempFilePaths[0]);
  134. },
  135. fail: error => {
  136. console.error(error);
  137. },
  138. });
  139. },
  140. // 上传头像
  141. uploadAvatar(filePath) {
  142. this.uoloadImg = filePath
  143. uploadFile(filePath).then(res => {
  144. this.queryParams.avatar = JSON.parse(res.data).data.url;
  145. })
  146. this.$forceUpdate(); // 手动触发组件的重新渲染
  147. },
  148. // 点击提交按钮
  149. handlerSubmitBtn() {
  150. this.queryParams.gender = this.sexList[this.current].type
  151. this.$store.dispatch('UpdateUserInfo', this.queryParams).then(res => {
  152. setTimeout(() => {
  153. uni.navigateBack(-1)
  154. }, 1500)
  155. })
  156. }
  157. }
  158. }
  159. </script>
  160. <style lang="scss" scoped>
  161. .ccontainer {
  162. padding: 20rpx;
  163. box-sizing: border-box;
  164. font-size: 28rpx;
  165. .item-box {
  166. display: flex;
  167. padding: 20rpx;
  168. box-shadow: 0 5rpx 15rpx 0rpx rgba(0, 0, 0, 0.1);
  169. border-radius: 20rpx;
  170. margin-bottom: 20rpx;
  171. align-items: center;
  172. .item-left {
  173. margin-right: 20rpx;
  174. width: 13%;
  175. }
  176. .item-right {
  177. width: 87%;
  178. .img {
  179. width: 80rpx;
  180. height: 80rpx;
  181. border-radius: 50%;
  182. }
  183. .sex-item {
  184. display: flex;
  185. .item {
  186. padding: 20rpx 30rpx;
  187. background-color: #F7F7F7;
  188. border-radius: 10rpx;
  189. text-align: center;
  190. font-size: 28rpx;
  191. }
  192. .act-sex {
  193. border: 2rpx solid #5992BB;
  194. background-color: #5992BB;
  195. color: #fff !important;
  196. }
  197. }
  198. }
  199. .right-time-box {
  200. display: flex;
  201. justify-content: space-between;
  202. }
  203. .flex-end {
  204. display: flex;
  205. justify-content: flex-end;
  206. }
  207. }
  208. .btn-box {
  209. margin-top: 100rpx;
  210. .btn {
  211. border-radius: 20rpx;
  212. padding: 10rpx;
  213. font-size: 28rpx;
  214. color: #fff;
  215. background: linear-gradient(to right, #e8cbc0, #636fa4);
  216. box-shadow: 0 5rpx 15rpx 0 rgba(99, 111, 164, 0.2);
  217. }
  218. }
  219. }
  220. .gray-color {
  221. color: #D2D5DB;
  222. }
  223. </style>