index.js 14 KB

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