index.js 12 KB

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