auth_logic.js 8.2 KB

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