handlePeople.vue 4.9 KB

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