在添加账户的添加二维码输入框右侧添加上传图片,跳转

This commit is contained in:
WindowBird 2025-08-21 14:02:26 +08:00
parent 6e73e36644
commit 678febf443
5 changed files with 84 additions and 531 deletions

View File

@ -1,318 +0,0 @@
<template>
<view class="usage-example">
<view class="example-header">
<text class="title">单图上传使用示例</text>
<text class="desc">展示如何在页面中集成图片上传功能</text>
</view>
<!-- 方法一跳转到上传页面 -->
<view class="method-section">
<text class="method-title">方法一跳转到上传页面</text>
<text class="method-desc">跳转到专门的上传页面上传完成后返回</text>
<button class="method-btn" @click="method1">跳转上传</button>
<view v-if="method1Result" class="result-box">
<text class="result-label">上传结果</text>
<image :src="method1Result" mode="widthFix" class="result-image"></image>
<text class="result-url">{{ method1Result }}</text>
</view>
</view>
<!-- 方法二内嵌上传组件 -->
<view class="method-section">
<text class="method-title">方法二内嵌上传组件</text>
<text class="method-desc">在当前页面直接使用上传组件</text>
<view class="inline-upload">
<view class="upload-area" @click="chooseImage">
<view v-if="!selectedImage" class="upload-placeholder">
<text>点击选择图片</text>
</view>
<image v-else :src="selectedImage" mode="aspectFit" class="preview-image"></image>
</view>
<button
class="upload-btn"
@click="uploadSelectedImage"
:disabled="!selectedImage || uploading"
>
{{ uploading ? '上传中...' : '上传' }}
</button>
</view>
<view v-if="method2Result" class="result-box">
<text class="result-label">上传结果</text>
<image :src="method2Result" mode="widthFix" class="result-image"></image>
<text class="result-url">{{ method2Result }}</text>
</view>
</view>
</view>
</template>
<script>
import { getQiniuUploadToken, uploadToQiniu } from '@/api/upload.js'
export default {
data() {
return {
//
method1Result: '',
//
selectedImage: '',
uploading: false,
method2Result: '',
qiniuToken: '',
}
},
onLoad() {
//
uni.$on('image-upload-success', this.onImageUploadSuccess)
},
onUnload() {
//
uni.$off('image-upload-success', this.onImageUploadSuccess)
},
methods: {
//
method1() {
uni.navigateTo({
url: '/pages/image-upload/image-upload'
})
},
//
onImageUploadSuccess(imageUrl) {
this.method1Result = imageUrl
uni.showToast({
title: '图片上传成功',
icon: 'success'
})
},
//
chooseImage() {
uni.chooseImage({
count: 1,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: (res) => {
this.selectedImage = res.tempFilePaths[0]
this.method2Result = ''
}
})
},
//
async uploadSelectedImage() {
if (!this.selectedImage) {
uni.showToast({
title: '请先选择图片',
icon: 'none'
})
return
}
this.uploading = true
try {
// token
if (!this.qiniuToken) {
await this.getQiniuToken()
}
//
const key = `examples/${Date.now()}_${Math.random().toString(36).slice(2)}.jpg`
//
const result = await uploadToQiniu(this.selectedImage, this.qiniuToken, key)
this.method2Result = `https://api.ccttiot.com/${result.key}`
uni.showToast({
title: '上传成功',
icon: 'success'
})
} catch (error) {
console.error('上传失败:', error)
uni.showToast({
title: '上传失败',
icon: 'none'
})
} finally {
this.uploading = false
}
},
// token
async getQiniuToken() {
try {
const res = await getQiniuUploadToken()
if (res.code === 200) {
const token = res.data?.token || res.data?.uploadToken || res.token || res.data
if (token) {
this.qiniuToken = token
} else {
throw new Error('Token字段不存在')
}
} else {
throw new Error(res.msg || '获取Token失败')
}
} catch (error) {
console.error('获取Token失败:', error)
throw error
}
}
}
}
</script>
<style lang="scss" scoped>
.usage-example {
padding: 30rpx;
background: #f5f5f5;
min-height: 100vh;
.example-header {
text-align: center;
margin-bottom: 40rpx;
.title {
display: block;
font-size: 36rpx;
font-weight: bold;
color: #333;
margin-bottom: 10rpx;
}
.desc {
display: block;
font-size: 26rpx;
color: #666;
}
}
.method-section {
background: #fff;
border-radius: 16rpx;
padding: 30rpx;
margin-bottom: 30rpx;
.method-title {
display: block;
font-size: 30rpx;
font-weight: bold;
color: #333;
margin-bottom: 10rpx;
}
.method-desc {
display: block;
font-size: 24rpx;
color: #666;
margin-bottom: 20rpx;
}
.method-btn {
width: 100%;
height: 80rpx;
line-height: 80rpx;
background: #007aff;
color: #fff;
border-radius: 12rpx;
font-size: 28rpx;
border: none;
margin-bottom: 20rpx;
&:active {
background: #0056cc;
}
}
.inline-upload {
margin-bottom: 20rpx;
.upload-area {
width: 100%;
height: 200rpx;
background: #f8f8f8;
border-radius: 12rpx;
display: flex;
align-items: center;
justify-content: center;
margin-bottom: 20rpx;
border: 2rpx dashed #ddd;
.upload-placeholder {
text-align: center;
text {
font-size: 28rpx;
color: #999;
}
}
.preview-image {
width: 100%;
height: 100%;
border-radius: 12rpx;
}
}
.upload-btn {
width: 100%;
height: 70rpx;
line-height: 70rpx;
background: #52c41a;
color: #fff;
border-radius: 8rpx;
font-size: 26rpx;
border: none;
&:active {
background: #389e0d;
}
&:disabled {
background: #ccc;
color: #999;
}
}
}
.result-box {
border-top: 1rpx solid #eee;
padding-top: 20rpx;
.result-label {
display: block;
font-size: 26rpx;
font-weight: bold;
color: #333;
margin-bottom: 15rpx;
}
.result-image {
width: 100%;
border-radius: 8rpx;
margin-bottom: 15rpx;
}
.result-url {
display: block;
font-size: 22rpx;
color: #007aff;
word-break: break-all;
line-height: 1.4;
background: #f8f8f8;
padding: 15rpx;
border-radius: 6rpx;
}
}
}
}
</style>

