updateUserInfo.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <view class="info-item">
  3. <view class="item-label">
  4. <text>姓名:</text>
  5. </view>
  6. <view class="item-content">
  7. <input v-model="name" type="text" placeholder="请输入姓名" />
  8. </view>
  9. </view>
  10. <view class="info-item">
  11. <view class="item-label">
  12. <text>身份证号:</text>
  13. </view>
  14. <view class="item-content">
  15. <input v-model="idNumber" type="idcard" placeholder="请输入身份证号" maxlength="18" />
  16. </view>
  17. </view>
  18. <view class="update-button">
  19. <button @click="updateUserInfo">确定</button>
  20. </view>
  21. </template>
  22. <script setup>
  23. import {
  24. ref
  25. } from 'vue'
  26. import {
  27. onLoad
  28. } from '@dcloudio/uni-app'
  29. import {
  30. useUserStore
  31. } from '../../store/userStore'
  32. let name = ref('')
  33. let idNumber = ref('')
  34. let user = useUserStore()
  35. let userInfo = useUserStore().userInfo
  36. async function updateUserInfo() {
  37. if (!name.value || name.value.length < 2) {
  38. uni.showToast({
  39. icon: 'none',
  40. title: '姓名格式错误!'
  41. })
  42. return;
  43. }
  44. if (!idNumber.value || idNumber.value.length < 18) {
  45. uni.showToast({
  46. icon: 'none',
  47. title: '身份证格式错误!'
  48. })
  49. return;
  50. }
  51. if (name.value !== userInfo.name || idNumber.value !== userInfo.idNumber) {
  52. let res = await uni.request({
  53. url: '/user',
  54. method: 'PUT',
  55. data: {
  56. name: name.value,
  57. idNumber: idNumber.value
  58. }
  59. })
  60. if (res.data.success) {
  61. userInfo.name = name.value
  62. userInfo.idNumber = idNumber.value
  63. user.updateUserInfo(userInfo)
  64. uni.showToast({
  65. icon: 'none',
  66. title: '修改成功!'
  67. })
  68. setTimeout(() => {
  69. uni.navigateBack()
  70. }, 1000)
  71. } else {
  72. uni.showToast({
  73. icon: 'none',
  74. title: '修改失败!'
  75. })
  76. }
  77. } else {
  78. uni.showToast({
  79. icon: 'none',
  80. title: '修改成功!'
  81. })
  82. setTimeout(() => {
  83. uni.navigateBack()
  84. }, 1000)
  85. }
  86. }
  87. onLoad(() => {
  88. name.value = userInfo.name
  89. idNumber.value = userInfo.idNumber
  90. })
  91. </script>
  92. <style lang="scss">
  93. .info-item {
  94. height: 100rpx;
  95. display: flex;
  96. align-items: center;
  97. margin: 0 40rpx;
  98. border-bottom: #000000 2rpx solid;
  99. .item-label {
  100. width: 30vw;
  101. }
  102. .item-content {
  103. input {
  104. // background-color: #666666;
  105. border: #d3d3d3 2rpx solid;
  106. padding: 10rpx;
  107. width: 50vw;
  108. }
  109. }
  110. }
  111. .update-button {
  112. button {
  113. margin: 40rpx auto;
  114. width: 80vw;
  115. background-color: #a09cc4;
  116. color: #ffffff;
  117. }
  118. }
  119. </style>