auth_logic.js 8.3 KB

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