job-detail.js 12 KB

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