View File

@ -98,13 +98,6 @@
"style": {
"navigationBarTitleText": "图片上传"
}
},
{
"path": "pages/image-upload-demo/image-upload-demo",
"style": {
"navigationBarTitleText": "图片上传演示",
"navigationStyle": "custom"
}
}
],
"tabBar": {

View File

@ -1,174 +0,0 @@
<template>
<view class="demo-page">
<u-navbar
:is-back="true"
title="图片上传演示"
title-color="#000"
:border-bottom="false"
:background="true"
></u-navbar>
<view class="content">
<view class="demo-section">
<text class="section-title">图片上传功能演示</text>
<text class="section-desc">点击下方按钮跳转到图片上传页面</text>
</view>
<view class="button-section">
<button class="demo-btn" @click="goToUpload">开始上传图片</button>
</view>
<view v-if="uploadedImageUrl" class="result-section">
<text class="result-title">上传结果</text>
<image
:src="uploadedImageUrl"
mode="widthFix"
class="result-image"
></image>
<text class="result-url">{{ uploadedImageUrl }}</text>
<button class="copy-btn" @click="copyImageUrl">复制图片URL</button>
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
uploadedImageUrl: '', // URL
}
},
onLoad(options) {
//
if (options.imageUrl) {
this.uploadedImageUrl = decodeURIComponent(options.imageUrl)
}
},
methods: {
//
goToUpload() {
uni.navigateTo({
url: '/pages/image-upload/image-upload'
})
},
// URL
copyImageUrl() {
if (this.uploadedImageUrl) {
uni.setClipboardData({
data: this.uploadedImageUrl,
success: () => {
uni.showToast({
title: '图片URL已复制',
icon: 'success'
})
}
})
}
}
}
}
</script>
<style lang="scss" scoped>
.demo-page {
min-height: 100vh;
background: #f5f5f5;
.content {
padding: 30rpx;
}
.demo-section {
background: #fff;
border-radius: 16rpx;
padding: 30rpx;
margin-bottom: 30rpx;
text-align: center;
.section-title {
display: block;
font-size: 32rpx;
font-weight: bold;
color: #333;
margin-bottom: 20rpx;
}
.section-desc {
display: block;
font-size: 26rpx;
color: #666;
line-height: 1.5;
}
}
.button-section {
margin-bottom: 30rpx;
.demo-btn {
width: 100%;
height: 88rpx;
line-height: 88rpx;
background: #007aff;
color: #fff;
border-radius: 12rpx;
font-size: 32rpx;
border: none;
&:active {
background: #0056cc;
}
}
}
.result-section {
background: #fff;
border-radius: 16rpx;
padding: 30rpx;
.result-title {
display: block;
font-size: 28rpx;
font-weight: bold;
color: #333;
margin-bottom: 20rpx;
}
.result-image {
width: 100%;
border-radius: 8rpx;
margin-bottom: 20rpx;
}
.result-url {
display: block;
font-size: 24rpx;
color: #007aff;
word-break: break-all;
line-height: 1.4;
margin-bottom: 20rpx;
background: #f8f8f8;
padding: 20rpx;
border-radius: 8rpx;
}
.copy-btn {
width: 100%;
height: 70rpx;
line-height: 70rpx;
background: #f0f0f0;
color: #333;
border-radius: 8rpx;
font-size: 28rpx;
border: none;
&:active {
background: #e0e0e0;
}
}
}
}
</style>

