auth_logic.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { uploadFile } from '@/api/fulfiller'
  2. export default {
  3. data() {
  4. return {
  5. formData: {
  6. idType: '居民身份证',
  7. name: '',
  8. idNumber: '',
  9. expiryDate: ''
  10. },
  11. idCardFront: '', // 身份证正面本地预览路径
  12. idCardBack: '', // 身份证反面本地预览路径
  13. idCardFrontOssId: '', // 身份证正面 OSS ID
  14. idCardBackOssId: '', // 身份证反面 OSS ID
  15. showDatePicker: false,
  16. pickerValue: [0, 0, 0], // YYYY-MM-DD
  17. years: [],
  18. months: [],
  19. days: [],
  20. serviceType: [] // 接收上一页的服务类型
  21. }
  22. },
  23. onLoad(options) {
  24. if (options.services) {
  25. try {
  26. this.serviceType = JSON.parse(decodeURIComponent(options.services));
  27. } catch (e) {
  28. console.error('Parse services failed', e);
  29. }
  30. }
  31. this.initDateData();
  32. // 从本地缓存恢复实名认证数据(返回上一级再进来时不丢失)
  33. this.restoreAuthData();
  34. },
  35. methods: {
  36. // --- 日期选择器逻辑 (简化版, 仅示意) ---
  37. initDateData() {
  38. const date = new Date();
  39. const year = date.getFullYear();
  40. for (let i = year; i <= year + 20; i++) {
  41. this.years.push(i);
  42. }
  43. for (let i = 1; i <= 12; i++) {
  44. this.months.push(i);
  45. }
  46. for (let i = 1; i <= 31; i++) {
  47. this.days.push(i);
  48. }
  49. },
  50. openDatePicker() {
  51. // this.showDatePicker = true;
  52. // 简单起见,这里可以用 uni.chooseImage 逻辑,但日期通常用 picker
  53. // 为了完全还原UI的自定义picker,这里先用原生 picker 简化,或者复用 form 的逻辑
  54. // 鉴于图1是普通的列表选择样式,这里暂时使用普通 picker 或者 input禁用+点击事件
  55. },
  56. onDateChange(e) {
  57. this.formData.expiryDate = e.detail.value;
  58. },
  59. // --- 数据持久化(防止返回上一级丢失) ---
  60. restoreAuthData() {
  61. try {
  62. const saved = uni.getStorageSync('recruit_auth_data');
  63. if (saved) {
  64. const d = JSON.parse(saved);
  65. this.formData.name = d.name || '';
  66. this.formData.idNumber = d.idNumber || '';
  67. this.formData.expiryDate = d.expiryDate || '';
  68. this.idCardFront = d.idCardFront || '';
  69. this.idCardBack = d.idCardBack || '';
  70. this.idCardFrontOssId = d.idCardFrontOssId || '';
  71. this.idCardBackOssId = d.idCardBackOssId || '';
  72. }
  73. } catch (e) {
  74. console.error('恢复认证数据失败', e);
  75. }
  76. },
  77. saveAuthData() {
  78. try {
  79. uni.setStorageSync('recruit_auth_data', JSON.stringify({
  80. name: this.formData.name,
  81. idNumber: this.formData.idNumber,
  82. expiryDate: this.formData.expiryDate,
  83. idCardFront: this.idCardFront,
  84. idCardBack: this.idCardBack,
  85. idCardFrontOssId: this.idCardFrontOssId,
  86. idCardBackOssId: this.idCardBackOssId
  87. }));
  88. } catch (e) {
  89. console.error('保存认证数据失败', e);
  90. }
  91. },
  92. // --- 图片上传(选择后自动上传到OSS) ---
  93. chooseImage(side) {
  94. uni.chooseImage({
  95. count: 1,
  96. sizeType: ['compressed'],
  97. sourceType: ['album', 'camera'],
  98. success: async (res) => {
  99. const tempPath = res.tempFilePaths[0];
  100. if (side === 'front') {
  101. this.idCardFront = tempPath;
  102. } else {
  103. this.idCardBack = tempPath;
  104. }
  105. // 上传到OSS
  106. try {
  107. uni.showLoading({ title: '上传中...' });
  108. const uploadRes = await uploadFile(tempPath);
  109. if (side === 'front') {
  110. this.idCardFrontOssId = uploadRes.data.ossId;
  111. } else {
  112. this.idCardBackOssId = uploadRes.data.ossId;
  113. }
  114. uni.hideLoading();
  115. this.saveAuthData();
  116. } catch (err) {
  117. uni.hideLoading();
  118. console.error('上传身份证图片失败:', err);
  119. }
  120. }
  121. });
  122. },
  123. // --- 提交 ---
  124. goToQualifications() {
  125. // 简单校验
  126. // 简单校验
  127. /*
  128. if (!this.formData.name || !this.formData.idNumber) {
  129. uni.showToast({ title: '请完善信息', icon: 'none' });
  130. return;
  131. }
  132. if (!this.idCardFront || !this.idCardBack) {
  133. uni.showToast({ title: '请上传证件照', icon: 'none' });
  134. return;
  135. }
  136. */
  137. // 保存认证数据到缓存
  138. this.saveAuthData();
  139. // 合并身份认证数据到已暂存的招募表单
  140. try {
  141. const stored = uni.getStorageSync('recruit_form_data')
  142. if (stored) {
  143. const data = JSON.parse(stored)
  144. data.realName = this.formData.name
  145. data.idNumber = this.formData.idNumber
  146. data.expiryDate = this.formData.expiryDate
  147. data.idCardFrontOssId = this.idCardFrontOssId
  148. data.idCardBackOssId = this.idCardBackOssId
  149. uni.setStorageSync('recruit_form_data', JSON.stringify(data))
  150. }
  151. } catch (e) {
  152. console.error('保存认证数据失败', e)
  153. }
  154. // 传递数据(服务类型对象数组 {id, name})
  155. const services = JSON.stringify(this.serviceType);
  156. uni.navigateTo({
  157. url: `/pages/recruit/qualifications?services=${encodeURIComponent(services)}`
  158. });
  159. }
  160. }
  161. }