handlePeople.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <template>
  2. <view class="container">
  3. <wd-form ref="form" :model="formValue">
  4. <wd-cell-group border>
  5. <wd-input
  6. v-model="formValue.username"
  7. label="中文姓名"
  8. label-width="100px"
  9. prop="username"
  10. clearable
  11. placeholder="请输入姓名"
  12. :rules="[{ required: false, validator: validatorName, message: '请输入姓名' }]"
  13. />
  14. <wd-input
  15. v-model="formValue.sfzh"
  16. label="身份证"
  17. label-width="100px"
  18. prop="sfzh"
  19. clearable
  20. placeholder="请输入身份证件号码"
  21. :rules="[
  22. {
  23. required: false,
  24. validator: validatorNum,
  25. message: '请输入身份证件号码'
  26. }
  27. ]"
  28. />
  29. <wd-input
  30. v-model="formValue.mobile"
  31. label="手机号"
  32. label-width="100px"
  33. prop="mobile"
  34. clearable
  35. placeholder="请输入手机号码"
  36. :rules="[
  37. {
  38. required: false,
  39. validator: validatorPhone,
  40. message: '请输入手机号码'
  41. }
  42. ]"
  43. />
  44. <wd-input
  45. v-model="formValue.cardNumber"
  46. label="学号"
  47. label-width="100px"
  48. prop="mobile"
  49. class="custom-input"
  50. clearable
  51. placeholder=""
  52. />
  53. <!-- 模拟右侧占位符的文本 -->
  54. <text class="placeholder-text" v-if="!formValue.cardNumber">(填学号,便于发放校补)</text>
  55. </wd-cell-group>
  56. <view class="footer">
  57. <wd-button type="primary" size="large" block @click="handleSubmit">保存</wd-button>
  58. </view>
  59. </wd-form>
  60. </view>
  61. </template>
  62. <script setup>
  63. import { onLoad } from '@dcloudio/uni-app'
  64. import { ref } from 'vue'
  65. import { myRequest } from '@/utils/api.ts'
  66. console.log(uni.getStorageSync('carUserInfo').id,'新增')
  67. // 表格绑定数据
  68. const formValue = ref({
  69. uid: uni.getStorageSync('carUserInfo').id ,
  70. username: '',
  71. sfzh: '',
  72. mobile: '',
  73. cardNumber:''
  74. })
  75. // 表格DOM
  76. const form = ref(null)
  77. onLoad((options) => {
  78. if (options.info) {
  79. uni.setNavigationBarTitle({
  80. title: '修改乘客信息'
  81. })
  82. formValue.value = JSON.parse(decodeURIComponent(options.info))
  83. } else {
  84. uni.setNavigationBarTitle({
  85. title: '添加乘客信息'
  86. })
  87. }
  88. })
  89. // 保存按钮回调
  90. function handleSubmit() {
  91. form.value
  92. .validate()
  93. .then(({ valid, errors }) => {
  94. if (valid) {
  95. if (formValue.value.id) {
  96. // 修改请求
  97. submitEditReq()
  98. } else {
  99. // 新增请求
  100. submitAddReq()
  101. }
  102. }
  103. })
  104. .catch((error) => {
  105. console.log(error, 'error')
  106. })
  107. }
  108. // 新增请求
  109. const submitAddReq = async () => {
  110. const res = await myRequest({
  111. url: '/ctUserinsert.action',
  112. method: 'post',
  113. data: formValue.value,
  114. })
  115. // console.log(res)
  116. if (res.code == 200) {
  117. uni.showToast({
  118. title: res.message,
  119. icon: 'success'
  120. })
  121. setTimeout(() => {
  122. uni.reLaunch({
  123. url: '/pages/commonPeople/commonPeople'
  124. })
  125. }, 1500)
  126. }
  127. }
  128. // 修改请求
  129. const submitEditReq = async () => {
  130. const res = await myRequest({
  131. url: '/ctUserupdate.action',
  132. method: 'post',
  133. data: formValue.value
  134. })
  135. // console.log(res)
  136. if (res.code == 200) {
  137. uni.showToast({
  138. title: res.message,
  139. icon: 'success'
  140. })
  141. setTimeout(() => {
  142. uni.reLaunch({
  143. url: '/pages/commonPeople/commonPeople'
  144. })
  145. }, 1500)
  146. }
  147. }
  148. // 姓名验证规则
  149. const validatorName = (val) => {
  150. const result = val.replace(/\s+/g, '')
  151. if (result) {
  152. if (result.length > 1 && result.length < 5) {
  153. return Promise.resolve()
  154. } else {
  155. return Promise.reject('姓名为2-4个字符')
  156. }
  157. }
  158. }
  159. // 身份证号码验证规则
  160. const validatorNum = (val) => {
  161. if (val) {
  162. // 去除空格
  163. const cleanedId = val.replace(/\s+/g, '')
  164. // 基本格式校验:18位,前17位数字,最后一位可以是数字或X
  165. const idReg = /(^\d{18}$)|(^\d{17}(\d|X|x)$)/
  166. if (!idReg.test(cleanedId)) {
  167. return Promise.reject('请输入有效的18位身份证号码')
  168. }
  169. // 校验码验证
  170. // 加权因子
  171. const factors = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
  172. // 校验码对应值
  173. const parityBits = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
  174. let sum = 0
  175. const code = cleanedId.toUpperCase() // 转为大写
  176. // 计算前17位与对应因子乘积的和
  177. for (let i = 0; i < 17; i++) {
  178. sum += parseInt(code.charAt(i)) * factors[i]
  179. }
  180. // 计算校验码
  181. const checkCode = parityBits[sum % 11]
  182. // 验证校验码
  183. if (code.charAt(17) !== checkCode) {
  184. return Promise.reject('身份证号码校验无效')
  185. }
  186. // 验证出生日期
  187. const year = parseInt(code.substr(6, 4))
  188. const month = parseInt(code.substr(10, 2))
  189. const day = parseInt(code.substr(12, 2))
  190. const birthDate = new Date(year, month - 1, day)
  191. if (birthDate.getFullYear() !== year || birthDate.getMonth() + 1 !== month || birthDate.getDate() !== day) {
  192. return Promise.reject('身份证号码中的出生日期无效')
  193. }
  194. return Promise.resolve()
  195. }
  196. }
  197. // 时间号码验证规则
  198. const validatorPhone = (val) => {
  199. const phoneReg = /^1[3-9]\d{9}$/
  200. if (val) {
  201. if (phoneReg.test(val)) {
  202. return Promise.resolve()
  203. } else {
  204. return Promise.reject('手机号码格式错误')
  205. }
  206. }
  207. }
  208. </script>
  209. <style lang="scss" scoped>
  210. .container {
  211. box-sizing: border-box;
  212. padding: 20rpx 0;
  213. height: 100vh;
  214. background-color: #fff;
  215. .custom-input {
  216. width: 100%;
  217. height: 80rpx;
  218. line-height: 80rpx;
  219. padding-right: 200rpx; /* 给右侧占位文本留空间 */
  220. border: 1px solid #eee;
  221. border-radius: 8rpx;
  222. font-size: 28rpx;
  223. }
  224. .placeholder-text {
  225. position: absolute; /* 绝对定位到输入框右侧 */
  226. right: 30rpx;
  227. margin-top: -9%;
  228. font-size: 26rpx;
  229. color: #999; /* 模拟原生placeholder颜色 */
  230. pointer-events: none; /* 避免遮挡输入框点击 */
  231. }
  232. .footer {
  233. position: absolute;
  234. left: 40rpx;
  235. right: 40rpx;
  236. bottom: 100rpx;
  237. :deep(.wd-button) {
  238. background-color: #ff8205;
  239. }
  240. }
  241. }
  242. </style>