chore: initial import
This commit is contained in:
112
utils/booking-guard.js
Normal file
112
utils/booking-guard.js
Normal file
@@ -0,0 +1,112 @@
|
||||
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.',
|
||||
loginConfirmText: zh ? '去登录' : 'Sign In',
|
||||
existingBookingTitle: zh ? '已有进行中的预订' : 'Existing Booking',
|
||||
existingBookingContent: zh
|
||||
? '您当前状态为“{status}”,请先查看预订详情。'
|
||||
: 'Your current booking status is "{status}". Please review the booking detail first.',
|
||||
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,
|
||||
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),
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user