index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const common_config = require("../../common/config.js");
  4. const api_user = require("../../api/user.js");
  5. const _sfc_main = {
  6. data() {
  7. return {
  8. formData: {
  9. name: "",
  10. gender: "",
  11. phone: "",
  12. email: "",
  13. idCard: "",
  14. emergencyContact: "",
  15. emergencyPhone: "",
  16. relation: ""
  17. },
  18. relationOptions: ["父母", "配偶", "子女", "兄弟姐妹", "朋友", "其他"],
  19. relationIndex: 0,
  20. isAgreed: false,
  21. userInfoFilled: false,
  22. jobList: [],
  23. selectedJobId: null,
  24. selectedJob: null,
  25. isPhoneValid: false,
  26. isNameValid: false,
  27. isIdCardValid: false,
  28. genderOptions: ["男", "女"],
  29. genderIndex: 0,
  30. genderMapping: {
  31. "男": "1",
  32. "女": "2",
  33. "1": "男",
  34. "2": "女"
  35. }
  36. };
  37. },
  38. onLoad(options) {
  39. console.log("options:", options);
  40. if (options.scene) {
  41. const scene = decodeURIComponent(options.scene);
  42. console.log("解码后的scene:", scene);
  43. const sceneParams = {};
  44. scene.split("&").forEach((pair) => {
  45. const [key, value] = pair.split("=");
  46. sceneParams[key] = value;
  47. });
  48. console.log("解析后的参数:", sceneParams);
  49. }
  50. this.checkUserInfo();
  51. this.fetchJobList();
  52. },
  53. computed: {
  54. canSubmit() {
  55. return this.formData.name.trim() && this.formData.gender && this.formData.phone.trim() && /^1\d{10}$/.test(this.formData.phone) && (!this.formData.email || /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(this.formData.email)) && this.formData.idCard.trim() && this.formData.emergencyContact.trim() && this.formData.emergencyPhone.trim() && /^1\d{10}$/.test(this.formData.emergencyPhone) && this.formData.relation && this.isAgreed;
  56. },
  57. canSubmitSimple() {
  58. return this.formData.name.trim() && this.formData.phone.trim() && this.isPhoneValid && this.formData.idCard.trim() && this.isIdCardValid && this.isAgreed;
  59. }
  60. },
  61. methods: {
  62. checkLogin() {
  63. const userInfo = common_vendor.index.getStorageSync("userInfo");
  64. if (!userInfo) {
  65. common_vendor.index.reLaunch({
  66. url: "/pages/login/login",
  67. success: () => {
  68. console.log("跳转到身份验证登录页面成功");
  69. },
  70. fail: (err) => {
  71. console.error("跳转到身份验证登录页面失败:", err);
  72. common_vendor.index.showToast({
  73. title: "跳转失败,请重试",
  74. icon: "none"
  75. });
  76. }
  77. });
  78. return false;
  79. }
  80. try {
  81. const parsedUserInfo = JSON.parse(userInfo);
  82. if (!parsedUserInfo.openid) {
  83. common_vendor.index.navigateTo({
  84. url: "/pages/login/login"
  85. });
  86. return false;
  87. }
  88. return true;
  89. } catch (e) {
  90. console.error("解析用户信息失败:", e);
  91. common_vendor.index.removeStorageSync("userInfo");
  92. common_vendor.index.navigateTo({
  93. url: "/pages/login/login"
  94. });
  95. return false;
  96. }
  97. },
  98. goHome() {
  99. common_vendor.index.navigateBack({
  100. delta: 1
  101. });
  102. },
  103. toggleAgreement() {
  104. this.isAgreed = !this.isAgreed;
  105. },
  106. relationChange(e) {
  107. this.relationIndex = e.detail.value;
  108. this.formData.relation = this.relationOptions[this.relationIndex];
  109. },
  110. checkUserInfo() {
  111. common_vendor.index.showLoading({
  112. title: "加载中..."
  113. });
  114. try {
  115. const userInfo = JSON.parse(common_vendor.index.getStorageSync("userInfo"));
  116. console.log("id:", userInfo.id);
  117. api_user.getUserInfo(userInfo.id, userInfo.openid).then((res) => {
  118. common_vendor.index.hideLoading();
  119. const userData = res;
  120. if (!userData.name || !userData.phone) {
  121. this.userInfoFilled = true;
  122. this.formData.name = userData.name || "";
  123. if (userData.gender) {
  124. this.formData.gender = this.genderMapping[userData.gender] || userData.gender;
  125. const index = this.genderOptions.findIndex((item) => item === this.formData.gender);
  126. if (index !== -1) {
  127. this.genderIndex = index;
  128. }
  129. } else {
  130. this.formData.gender = "";
  131. }
  132. this.formData.phone = userData.phone || "";
  133. this.formData.idCard = userData.id_card || "";
  134. this.formData.emergencyContact = userData.emergency_contact || "";
  135. this.formData.emergencyPhone = userData.emergency_phone || "";
  136. this.formData.relation = userData.relation || "";
  137. if (userData.relation) {
  138. const index = this.relationOptions.findIndex((item) => item === userData.relation);
  139. if (index !== -1) {
  140. this.relationIndex = index;
  141. }
  142. }
  143. }
  144. }).catch((err) => {
  145. common_vendor.index.hideLoading();
  146. console.error("获取用户信息失败:", err);
  147. common_vendor.index.showToast({
  148. title: "获取用户信息失败",
  149. icon: "none"
  150. });
  151. });
  152. } catch (e) {
  153. common_vendor.index.hideLoading();
  154. console.error("获取用户信息失败:", e);
  155. common_vendor.index.showToast({
  156. title: "获取用户信息失败",
  157. icon: "none"
  158. });
  159. }
  160. },
  161. fetchJobList() {
  162. common_vendor.index.showLoading({
  163. title: "加载职位列表..."
  164. });
  165. api_user.getJobList().then((res) => {
  166. common_vendor.index.hideLoading();
  167. console.log("职位列表数据:", res);
  168. this.jobList = Array.isArray(res) ? res.filter((job) => job !== null && job !== void 0) : [];
  169. if (this.jobList.length === 0) {
  170. common_vendor.index.showToast({
  171. title: "暂无可用职位",
  172. icon: "none"
  173. });
  174. }
  175. }).catch((err) => {
  176. common_vendor.index.hideLoading();
  177. console.error("获取职位列表失败:", err);
  178. this.jobList = [];
  179. common_vendor.index.showToast({
  180. title: "网络错误,请稍后重试",
  181. icon: "none"
  182. });
  183. });
  184. },
  185. selectJob(job) {
  186. try {
  187. common_vendor.index.removeStorageSync("interviewQuestions");
  188. common_vendor.index.removeStorageSync("currentQuestionIndex");
  189. } catch (e) {
  190. console.error("清除问题缓存失败:", e);
  191. }
  192. this.selectedJobId = job.id;
  193. this.selectedJob = job;
  194. common_vendor.index.setStorageSync("selectedJob", JSON.stringify(job));
  195. console.log("已选择职位:", job.id, job.title);
  196. },
  197. // 获取当前职位配置
  198. getConfig() {
  199. common_vendor.index.request({
  200. url: `${common_config.apiBaseUrl}/api/job/config/position/${this.selectedJobId}`,
  201. method: "GET",
  202. data: {
  203. openid: JSON.parse(common_vendor.index.getStorageSync("userInfo")).openid
  204. },
  205. header: {
  206. "content-type": "application/x-www-form-urlencoded"
  207. },
  208. success: (res) => {
  209. console.log(res);
  210. if (res.statusCode === 200) {
  211. if (res.data.code === 2e3) {
  212. common_vendor.index.setStorageSync("configData", JSON.stringify(res.data.data));
  213. common_vendor.index.navigateTo({
  214. url: "/pages/Personal/Personal",
  215. fail: (err) => {
  216. console.error("页面跳转失败:", err);
  217. common_vendor.index.showToast({
  218. title: "页面跳转失败",
  219. icon: "none"
  220. });
  221. }
  222. });
  223. }
  224. } else {
  225. common_vendor.index.hideLoading();
  226. }
  227. },
  228. fail: (err) => {
  229. common_vendor.index.hideLoading();
  230. common_vendor.index.showToast({
  231. title: "网络错误,请稍后重试",
  232. icon: "none"
  233. });
  234. }
  235. });
  236. },
  237. applyForJob() {
  238. if (!this.checkLogin()) {
  239. return;
  240. }
  241. if (!this.selectedJobId) {
  242. common_vendor.index.showToast({
  243. title: "请选择一个职位",
  244. icon: "none"
  245. });
  246. return;
  247. }
  248. common_vendor.index.setStorageSync("selectedJob", JSON.stringify(this.selectedJob));
  249. api_user.applyJob({
  250. job_id: this.selectedJobId,
  251. tenant_id: 1,
  252. openid: JSON.parse(common_vendor.index.getStorageSync("userInfo")).openid
  253. }).then((res) => {
  254. if (res && res.id) {
  255. common_vendor.index.setStorageSync("appId", res.id);
  256. try {
  257. const userInfo = JSON.parse(common_vendor.index.getStorageSync("userInfo") || "{}");
  258. userInfo.appId = res.id;
  259. common_vendor.index.setStorageSync("userInfo", JSON.stringify(userInfo));
  260. } catch (e) {
  261. console.error("更新用户信息失败:", e);
  262. }
  263. }
  264. this.getConfig();
  265. }).catch((err) => {
  266. console.error("申请职位失败:", err);
  267. common_vendor.index.showToast({
  268. title: "申请职位失败,请重试",
  269. icon: "none"
  270. });
  271. });
  272. },
  273. submitForm() {
  274. if (!this.checkLogin()) {
  275. return;
  276. }
  277. if (!this.formData.name.trim()) {
  278. common_vendor.index.showToast({
  279. title: "请输入姓名",
  280. icon: "none"
  281. });
  282. return;
  283. }
  284. if (!this.formData.gender) {
  285. common_vendor.index.showToast({
  286. title: "请选择性别",
  287. icon: "none"
  288. });
  289. return;
  290. }
  291. if (!this.formData.phone.trim()) {
  292. common_vendor.index.showToast({
  293. title: "请输入手机号",
  294. icon: "none"
  295. });
  296. return;
  297. }
  298. if (!/^1\d{10}$/.test(this.formData.phone)) {
  299. common_vendor.index.showToast({
  300. title: "请输入正确的手机号",
  301. icon: "none"
  302. });
  303. return;
  304. }
  305. if (!this.formData.idCard.trim()) {
  306. common_vendor.index.showToast({
  307. title: "请输入身份证号",
  308. icon: "none"
  309. });
  310. return;
  311. }
  312. const idCardReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  313. if (!idCardReg.test(this.formData.idCard)) {
  314. common_vendor.index.showToast({
  315. title: "请输入正确的身份证号",
  316. icon: "none"
  317. });
  318. return;
  319. }
  320. if (!this.isAgreed) {
  321. common_vendor.index.showToast({
  322. title: "请阅读并同意相关协议",
  323. icon: "none"
  324. });
  325. return;
  326. }
  327. common_vendor.index.showLoading({
  328. title: "验证身份信息..."
  329. });
  330. common_vendor.index.request({
  331. url: `${common_config.apiBaseUrl}/wechat/identity/verify`,
  332. method: "POST",
  333. data: {
  334. name: this.formData.name,
  335. id_number: this.formData.idCard,
  336. mobile: this.formData.phone
  337. },
  338. header: {
  339. "content-type": "application/x-www-form-urlencoded"
  340. },
  341. success: (res) => {
  342. console.log(res);
  343. if (res.statusCode === 200) {
  344. if (res.data.code === 200) {
  345. this.submitUserInfo();
  346. } else {
  347. common_vendor.index.showToast({
  348. title: res.data.data.message,
  349. icon: "none"
  350. });
  351. }
  352. } else {
  353. common_vendor.index.hideLoading();
  354. common_vendor.index.showToast({
  355. title: "身份验证失败,请检查信息是否正确",
  356. icon: "none"
  357. });
  358. }
  359. },
  360. fail: (err) => {
  361. common_vendor.index.hideLoading();
  362. console.error("身份验证失败:", err);
  363. common_vendor.index.showToast({
  364. title: "网络错误,请稍后重试",
  365. icon: "none"
  366. });
  367. }
  368. });
  369. },
  370. submitUserInfo() {
  371. const genderValue = this.genderMapping[this.formData.gender] || this.formData.gender;
  372. const submitData = {
  373. openid: JSON.parse(common_vendor.index.getStorageSync("userInfo") || "{}").openid || "",
  374. name: this.formData.name,
  375. phone: this.formData.phone,
  376. id_card: this.formData.idCard,
  377. status: 1,
  378. source: "mini",
  379. examine: 0,
  380. tenant_id: "1",
  381. /* emergency_contact: this.formData.emergencyContact,
  382. emergency_phone: this.formData.emergencyPhone,
  383. relation: this.formData.relation, */
  384. age: "20",
  385. job_id: this.selectedJobId,
  386. gender: genderValue
  387. };
  388. api_user.fillUserInfo(submitData).then((res) => {
  389. common_vendor.index.hideLoading();
  390. this.updateLocalUserInfo(res.id, JSON.parse(common_vendor.index.getStorageSync("userInfo")).openid);
  391. common_vendor.index.showToast({
  392. title: "提交成功",
  393. icon: "success",
  394. duration: 1500,
  395. success: () => {
  396. this.userInfoFilled = false;
  397. }
  398. });
  399. }).catch((err) => {
  400. common_vendor.index.hideLoading();
  401. console.error("提交表单失败:", err);
  402. common_vendor.index.showToast({
  403. title: "网络错误,请稍后重试",
  404. icon: "none"
  405. });
  406. });
  407. },
  408. updateLocalUserInfo(data) {
  409. api_user.getUserInfo(data).then((res) => {
  410. if (res.code === 200 && res.data) {
  411. let userInfo = {};
  412. try {
  413. userInfo = JSON.parse(common_vendor.index.getStorageSync("userInfo") || "{}");
  414. } catch (e) {
  415. console.error("解析本地存储用户信息失败:", e);
  416. userInfo = {};
  417. }
  418. const updatedUserInfo = {
  419. ...userInfo,
  420. ...res.data
  421. };
  422. common_vendor.index.setStorageSync("userInfo", JSON.stringify(updatedUserInfo));
  423. }
  424. }).catch((err) => {
  425. console.error("更新本地用户信息失败:", err);
  426. });
  427. },
  428. validatePhone() {
  429. this.isPhoneValid = /^1\d{10}$/.test(this.formData.phone);
  430. },
  431. validateName() {
  432. this.isNameValid = this.formData.name.trim().length >= 2;
  433. },
  434. validateIdCard() {
  435. const idCardReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  436. this.isIdCardValid = idCardReg.test(this.formData.idCard);
  437. },
  438. genderChange(e) {
  439. this.genderIndex = e.detail.value;
  440. const displayGender = this.genderOptions[this.genderIndex];
  441. this.formData.gender = displayGender;
  442. },
  443. viewJobDetail(job) {
  444. common_vendor.index.setStorageSync("currentJobDetail", JSON.stringify(job));
  445. console.log(job);
  446. common_vendor.index.navigateTo({
  447. url: "/pages/job-detail/job-detail?id=" + job.id,
  448. fail: (err) => {
  449. console.error("页面跳转失败:", err);
  450. common_vendor.index.showToast({
  451. title: "页面跳转失败",
  452. icon: "none"
  453. });
  454. }
  455. });
  456. },
  457. formatLocation(location) {
  458. if (!location)
  459. return "";
  460. if (typeof location === "string" && location.startsWith("[")) {
  461. try {
  462. const locationArray = JSON.parse(location.replace(/'/g, '"'));
  463. return locationArray.join(" ");
  464. } catch (e) {
  465. console.error("解析location失败:", e);
  466. return location;
  467. }
  468. }
  469. if (Array.isArray(location)) {
  470. return location.join(" ");
  471. }
  472. if (typeof location === "object" && location !== null) {
  473. const { province, city, district } = location;
  474. if (province && city) {
  475. return province + " " + city + (district ? " " + district : "");
  476. }
  477. }
  478. return location;
  479. }
  480. }
  481. };
  482. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  483. return {
  484. a: common_vendor.f($data.jobList, (job, index, i0) => {
  485. return {
  486. a: common_vendor.t(job.title),
  487. b: common_vendor.o(($event) => $options.viewJobDetail(job), index),
  488. c: common_vendor.t($options.formatLocation(job.location)),
  489. d: index,
  490. e: $data.selectedJobId === job.id ? 1 : "",
  491. f: job.status == 1,
  492. g: common_vendor.o(($event) => $options.selectJob(job), index)
  493. };
  494. }),
  495. b: !$data.selectedJobId,
  496. c: common_vendor.o((...args) => $options.applyForJob && $options.applyForJob(...args))
  497. };
  498. }
  499. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  500. wx.createPage(MiniProgramPage);