logic.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. import { sendSmsCode } from '@/api/resource/sms'
  2. import { getAreaStationList } from '@/api/system/areaStation'
  3. import { listAllService } from '@/api/service/list'
  4. import { getAgreement } from '@/api/system/agreement'
  5. export default {
  6. data() {
  7. return {
  8. formData: {
  9. mobile: '',
  10. code: '',
  11. name: '',
  12. gender: 1, // 1男 2女
  13. birthday: '',
  14. password: '',
  15. serviceType: [],
  16. station: '',
  17. stationId: null,
  18. areaPath: '' // 用于回显“区域+站点”名称
  19. },
  20. showPwd: false,
  21. isAgreed: false,
  22. serviceTypes: [],
  23. // 验证码倒计时
  24. countDown: 0,
  25. timer: null,
  26. // 日期选择器相关
  27. showPicker: false,
  28. years: [],
  29. months: [],
  30. days: [],
  31. pickerValue: [0, 0, 0],
  32. tempYear: 0,
  33. tempMonth: 0,
  34. tempDay: 0,
  35. // 站点选择器(级联版)
  36. showStationPickerCascader: false,
  37. selectStep: 0,
  38. selectedPathway: [],
  39. currentList: [],
  40. fullStationData: [], // 全量数据
  41. selectedStationId: null,
  42. // 协议弹窗
  43. showPrivacy: false,
  44. agreementTitle: '', // 协议标题
  45. agreementContent: '', // 协议内容
  46. currentAgreementId: '' // 当前协议ID
  47. }
  48. },
  49. onLoad() {
  50. this.initDateData();
  51. this.loadServiceTypes();
  52. this.loadAreaStationData(); // 预加载站点全量数据
  53. // 尝试从缓存中恢复数据(回显)
  54. this.restoreFormData();
  55. },
  56. beforeDestroy() {
  57. if (this.timer) clearInterval(this.timer);
  58. },
  59. methods: {
  60. async loadAreaStationData() {
  61. try {
  62. const res = await getAreaStationList();
  63. this.fullStationData = res.data || [];
  64. // 暂时保存在内存
  65. } catch (err) {
  66. console.error('加载站点列表失败:', err);
  67. }
  68. },
  69. restoreFormData() {
  70. try {
  71. const saved = uni.getStorageSync('recruit_form_data');
  72. if (saved) {
  73. const d = JSON.parse(saved);
  74. // 深度合并或手动赋值
  75. Object.assign(this.formData, d);
  76. // 恢复私有的路径状态
  77. if (d._selectedPathway) {
  78. this.selectedPathway = d._selectedPathway;
  79. this.selectStep = this.selectedPathway.length;
  80. }
  81. // 加载站点列表(如果选了区域的话)
  82. if (this.selectedPathway.length > 0) {
  83. const last = this.selectedPathway[this.selectedPathway.length - 1];
  84. if (last) this.loadStations(last.id);
  85. }
  86. }
  87. } catch (e) {
  88. console.error('恢复表单数据失败', e);
  89. }
  90. },
  91. initDateData() {
  92. const now = new Date();
  93. const currentYear = now.getFullYear();
  94. // 初始化年份 (1980 - 2030)
  95. for (let i = 1980; i <= currentYear + 5; i++) {
  96. this.years.push(i);
  97. }
  98. // 初始化月份
  99. for (let i = 1; i <= 12; i++) {
  100. this.months.push(i);
  101. }
  102. // 初始化日期 (默认31天, 实际应动态计算, 这里简化处理或在change中联动)
  103. for (let i = 1; i <= 31; i++) {
  104. this.days.push(i);
  105. }
  106. },
  107. // 打开选择器
  108. openPicker() {
  109. // 解析当前选中日期或默认日期
  110. const dateStr = this.formData.birthday || '2000-01-01';
  111. const [y, m, d] = dateStr.split('-').map(Number);
  112. // 查找索引
  113. const yIndex = this.years.indexOf(y);
  114. const mIndex = this.months.indexOf(m);
  115. const dIndex = this.days.indexOf(d);
  116. this.pickerValue = [
  117. yIndex > -1 ? yIndex : 0,
  118. mIndex > -1 ? mIndex : 0,
  119. dIndex > -1 ? dIndex : 0
  120. ];
  121. this.tempYear = this.years[this.pickerValue[0]];
  122. this.tempMonth = this.months[this.pickerValue[1]];
  123. this.tempDay = this.days[this.pickerValue[2]];
  124. this.showPicker = true;
  125. },
  126. closePicker() {
  127. this.showPicker = false;
  128. },
  129. onPickerChange(e) {
  130. const val = e.detail.value;
  131. this.tempYear = this.years[val[0]];
  132. this.tempMonth = this.months[val[1]];
  133. this.tempDay = this.days[val[2]];
  134. },
  135. confirmPicker() {
  136. // 格式化日期
  137. const mStr = this.tempMonth < 10 ? '0' + this.tempMonth : this.tempMonth;
  138. const dStr = this.tempDay < 10 ? '0' + this.tempDay : this.tempDay;
  139. this.formData.birthday = `${this.tempYear}-${mStr}-${dStr}`;
  140. this.closePicker();
  141. },
  142. async loadServiceTypes() {
  143. try {
  144. const res = await listAllService();
  145. this.serviceTypes = (res.data || []).map(item => ({
  146. id: item.id,
  147. name: item.name
  148. }));
  149. } catch (err) {
  150. console.error('加载服务类型失败:', err);
  151. this.serviceTypes = [];
  152. }
  153. },
  154. toggleService(item) {
  155. const idx = this.formData.serviceType.indexOf(item.id);
  156. if (idx > -1) {
  157. this.formData.serviceType.splice(idx, 1);
  158. } else {
  159. this.formData.serviceType.push(item.id);
  160. }
  161. },
  162. // 验证码
  163. /* async getVerifyCode() {
  164. if (this.countDown > 0) return;
  165. if (!this.formData.mobile || this.formData.mobile.length !== 11) {
  166. uni.showToast({ title: '请输入正确的手机号', icon: 'none' });
  167. return;
  168. }
  169. try {
  170. const res = await sendSmsCode(this.formData.mobile);
  171. this.countDown = 60;
  172. this.timer = setInterval(() => {
  173. this.countDown--;
  174. if (this.countDown <= 0) clearInterval(this.timer);
  175. }, 1000);
  176. // TODO 【生产环境必须删除】开发模式自动填入验证码
  177. const devCode = res.data;
  178. if (devCode) {
  179. this.formData.code = devCode;
  180. uni.showToast({ title: '验证码: ' + devCode, icon: 'none', duration: 3000 });
  181. } else {
  182. uni.showToast({ title: '验证码已发送', icon: 'none' });
  183. }
  184. } catch (err) {
  185. console.error('发送验证码失败:', err);
  186. }
  187. }, */
  188. // 站点级联选择逻辑 (从全量本地数据中根据 parentId 过滤)
  189. async openStationPickerCascader() {
  190. this.showStationPickerCascader = true;
  191. if (this.selectedPathway.length === 0) {
  192. await this.resetStationPicker();
  193. }
  194. },
  195. async resetStationPicker() {
  196. this.selectStep = 0;
  197. this.selectedPathway = [];
  198. this.filterLocalChildren(0);
  199. },
  200. closeStationPickerCascader() {
  201. this.showStationPickerCascader = false;
  202. },
  203. filterLocalChildren(parentId) {
  204. // 从全量数据中筛选当前层级的子项
  205. this.currentList = this.fullStationData.filter(item => item.parentId == parentId);
  206. },
  207. async selectStationItem(item) {
  208. this.selectedPathway[this.selectStep] = item;
  209. // 在全量数据中查找这是否有子节点 (子级联)
  210. const sons = this.fullStationData.filter(i => i.parentId == item.id);
  211. if (sons.length > 0) {
  212. // 进入下一步
  213. this.selectStep++;
  214. this.selectedPathway = this.selectedPathway.slice(0, this.selectStep);
  215. this.currentList = sons;
  216. } else {
  217. // 没有子节点了,说明是最终的站点(叶子节点)
  218. this.confirmStation();
  219. }
  220. },
  221. async jumpToStep(step) {
  222. this.selectStep = step;
  223. if (step === 0) {
  224. this.filterLocalChildren(0);
  225. } else {
  226. const parent = this.selectedPathway[step - 1];
  227. if (parent) {
  228. this.filterLocalChildren(parent.id);
  229. }
  230. }
  231. },
  232. confirmStation() {
  233. const path = this.selectedPathway.map(i => i.name);
  234. const stationName = path[path.length - 1];
  235. const areaName = path.slice(0, -1).join(' '); // 排除站点后的父级名
  236. this.formData.station = stationName;
  237. this.formData.stationId = this.selectedPathway[this.selectedPathway.length - 1].id;
  238. this.formData.areaPath = areaName;
  239. this.closeStationPickerCascader();
  240. },
  241. // --- 废弃的功能逻辑 (已被站点选择合并或移除) ---
  242. /* async loadStations(parentId) { ... } */
  243. /* openStationPicker() { ... } */
  244. /* selectStation(item) { ... } */
  245. /* async openCityPicker() { ... } */
  246. /* loadAreaChildren(parentId) { ... } */
  247. /* async selectCityItem(item) { ... } */
  248. /* confirmCity() { ... } */
  249. async openPrivacy() {
  250. try {
  251. uni.showLoading({ title: '加载中...' });
  252. const res = await getAgreement(3); // 3-履约者说明
  253. if (res.code === 200 && res.data) {
  254. this.agreementTitle = res.data.title;
  255. this.agreementContent = res.data.content;
  256. this.showPrivacy = true;
  257. } else {
  258. uni.showToast({ title: res.msg || '获取协议失败', icon: 'none' });
  259. }
  260. } catch (err) {
  261. console.error('获取协议详情失败:', err);
  262. } finally {
  263. uni.hideLoading();
  264. }
  265. },
  266. goToAuth() {
  267. if (!this.isAgreed) {
  268. uni.showToast({ title: '请勾选协议', icon: 'none' });
  269. return;
  270. }
  271. if (!this.formData.mobile || this.formData.mobile.length !== 11) {
  272. uni.showToast({ title: '请输入正确的手机号', icon: 'none' });
  273. return;
  274. }
  275. if (!this.formData.name) {
  276. uni.showToast({ title: '请输入姓名', icon: 'none' });
  277. return;
  278. }
  279. if (this.formData.serviceType.length === 0) {
  280. uni.showToast({ title: '请选择服务类型', icon: 'none' });
  281. return;
  282. }
  283. if (!this.formData.stationId) {
  284. uni.showToast({ title: '请选择所属站点', icon: 'none' });
  285. return;
  286. }
  287. // 暂存表单数据到本地,供后续页面组装提交
  288. // 同时保存 selectedPathway 确保站点级联状态能恢复
  289. uni.setStorageSync('recruit_form_data', JSON.stringify({
  290. ...this.formData,
  291. _selectedPathway: this.selectedPathway // 私有存储,仅用于回显
  292. }));
  293. // 传递选中的服务类型对象(id+name)给后续页面
  294. const selectedServices = this.serviceTypes.filter(s => this.formData.serviceType.includes(s.id));
  295. const services = JSON.stringify(selectedServices);
  296. uni.navigateTo({
  297. url: `/pages/recruit/auth?services=${encodeURIComponent(services)}`
  298. });
  299. }
  300. }
  301. }