index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. console.log("res:", res);
  98. const userData = res;
  99. if (userData.name == null && userData.phone == null) {
  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. }
  115. }).catch((err) => {
  116. common_vendor.index.hideLoading();
  117. console.error("获取用户信息失败:", err);
  118. common_vendor.index.showToast({
  119. title: "获取用户信息失败",
  120. icon: "none"
  121. });
  122. });
  123. } catch (e) {
  124. common_vendor.index.hideLoading();
  125. console.error("获取用户信息失败:", e);
  126. common_vendor.index.showToast({
  127. title: "获取用户信息失败",
  128. icon: "none"
  129. });
  130. common_vendor.index.navigateTo({
  131. url: "/pages/my/my"
  132. });
  133. }
  134. },
  135. fetchJobList() {
  136. common_vendor.index.showLoading({
  137. title: "加载职位列表..."
  138. });
  139. api_user.getJobList().then((res) => {
  140. common_vendor.index.hideLoading();
  141. console.log(res);
  142. this.jobList = res;
  143. }).catch((err) => {
  144. common_vendor.index.hideLoading();
  145. console.error("获取职位列表失败:", err);
  146. common_vendor.index.showToast({
  147. title: "网络错误,请稍后重试",
  148. icon: "none"
  149. });
  150. });
  151. },
  152. selectJob(job) {
  153. this.selectedJobId = job.id;
  154. this.selectedJob = job;
  155. },
  156. applyForJob() {
  157. if (!this.checkLogin()) {
  158. return;
  159. }
  160. if (!this.selectedJobId) {
  161. common_vendor.index.showToast({
  162. title: "请选择一个职位",
  163. icon: "none"
  164. });
  165. return;
  166. }
  167. common_vendor.index.setStorageSync("selectedJob", JSON.stringify(this.selectedJob));
  168. api_user.applyJob({
  169. job_id: this.selectedJobId,
  170. tenant_id: 1,
  171. openid: JSON.parse(common_vendor.index.getStorageSync("userInfo")).openid
  172. }).then((res) => {
  173. if (res && res.id) {
  174. common_vendor.index.setStorageSync("appId", res.id);
  175. try {
  176. const userInfo = JSON.parse(common_vendor.index.getStorageSync("userInfo") || "{}");
  177. userInfo.appId = res.id;
  178. common_vendor.index.setStorageSync("userInfo", JSON.stringify(userInfo));
  179. } catch (e) {
  180. console.error("更新用户信息失败:", e);
  181. }
  182. }
  183. common_vendor.index.navigateTo({
  184. url: "/pages/interview-notice/interview-notice",
  185. fail: (err) => {
  186. console.error("页面跳转失败:", err);
  187. common_vendor.index.showToast({
  188. title: "页面跳转失败",
  189. icon: "none"
  190. });
  191. }
  192. });
  193. }).catch((err) => {
  194. console.error("申请职位失败:", err);
  195. common_vendor.index.showToast({
  196. title: "申请职位失败,请重试",
  197. icon: "none"
  198. });
  199. });
  200. },
  201. submitForm() {
  202. if (!this.checkLogin()) {
  203. return;
  204. }
  205. if (!this.formData.name.trim()) {
  206. common_vendor.index.showToast({
  207. title: "请输入姓名",
  208. icon: "none"
  209. });
  210. return;
  211. }
  212. if (!this.formData.gender) {
  213. common_vendor.index.showToast({
  214. title: "请选择性别",
  215. icon: "none"
  216. });
  217. return;
  218. }
  219. if (!this.formData.phone.trim()) {
  220. common_vendor.index.showToast({
  221. title: "请输入手机号",
  222. icon: "none"
  223. });
  224. return;
  225. }
  226. if (!/^1\d{10}$/.test(this.formData.phone)) {
  227. common_vendor.index.showToast({
  228. title: "请输入正确的手机号",
  229. icon: "none"
  230. });
  231. return;
  232. }
  233. if (this.formData.email && !/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(this.formData.email)) {
  234. common_vendor.index.showToast({
  235. title: "请输入正确的邮箱",
  236. icon: "none"
  237. });
  238. return;
  239. }
  240. if (!this.formData.idCard.trim()) {
  241. common_vendor.index.showToast({
  242. title: "请输入身份证号",
  243. icon: "none"
  244. });
  245. return;
  246. }
  247. const idCardReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  248. if (!idCardReg.test(this.formData.idCard)) {
  249. common_vendor.index.showToast({
  250. title: "请输入正确的身份证号",
  251. icon: "none"
  252. });
  253. return;
  254. }
  255. if (!this.formData.emergencyContact.trim()) {
  256. common_vendor.index.showToast({
  257. title: "请输入紧急联系人",
  258. icon: "none"
  259. });
  260. return;
  261. }
  262. if (!this.formData.emergencyPhone.trim()) {
  263. common_vendor.index.showToast({
  264. title: "请输入紧急联系人电话",
  265. icon: "none"
  266. });
  267. return;
  268. }
  269. if (!/^1\d{10}$/.test(this.formData.emergencyPhone)) {
  270. common_vendor.index.showToast({
  271. title: "请输入正确的紧急联系人电话",
  272. icon: "none"
  273. });
  274. return;
  275. }
  276. if (!this.formData.relation) {
  277. common_vendor.index.showToast({
  278. title: "请选择与紧急联系人关系",
  279. icon: "none"
  280. });
  281. return;
  282. }
  283. if (!this.isAgreed) {
  284. common_vendor.index.showToast({
  285. title: "请阅读并同意相关协议",
  286. icon: "none"
  287. });
  288. return;
  289. }
  290. const submitData = {
  291. openid: JSON.parse(common_vendor.index.getStorageSync("userInfo") || "{}").openid || "",
  292. name: this.formData.name,
  293. phone: this.formData.phone,
  294. id_card: this.formData.idCard,
  295. status: 1,
  296. source: "mini",
  297. examine: 0,
  298. tenant_id: "1",
  299. emergency_contact: this.formData.emergencyContact,
  300. emergency_phone: this.formData.emergencyPhone,
  301. relation: this.formData.relation,
  302. age: "20",
  303. job_id: this.selectedJobId
  304. };
  305. common_vendor.index.showLoading({
  306. title: "提交中..."
  307. });
  308. api_user.fillUserInfo(submitData).then((res) => {
  309. common_vendor.index.hideLoading();
  310. this.updateLocalUserInfo(res.id);
  311. common_vendor.index.showToast({
  312. title: "提交成功",
  313. icon: "success",
  314. duration: 1500,
  315. success: () => {
  316. this.userInfoFilled = false;
  317. }
  318. });
  319. }).catch((err) => {
  320. common_vendor.index.hideLoading();
  321. console.error("提交表单失败:", err);
  322. common_vendor.index.showToast({
  323. title: "网络错误,请稍后重试",
  324. icon: "none"
  325. });
  326. });
  327. },
  328. updateLocalUserInfo(data) {
  329. api_user.getUserInfo(data).then((res) => {
  330. if (res.code === 200 && res.data) {
  331. let userInfo = {};
  332. try {
  333. userInfo = JSON.parse(common_vendor.index.getStorageSync("userInfo") || "{}");
  334. } catch (e) {
  335. console.error("解析本地存储用户信息失败:", e);
  336. userInfo = {};
  337. }
  338. const updatedUserInfo = {
  339. ...userInfo,
  340. ...res.data
  341. };
  342. common_vendor.index.setStorageSync("userInfo", JSON.stringify(updatedUserInfo));
  343. }
  344. }).catch((err) => {
  345. console.error("更新本地用户信息失败:", err);
  346. });
  347. }
  348. }
  349. };
  350. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  351. return common_vendor.e({
  352. a: !$data.userInfoFilled
  353. }, !$data.userInfoFilled ? {
  354. b: common_vendor.f($data.jobList, (job, index, i0) => {
  355. return {
  356. a: common_vendor.t(job.title),
  357. b: common_vendor.t(job.publish_date),
  358. c: common_vendor.t(job.location),
  359. d: index,
  360. e: $data.selectedJobId === job.id ? 1 : "",
  361. f: common_vendor.o(($event) => $options.selectJob(job), index)
  362. };
  363. }),
  364. c: !$data.selectedJobId,
  365. d: common_vendor.o((...args) => $options.applyForJob && $options.applyForJob(...args))
  366. } : {}, {
  367. e: $data.userInfoFilled
  368. }, $data.userInfoFilled ? {
  369. f: $data.formData.name,
  370. g: common_vendor.o(($event) => $data.formData.name = $event.detail.value),
  371. h: $data.formData.gender === "男" ? 1 : "",
  372. i: common_vendor.o(($event) => $data.formData.gender = "男"),
  373. j: $data.formData.gender === "女" ? 1 : "",
  374. k: common_vendor.o(($event) => $data.formData.gender = "女"),
  375. l: $data.formData.idCard,
  376. m: common_vendor.o(($event) => $data.formData.idCard = $event.detail.value),
  377. n: $data.formData.phone,
  378. o: common_vendor.o(($event) => $data.formData.phone = $event.detail.value),
  379. p: $data.formData.emergencyContact,
  380. q: common_vendor.o(($event) => $data.formData.emergencyContact = $event.detail.value),
  381. r: $data.formData.emergencyPhone,
  382. s: common_vendor.o(($event) => $data.formData.emergencyPhone = $event.detail.value),
  383. t: common_vendor.t($data.formData.relation || "请选择关系"),
  384. v: common_vendor.o((...args) => $options.relationChange && $options.relationChange(...args)),
  385. w: $data.relationIndex,
  386. x: $data.relationOptions,
  387. y: $data.isAgreed,
  388. z: common_vendor.o((...args) => $options.toggleAgreement && $options.toggleAgreement(...args)),
  389. A: !$options.canSubmit,
  390. B: common_vendor.o((...args) => $options.submitForm && $options.submitForm(...args))
  391. } : {});
  392. }
  393. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  394. wx.createPage(MiniProgramPage);