logic.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import { sendSmsCode } from '@/api/auth'
  2. import { getAreaChildren, getServiceTypes } from '@/api/fulfiller'
  3. export default {
  4. data() {
  5. return {
  6. formData: {
  7. mobile: '',
  8. code: '',
  9. name: '',
  10. gender: 1, // 1男 2女
  11. birthday: '',
  12. password: '',
  13. serviceType: [],
  14. city: '',
  15. station: '',
  16. stationId: null
  17. },
  18. showPwd: false,
  19. isAgreed: false,
  20. serviceTypes: [],
  21. // 验证码倒计时
  22. countDown: 0,
  23. timer: null,
  24. // 日期选择器相关
  25. showPicker: false,
  26. years: [],
  27. months: [],
  28. days: [],
  29. pickerValue: [0, 0, 0],
  30. tempYear: 0,
  31. tempMonth: 0,
  32. tempDay: 0,
  33. // 城市选择器相关(从后端加载)
  34. showCityPicker: false,
  35. selectStep: 0,
  36. selectedPathway: [],
  37. currentList: [],
  38. selectedCityId: null,
  39. // 站点选择器相关(从后端加载)
  40. showStationPicker: false,
  41. stationList: [],
  42. // 协议弹窗
  43. showPrivacy: false,
  44. privacyTitle: '',
  45. privacyContent: ''
  46. }
  47. },
  48. created() {
  49. this.initDateData();
  50. this.loadServiceTypes();
  51. },
  52. beforeDestroy() {
  53. if (this.timer) clearInterval(this.timer);
  54. },
  55. methods: {
  56. initDateData() {
  57. const now = new Date();
  58. const currentYear = now.getFullYear();
  59. // 初始化年份 (1980 - 2030)
  60. for (let i = 1980; i <= currentYear + 5; i++) {
  61. this.years.push(i);
  62. }
  63. // 初始化月份
  64. for (let i = 1; i <= 12; i++) {
  65. this.months.push(i);
  66. }
  67. // 初始化日期 (默认31天, 实际应动态计算, 这里简化处理或在change中联动)
  68. for (let i = 1; i <= 31; i++) {
  69. this.days.push(i);
  70. }
  71. },
  72. // 打开选择器
  73. openPicker() {
  74. // 解析当前选中日期或默认日期
  75. const dateStr = this.formData.birthday || '2000-01-01';
  76. const [y, m, d] = dateStr.split('-').map(Number);
  77. // 查找索引
  78. const yIndex = this.years.indexOf(y);
  79. const mIndex = this.months.indexOf(m);
  80. const dIndex = this.days.indexOf(d);
  81. this.pickerValue = [
  82. yIndex > -1 ? yIndex : 0,
  83. mIndex > -1 ? mIndex : 0,
  84. dIndex > -1 ? dIndex : 0
  85. ];
  86. this.tempYear = this.years[this.pickerValue[0]];
  87. this.tempMonth = this.months[this.pickerValue[1]];
  88. this.tempDay = this.days[this.pickerValue[2]];
  89. this.showPicker = true;
  90. },
  91. closePicker() {
  92. this.showPicker = false;
  93. },
  94. onPickerChange(e) {
  95. const val = e.detail.value;
  96. this.tempYear = this.years[val[0]];
  97. this.tempMonth = this.months[val[1]];
  98. this.tempDay = this.days[val[2]];
  99. },
  100. confirmPicker() {
  101. // 格式化日期
  102. const mStr = this.tempMonth < 10 ? '0' + this.tempMonth : this.tempMonth;
  103. const dStr = this.tempDay < 10 ? '0' + this.tempDay : this.tempDay;
  104. this.formData.birthday = `${this.tempYear}-${mStr}-${dStr}`;
  105. this.closePicker();
  106. },
  107. async loadServiceTypes() {
  108. try {
  109. const res = await getServiceTypes();
  110. this.serviceTypes = (res.data || []).map(item => ({
  111. id: item.id,
  112. name: item.name
  113. }));
  114. } catch (err) {
  115. console.error('加载服务类型失败:', err);
  116. this.serviceTypes = [];
  117. }
  118. },
  119. toggleService(item) {
  120. const idx = this.formData.serviceType.indexOf(item.id);
  121. if (idx > -1) {
  122. this.formData.serviceType.splice(idx, 1);
  123. } else {
  124. this.formData.serviceType.push(item.id);
  125. }
  126. },
  127. // 验证码
  128. async getVerifyCode() {
  129. if (this.countDown > 0) return;
  130. if (!this.formData.mobile || this.formData.mobile.length !== 11) {
  131. uni.showToast({ title: '请输入正确的手机号', icon: 'none' });
  132. return;
  133. }
  134. try {
  135. const res = await sendSmsCode(this.formData.mobile);
  136. this.countDown = 60;
  137. this.timer = setInterval(() => {
  138. this.countDown--;
  139. if (this.countDown <= 0) clearInterval(this.timer);
  140. }, 1000);
  141. // TODO 【生产环境必须删除】开发模式自动填入验证码
  142. const devCode = res.data;
  143. if (devCode) {
  144. this.formData.code = devCode;
  145. uni.showToast({ title: '验证码: ' + devCode, icon: 'none', duration: 3000 });
  146. } else {
  147. uni.showToast({ title: '验证码已发送', icon: 'none' });
  148. }
  149. } catch (err) {
  150. console.error('发送验证码失败:', err);
  151. }
  152. },
  153. // 城市选择器 logic(从后端加载)
  154. async openCityPicker() {
  155. this.showCityPicker = true;
  156. if (this.selectedPathway.length === 0) {
  157. await this.resetCityPicker();
  158. }
  159. },
  160. async resetCityPicker() {
  161. this.selectStep = 0;
  162. this.selectedPathway = [];
  163. await this.loadAreaChildren(0);
  164. },
  165. closeCityPicker() {
  166. this.showCityPicker = false;
  167. },
  168. async loadAreaChildren(parentId) {
  169. try {
  170. const res = await getAreaChildren(parentId);
  171. // 城市选择器只显示 城市(0) 和 区域(1),不显示站点(2)
  172. this.currentList = (res.data || [])
  173. .filter(item => item.type !== 2)
  174. .map(item => ({
  175. id: item.id,
  176. name: item.name,
  177. type: item.type,
  178. parentId: item.parentId
  179. }));
  180. } catch (err) {
  181. console.error('加载区域数据失败:', err);
  182. this.currentList = [];
  183. }
  184. },
  185. async selectCityItem(item) {
  186. this.selectedPathway[this.selectStep] = item;
  187. // type: 0=城市, 1=区域
  188. // 城市级(0)继续加载子级区域
  189. if (item.type === 0) {
  190. this.selectStep++;
  191. this.selectedPathway = this.selectedPathway.slice(0, this.selectStep);
  192. await this.loadAreaChildren(item.id);
  193. // 如果已无子级区域,自动确认
  194. if (this.currentList.length === 0) {
  195. this.selectedCityId = item.id;
  196. this.confirmCity();
  197. }
  198. } else {
  199. // 区域级(1)选完即确认,站点由站点选择器单独加载
  200. this.selectedCityId = item.id;
  201. this.confirmCity();
  202. }
  203. },
  204. async jumpToStep(step) {
  205. this.selectStep = step;
  206. if (step === 0) {
  207. await this.loadAreaChildren(0);
  208. } else {
  209. const parent = this.selectedPathway[step - 1];
  210. if (parent) {
  211. await this.loadAreaChildren(parent.id);
  212. }
  213. }
  214. },
  215. confirmCity() {
  216. const fullPath = this.selectedPathway.map(i => i.name).join(' ');
  217. this.formData.city = fullPath;
  218. // 重置已选站点
  219. this.formData.station = '';
  220. this.formData.stationId = null;
  221. // 选完城市/区域后加载该区域下的站点(type=2)
  222. const lastSelected = this.selectedPathway[this.selectedPathway.length - 1];
  223. if (lastSelected) {
  224. this.loadStations(lastSelected.id);
  225. }
  226. this.closeCityPicker();
  227. },
  228. // 站点选择器(从后端加载,只取type=2的站点)
  229. async loadStations(parentId) {
  230. try {
  231. const res = await getAreaChildren(parentId);
  232. this.stationList = (res.data || [])
  233. .filter(item => item.type === 2)
  234. .map(item => ({
  235. id: item.id,
  236. name: item.name
  237. }));
  238. } catch (err) {
  239. console.error('加载站点数据失败:', err);
  240. this.stationList = [];
  241. }
  242. },
  243. openStationPicker() {
  244. if (this.stationList.length === 0) {
  245. uni.showToast({ title: '请先选择工作城市', icon: 'none' });
  246. return;
  247. }
  248. this.showStationPicker = true;
  249. },
  250. closeStationPicker() {
  251. this.showStationPicker = false;
  252. },
  253. selectStation(item) {
  254. this.formData.station = item.name;
  255. this.formData.stationId = item.id;
  256. this.closeStationPicker();
  257. },
  258. openPrivacy() {
  259. this.privacyTitle = '宠宝履约者说明';
  260. this.privacyContent = '1. 履约职责\n作为宠宝履约者,您需要按照平台标准完成宠物接送、喂遛或洗护服务,确保宠物安全与健康。\n\n2. 结算方式\n服务费用将根据订单类型和距离计算,定期结算至您的账户。具体结算周期请查看钱包说明。\n\n3. 行为规范\n请在这个过程中保持专业,穿着整洁,礼貌待人。严禁虐待宠物,违反者将承担法律责任。';
  261. this.showPrivacy = true;
  262. },
  263. goToAuth() {
  264. if (!this.isAgreed) {
  265. uni.showToast({ title: '请勾选协议', icon: 'none' });
  266. return;
  267. }
  268. if (!this.formData.mobile || this.formData.mobile.length !== 11) {
  269. uni.showToast({ title: '请输入正确的手机号', icon: 'none' });
  270. return;
  271. }
  272. if (!this.formData.name) {
  273. uni.showToast({ title: '请输入姓名', icon: 'none' });
  274. return;
  275. }
  276. if (this.formData.serviceType.length === 0) {
  277. uni.showToast({ title: '请选择服务类型', icon: 'none' });
  278. return;
  279. }
  280. // 暂存表单数据到本地,供后续页面组装提交
  281. uni.setStorageSync('recruit_form_data', JSON.stringify(this.formData));
  282. // 传递选中的服务类型对象(id+name)给后续页面
  283. const selectedServices = this.serviceTypes.filter(s => this.formData.serviceType.includes(s.id));
  284. const services = JSON.stringify(selectedServices);
  285. uni.navigateTo({
  286. url: `/pages/recruit/auth?services=${encodeURIComponent(services)}`
  287. });
  288. }
  289. }
  290. }