View File

@ -117,36 +117,16 @@ export default {
icon: 'success',
})
// URL
const pages = getCurrentPages()
const prevPage = pages[pages.length - 2]
//
console.log('触发图片上传成功事件:', this.uploadResult)
uni.$emit('image-upload-success', this.uploadResult)
if (prevPage) {
// 线
// URL
uni.navigateBack({
success: () => {
//
setTimeout(() => {
uni.$emit('image-upload-success', this.uploadResult)
}, 100)
},
})
} else {
// URL
//
setTimeout(() => {
uni.navigateBack({
delta: 1,
success: () => {
// URL
const currentPage = getCurrentPages()[getCurrentPages().length - 1]
if (currentPage && currentPage.onLoad) {
currentPage.onLoad({
imageUrl: encodeURIComponent(this.uploadResult),
})
}
},
})
}
}, 2000)
} catch (error) {
console.error('上传失败:', error)
this.errorMessage = error.message || '上传失败'

View File

@ -16,12 +16,18 @@
<view class="form-item">
<text class="form-label">{{ typeOptions[typeIndex].noLabel }}</text>
<input
v-model="bankForm.no"
:placeholder="typeOptions[typeIndex].noPlaceholder"
class="form-input"
maxlength="30"
/>
<view class="input-group">
<input
v-model="bankForm.no"
:placeholder="typeOptions[typeIndex].noPlaceholder"
class="form-input"
maxlength="30"
/>
<!-- 收款二维码类型时显示上传按钮 -->
<view v-if="typeOptions[typeIndex].value === 'QR'" class="upload-btn" @click="goToImageUpload">
<text class="upload-text">上传</text>
</view>
</view>
</view>
<view class="form-item">
@ -115,6 +121,22 @@ export default {
}
},
},
onLoad() {
//
uni.$on('image-upload-success', this.onImageUploadSuccess)
},
onShow() {
//
uni.$on('image-upload-success', this.onImageUploadSuccess)
},
onUnload() {
//
uni.$off('image-upload-success', this.onImageUploadSuccess)
},
onHide() {
//
uni.$off('image-upload-success', this.onImageUploadSuccess)
},
methods: {
//
resetForm() {
@ -196,6 +218,23 @@ export default {
return true
},
//
goToImageUpload() {
uni.navigateTo({
url: '/pages/image-upload/image-upload'
})
},
//
onImageUploadSuccess(imageUrl) {
console.log('接收到图片上传成功事件:', imageUrl)
this.bankForm.no = imageUrl
uni.showToast({
title: '图片上传成功',
icon: 'success'
})
},
//
async handleSubmit() {
if (!this.validateForm()) {
@ -321,6 +360,39 @@ export default {
display: flex;
align-items: center;
}
.input-group {
display: flex;
align-items: center;
gap: 15rpx;
.form-input {
flex: 1;
}
.upload-btn {
width: 120rpx;
height: 80rpx;
background: #ff803a;
border-radius: 10rpx;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: all 0.3s ease;
&:active {
background: #e67333;
transform: scale(0.95);
}
.upload-text {
color: #fff;
font-size: 26rpx;
font-weight: 500;
}
}
}
}
}