logic.js 11 KB

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