logic.js 11 KB

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