refactor(components): 重构 CouponCard 组件样式
- 优化了 CouponCard 组件的视觉效果,增加了更多细节和动画 - 添加了响应式样式,提高了移动端体验 - 新增了 CouponList组件样式,用于展示优惠券列表
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import Taro from '@tarojs/taro'
|
||||
import { goTo } from './navigation'
|
||||
|
||||
export default function navTo(url: string, isLogin = false) {
|
||||
if (isLogin) {
|
||||
@@ -11,14 +12,13 @@ export default function navTo(url: string, isLogin = false) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Taro.navigateTo({
|
||||
url: url
|
||||
})
|
||||
// 使用新的导航工具,自动处理路径格式化
|
||||
goTo(url)
|
||||
}
|
||||
|
||||
|
||||
// 转base64
|
||||
export function fileToBase64(filePath) {
|
||||
export function fileToBase64(filePath:string) {
|
||||
return new Promise((resolve) => {
|
||||
let fileManager = Taro.getFileSystemManager();
|
||||
fileManager.readFile({
|
||||
@@ -35,7 +35,7 @@ export function fileToBase64(filePath) {
|
||||
* 转义微信富文本图片样式
|
||||
* @param htmlText
|
||||
*/
|
||||
export function wxParse(htmlText) {
|
||||
export function wxParse(htmlText:string) {
|
||||
// Replace <img> tags with max-width, height and margin styles to remove spacing
|
||||
htmlText = htmlText.replace(/\<img/gi, '<img style="max-width:100%;height:auto;margin:0;padding:0;display:block;"');
|
||||
|
||||
|
||||
192
src/utils/navigation.ts
Normal file
192
src/utils/navigation.ts
Normal file
@@ -0,0 +1,192 @@
|
||||
import Taro from '@tarojs/taro'
|
||||
|
||||
/**
|
||||
* 导航选项接口
|
||||
*/
|
||||
export interface NavigationOptions {
|
||||
/** 页面路径 */
|
||||
url: string
|
||||
/** 页面参数 */
|
||||
params?: Record<string, any>
|
||||
/** 是否替换当前页面(使用redirectTo) */
|
||||
replace?: boolean
|
||||
/** 是否重新启动应用(使用reLaunch) */
|
||||
relaunch?: boolean
|
||||
/** 是否切换到tabBar页面(使用switchTab) */
|
||||
switchTab?: boolean
|
||||
/** 成功回调 */
|
||||
success?: (res: any) => void
|
||||
/** 失败回调 */
|
||||
fail?: (res: any) => void
|
||||
/** 完成回调 */
|
||||
complete?: (res: any) => void
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化页面路径
|
||||
* @param url 原始路径
|
||||
* @returns 格式化后的路径
|
||||
*/
|
||||
function formatUrl(url: string): string {
|
||||
// 如果不是以"/"开头,自动添加
|
||||
if (!url.startsWith('/')) {
|
||||
url = '/' + url
|
||||
}
|
||||
|
||||
// 如果不是以"/pages/"开头,自动添加
|
||||
// if (!url.startsWith('/pages/')) {
|
||||
// // 移除开头的"/",然后添加"/pages/"
|
||||
// url = '/pages/' + url.replace(/^\/+/, '')
|
||||
// }
|
||||
|
||||
return url
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建带参数的URL
|
||||
* @param url 基础URL
|
||||
* @param params 参数对象
|
||||
* @returns 完整的URL
|
||||
*/
|
||||
function buildUrlWithParams(url: string, params?: Record<string, any>): string {
|
||||
if (!params || Object.keys(params).length === 0) {
|
||||
return url
|
||||
}
|
||||
|
||||
const queryString = Object.entries(params)
|
||||
.map(([key, value]) => {
|
||||
if (value === null || value === undefined) {
|
||||
return ''
|
||||
}
|
||||
return `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`
|
||||
})
|
||||
.filter(Boolean)
|
||||
.join('&')
|
||||
|
||||
return queryString ? `${url}?${queryString}` : url
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一的页面导航函数
|
||||
* @param options 导航选项
|
||||
*/
|
||||
export function navigateTo(options: NavigationOptions | string): void {
|
||||
console.log(options,'options')
|
||||
// 如果传入的是字符串,转换为选项对象
|
||||
const opts: NavigationOptions = typeof options === 'string'
|
||||
? { url: options }
|
||||
: options
|
||||
// 格式化URL
|
||||
const formattedUrl = formatUrl(opts.url)
|
||||
|
||||
// 构建完整URL(包含参数)
|
||||
const fullUrl = buildUrlWithParams(formattedUrl, opts.params)
|
||||
|
||||
// 默认错误处理函数
|
||||
const defaultFail = (res?: any) => {
|
||||
console.error('页面导航失败:', res)
|
||||
if (opts.fail) {
|
||||
opts.fail(res)
|
||||
} else {
|
||||
Taro.showToast({
|
||||
title: '页面跳转失败',
|
||||
icon: 'error'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 根据不同的导航类型选择对应的Taro方法
|
||||
if (opts.switchTab) {
|
||||
Taro.switchTab({
|
||||
url: fullUrl,
|
||||
success: opts.success,
|
||||
fail: defaultFail,
|
||||
complete: opts.complete
|
||||
})
|
||||
} else if (opts.relaunch) {
|
||||
Taro.reLaunch({
|
||||
url: fullUrl,
|
||||
success: opts.success,
|
||||
fail: defaultFail,
|
||||
complete: opts.complete
|
||||
})
|
||||
} else if (opts.replace) {
|
||||
Taro.redirectTo({
|
||||
url: fullUrl,
|
||||
success: opts.success,
|
||||
fail: defaultFail,
|
||||
complete: opts.complete
|
||||
})
|
||||
} else {
|
||||
console.log('这里🌶。 ', fullUrl)
|
||||
|
||||
Taro.navigateTo({
|
||||
url: fullUrl,
|
||||
success: opts.success,
|
||||
fail: defaultFail,
|
||||
complete: opts.complete
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 导航到指定页面(默认方式)
|
||||
* @param url 页面路径
|
||||
* @param params 页面参数
|
||||
*/
|
||||
export function goTo(url: string, params?: Record<string, any>): void {
|
||||
navigateTo({ url, params })
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换当前页面
|
||||
* @param url 页面路径
|
||||
* @param params 页面参数
|
||||
*/
|
||||
export function redirectTo(url: string, params?: Record<string, any>): void {
|
||||
navigateTo({ url, params, replace: true })
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新启动应用
|
||||
* @param url 页面路径
|
||||
* @param params 页面参数
|
||||
*/
|
||||
export function reLaunch(url: string, params?: Record<string, any>): void {
|
||||
navigateTo({ url, params, relaunch: true })
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换到tabBar页面
|
||||
* @param url 页面路径
|
||||
*/
|
||||
export function switchTab(url: string): void {
|
||||
navigateTo({ url, switchTab: true })
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回上一页
|
||||
* @param delta 返回的页面数,默认为1
|
||||
*/
|
||||
export function goBack(delta: number = 1): void {
|
||||
Taro.navigateBack({ delta })
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前页面栈
|
||||
*/
|
||||
export function getCurrentPages() {
|
||||
return Taro.getCurrentPages()
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前页面路径
|
||||
*/
|
||||
export function getCurrentRoute(): string {
|
||||
const pages = getCurrentPages()
|
||||
const currentPage = pages[pages.length - 1]
|
||||
return currentPage ? currentPage.route || '' : ''
|
||||
}
|
||||
|
||||
// 导出默认的导航函数
|
||||
export default navigateTo
|
||||
Reference in New Issue
Block a user