index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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() {
  39. this.checkUserInfo();
  40. this.fetchJobList();
  41. },
  42. computed: {
  43. canSubmit() {
  44. 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;
  45. },
  46. canSubmitSimple() {
  47. return this.formData.name.trim() && this.formData.phone.trim() && this.isPhoneValid && this.formData.idCard.trim() && this.isIdCardValid && this.isAgreed;
  48. }
  49. },
  50. methods: {
  51. checkLogin() {
  52. const userInfo = common_vendor.index.getStorageSync("userInfo");
  53. if (!userInfo) {
  54. common_vendor.index.reLaunch({
  55. url: "/pages/login/login",
  56. success: () => {
  57. console.log("跳转到身份验证登录页面成功");
  58. },
  59. fail: (err) => {
  60. console.error("跳转到身份验证登录页面失败:", err);
  61. common_vendor.index.showToast({
  62. title: "跳转失败,请重试",
  63. icon: "none"
  64. });
  65. }
  66. });
  67. return false;
  68. }
  69. try {
  70. const parsedUserInfo = JSON.parse(userInfo);
  71. if (!parsedUserInfo.openid) {
  72. common_vendor.index.navigateTo({
  73. url: "/pages/login/login"
  74. });
  75. return false;
  76. }
  77. return true;
  78. } catch (e) {
  79. console.error("解析用户信息失败:", e);
  80. common_vendor.index.removeStorageSync("userInfo");
  81. common_vendor.index.navigateTo({
  82. url: "/pages/login/login"
  83. });
  84. return false;
  85. }
  86. },
  87. goHome() {
  88. common_vendor.index.navigateBack({
  89. delta: 1
  90. });
  91. },
  92. toggleAgreement() {
  93. this.isAgreed = !this.isAgreed;
  94. },
  95. relationChange(e) {
  96. this.relationIndex = e.detail.value;
  97. this.formData.relation = this.relationOptions[this.relationIndex];
  98. },
  99. checkUserInfo() {
  100. common_vendor.index.showLoading({
  101. title: "加载中..."
  102. });
  103. try {
  104. const userInfo = JSON.parse(common_vendor.index.getStorageSync("userInfo"));
  105. console.log("id:", userInfo.id);
  106. api_user.getUserInfo(userInfo.id).then((res) => {
  107. common_vendor.index.hideLoading();
  108. const userData = res;
  109. if (!userData.name || !userData.phone) {
  110. this.userInfoFilled = true;
  111. this.formData.name = userData.name || "";
  112. if (userData.gender) {
  113. this.formData.gender = this.genderMapping[userData.gender] || userData.gender;
  114. const index = this.genderOptions.findIndex((item) => item === this.formData.gender);
  115. if (index !== -1) {
  116. this.genderIndex = index;
  117. }
  118. } else {
  119. this.formData.gender = "";
  120. }
  121. this.formData.phone = userData.phone || "";
  122. this.formData.idCard = userData.id_card || "";
  123. this.formData.emergencyContact = userData.emergency_contact || "";
  124. this.formData.emergencyPhone = userData.emergency_phone || "";
  125. this.formData.relation = userData.relation || "";
  126. if (userData.relation) {
  127. const index = this.relationOptions.findIndex((item) => item === userData.relation);
  128. if (index !== -1) {
  129. this.relationIndex = index;
  130. }
  131. }
  132. }
  133. }).catch((err) => {
  134. common_vendor.index.hideLoading();
  135. console.error("获取用户信息失败:", err);
  136. common_vendor.index.showToast({
  137. title: "获取用户信息失败",
  138. icon: "none"
  139. });
  140. });
  141. } catch (e) {
  142. common_vendor.index.hideLoading();
  143. console.error("获取用户信息失败:", e);
  144. common_vendor.index.showToast({
  145. title: "获取用户信息失败",
  146. icon: "none"
  147. });
  148. }
  149. },
  150. fetchJobList() {
  151. common_vendor.index.showLoading({
  152. title: "加载职位列表..."
  153. });
  154. api_user.getJobList().then((res) => {
  155. common_vendor.index.hideLoading();
  156. console.log(res);
  157. this.jobList = res;
  158. }).catch((err) => {
  159. common_vendor.index.hideLoading();
  160. console.error("获取职位列表失败:", err);
  161. common_vendor.index.showToast({
  162. title: "网络错误,请稍后重试",
  163. icon: "none"
  164. });
  165. });
  166. },
  167. selectJob(job) {
  168. this.selectedJobId = job.id;
  169. this.selectedJob = job;
  170. },
  171. applyForJob() {
  172. if (!this.checkLogin()) {
  173. return;
  174. }
  175. if (!this.selectedJobId) {
  176. common_vendor.index.showToast({
  177. title: "请选择一个职位",
  178. icon: "none"
  179. });
  180. return;
  181. }
  182. common_vendor.index.setStorageSync("selectedJob", JSON.stringify(this.selectedJob));
  183. api_user.applyJob({
  184. job_id: this.selectedJobId,
  185. tenant_id: 1,
  186. openid: JSON.parse(common_vendor.index.getStorageSync("userInfo")).openid
  187. }).then((res) => {
  188. if (res && res.id) {
  189. common_vendor.index.setStorageSync("appId", res.id);
  190. try {
  191. const userInfo = JSON.parse(common_vendor.index.getStorageSync("userInfo") || "{}");
  192. userInfo.appId = res.id;
  193. common_vendor.index.setStorageSync("userInfo", JSON.stringify(userInfo));
  194. } catch (e) {
  195. console.error("更新用户信息失败:", e);
  196. }
  197. }
  198. common_vendor.index.navigateTo({
  199. url: "/pages/interview-notice/interview-notice",
  200. fail: (err) => {
  201. console.error("页面跳转失败:", err);
  202. common_vendor.index.showToast({
  203. title: "页面跳转失败",
  204. icon: "none"
  205. });
  206. }
  207. });
  208. }).catch((err) => {
  209. console.error("申请职位失败:", err);
  210. common_vendor.index.showToast({
  211. title: "申请职位失败,请重试",
  212. icon: "none"
  213. });
  214. });
  215. },
  216. submitForm() {
  217. if (!this.checkLogin()) {
  218. return;
  219. }
  220. if (!this.formData.name.trim()) {
  221. common_vendor.index.showToast({
  222. title: "请输入姓名",
  223. icon: "none"
  224. });
  225. return;
  226. }
  227. if (!this.formData.gender) {
  228. common_vendor.index.showToast({
  229. title: "请选择性别",
  230. icon: "none"
  231. });
  232. return;
  233. }
  234. if (!this.formData.phone.trim()) {
  235. common_vendor.index.showToast({
  236. title: "请输入手机号",
  237. icon: "none"
  238. });
  239. return;
  240. }
  241. if (!/^1\d{10}$/.test(this.formData.phone)) {
  242. common_vendor.index.showToast({
  243. title: "请输入正确的手机号",
  244. icon: "none"
  245. });
  246. return;
  247. }
  248. if (!this.formData.idCard.trim()) {
  249. common_vendor.index.showToast({
  250. title: "请输入身份证号",
  251. icon: "none"
  252. });
  253. return;
  254. }
  255. const idCardReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  256. if (!idCardReg.test(this.formData.idCard)) {
  257. common_vendor.index.showToast({
  258. title: "请输入正确的身份证号",
  259. icon: "none"
  260. });
  261. return;
  262. }
  263. if (!this.isAgreed) {
  264. common_vendor.index.showToast({
  265. title: "请阅读并同意相关协议",
  266. icon: "none"
  267. });
  268. return;
  269. }
  270. common_vendor.index.showLoading({
  271. title: "验证身份信息..."
  272. });
  273. common_vendor.index.request({
  274. url: `${common_config.apiBaseUrl}/wechat/identity/verify`,
  275. method: "POST",
  276. data: {
  277. name: this.formData.name,
  278. id_number: this.formData.idCard,
  279. mobile: this.formData.phone
  280. },
  281. header: {
  282. "content-type": "application/x-www-form-urlencoded"
  283. },
  284. success: (res) => {
  285. console.log(res);
  286. if (res.statusCode === 200) {
  287. if (res.data.code === 200) {
  288. this.submitUserInfo();
  289. } else {
  290. common_vendor.index.showToast({
  291. title: res.data.data.message,
  292. icon: "none"
  293. });
  294. }
  295. } else {
  296. common_vendor.index.hideLoading();
  297. common_vendor.index.showToast({
  298. title: "身份验证失败,请检查信息是否正确",
  299. icon: "none"
  300. });
  301. }
  302. },
  303. fail: (err) => {
  304. common_vendor.index.hideLoading();
  305. console.error("身份验证失败:", err);
  306. common_vendor.index.showToast({
  307. title: "网络错误,请稍后重试",
  308. icon: "none"
  309. });
  310. }
  311. });
  312. },
  313. submitUserInfo() {
  314. const genderValue = this.genderMapping[this.formData.gender] || this.formData.gender;
  315. const submitData = {
  316. openid: JSON.parse(common_vendor.index.getStorageSync("userInfo") || "{}").openid || "",
  317. name: this.formData.name,
  318. phone: this.formData.phone,
  319. id_card: this.formData.idCard,
  320. status: 1,
  321. source: "mini",
  322. examine: 0,
  323. tenant_id: "1",
  324. /* emergency_contact: this.formData.emergencyContact,
  325. emergency_phone: this.formData.emergencyPhone,
  326. relation: this.formData.relation, */
  327. age: "20",
  328. job_id: this.selectedJobId,
  329. gender: genderValue
  330. };
  331. api_user.fillUserInfo(submitData).then((res) => {
  332. common_vendor.index.hideLoading();
  333. this.updateLocalUserInfo(res.id);
  334. common_vendor.index.showToast({
  335. title: "提交成功",
  336. icon: "success",
  337. duration: 1500,
  338. success: () => {
  339. this.userInfoFilled = false;
  340. }
  341. });
  342. }).catch((err) => {
  343. common_vendor.index.hideLoading();
  344. console.error("提交表单失败:", err);
  345. common_vendor.index.showToast({
  346. title: "网络错误,请稍后重试",
  347. icon: "none"
  348. });
  349. });
  350. },
  351. updateLocalUserInfo(data) {
  352. api_user.getUserInfo(data).then((res) => {
  353. if (res.code === 200 && res.data) {
  354. let userInfo = {};
  355. try {
  356. userInfo = JSON.parse(common_vendor.index.getStorageSync("userInfo") || "{}");
  357. } catch (e) {
  358. console.error("解析本地存储用户信息失败:", e);
  359. userInfo = {};
  360. }
  361. const updatedUserInfo = {
  362. ...userInfo,
  363. ...res.data
  364. };
  365. common_vendor.index.setStorageSync("userInfo", JSON.stringify(updatedUserInfo));
  366. }
  367. }).catch((err) => {
  368. console.error("更新本地用户信息失败:", err);
  369. });
  370. },
  371. validatePhone() {
  372. this.isPhoneValid = /^1\d{10}$/.test(this.formData.phone);
  373. },
  374. validateName() {
  375. this.isNameValid = this.formData.name.trim().length >= 2;
  376. },
  377. validateIdCard() {
  378. const idCardReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  379. this.isIdCardValid = idCardReg.test(this.formData.idCard);
  380. },
  381. genderChange(e) {
  382. this.genderIndex = e.detail.value;
  383. const displayGender = this.genderOptions[this.genderIndex];
  384. this.formData.gender = displayGender;
  385. }
  386. }
  387. };
  388. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  389. return common_vendor.e({
  390. a: !$data.userInfoFilled
  391. }, !$data.userInfoFilled ? {
  392. b: common_vendor.f($data.jobList, (job, index, i0) => {
  393. return {
  394. a: common_vendor.t(job.title),
  395. b: common_vendor.t(job.publish_date),
  396. c: common_vendor.t(job.location),
  397. d: index,
  398. e: $data.selectedJobId === job.id ? 1 : "",
  399. f: common_vendor.o(($event) => $options.selectJob(job), index)
  400. };
  401. }),
  402. c: !$data.selectedJobId,
  403. d: common_vendor.o((...args) => $options.applyForJob && $options.applyForJob(...args))
  404. } : {}, {
  405. e: $data.userInfoFilled
  406. }, $data.userInfoFilled ? common_vendor.e({
  407. f: common_vendor.o([($event) => $data.formData.phone = $event.detail.value, (...args) => $options.validatePhone && $options.validatePhone(...args)]),
  408. g: $data.formData.phone,
  409. h: $data.formData.phone
  410. }, $data.formData.phone ? common_vendor.e({
  411. i: $data.isPhoneValid
  412. }, $data.isPhoneValid ? {} : {}) : {}, {
  413. j: common_vendor.o([($event) => $data.formData.name = $event.detail.value, (...args) => $options.validateName && $options.validateName(...args)]),
  414. k: $data.formData.name,
  415. l: $data.formData.name
  416. }, $data.formData.name ? common_vendor.e({
  417. m: $data.isNameValid
  418. }, $data.isNameValid ? {} : {}) : {}, {
  419. n: common_vendor.o([($event) => $data.formData.idCard = $event.detail.value, (...args) => $options.validateIdCard && $options.validateIdCard(...args)]),
  420. o: $data.formData.idCard,
  421. p: $data.formData.idCard
  422. }, $data.formData.idCard ? common_vendor.e({
  423. q: $data.isIdCardValid
  424. }, $data.isIdCardValid ? {} : {}) : {}, {
  425. r: $data.formData.gender,
  426. s: common_vendor.o((...args) => $options.genderChange && $options.genderChange(...args)),
  427. t: $data.genderIndex,
  428. v: $data.genderOptions,
  429. w: !$options.canSubmitSimple,
  430. x: common_vendor.o((...args) => $options.submitForm && $options.submitForm(...args)),
  431. y: $data.isAgreed,
  432. z: common_vendor.o((...args) => $options.toggleAgreement && $options.toggleAgreement(...args))
  433. }) : {});
  434. }
  435. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  436. wx.createPage(MiniProgramPage);