🎈 perf(邀请码): 添加错误❌处理机制
This commit is contained in:
parent
7d3f6e22c5
commit
836120ddba
@ -9,7 +9,6 @@ const Loading = ({ width = 60, color = '#6190e8' }: { width?: number; color?: st
|
|||||||
if (color) { obj = { ...obj, borderColor: `${color} transparent transparent` } }
|
if (color) { obj = { ...obj, borderColor: `${color} transparent transparent` } }
|
||||||
return obj
|
return obj
|
||||||
}, [width, color])
|
}, [width, color])
|
||||||
console.log('loading:::')
|
|
||||||
return (
|
return (
|
||||||
<View className={style.loading}
|
<View className={style.loading}
|
||||||
style={styleObj}
|
style={styleObj}
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
text-align: center;
|
text-align: center;
|
||||||
color: #fff;
|
color: #fff;
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
|
|
||||||
&--normal {
|
&--normal {
|
||||||
height: 72px;
|
height: 72px;
|
||||||
font-size: $font_size_medium;
|
font-size: $font_size_medium;
|
||||||
@ -69,10 +70,19 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
&--text {
|
&--text {
|
||||||
|
position: relative;
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-flow: row nowrap;
|
flex-flow: row nowrap;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
color: currentColor;
|
color: currentColor;
|
||||||
|
.loading {
|
||||||
|
position: absolute;
|
||||||
|
pointer-events: none;
|
||||||
|
left: -40px;
|
||||||
|
top: 50%;
|
||||||
|
transform: translateY(-50%);
|
||||||
|
z-index: 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// active 伪类
|
// active 伪类
|
||||||
&--primary:active {
|
&--primary:active {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { Text, View } from '@tarojs/components'
|
import { View } from '@tarojs/components'
|
||||||
import type { FC, ReactNode } from 'react'
|
import type { FC, ReactNode } from 'react'
|
||||||
import classnames from 'classnames'
|
import classnames from 'classnames'
|
||||||
|
import Loading from '../loading'
|
||||||
import styles from './index.module.scss'
|
import styles from './index.module.scss'
|
||||||
|
|
||||||
type ButtonType = 'primary' | 'danger' | 'warning' | 'info'
|
type ButtonType = 'primary' | 'danger' | 'warning' | 'info'
|
||||||
@ -18,6 +19,7 @@ interface PropsType {
|
|||||||
customClassName?: string
|
customClassName?: string
|
||||||
customStyles?: React.CSSProperties
|
customStyles?: React.CSSProperties
|
||||||
customTextClassName?: string
|
customTextClassName?: string
|
||||||
|
loading?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
const NormalButton: FC<PropsType> = (props) => {
|
const NormalButton: FC<PropsType> = (props) => {
|
||||||
@ -33,6 +35,7 @@ const NormalButton: FC<PropsType> = (props) => {
|
|||||||
customStyles = {},
|
customStyles = {},
|
||||||
customClassName = '',
|
customClassName = '',
|
||||||
customTextClassName = '',
|
customTextClassName = '',
|
||||||
|
loading = false,
|
||||||
} = props
|
} = props
|
||||||
const getClassName = () => {
|
const getClassName = () => {
|
||||||
const classObject = {
|
const classObject = {
|
||||||
@ -55,7 +58,9 @@ const NormalButton: FC<PropsType> = (props) => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<View className={classnames(styles.button, getClassName(), customClassName)} style={customStyles} onClick={handleClick}>
|
<View className={classnames(styles.button, getClassName(), customClassName)} style={customStyles} onClick={handleClick}>
|
||||||
<View className={classnames(styles['button--text'], customTextClassName)}>{children}</View>
|
<View className={classnames(styles['button--text'], customTextClassName)}>
|
||||||
|
<View className={styles.loading}>{loading && <Loading width={30} />}</View>
|
||||||
|
{children}</View>
|
||||||
</View>
|
</View>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { Canvas, Image, Text, View } from '@tarojs/components'
|
import { Canvas, Image, Text, View } from '@tarojs/components'
|
||||||
import Taro, { useReady } from '@tarojs/taro'
|
import Taro, { useReady } from '@tarojs/taro'
|
||||||
import { useCallback, useRef, useState } from 'react'
|
import { useCallback, useRef, useState } from 'react'
|
||||||
|
import { imageAudit } from '@tarojs/taro-h5'
|
||||||
import style from './index.module.scss'
|
import style from './index.module.scss'
|
||||||
import inviteCodePng from './inviteCode.png'
|
import inviteCodePng from './inviteCode.png'
|
||||||
import QRcode from './inviteCodePopup.png'
|
import QRcode from './inviteCodePopup.png'
|
||||||
@ -37,6 +38,7 @@ const InviteCode = () => {
|
|||||||
columns: inviteColumns,
|
columns: inviteColumns,
|
||||||
dataSource: { list: [], total: 0 },
|
dataSource: { list: [], total: 0 },
|
||||||
})
|
})
|
||||||
|
const [loading, setLoading] = useState(false)
|
||||||
// 获取邀请码
|
// 获取邀请码
|
||||||
const getInviteCode = async() => {
|
const getInviteCode = async() => {
|
||||||
const res = await fetchData()
|
const res = await fetchData()
|
||||||
@ -59,10 +61,13 @@ const InviteCode = () => {
|
|||||||
}
|
}
|
||||||
// 生成二维码
|
// 生成二维码
|
||||||
const genQRcode = async() => {
|
const genQRcode = async() => {
|
||||||
const res = await genCode({ content: inviteInfo.invitation_code })
|
const res = await genCode({ content: `InviteCode:${inviteInfo.invitation_code}` })
|
||||||
if (res.success) {
|
if (res.success) {
|
||||||
return res.data.qrcode_base64
|
return res.data.qrcode_base64
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
setLoading(false)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const [, setForceUpdate] = useState({})
|
const [, setForceUpdate] = useState({})
|
||||||
@ -97,6 +102,9 @@ const InviteCode = () => {
|
|||||||
console.log('tempFilePath', res.tempFilePath)
|
console.log('tempFilePath', res.tempFilePath)
|
||||||
setTargetImageUrl(res.tempFilePath)
|
setTargetImageUrl(res.tempFilePath)
|
||||||
},
|
},
|
||||||
|
complete: () => {
|
||||||
|
setLoading(false)
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
const getImageInfo = (filePath) => {
|
const getImageInfo = (filePath) => {
|
||||||
@ -109,6 +117,7 @@ const InviteCode = () => {
|
|||||||
}
|
}
|
||||||
// 初始化 canvas
|
// 初始化 canvas
|
||||||
const initCanvas = async() => {
|
const initCanvas = async() => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
Taro.nextTick(() => {
|
Taro.nextTick(() => {
|
||||||
const query = Taro.createSelectorQuery()
|
const query = Taro.createSelectorQuery()
|
||||||
query.select('#canvas').node(({ node: canvas }: { node: Taro.Canvas }) => {
|
query.select('#canvas').node(({ node: canvas }: { node: Taro.Canvas }) => {
|
||||||
@ -119,8 +128,10 @@ const InviteCode = () => {
|
|||||||
ctx.current = context
|
ctx.current = context
|
||||||
console.log('canvas', canvas)
|
console.log('canvas', canvas)
|
||||||
setForceUpdate({})
|
setForceUpdate({})
|
||||||
|
resolve(true)
|
||||||
}).exec()
|
}).exec()
|
||||||
})
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
const startPaint = async(ctx, canvas, image) => {
|
const startPaint = async(ctx, canvas, image) => {
|
||||||
console.log('startPaint param', ctx, canvas, image)
|
console.log('startPaint param', ctx, canvas, image)
|
||||||
@ -160,18 +171,20 @@ const InviteCode = () => {
|
|||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
console.error('合成二维邀请码失败')
|
console.error('合成二维邀请码失败')
|
||||||
|
setLoading(false)
|
||||||
|
throw new Error('合成二维邀请码失败')
|
||||||
}
|
}
|
||||||
|
|
||||||
saveCanvasToImage(canvas)
|
saveCanvasToImage(canvas)
|
||||||
}
|
}
|
||||||
// 绘制最终的海报,图片使用两倍大小,canvas大小是图片的二分之一 就能让canvas生成出来的图片清晰了
|
// 绘制最终的海报,图片使用两倍大小,canvas大小是图片的二分之一 就能让canvas生成出来的图片清晰了
|
||||||
const drawPictorial = async() => {
|
const drawPictorial = async() => {
|
||||||
return new Promise((resolve, reject) => {
|
// eslint-disable-next-line no-async-promise-executor
|
||||||
|
return new Promise(async(resolve, reject) => {
|
||||||
|
setLoading(true)
|
||||||
if (!ctx.current) {
|
if (!ctx.current) {
|
||||||
// 重新初始化canvas
|
// 重新初始化canvas
|
||||||
initCanvas()
|
await initCanvas()
|
||||||
reject(new Error('ctx error'))
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
const canvas = canvasNode.current
|
const canvas = canvasNode.current
|
||||||
Taro.getImageInfo({
|
Taro.getImageInfo({
|
||||||
@ -179,11 +192,19 @@ const InviteCode = () => {
|
|||||||
success: (res) => {
|
success: (res) => {
|
||||||
canvas.width = res.width / 2
|
canvas.width = res.width / 2
|
||||||
canvas.height = res.height / 2
|
canvas.height = res.height / 2
|
||||||
getImageObject(canvas, `${res.path}`).then((image) => {
|
getImageObject(canvas, `${res.path}`).then(async(image) => {
|
||||||
|
try {
|
||||||
// 开始绘制
|
// 开始绘制
|
||||||
startPaint(ctx.current, canvasNode.current, image)
|
await startPaint(ctx.current, canvasNode.current, image)
|
||||||
resolve(true)
|
resolve(true)
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
console.log(err)
|
||||||
|
setLoading(false)
|
||||||
|
reject(new Error('绘制失败'))
|
||||||
|
}
|
||||||
}).catch((error) => {
|
}).catch((error) => {
|
||||||
|
setLoading(false)
|
||||||
throw new Error(error)
|
throw new Error(error)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
@ -195,9 +216,16 @@ const InviteCode = () => {
|
|||||||
|
|
||||||
}
|
}
|
||||||
const handleQRcodeShare = async() => {
|
const handleQRcodeShare = async() => {
|
||||||
await drawPictorial()
|
try {
|
||||||
|
const flag = await drawPictorial()
|
||||||
|
if (flag) {
|
||||||
setShowPopup(true)
|
setShowPopup(true)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
throw new Error('弹出二维码失败')
|
||||||
|
}
|
||||||
|
}
|
||||||
// 复制二维码
|
// 复制二维码
|
||||||
const handleCopyInviteCode = () => {
|
const handleCopyInviteCode = () => {
|
||||||
Taro.setClipboardData({
|
Taro.setClipboardData({
|
||||||
@ -253,7 +281,7 @@ const InviteCode = () => {
|
|||||||
</View>
|
</View>
|
||||||
<Canvas style="position: absolute; left: -9999rpx" id="canvas" canvas-id="canvas" type="2d" />
|
<Canvas style="position: absolute; left: -9999rpx" id="canvas" canvas-id="canvas" type="2d" />
|
||||||
<View className={style.bottomBar}>
|
<View className={style.bottomBar}>
|
||||||
<NormalButton plain type="primary" customStyles={{ width: '45%' }} round onClick={handleQRcodeShare}>
|
<NormalButton loading={loading} plain type="primary" customStyles={{ width: '45%' }} round onClick={handleQRcodeShare}>
|
||||||
二维码分享
|
二维码分享
|
||||||
</NormalButton>
|
</NormalButton>
|
||||||
<NormalButton type="primary" round customStyles={{ width: '45%' }} onClick={handleCopyInviteCode}>
|
<NormalButton type="primary" round customStyles={{ width: '45%' }} onClick={handleCopyInviteCode}>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user