add-student.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <view class="page">
  3. <van-nav-bar title="新增学生" left-arrow @click-left="onBack" fixed />
  4. <view class="content">
  5. <view class="form-item">
  6. <text class="label">学生姓名</text>
  7. <input class="input" v-model="formData.name" placeholder="请输入学生姓名" />
  8. </view>
  9. <view class="form-item">
  10. <text class="label">身份证号</text>
  11. <input class="input" v-model="formData.idCard" placeholder="请输入身份证号" maxlength="18" @blur="parseGender" />
  12. </view>
  13. <view class="form-item">
  14. <text class="label">性别</text>
  15. <van-radio-group v-model="formData.gender" direction="horizontal">
  16. <van-radio name="男">男</van-radio>
  17. <van-radio name="女">女</van-radio>
  18. </van-radio-group>
  19. </view>
  20. </view>
  21. <view class="footer">
  22. <van-button plain type="primary" round size="large" @click="onBack">返回</van-button>
  23. <van-button type="primary" round size="large" @click="handleSave">保存</van-button>
  24. </view>
  25. </view>
  26. </template>
  27. <script setup>
  28. import { addStudent } from '@/services/common'
  29. const classId = ref('')
  30. const userName = ref('')
  31. const userPhone = ref('')
  32. const formData = ref({
  33. name: '',
  34. idCard: '',
  35. gender: '男'
  36. })
  37. onLoad((options) => {
  38. classId.value = options.classId || ''
  39. userName.value = decodeURIComponent(options.userName || '')
  40. userPhone.value = decodeURIComponent(options.userPhone || '')
  41. })
  42. const parseGender = () => {
  43. const idCard = formData.value.idCard
  44. if (idCard.length === 18) {
  45. const genderCode = parseInt(idCard.charAt(16))
  46. formData.value.gender = genderCode % 2 === 0 ? '女' : '男'
  47. }
  48. }
  49. const onBack = () => {
  50. uni.navigateBack()
  51. }
  52. const handleSave = async () => {
  53. if (!formData.value.name) {
  54. return uni.showToast({
  55. title: '请输入学生姓名',
  56. icon: 'none'
  57. })
  58. }
  59. if (!formData.value.idCard) {
  60. return uni.showToast({
  61. title: '请输入身份证号',
  62. icon: 'none'
  63. })
  64. }
  65. if (!/^\d{17}[\dXx]$/.test(formData.value.idCard)) {
  66. return uni.showToast({
  67. title: '身份证号格式不正确',
  68. icon: 'none'
  69. })
  70. }
  71. try {
  72. const params = {
  73. class_id: classId.value,
  74. name: formData.value.name,
  75. gender: formData.value.gender,
  76. id_card: formData.value.idCard,
  77. created_user: userName.value,
  78. operator_phone: userPhone.value
  79. }
  80. const res = await addStudent(params)
  81. if (res && res.code === 200) {
  82. uni.showToast({
  83. title: '新增成功',
  84. icon: 'success'
  85. })
  86. setTimeout(() => {
  87. uni.navigateBack()
  88. }, 1000)
  89. }
  90. } catch (error) {
  91. console.error('新增学生失败', error)
  92. }
  93. }
  94. </script>
  95. <style lang="scss" scoped>
  96. .page {
  97. min-height: 100vh;
  98. background: #f5f5f5;
  99. }
  100. .content {
  101. padding: 20rpx 30rpx;
  102. padding-top: calc(20rpx + 46px);
  103. }
  104. .form-item {
  105. background: #fff;
  106. border-radius: 20rpx;
  107. padding: 30rpx;
  108. margin-bottom: 20rpx;
  109. }
  110. .label {
  111. font-size: 28rpx;
  112. color: #333;
  113. display: block;
  114. margin-bottom: 20rpx;
  115. }
  116. .input {
  117. font-size: 28rpx;
  118. color: #333;
  119. width: 100%;
  120. }
  121. .footer {
  122. position: fixed;
  123. bottom: 0;
  124. left: 0;
  125. right: 0;
  126. padding: 30rpx;
  127. background: #fff;
  128. display: flex;
  129. gap: 20rpx;
  130. }
  131. .footer button {
  132. flex: 1;
  133. }
  134. </style>