logic.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. import { sendSmsCode } from '@/api/resource/sms'
  2. import { getAreaChildren } from '@/api/fulfiller/app'
  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. city: '',
  17. station: '',
  18. stationId: null
  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. showCityPicker: false,
  37. selectStep: 0,
  38. selectedPathway: [],
  39. currentList: [],
  40. selectedCityId: null,
  41. // 站点选择器相关(从后端加载)
  42. showStationPicker: false,
  43. stationList: [],
  44. // 协议弹窗
  45. showPrivacy: false,
  46. agreementTitle: '', // 协议标题
  47. agreementContent: '', // 协议内容
  48. currentAgreementId: '' // 当前协议ID
  49. }
  50. },
  51. created() {
  52. this.initDateData();
  53. this.loadServiceTypes();
  54. },
  55. beforeDestroy() {
  56. if (this.timer) clearInterval(this.timer);
  57. },
  58. methods: {
  59. initDateData() {
  60. const now = new Date();
  61. const currentYear = now.getFullYear();
  62. // 初始化年份 (1980 - 2030)
  63. for (let i = 1980; i <= currentYear + 5; i++) {
  64. this.years.push(i);
  65. }
  66. // 初始化月份
  67. for (let i = 1; i <= 12; i++) {
  68. this.months.push(i);
  69. }
  70. // 初始化日期 (默认31天, 实际应动态计算, 这里简化处理或在change中联动)
  71. for (let i = 1; i <= 31; i++) {
  72. this.days.push(i);
  73. }
  74. },
  75. // 打开选择器
  76. openPicker() {
  77. // 解析当前选中日期或默认日期
  78. const dateStr = this.formData.birthday || '2000-01-01';
  79. const [y, m, d] = dateStr.split('-').map(Number);
  80. // 查找索引
  81. const yIndex = this.years.indexOf(y);
  82. const mIndex = this.months.indexOf(m);
  83. const dIndex = this.days.indexOf(d);
  84. this.pickerValue = [
  85. yIndex > -1 ? yIndex : 0,
  86. mIndex > -1 ? mIndex : 0,
  87. dIndex > -1 ? dIndex : 0
  88. ];
  89. this.tempYear = this.years[this.pickerValue[0]];
  90. this.tempMonth = this.months[this.pickerValue[1]];
  91. this.tempDay = this.days[this.pickerValue[2]];
  92. this.showPicker = true;
  93. },
  94. closePicker() {
  95. this.showPicker = false;
  96. },
  97. onPickerChange(e) {
  98. const val = e.detail.value;
  99. this.tempYear = this.years[val[0]];
  100. this.tempMonth = this.months[val[1]];
  101. this.tempDay = this.days[val[2]];
  102. },
  103. confirmPicker() {
  104. // 格式化日期
  105. const mStr = this.tempMonth < 10 ? '0' + this.tempMonth : this.tempMonth;
  106. const dStr = this.tempDay < 10 ? '0' + this.tempDay : this.tempDay;
  107. this.formData.birthday = `${this.tempYear}-${mStr}-${dStr}`;
  108. this.closePicker();
  109. },
  110. async loadServiceTypes() {
  111. try {
  112. const res = await listAllService();
  113. this.serviceTypes = (res.data || []).map(item => ({
  114. id: item.id,
  115. name: item.name
  116. }));
  117. } catch (err) {
  118. console.error('加载服务类型失败:', err);
  119. this.serviceTypes = [];
  120. }
  121. },
  122. toggleService(item) {
  123. const idx = this.formData.serviceType.indexOf(item.id);
  124. if (idx > -1) {
  125. this.formData.serviceType.splice(idx, 1);
  126. } else {
  127. this.formData.serviceType.push(item.id);
  128. }
  129. },
  130. // 验证码
  131. /* async getVerifyCode() {
  132. if (this.countDown > 0) return;
  133. if (!this.formData.mobile || this.formData.mobile.length !== 11) {
  134. uni.showToast({ title: '请输入正确的手机号', icon: 'none' });
  135. return;
  136. }
  137. try {
  138. const res = await sendSmsCode(this.formData.mobile);
  139. this.countDown = 60;
  140. this.timer = setInterval(() => {
  141. this.countDown--;
  142. if (this.countDown <= 0) clearInterval(this.timer);
  143. }, 1000);
  144. // TODO 【生产环境必须删除】开发模式自动填入验证码
  145. const devCode = res.data;
  146. if (devCode) {
  147. this.formData.code = devCode;
  148. uni.showToast({ title: '验证码: ' + devCode, icon: 'none', duration: 3000 });
  149. } else {
  150. uni.showToast({ title: '验证码已发送', icon: 'none' });
  151. }
  152. } catch (err) {
  153. console.error('发送验证码失败:', err);
  154. }
  155. }, */
  156. // 城市选择器 logic(从后端加载)
  157. async openCityPicker() {
  158. this.showCityPicker = true;
  159. if (this.selectedPathway.length === 0) {
  160. await this.resetCityPicker();
  161. }
  162. },
  163. async resetCityPicker() {
  164. this.selectStep = 0;
  165. this.selectedPathway = [];
  166. await this.loadAreaChildren(0);
  167. },
  168. closeCityPicker() {
  169. this.showCityPicker = false;
  170. },
  171. async loadAreaChildren(parentId) {
  172. try {
  173. const res = await getAreaChildren(parentId);
  174. // 城市选择器只显示 城市(0) 和 区域(1),不显示站点(2)
  175. this.currentList = (res.data || [])
  176. .filter(item => item.type !== 2)
  177. .map(item => ({
  178. id: item.id,
  179. name: item.name,
  180. type: item.type,
  181. parentId: item.parentId
  182. }));
  183. } catch (err) {
  184. console.error('加载区域数据失败:', err);
  185. this.currentList = [];
  186. }
  187. },
  188. async selectCityItem(item) {
  189. this.selectedPathway[this.selectStep] = item;
  190. // type: 0=城市, 1=区域
  191. // 城市级(0)继续加载子级区域
  192. if (item.type === 0) {
  193. this.selectStep++;
  194. this.selectedPathway = this.selectedPathway.slice(0, this.selectStep);
  195. await this.loadAreaChildren(item.id);
  196. // 如果已无子级区域,自动确认
  197. if (this.currentList.length === 0) {
  198. this.selectedCityId = item.id;
  199. this.confirmCity();
  200. }
  201. } else {
  202. // 区域级(1)选完即确认,站点由站点选择器单独加载
  203. this.selectedCityId = item.id;
  204. this.confirmCity();
  205. }
  206. },
  207. async jumpToStep(step) {
  208. this.selectStep = step;
  209. if (step === 0) {
  210. await this.loadAreaChildren(0);
  211. } else {
  212. const parent = this.selectedPathway[step - 1];
  213. if (parent) {
  214. await this.loadAreaChildren(parent.id);
  215. }
  216. }
  217. },
  218. confirmCity() {
  219. const fullPath = this.selectedPathway.map(i => i.name).join(' ');
  220. this.formData.city = fullPath;
  221. // 重置已选站点
  222. this.formData.station = '';
  223. this.formData.stationId = null;
  224. // 选完城市/区域后加载该区域下的站点(type=2)
  225. const lastSelected = this.selectedPathway[this.selectedPathway.length - 1];
  226. if (lastSelected) {
  227. this.loadStations(lastSelected.id);
  228. }
  229. this.closeCityPicker();
  230. },
  231. // 站点选择器(从后端加载,只取type=2的站点)
  232. async loadStations(parentId) {
  233. try {
  234. const res = await getAreaChildren(parentId);
  235. this.stationList = (res.data || [])
  236. .filter(item => item.type === 2)
  237. .map(item => ({
  238. id: item.id,
  239. name: item.name
  240. }));
  241. } catch (err) {
  242. console.error('加载站点数据失败:', err);
  243. this.stationList = [];
  244. }
  245. },
  246. openStationPicker() {
  247. if (this.stationList.length === 0) {
  248. uni.showToast({ title: '请先选择工作城市', icon: 'none' });
  249. return;
  250. }
  251. this.showStationPicker = true;
  252. },
  253. closeStationPicker() {
  254. this.showStationPicker = false;
  255. },
  256. selectStation(item) {
  257. this.formData.station = item.name;
  258. this.formData.stationId = item.id;
  259. this.closeStationPicker();
  260. },
  261. async openPrivacy() {
  262. try {
  263. uni.showLoading({ title: '加载中...' });
  264. const res = await getAgreement(3); // 3-履约者说明
  265. if (res.code === 200 && res.data) {
  266. this.agreementTitle = res.data.title;
  267. this.agreementContent = res.data.content;
  268. this.showPrivacy = true;
  269. } else {
  270. uni.showToast({ title: res.msg || '获取协议失败', icon: 'none' });
  271. }
  272. } catch (err) {
  273. console.error('获取协议详情失败:', err);
  274. } finally {
  275. uni.hideLoading();
  276. }
  277. },
  278. goToAuth() {
  279. if (!this.isAgreed) {
  280. uni.showToast({ title: '请勾选协议', icon: 'none' });
  281. return;
  282. }
  283. if (!this.formData.mobile || this.formData.mobile.length !== 11) {
  284. uni.showToast({ title: '请输入正确的手机号', icon: 'none' });
  285. return;
  286. }
  287. if (!this.formData.name) {
  288. uni.showToast({ title: '请输入姓名', icon: 'none' });
  289. return;
  290. }
  291. if (this.formData.serviceType.length === 0) {
  292. uni.showToast({ title: '请选择服务类型', icon: 'none' });
  293. return;
  294. }
  295. // 暂存表单数据到本地,供后续页面组装提交
  296. uni.setStorageSync('recruit_form_data', JSON.stringify(this.formData));
  297. // 传递选中的服务类型对象(id+name)给后续页面
  298. const selectedServices = this.serviceTypes.filter(s => this.formData.serviceType.includes(s.id));
  299. const services = JSON.stringify(selectedServices);
  300. uni.navigateTo({
  301. url: `/pages/recruit/auth?services=${encodeURIComponent(services)}`
  302. });
  303. }
  304. }
  305. }