index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 = res;
  169. }).catch((err) => {
  170. common_vendor.index.hideLoading();
  171. console.error("获取职位列表失败:", err);
  172. common_vendor.index.showToast({
  173. title: "网络错误,请稍后重试",
  174. icon: "none"
  175. });
  176. });
  177. },
  178. selectJob(job) {
  179. try {
  180. common_vendor.index.removeStorageSync("interviewQuestions");
  181. common_vendor.index.removeStorageSync("currentQuestionIndex");
  182. } catch (e) {
  183. console.error("清除问题缓存失败:", e);
  184. }
  185. this.selectedJobId = job.id;
  186. this.selectedJob = job;
  187. common_vendor.index.setStorageSync("selectedJob", JSON.stringify(job));
  188. console.log("已选择职位:", job.id, job.title);
  189. },
  190. // 获取当前职位配置
  191. getConfig() {
  192. common_vendor.index.request({
  193. url: `${common_config.apiBaseUrl}/api/job/config/position/${this.selectedJobId}`,
  194. method: "GET",
  195. data: {
  196. openid: JSON.parse(common_vendor.index.getStorageSync("userInfo")).openid
  197. },
  198. header: {
  199. "content-type": "application/x-www-form-urlencoded"
  200. },
  201. success: (res) => {
  202. console.log(res);
  203. if (res.statusCode === 200) {
  204. if (res.data.code === 2e3) {
  205. common_vendor.index.setStorageSync("configData", JSON.stringify(res.data.data));
  206. }
  207. } else {
  208. common_vendor.index.hideLoading();
  209. }
  210. },
  211. fail: (err) => {
  212. common_vendor.index.hideLoading();
  213. common_vendor.index.showToast({
  214. title: "网络错误,请稍后重试",
  215. icon: "none"
  216. });
  217. }
  218. });
  219. },
  220. applyForJob() {
  221. if (!this.checkLogin()) {
  222. return;
  223. }
  224. if (!this.selectedJobId) {
  225. common_vendor.index.showToast({
  226. title: "请选择一个职位",
  227. icon: "none"
  228. });
  229. return;
  230. }
  231. common_vendor.index.setStorageSync("selectedJob", JSON.stringify(this.selectedJob));
  232. api_user.applyJob({
  233. job_id: this.selectedJobId,
  234. tenant_id: 1,
  235. openid: JSON.parse(common_vendor.index.getStorageSync("userInfo")).openid
  236. }).then((res) => {
  237. if (res && res.id) {
  238. common_vendor.index.setStorageSync("appId", res.id);
  239. try {
  240. const userInfo = JSON.parse(common_vendor.index.getStorageSync("userInfo") || "{}");
  241. userInfo.appId = res.id;
  242. common_vendor.index.setStorageSync("userInfo", JSON.stringify(userInfo));
  243. } catch (e) {
  244. console.error("更新用户信息失败:", e);
  245. }
  246. }
  247. this.getConfig();
  248. common_vendor.index.navigateTo({
  249. url: "/pages/Personal/Personal",
  250. fail: (err) => {
  251. console.error("页面跳转失败:", err);
  252. common_vendor.index.showToast({
  253. title: "页面跳转失败",
  254. icon: "none"
  255. });
  256. }
  257. });
  258. }).catch((err) => {
  259. console.error("申请职位失败:", err);
  260. common_vendor.index.showToast({
  261. title: "申请职位失败,请重试",
  262. icon: "none"
  263. });
  264. });
  265. },
  266. submitForm() {
  267. if (!this.checkLogin()) {
  268. return;
  269. }
  270. if (!this.formData.name.trim()) {
  271. common_vendor.index.showToast({
  272. title: "请输入姓名",
  273. icon: "none"
  274. });
  275. return;
  276. }
  277. if (!this.formData.gender) {
  278. common_vendor.index.showToast({
  279. title: "请选择性别",
  280. icon: "none"
  281. });
  282. return;
  283. }
  284. if (!this.formData.phone.trim()) {
  285. common_vendor.index.showToast({
  286. title: "请输入手机号",
  287. icon: "none"
  288. });
  289. return;
  290. }
  291. if (!/^1\d{10}$/.test(this.formData.phone)) {
  292. common_vendor.index.showToast({
  293. title: "请输入正确的手机号",
  294. icon: "none"
  295. });
  296. return;
  297. }
  298. if (!this.formData.idCard.trim()) {
  299. common_vendor.index.showToast({
  300. title: "请输入身份证号",
  301. icon: "none"
  302. });
  303. return;
  304. }
  305. const idCardReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  306. if (!idCardReg.test(this.formData.idCard)) {
  307. common_vendor.index.showToast({
  308. title: "请输入正确的身份证号",
  309. icon: "none"
  310. });
  311. return;
  312. }
  313. if (!this.isAgreed) {
  314. common_vendor.index.showToast({
  315. title: "请阅读并同意相关协议",
  316. icon: "none"
  317. });
  318. return;
  319. }
  320. common_vendor.index.showLoading({
  321. title: "验证身份信息..."
  322. });
  323. common_vendor.index.request({
  324. url: `${common_config.apiBaseUrl}/wechat/identity/verify`,
  325. method: "POST",
  326. data: {
  327. name: this.formData.name,
  328. id_number: this.formData.idCard,
  329. mobile: this.formData.phone
  330. },
  331. header: {
  332. "content-type": "application/x-www-form-urlencoded"
  333. },
  334. success: (res) => {
  335. console.log(res);
  336. if (res.statusCode === 200) {
  337. if (res.data.code === 200) {
  338. this.submitUserInfo();
  339. } else {
  340. common_vendor.index.showToast({
  341. title: res.data.data.message,
  342. icon: "none"
  343. });
  344. }
  345. } else {
  346. common_vendor.index.hideLoading();
  347. common_vendor.index.showToast({
  348. title: "身份验证失败,请检查信息是否正确",
  349. icon: "none"
  350. });
  351. }
  352. },
  353. fail: (err) => {
  354. common_vendor.index.hideLoading();
  355. console.error("身份验证失败:", err);
  356. common_vendor.index.showToast({
  357. title: "网络错误,请稍后重试",
  358. icon: "none"
  359. });
  360. }
  361. });
  362. },
  363. submitUserInfo() {
  364. const genderValue = this.genderMapping[this.formData.gender] || this.formData.gender;
  365. const submitData = {
  366. openid: JSON.parse(common_vendor.index.getStorageSync("userInfo") || "{}").openid || "",
  367. name: this.formData.name,
  368. phone: this.formData.phone,
  369. id_card: this.formData.idCard,
  370. status: 1,
  371. source: "mini",
  372. examine: 0,
  373. tenant_id: "1",
  374. /* emergency_contact: this.formData.emergencyContact,
  375. emergency_phone: this.formData.emergencyPhone,
  376. relation: this.formData.relation, */
  377. age: "20",
  378. job_id: this.selectedJobId,
  379. gender: genderValue
  380. };
  381. api_user.fillUserInfo(submitData).then((res) => {
  382. common_vendor.index.hideLoading();
  383. this.updateLocalUserInfo(res.id, JSON.parse(common_vendor.index.getStorageSync("userInfo")).openid);
  384. common_vendor.index.showToast({
  385. title: "提交成功",
  386. icon: "success",
  387. duration: 1500,
  388. success: () => {
  389. this.userInfoFilled = false;
  390. }
  391. });
  392. }).catch((err) => {
  393. common_vendor.index.hideLoading();
  394. console.error("提交表单失败:", err);
  395. common_vendor.index.showToast({
  396. title: "网络错误,请稍后重试",
  397. icon: "none"
  398. });
  399. });
  400. },
  401. updateLocalUserInfo(data) {
  402. api_user.getUserInfo(data).then((res) => {
  403. if (res.code === 200 && res.data) {
  404. let userInfo = {};
  405. try {
  406. userInfo = JSON.parse(common_vendor.index.getStorageSync("userInfo") || "{}");
  407. } catch (e) {
  408. console.error("解析本地存储用户信息失败:", e);
  409. userInfo = {};
  410. }
  411. const updatedUserInfo = {
  412. ...userInfo,
  413. ...res.data
  414. };
  415. common_vendor.index.setStorageSync("userInfo", JSON.stringify(updatedUserInfo));
  416. }
  417. }).catch((err) => {
  418. console.error("更新本地用户信息失败:", err);
  419. });
  420. },
  421. validatePhone() {
  422. this.isPhoneValid = /^1\d{10}$/.test(this.formData.phone);
  423. },
  424. validateName() {
  425. this.isNameValid = this.formData.name.trim().length >= 2;
  426. },
  427. validateIdCard() {
  428. const idCardReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  429. this.isIdCardValid = idCardReg.test(this.formData.idCard);
  430. },
  431. genderChange(e) {
  432. this.genderIndex = e.detail.value;
  433. const displayGender = this.genderOptions[this.genderIndex];
  434. this.formData.gender = displayGender;
  435. },
  436. viewJobDetail(job) {
  437. common_vendor.index.setStorageSync("currentJobDetail", JSON.stringify(job));
  438. console.log(job);
  439. common_vendor.index.navigateTo({
  440. url: "/pages/job-detail/job-detail?id=" + job.id,
  441. fail: (err) => {
  442. console.error("页面跳转失败:", err);
  443. common_vendor.index.showToast({
  444. title: "页面跳转失败",
  445. icon: "none"
  446. });
  447. }
  448. });
  449. }
  450. }
  451. };
  452. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  453. return {
  454. a: common_vendor.f($data.jobList, (job, index, i0) => {
  455. return {
  456. a: common_vendor.t(job.title),
  457. b: common_vendor.o(($event) => $options.viewJobDetail(job), index),
  458. c: common_vendor.t(job.location),
  459. d: index,
  460. e: $data.selectedJobId === job.id ? 1 : "",
  461. f: common_vendor.o(($event) => $options.selectJob(job), index)
  462. };
  463. }),
  464. b: !$data.selectedJobId,
  465. c: common_vendor.o((...args) => $options.applyForJob && $options.applyForJob(...args))
  466. };
  467. }
  468. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  469. wx.createPage(MiniProgramPage);