index.js 15 KB

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