birthday-change.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <template>
  2. <view class="Date">
  3. <view class="content">
  4. <!-- 顶部标题 -->
  5. <view class="top-title">出生日期</view>
  6. <picker-view :value="value" :indicator-style="indicatorStyle" @change="bindChange" class="picker-view">
  7. <picker-view-column>
  8. <view class="item" v-for="(item,index) in years" :key="index">{{item}}年</view>
  9. </picker-view-column>
  10. <picker-view-column>
  11. <view class="item" v-for="(item,index) in months" :key="index">{{item}}月</view>
  12. </picker-view-column>
  13. <picker-view-column>
  14. <view class="item" v-for="(item,index) in days" :key="index">{{item}}日</view>
  15. </picker-view-column>
  16. </picker-view>
  17. <!-- 底部保存按钮 -->
  18. </view>
  19. <button class="save-button" @click="ack">保存</button>
  20. </view>
  21. </template>
  22. <script>
  23. import {
  24. memberUpdate,
  25. memberDetail
  26. } from '@/config/api.js';
  27. export default {
  28. props: {
  29. selectedDate: {
  30. type: String,
  31. default: "",
  32. },
  33. },
  34. data() {
  35. return {
  36. user: {},
  37. years: [],
  38. year: '',
  39. months: [],
  40. month: '',
  41. days: [],
  42. day: '',
  43. value: [],
  44. indicatorStyle: 'height: 50px;'
  45. }
  46. },
  47. mounted() {
  48. const date = new Date();
  49. const year = date.getFullYear();
  50. const month = date.getMonth() + 1;
  51. const day = date.getDate();
  52. const selectedYear = new Date(this.selectedDate).getFullYear() || year;
  53. const selectedMonth = new Date(this.selectedDate).getMonth() + 1 || month;
  54. const selectedDay = new Date(this.selectedDate).getDate() || day;
  55. setTimeout(() => {
  56. this.value = [this.years.indexOf(selectedYear), selectedMonth - 1, selectedDay - 1];
  57. this.year = selectedYear;
  58. this.month = selectedMonth < 10 ? `0${selectedMonth}` : selectedMonth;
  59. this.day = selectedDay < 10 ? `0${selectedDay}` : selectedDay;
  60. }, 0);
  61. },
  62. created() {
  63. this.init();
  64. },
  65. onLoad(op) {
  66. this.detail();
  67. },
  68. methods: {
  69. async detail() {
  70. const data = await memberDetail();
  71. if (data.code == 200) {
  72. this.user = data.data;
  73. }
  74. },
  75. init() {
  76. const date = new Date();
  77. const year = date.getFullYear();
  78. const month = date.getMonth() + 1;
  79. const day = date.getDate();
  80. // 生成年份范围(例如从 1900 年到当前年份)
  81. const years = [];
  82. const startYear = 1900; // 你可以根据需要调整起始年份
  83. for (let i = startYear; i <= year; i++) {
  84. years.push(i);
  85. }
  86. // 生成月份范围(1 到 12)
  87. const months = [];
  88. for (let i = 1; i <= 12; i++) {
  89. months.push(i);
  90. }
  91. // 更新数据
  92. this.years = years;
  93. this.months = months;
  94. this.getDays(year, month);
  95. },
  96. getDays(year, month) {
  97. const day = new Date(year, month, 0).getDate();
  98. const days = [];
  99. for (let i = 1; i <= day; i++) {
  100. days.push(i);
  101. }
  102. this.days = days;
  103. },
  104. bindChange: function(e) {
  105. const val = e.detail.value;
  106. this.year = this.years[val[0]];
  107. this.month = this.months[val[1]];
  108. this.day = this.days[val[2]];
  109. // 切换年份或月份时,更新天数
  110. this.getDays(this.year, this.month);
  111. },
  112. esc() {
  113. this.$emit('popType', false);
  114. },
  115. ack() {
  116. const date = new Date(this.year, this.month - 1, this.day);
  117. const formattedDate = `${this.year}-${this.month}-${this.day}`; // 手动格式化日期
  118. console.log(formattedDate);
  119. this.user.birthday = formattedDate;
  120. memberUpdate(this.user).then((res) => {
  121. if (res.code == 200) {
  122. uni.$u.toast("操作成功");
  123. uni.navigateBack({
  124. delta: 1 // delta 表示返回的页面数,1 表示返回上一页
  125. });
  126. uni.setStorageSync('user', null)
  127. }
  128. });
  129. },
  130. }
  131. }
  132. </script>
  133. <style lang="scss" scoped>
  134. .Date {
  135. background-color: white;
  136. padding: 30px;
  137. min-height: 100vh;
  138. .content {
  139. width: 100%;
  140. /* 内容区域宽度 */
  141. background: #FFFFFF;
  142. padding: 20px;
  143. display: flex;
  144. flex-direction: column;
  145. .top-title {
  146. text-align: center;
  147. font-size: 40rpx;
  148. font-weight: bold;
  149. margin-bottom: 20px;
  150. }
  151. .picker-view {
  152. width: 100%;
  153. height: 200px;
  154. /* 选择器高度 */
  155. margin-bottom: 20px;
  156. }
  157. .item {
  158. line-height: 30px;
  159. font-size: 32rpx;
  160. text-align: center;
  161. }
  162. }
  163. }
  164. .save-button {
  165. background-color: #007aff;
  166. color: white;
  167. margin-top: 50rpx;
  168. border: none;
  169. border-radius: 10rpx;
  170. padding: 10rpx;
  171. font-size: 16px;
  172. }
  173. </style>