index.js 13 KB

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