job-detail.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. jobDetail: {
  9. title: "",
  10. salary: "",
  11. department: "",
  12. location: "",
  13. experience: "",
  14. benefits: [],
  15. detailed_address: "",
  16. description: [
  17. {
  18. subtitle: "岗位要求",
  19. items: []
  20. }
  21. ]
  22. },
  23. selectedJobId: null,
  24. jobId: null,
  25. mapInfo: {
  26. latitude: 0,
  27. longitude: 0,
  28. markers: []
  29. }
  30. };
  31. },
  32. onLoad(options) {
  33. this.jobId = options.id;
  34. this.getJobDetail(options.id);
  35. try {
  36. const jobDetailStr = common_vendor.index.getStorageSync("currentJobDetail");
  37. if (jobDetailStr) {
  38. const jobData = JSON.parse(jobDetailStr);
  39. this.jobDetail = {
  40. ...this.jobDetail,
  41. title: jobData.title || this.jobDetail.title,
  42. salary: jobData.salary || this.jobDetail.salary,
  43. department: jobData.department || this.jobDetail.department,
  44. location: jobData.location || this.jobDetail.location,
  45. experience: jobData.experience || this.jobDetail.experience,
  46. benefits: jobData.benefits || this.jobDetail.benefits,
  47. description: jobData.description || this.jobDetail.description,
  48. detailed_address: jobData.detailed_address || this.jobDetail.detailed_address
  49. };
  50. }
  51. } catch (e) {
  52. console.error("获取职位详情失败:", e);
  53. }
  54. },
  55. methods: {
  56. async getJobDetail(jobId) {
  57. try {
  58. const { data } = await common_vendor.index.request({
  59. url: `${common_config.apiBaseUrl}/api/mini/job/detail?id=${jobId}&tenant_id=1`,
  60. method: "GET"
  61. });
  62. if (data.code === 2e3) {
  63. let descriptionItems = [];
  64. if (data.data.description) {
  65. const liRegex = /<li[^>]*>(.*?)<\/li>/g;
  66. const stripTagsRegex = /<[^>]*>/g;
  67. let match;
  68. while ((match = liRegex.exec(data.data.description)) !== null) {
  69. const text = match[1].replace(stripTagsRegex, "").replace(/&nbsp;/g, " ").trim();
  70. if (text) {
  71. descriptionItems.push(text);
  72. }
  73. }
  74. }
  75. if (data.data.requirements) {
  76. descriptionItems.unshift(data.data.requirements);
  77. }
  78. this.jobDetail = {
  79. id: data.data.id,
  80. title: data.data.title || "",
  81. salary: data.data.salary_range ? `${data.data.salary_range}/月` : "",
  82. department: `${data.data.department || ""} ${data.data.job_type === 1 ? "全职" : "兼职"}`,
  83. location: data.data.location || "",
  84. experience: data.data.work_experience_required || "不限",
  85. benefits: data.data.competency_tags || ["五险一金", "带薪年假", "定期体检"],
  86. detailed_address: data.data.detailed_address || "",
  87. description: [
  88. {
  89. subtitle: "岗位要求",
  90. items: descriptionItems
  91. }
  92. ]
  93. };
  94. this.updateMapLocation(data.data.location, data.data.detailed_address);
  95. } else {
  96. common_vendor.index.showToast({
  97. title: "获取职位详情失败",
  98. icon: "none"
  99. });
  100. }
  101. } catch (e) {
  102. console.error("获取职位详情失败:", e);
  103. common_vendor.index.showToast({
  104. title: "获取职位详情失败",
  105. icon: "none"
  106. });
  107. }
  108. },
  109. checkLogin() {
  110. const userInfo = common_vendor.index.getStorageSync("userInfo");
  111. if (!userInfo) {
  112. common_vendor.index.showToast({
  113. title: "请先登录",
  114. icon: "none"
  115. });
  116. return false;
  117. }
  118. return true;
  119. },
  120. // 获取当前职位配置
  121. getConfig(selectedJobId) {
  122. common_vendor.index.request({
  123. url: `${common_config.apiBaseUrl}/api/job/config/position/${this.jobId}`,
  124. method: "GET",
  125. data: {
  126. openid: JSON.parse(common_vendor.index.getStorageSync("userInfo")).openid
  127. },
  128. header: {
  129. "content-type": "application/x-www-form-urlencoded"
  130. },
  131. success: (res) => {
  132. console.log(res);
  133. if (res.statusCode === 200) {
  134. if (res.data.code === 2e3) {
  135. common_vendor.index.setStorageSync("configData", JSON.stringify(res.data.data));
  136. common_vendor.index.navigateTo({
  137. url: "/pages/Personal/Personal",
  138. fail: (err) => {
  139. console.error("页面跳转失败:", err);
  140. common_vendor.index.showToast({
  141. title: "页面跳转失败",
  142. icon: "none"
  143. });
  144. }
  145. });
  146. }
  147. } else {
  148. common_vendor.index.hideLoading();
  149. }
  150. },
  151. fail: (err) => {
  152. common_vendor.index.hideLoading();
  153. common_vendor.index.showToast({
  154. title: "网络错误,请稍后重试",
  155. icon: "none"
  156. });
  157. }
  158. });
  159. },
  160. async startInterview() {
  161. if (!this.checkLogin()) {
  162. return;
  163. }
  164. try {
  165. common_vendor.index.setStorageSync("selectedJob", JSON.stringify(this.jobDetail));
  166. const userInfo = JSON.parse(common_vendor.index.getStorageSync("userInfo"));
  167. const response = await api_user.applyJob({
  168. job_id: this.jobId,
  169. tenant_id: 1,
  170. openid: userInfo.openid
  171. });
  172. if (response && response.id) {
  173. common_vendor.index.setStorageSync("appId", response.id);
  174. try {
  175. userInfo.appId = response.id;
  176. common_vendor.index.setStorageSync("userInfo", JSON.stringify(userInfo));
  177. } catch (e) {
  178. console.error("更新用户信息失败:", e);
  179. }
  180. this.getConfig(userInfo.appId);
  181. }
  182. } catch (err) {
  183. console.error("申请职位失败:", err);
  184. common_vendor.index.showToast({
  185. title: "申请职位失败,请重试",
  186. icon: "none"
  187. });
  188. }
  189. },
  190. hasHtmlTags(text) {
  191. const htmlRegex = /<[^>]*>/;
  192. return htmlRegex.test(text);
  193. },
  194. formatLocation(location) {
  195. if (!location)
  196. return "";
  197. if (typeof location === "string" && location.startsWith("[")) {
  198. try {
  199. const locationArray = JSON.parse(location.replace(/'/g, '"'));
  200. return locationArray.join(" ") + "" + this.jobDetail.detailed_address || JSON.parse(common_vendor.index.getStorageSync("currentJobDetail")).detailed_address;
  201. } catch (e) {
  202. console.error("解析location失败:", e);
  203. return location;
  204. }
  205. }
  206. if (Array.isArray(location)) {
  207. return location.join(" ") + "" + this.jobDetail.detailed_address || JSON.parse(common_vendor.index.getStorageSync("currentJobDetail")).detailed_address;
  208. }
  209. if (typeof location === "object" && location !== null) {
  210. const { province, city, district } = location;
  211. if (province && city) {
  212. return province + " " + city + (district ? " " + district : "");
  213. }
  214. }
  215. return location + "" + this.jobDetail.detailed_address || JSON.parse(common_vendor.index.getStorageSync("currentJobDetail")).detailed_address;
  216. },
  217. // 更新地图位置信息
  218. async updateMapLocation(location, address) {
  219. try {
  220. let addressStr = "";
  221. if (typeof location === "string" && location.startsWith("[")) {
  222. try {
  223. const locationArray = JSON.parse(location.replace(/'/g, '"'));
  224. addressStr = locationArray.join("") + address;
  225. } catch (e) {
  226. addressStr = location + address;
  227. }
  228. } else if (Array.isArray(location)) {
  229. addressStr = location.join("") + address;
  230. } else if (typeof location === "object" && location !== null) {
  231. const { province, city, district } = location;
  232. addressStr = `${province || ""}${city || ""}${district || ""}${address}`;
  233. } else {
  234. addressStr = location + address;
  235. }
  236. console.log(addressStr, address);
  237. common_vendor.index.request({
  238. url: `https://apis.map.qq.com/ws/geocoder/v1/?address=${encodeURIComponent(addressStr)}&key=ZS4BZ-NKAA7-4VLXR-PHVI4-HAGPH-Z4FJ3`,
  239. // 需要替换为实际的地图Key
  240. success: (res) => {
  241. if (res.data.status === 0) {
  242. const { lat, lng } = res.data.result.location;
  243. this.mapInfo.latitude = lat;
  244. this.mapInfo.longitude = lng;
  245. this.mapInfo.markers = [{
  246. id: 1,
  247. latitude: lat,
  248. longitude: lng,
  249. title: addressStr
  250. }];
  251. } else {
  252. console.error("地址解析失败:", res);
  253. }
  254. },
  255. fail: (err) => {
  256. console.error("地址解析请求失败:", err);
  257. }
  258. });
  259. } catch (error) {
  260. console.error("更新地图位置失败:", error);
  261. }
  262. },
  263. openLocation() {
  264. if (this.mapInfo.latitude && this.mapInfo.longitude) {
  265. common_vendor.index.openLocation({
  266. latitude: this.mapInfo.latitude,
  267. longitude: this.mapInfo.longitude,
  268. name: this.jobDetail.title,
  269. address: this.formatLocation(this.jobDetail.location),
  270. success: function() {
  271. console.log("导航打开成功");
  272. },
  273. fail: function(err) {
  274. console.error("导航打开失败:", err);
  275. common_vendor.index.showToast({
  276. title: "导航打开失败",
  277. icon: "none"
  278. });
  279. }
  280. });
  281. } else {
  282. common_vendor.index.showToast({
  283. title: "暂无位置信息",
  284. icon: "none"
  285. });
  286. }
  287. }
  288. }
  289. };
  290. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  291. return {
  292. a: common_vendor.t($data.jobDetail.title),
  293. b: common_vendor.t($data.jobDetail.salary),
  294. c: common_vendor.t($data.jobDetail.department),
  295. d: common_vendor.t($options.formatLocation($data.jobDetail.location)),
  296. e: common_vendor.t($data.jobDetail.experience),
  297. f: $data.mapInfo.latitude,
  298. g: $data.mapInfo.longitude,
  299. h: $data.mapInfo.markers,
  300. i: common_vendor.o((...args) => $options.openLocation && $options.openLocation(...args)),
  301. j: common_vendor.f($data.jobDetail.description[0].items, (item, index, i0) => {
  302. return common_vendor.e({
  303. a: $options.hasHtmlTags(item)
  304. }, $options.hasHtmlTags(item) ? {
  305. b: item
  306. } : {
  307. c: common_vendor.t(item)
  308. }, {
  309. d: index
  310. });
  311. }),
  312. k: common_vendor.o((...args) => $options.startInterview && $options.startInterview(...args))
  313. };
  314. }
  315. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-2bde8e2a"]]);
  316. wx.createPage(MiniProgramPage);