visitor.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { defineStore } from 'pinia'
  2. import { ref } from 'vue'
  3. import * as api from '@/api/screen'
  4. export const useVisitorStore = defineStore('visitor', () => {
  5. // 当前访客信息
  6. const currentVisitor = ref({
  7. name: '',
  8. mobile: '',
  9. idCardNo: '',
  10. gender: '',
  11. nation: '',
  12. address: '',
  13. photoUrl: ''
  14. })
  15. // 预约信息
  16. const appointmentInfo = ref(null)
  17. // 登记信息
  18. const registrationInfo = ref({
  19. visitorName: '',
  20. mobile: '',
  21. idCardNo: '',
  22. visitType: 'walk_in',
  23. registerType: 'manual',
  24. visitorSource: '',
  25. visitReason: '',
  26. visitedPerson: '',
  27. appointmentNo: ''
  28. })
  29. // 登记表单验证状态
  30. const formErrors = ref({})
  31. // 来访事由选项
  32. const visitReasonOptions = [
  33. { value: 'business', label: '商务洽谈' },
  34. { value: 'interview', label: '面试应聘' },
  35. { value: 'delivery', label: '快递/外卖' },
  36. { value: 'maintenance', label: '设备维修' },
  37. { value: 'visit_friend', label: '探亲访友' },
  38. { value: 'consultation', label: '咨询业务' },
  39. { value: 'other', label: '其他事宜' }
  40. ]
  41. // 方法
  42. function setIdCardInfo(info) {
  43. currentVisitor.value = {
  44. ...currentVisitor.value,
  45. name: info.name || '',
  46. idCardNo: info.idCardNo || '',
  47. gender: info.gender || '',
  48. nation: info.nation || '',
  49. address: info.address || '',
  50. photoUrl: info.photoUrl || ''
  51. }
  52. // 自动填充登记信息
  53. registrationInfo.value.visitorName = info.name || ''
  54. registrationInfo.value.idCardNo = info.idCardNo || ''
  55. }
  56. function setMobile(mobile) {
  57. currentVisitor.value.mobile = mobile
  58. registrationInfo.value.mobile = mobile
  59. }
  60. function setFormField(field, value) {
  61. registrationInfo.value[field] = value
  62. if (formErrors.value[field]) {
  63. delete formErrors.value[field]
  64. }
  65. }
  66. function validateForm() {
  67. const errors = {}
  68. if (!registrationInfo.value.visitorName?.trim()) {
  69. errors.visitorName = '请输入访客姓名'
  70. }
  71. if (!registrationInfo.value.mobile?.trim()) {
  72. errors.mobile = '请输入手机号'
  73. } else if (!/^1[3-9]\d{9}$/.test(registrationInfo.value.mobile)) {
  74. errors.mobile = '手机号格式不正确'
  75. }
  76. formErrors.value = errors
  77. return Object.keys(errors).length === 0
  78. }
  79. async function queryAppointment(mobile) {
  80. try {
  81. const res = await api.queryAppointment({ mobile })
  82. appointmentInfo.value = res
  83. return res
  84. } catch (e) {
  85. appointmentInfo.value = null
  86. throw e
  87. }
  88. }
  89. async function submitRegistration() {
  90. if (!validateForm()) {
  91. throw new Error('表单验证失败')
  92. }
  93. try {
  94. const res = await api.submitVisitorRegistration(registrationInfo.value)
  95. return res
  96. } catch (e) {
  97. throw e
  98. }
  99. }
  100. async function readIdCard() {
  101. try {
  102. const res = await api.readIdCard()
  103. setIdCardInfo(res)
  104. return res
  105. } catch (e) {
  106. throw e
  107. }
  108. }
  109. function clearVisitorData() {
  110. currentVisitor.value = {
  111. name: '',
  112. mobile: '',
  113. idCardNo: '',
  114. gender: '',
  115. nation: '',
  116. address: '',
  117. photoUrl: ''
  118. }
  119. appointmentInfo.value = null
  120. registrationInfo.value = {
  121. visitorName: '',
  122. mobile: '',
  123. idCardNo: '',
  124. visitType: 'walk_in',
  125. registerType: 'manual',
  126. visitorSource: '',
  127. visitReason: '',
  128. visitedPerson: '',
  129. appointmentNo: ''
  130. }
  131. formErrors.value = {}
  132. }
  133. return {
  134. currentVisitor,
  135. appointmentInfo,
  136. registrationInfo,
  137. formErrors,
  138. visitReasonOptions,
  139. setIdCardInfo,
  140. setMobile,
  141. setFormField,
  142. validateForm,
  143. queryAppointment,
  144. submitRegistration,
  145. readIdCard,
  146. clearVisitorData
  147. }
  148. })