auth_logic.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. showPicker: false,
  16. pickerValue: [0, 0, 0], // YYYY-MM-DD
  17. years: [],
  18. months: [],
  19. days: [],
  20. tempYear: 0,
  21. tempMonth: 0,
  22. tempDay: 0,
  23. serviceType: [] // 接收上一页的服务类型
  24. }
  25. },
  26. onLoad(options) {
  27. if (options.services) {
  28. try {
  29. this.serviceType = JSON.parse(decodeURIComponent(options.services));
  30. } catch (e) {
  31. console.error('Parse services failed', e);
  32. }
  33. }
  34. this.initDateData();
  35. // 从本地缓存恢复实名认证数据(返回上一级再进来时不丢失)
  36. this.restoreAuthData();
  37. },
  38. methods: {
  39. // --- 日期选择器逻辑 ---
  40. initDateData() {
  41. const date = new Date();
  42. const year = date.getFullYear();
  43. for (let i = year; i <= year + 50; i++) {
  44. this.years.push(i);
  45. }
  46. for (let i = 1; i <= 12; i++) {
  47. this.months.push(i);
  48. }
  49. for (let i = 1; i <= 31; i++) {
  50. this.days.push(i);
  51. }
  52. },
  53. openPicker() {
  54. const date = new Date();
  55. const dateStr = this.formData.expiryDate || `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
  56. const [y, m, d] = dateStr.split('-').map(Number);
  57. let yIndex = this.years.indexOf(y);
  58. let mIndex = this.months.indexOf(m);
  59. let dIndex = this.days.indexOf(d);
  60. this.pickerValue = [
  61. yIndex > -1 ? yIndex : 0,
  62. mIndex > -1 ? mIndex : 0,
  63. dIndex > -1 ? dIndex : 0
  64. ];
  65. this.tempYear = this.years[this.pickerValue[0]];
  66. this.tempMonth = this.months[this.pickerValue[1]];
  67. this.tempDay = this.days[this.pickerValue[2]];
  68. this.showPicker = true;
  69. },
  70. closePicker() {
  71. this.showPicker = false;
  72. },
  73. onPickerChange(e) {
  74. const val = e.detail.value;
  75. this.tempYear = this.years[val[0]];
  76. this.tempMonth = this.months[val[1]];
  77. this.tempDay = this.days[val[2]];
  78. },
  79. confirmPicker() {
  80. const mStr = this.tempMonth < 10 ? '0' + this.tempMonth : this.tempMonth;
  81. const dStr = this.tempDay < 10 ? '0' + this.tempDay : this.tempDay;
  82. this.formData.expiryDate = `${this.tempYear}-${mStr}-${dStr}`;
  83. this.closePicker();
  84. },
  85. // --- 数据持久化(防止返回上一级丢失) ---
  86. restoreAuthData() {
  87. try {
  88. const saved = uni.getStorageSync('recruit_auth_data');
  89. if (saved) {
  90. const d = JSON.parse(saved);
  91. this.formData.name = d.name || '';
  92. this.formData.idNumber = d.idNumber || '';
  93. this.formData.expiryDate = d.expiryDate || '';
  94. this.idCardFront = d.idCardFront || '';
  95. this.idCardBack = d.idCardBack || '';
  96. this.idCardFrontOssId = d.idCardFrontOssId || '';
  97. this.idCardBackOssId = d.idCardBackOssId || '';
  98. }
  99. } catch (e) {
  100. console.error('恢复认证数据失败', e);
  101. }
  102. },
  103. saveAuthData() {
  104. try {
  105. uni.setStorageSync('recruit_auth_data', JSON.stringify({
  106. name: this.formData.name,
  107. idNumber: this.formData.idNumber,
  108. expiryDate: this.formData.expiryDate,
  109. idCardFront: this.idCardFront,
  110. idCardBack: this.idCardBack,
  111. idCardFrontOssId: this.idCardFrontOssId,
  112. idCardBackOssId: this.idCardBackOssId
  113. }));
  114. } catch (e) {
  115. console.error('保存认证数据失败', e);
  116. }
  117. },
  118. // --- 图片上传(选择后自动上传到OSS) ---
  119. chooseImage(side) {
  120. uni.chooseImage({
  121. count: 1,
  122. sizeType: ['compressed'],
  123. sourceType: ['album', 'camera'],
  124. success: async (res) => {
  125. const tempPath = res.tempFilePaths[0];
  126. if (side === 'front') {
  127. this.idCardFront = tempPath;
  128. } else {
  129. this.idCardBack = tempPath;
  130. }
  131. // 上传到OSS
  132. try {
  133. uni.showLoading({ title: '上传中...' });
  134. const uploadRes = await uploadFile(tempPath);
  135. if (side === 'front') {
  136. this.idCardFrontOssId = uploadRes.data.ossId;
  137. } else {
  138. this.idCardBackOssId = uploadRes.data.ossId;
  139. }
  140. uni.hideLoading();
  141. this.saveAuthData();
  142. } catch (err) {
  143. uni.hideLoading();
  144. console.error('上传身份证图片失败:', err);
  145. }
  146. }
  147. });
  148. },
  149. // --- 提交 ---
  150. goToQualifications() {
  151. // 简单校验
  152. // 简单校验
  153. /*
  154. if (!this.formData.name || !this.formData.idNumber) {
  155. uni.showToast({ title: '请完善信息', icon: 'none' });
  156. return;
  157. }
  158. if (!this.idCardFront || !this.idCardBack) {
  159. uni.showToast({ title: '请上传证件照', icon: 'none' });
  160. return;
  161. }
  162. */
  163. // 保存认证数据到缓存
  164. this.saveAuthData();
  165. // 合并身份认证数据到已暂存的招募表单
  166. try {
  167. const stored = uni.getStorageSync('recruit_form_data')
  168. if (stored) {
  169. const data = JSON.parse(stored)
  170. data.realName = this.formData.name
  171. data.idNumber = this.formData.idNumber
  172. data.expiryDate = this.formData.expiryDate
  173. data.idCardFrontOssId = this.idCardFrontOssId
  174. data.idCardBackOssId = this.idCardBackOssId
  175. uni.setStorageSync('recruit_form_data', JSON.stringify(data))
  176. }
  177. } catch (e) {
  178. console.error('保存认证数据失败', e)
  179. }
  180. // 传递数据(服务类型对象数组 {id, name})
  181. const services = JSON.stringify(this.serviceType);
  182. uni.navigateTo({
  183. url: `/pages/recruit/qualifications?services=${encodeURIComponent(services)}`
  184. });
  185. }
  186. }
  187. }