const { GIGS } = require('./_seed') const API = 'https://api.nomadro.cn' function text(t) { return [{ type: 'text', text: t }] } function guestId() { let id = '' try { id = wx.getStorageSync('nomadro_social_guest') || '' } catch (e) { id = '' } if (!id) { id = `guest_${Date.now().toString(36)}` try { wx.setStorageSync('nomadro_social_guest', id) } catch (e) { /* ignore */ } } return id } async function applyTeamGig({ gigId, name, contact, note } = {}, ctx) { const id = String(gigId || '').trim() if (!id) { return { isError: true, content: text('请提供岗位 gigId。可先调用 listTeamGigs。'), } } const gig = GIGS.find((g) => g.id === id) const n = String(name || '').trim() const c = String(contact || '').trim() const noteText = String(note || (ctx && ctx.query) || '').trim() if (!n || !c) { return { isError: false, content: text('申请需要「怎么称呼」和「联系方式」。点卡片打开表单补全后再提交。'), structuredContent: { status: 'need_form', gigId: id, title: (gig && gig.title) || id, pagePath: `/pages/social/team/index?gig=${encodeURIComponent(id)}`, linkText: '打开申请表', }, handoff: () => ({ path: '/pages/social/team/index', query: `gig=${encodeURIComponent(id)}&apply=1`, payload: { gigId: id, openApply: true, draft: { name: n, contact: c, note: noteText }, }, }), } } let delivery = 'local' try { const res = await new Promise((resolve, reject) => { wx.request({ url: `${API}/api/social/v1/gigs/${encodeURIComponent(id)}/apply`, method: 'POST', header: { 'Content-Type': 'application/json', 'X-Social-Guest-Id': guestId(), }, data: { name: n, contact: c, note: noteText }, success: resolve, fail: reject, }) }) delivery = res.statusCode < 400 ? 'sent' : 'local' } catch (e) { delivery = 'local' } try { const raw = wx.getStorageSync('nomadro_social_gig_apps') || {} raw[id] = { name: n, contact: c, note: noteText, delivery, at: Date.now() } wx.setStorageSync('nomadro_social_gig_apps', raw) } catch (e) { /* ignore */ } return { isError: false, content: text( delivery === 'sent' ? `已提交「${(gig && gig.title) || id}」的兴趣申请,对方看到后会再联系你。` : `申请已先留在本机。有网后可在找搭子页重试提交。`, ), structuredContent: { status: delivery, gigId: id, title: (gig && gig.title) || id, pagePath: `/pages/social/team/index?gig=${encodeURIComponent(id)}`, linkText: '查看申请', }, handoff: () => ({ path: '/pages/social/team/index', query: `gig=${encodeURIComponent(id)}`, payload: { gigId: id, delivery, draft: { name: n, contact: c, note: noteText } }, }), } } module.exports = applyTeamGig