index.js 14 KB

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