handlePeople.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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-cell-group>
  45. <view class="footer">
  46. <wd-button type="primary" size="large" block @click="handleSubmit">保存</wd-button>
  47. </view>
  48. </wd-form>
  49. </view>
  50. </template>
  51. <script setup>
  52. import { onLoad } from '@dcloudio/uni-app'
  53. import { ref } from 'vue'
  54. import { myRequest } from '@/utils/api.ts'
  55. // 表格绑定数据
  56. const formValue = ref({
  57. uid: uni.getStorageSync('carUserInfo').id || '',
  58. username: '',
  59. sfzh: '',
  60. mobile: ''
  61. })
  62. // 表格DOM
  63. const form = ref(null)
  64. onLoad((options) => {
  65. if (options.info) {
  66. uni.setNavigationBarTitle({
  67. title: '修改乘客信息'
  68. })
  69. formValue.value = JSON.parse(decodeURIComponent(options.info))
  70. } else {
  71. uni.setNavigationBarTitle({
  72. title: '添加乘客信息'
  73. })
  74. }
  75. })
  76. // 保存按钮回调
  77. function handleSubmit() {
  78. form.value
  79. .validate()
  80. .then(({ valid, errors }) => {
  81. if (valid) {
  82. if (formValue.value.id) {
  83. // 修改请求
  84. submitEditReq()
  85. } else {
  86. // 新增请求
  87. submitAddReq()
  88. }
  89. }
  90. })
  91. .catch((error) => {
  92. console.log(error, 'error')
  93. })
  94. }
  95. // 新增请求
  96. const submitAddReq = async () => {
  97. const res = await myRequest({
  98. url: '/ctUserinsert.action',
  99. method: 'post',
  100. data: formValue.value
  101. })
  102. // console.log(res)
  103. if (res.code == 200) {
  104. uni.showToast({
  105. title: res.message,
  106. icon: 'success'
  107. })
  108. setTimeout(() => {
  109. uni.reLaunch({
  110. url: '/pages/commonPeople/commonPeople'
  111. })
  112. }, 1500)
  113. }
  114. }
  115. // 修改请求
  116. const submitEditReq = async () => {
  117. const res = await myRequest({
  118. url: '/ctUserupdate.action',
  119. method: 'post',
  120. data: formValue.value
  121. })
  122. // console.log(res)
  123. if (res.code == 200) {
  124. uni.showToast({
  125. title: res.message,
  126. icon: 'success'
  127. })
  128. setTimeout(() => {
  129. uni.reLaunch({
  130. url: '/pages/commonPeople/commonPeople'
  131. })
  132. }, 1500)
  133. }
  134. }
  135. // 姓名验证规则
  136. const validatorName = (val) => {
  137. const result = val.replace(/\s+/g, '')
  138. if (result) {
  139. if (result.length > 1 && result.length < 5) {
  140. return Promise.resolve()
  141. } else {
  142. return Promise.reject('姓名为2-4个字符')
  143. }
  144. }
  145. }
  146. // 身份证号码验证规则
  147. const validatorNum = (val) => {
  148. if (val) {
  149. // 去除空格
  150. const cleanedId = val.replace(/\s+/g, '')
  151. // 基本格式校验:18位,前17位数字,最后一位可以是数字或X
  152. const idReg = /(^\d{18}$)|(^\d{17}(\d|X|x)$)/
  153. if (!idReg.test(cleanedId)) {
  154. return Promise.reject('请输入有效的18位身份证号码')
  155. }
  156. // 校验码验证
  157. // 加权因子
  158. const factors = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
  159. // 校验码对应值
  160. const parityBits = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
  161. let sum = 0
  162. const code = cleanedId.toUpperCase() // 转为大写
  163. // 计算前17位与对应因子乘积的和
  164. for (let i = 0; i < 17; i++) {
  165. sum += parseInt(code.charAt(i)) * factors[i]
  166. }
  167. // 计算校验码
  168. const checkCode = parityBits[sum % 11]
  169. // 验证校验码
  170. if (code.charAt(17) !== checkCode) {
  171. return Promise.reject('身份证号码校验无效')
  172. }
  173. // 验证出生日期
  174. const year = parseInt(code.substr(6, 4))
  175. const month = parseInt(code.substr(10, 2))
  176. const day = parseInt(code.substr(12, 2))
  177. const birthDate = new Date(year, month - 1, day)
  178. if (birthDate.getFullYear() !== year || birthDate.getMonth() + 1 !== month || birthDate.getDate() !== day) {
  179. return Promise.reject('身份证号码中的出生日期无效')
  180. }
  181. return Promise.resolve()
  182. }
  183. }
  184. // 时间号码验证规则
  185. const validatorPhone = (val) => {
  186. const phoneReg = /^1[3-9]\d{9}$/
  187. if (val) {
  188. if (phoneReg.test(val)) {
  189. return Promise.resolve()
  190. } else {
  191. return Promise.reject('手机号码格式错误')
  192. }
  193. }
  194. }
  195. </script>
  196. <style lang="scss" scoped>
  197. .container {
  198. box-sizing: border-box;
  199. padding: 20rpx 0;
  200. height: 100vh;
  201. background-color: #fff;
  202. .footer {
  203. position: absolute;
  204. left: 40rpx;
  205. right: 40rpx;
  206. bottom: 100rpx;
  207. :deep(.wd-button) {
  208. background-color: #ff8205;
  209. }
  210. }
  211. }
  212. </style>