117 lines
4.0 KiB
JavaScript
117 lines
4.0 KiB
JavaScript
import { getSessionSnapshot } from './app-state'
|
|
import { getBookingStatusText, getStoredUser } from './flow'
|
|
import { getCurrentLanguage } from './i18n'
|
|
|
|
const ACTIVE_BOOKING_STATUSES = ['pending_review', 'approved_wait_assign', 'assigned_wait_room']
|
|
|
|
function isZh(language) {
|
|
return language !== 'en'
|
|
}
|
|
|
|
function getGuardText(language = 'zh') {
|
|
const zh = isZh(language)
|
|
return {
|
|
loginTitle: zh ? '提示' : 'Notice',
|
|
loginContent: zh ? '您还未登录,请先登录或注册账号。' : 'Please sign in or register an account first.',
|
|
loginCancelText: zh ? '取消' : 'Cancel',
|
|
loginConfirmText: zh ? '去登录' : 'Sign In',
|
|
existingBookingTitle: zh ? '已有进行中的预订' : 'Existing Booking',
|
|
existingBookingContent: zh
|
|
? '您当前状态为“{status}”,请先查看预订详情。'
|
|
: 'Your current booking status is "{status}". Please review the booking detail first.',
|
|
existingBookingCancelText: zh ? '取消' : 'Cancel',
|
|
viewBooking: zh ? '查看预订' : 'View Booking'
|
|
}
|
|
}
|
|
|
|
function buildQueryString(query = {}) {
|
|
return Object.keys(query).reduce((result, key) => {
|
|
const value = query[key]
|
|
if (value === '' || value === null || typeof value === 'undefined') {
|
|
return result
|
|
}
|
|
result.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`)
|
|
return result
|
|
}, []).join('&')
|
|
}
|
|
|
|
function getCurrentPageUrl() {
|
|
const pages = getCurrentPages()
|
|
if (!Array.isArray(pages) || !pages.length) {
|
|
return ''
|
|
}
|
|
const currentPage = pages[pages.length - 1] || {}
|
|
const route = currentPage.route || ''
|
|
const options = currentPage.options || (currentPage.$page && currentPage.$page.options) || {}
|
|
if (!route) {
|
|
return ''
|
|
}
|
|
const queryString = buildQueryString(options)
|
|
return `/${route}${queryString ? `?${queryString}` : ''}`
|
|
}
|
|
|
|
export function ensureBookingAccess(options = {}) {
|
|
const language = options.language || getCurrentLanguage()
|
|
const guardText = getGuardText(language)
|
|
const session = getSessionSnapshot()
|
|
const sessionUser = session.resolvedUser && Object.keys(session.resolvedUser).length
|
|
? session.resolvedUser
|
|
: {}
|
|
const optionUser = options.userInfo && Object.keys(options.userInfo).length
|
|
? options.userInfo
|
|
: null
|
|
const userInfo = optionUser
|
|
? {
|
|
...sessionUser,
|
|
...optionUser
|
|
}
|
|
: (Object.keys(sessionUser).length ? sessionUser : getStoredUser())
|
|
|
|
if (!session.isLogin) {
|
|
uni.showModal({
|
|
title: options.loginTitle || guardText.loginTitle,
|
|
content: options.loginContent || guardText.loginContent,
|
|
cancelText: options.loginCancelText || guardText.loginCancelText,
|
|
confirmText: options.loginConfirmText || guardText.loginConfirmText,
|
|
success: (res) => {
|
|
if (res.confirm) {
|
|
const redirectUrl = options.loginRedirectUrl || getCurrentPageUrl()
|
|
const query = [`bypass=1`]
|
|
if (redirectUrl) {
|
|
query.push(`redirect=${encodeURIComponent(redirectUrl)}`)
|
|
}
|
|
uni.navigateTo({ url: `/pkgAccount/login/login?${query.join('&')}` })
|
|
}
|
|
}
|
|
})
|
|
return false
|
|
}
|
|
|
|
if (ACTIVE_BOOKING_STATUSES.includes(userInfo.bookingStatus)) {
|
|
const statusText = getBookingStatusText(userInfo.bookingStatus, language)
|
|
uni.showModal({
|
|
title: guardText.existingBookingTitle,
|
|
content: guardText.existingBookingContent.replace('{status}', statusText),
|
|
cancelText: guardText.existingBookingCancelText,
|
|
confirmText: guardText.viewBooking,
|
|
success: (res) => {
|
|
if (res.confirm) {
|
|
const orderNo = encodeURIComponent(
|
|
userInfo.currentBooking && userInfo.currentBooking.orderNo
|
|
? userInfo.currentBooking.orderNo
|
|
: ''
|
|
)
|
|
uni.navigateTo({
|
|
url: orderNo
|
|
? `/pkgBooking/booking_detail/booking_detail?orderNo=${orderNo}`
|
|
: '/pkgBooking/booking_detail/booking_detail'
|
|
})
|
|
}
|
|
}
|
|
})
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|