index.js 15 KB

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